You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2013/06/04 11:10:52 UTC

svn commit: r1489350 - in /sling/trunk/launchpad: integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/xml/SaxTest.java test-services/src/main/java/org/apache/sling/launchpad/testservices/servlets/SaxServlet.java

Author: rombert
Date: Tue Jun  4 09:10:51 2013
New Revision: 1489350

URL: http://svn.apache.org/r1489350
Log:
SLING-2893 - Add documentation and integration tests to verify XML
parsing functionality

Added a SaxTest

Added:
    sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/xml/SaxTest.java   (with props)
    sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/servlets/SaxServlet.java   (with props)

Added: sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/xml/SaxTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/xml/SaxTest.java?rev=1489350&view=auto
==============================================================================
--- sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/xml/SaxTest.java (added)
+++ sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/xml/SaxTest.java Tue Jun  4 09:10:51 2013
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.launchpad.webapp.integrationtest.xml;
+
+import java.io.IOException;
+
+import org.apache.sling.commons.testing.integration.HttpTestBase;
+
+/**
+ * The <tt>SaxTest</tt> verifies that a simple SAX operation is successful
+ * 
+ */
+public class SaxTest extends HttpTestBase {
+
+    public void testSaxExecutionIsSuccessful() throws IOException {
+
+        String content = getContent(HTTP_BASE_URL + "/bin/sax.xml", CONTENT_TYPE_PLAIN);
+
+        assertEquals(content, "SAX");
+    }
+
+}

Propchange: sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/xml/SaxTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/xml/SaxTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/servlets/SaxServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/servlets/SaxServlet.java?rev=1489350&view=auto
==============================================================================
--- sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/servlets/SaxServlet.java (added)
+++ sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/servlets/SaxServlet.java Tue Jun  4 09:10:51 2013
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.launchpad.testservices.servlets;
+
+import java.io.IOException;
+import java.io.StringReader;
+
+import javax.servlet.ServletException;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+import javax.xml.xpath.XPathExpressionException;
+
+import org.apache.felix.scr.annotations.sling.SlingServlet;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.servlets.SlingAllMethodsServlet;
+import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ * The <tt>SaxServlet</tt> evaluates a simple XML document using a SAX handler
+ * 
+ */
+@SlingServlet(paths = "/bin/sax", extensions = "xml")
+public class SaxServlet extends SlingAllMethodsServlet {
+
+    private static final long serialVersionUID = 1L;
+    private static final String XML_INPUT = "<content><name>SAX</name></content>";
+
+    @Override
+    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
+            throws ServletException, IOException {
+
+        try {
+
+            SAXParserFactory factory = SAXParserFactory.newInstance();
+            SAXParser parser = factory.newSAXParser();
+            SimpleHandler handler = new SimpleHandler();
+
+            parser.parse(new InputSource(new StringReader(XML_INPUT)), handler);
+
+            response.setContentType("text/plain");
+            response.getWriter().write(handler.getValue());
+        } catch (ParserConfigurationException e) {
+            throw new ServletException(e.getMessage(), e);
+        } catch (SAXException e) {
+            throw new ServletException(e.getMessage(), e);
+        }
+    }
+
+    static class SimpleHandler extends DefaultHandler {
+
+        private String value;
+        private boolean matched;
+
+        @Override
+        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
+
+            matched = "name".equals(qName);
+        }
+
+        @Override
+        public void characters(char[] ch, int start, int length) throws SAXException {
+
+            if (matched) {
+                value = new String(ch, start, length);
+            }
+        }
+
+        public String getValue() {
+            return value;
+        }
+    }
+
+}

Propchange: sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/servlets/SaxServlet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/servlets/SaxServlet.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL