jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r616070 - in /jackrabbit/trunk/jackrabbit-jcr-commons/src: main/java/org/apache/jackrabbit/commons/ main/java/org/apache/jackrabbit/commons/xml/ test/java/org/apache/jackrabbit/commons/xml/
Date Mon, 28 Jan 2008 21:54:14 GMT
Author: jukka
Date: Mon Jan 28 13:54:12 2008
New Revision: 616070

URL: http://svn.apache.org/viewvc?rev=616070&view=rev
Log:
JCR-1350: Add a serializing content handler
    - Improved separation of concerns between the XML utility classes

Added:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/ParsingContentHandler.java
      - copied, changed from r616047, jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DefaultContentHandler.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/xml/ParsingContentHandlerTest.java
Modified:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractWorkspace.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DefaultContentHandler.java

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java?rev=616070&r1=616069&r2=616070&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java
Mon Jan 28 13:54:12 2008
@@ -34,7 +34,7 @@
 import javax.xml.transform.sax.TransformerHandler;
 import javax.xml.transform.stream.StreamResult;
 
-import org.apache.jackrabbit.commons.xml.DefaultContentHandler;
+import org.apache.jackrabbit.commons.xml.ParsingContentHandler;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;
 
@@ -121,9 +121,13 @@
     public void importXML(
             String parentAbsPath, InputStream in, int uuidBehavior)
             throws IOException, RepositoryException {
-        ContentHandler handler =
-            getImportContentHandler(parentAbsPath, uuidBehavior);
-        new DefaultContentHandler(handler).parse(in);
+        try {
+            ContentHandler handler =
+                getImportContentHandler(parentAbsPath, uuidBehavior);
+            new ParsingContentHandler(handler).parse(in);
+        } catch (SAXException e) {
+            throw new RepositoryException("XML import failed", e);
+        }
     }
 
     /**

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractWorkspace.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractWorkspace.java?rev=616070&r1=616069&r2=616070&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractWorkspace.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractWorkspace.java
Mon Jan 28 13:54:12 2008
@@ -22,8 +22,9 @@
 import javax.jcr.RepositoryException;
 import javax.jcr.Workspace;
 
-import org.apache.jackrabbit.commons.xml.DefaultContentHandler;
+import org.apache.jackrabbit.commons.xml.ParsingContentHandler;
 import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
 
 /**
  * Abstract base class for implementing the JCR {@link Workspace} interface.
@@ -44,9 +45,13 @@
     public void importXML(
             String parentAbsPath, InputStream in, int uuidBehavior)
             throws IOException, RepositoryException {
-        ContentHandler handler =
-            getImportContentHandler(parentAbsPath, uuidBehavior);
-        new DefaultContentHandler(handler).parse(in);
+        try {
+            ContentHandler handler =
+                getImportContentHandler(parentAbsPath, uuidBehavior);
+            new ParsingContentHandler(handler).parse(in);
+        } catch (SAXException e) {
+            throw new RepositoryException("XML import failed", e);
+        }
     }
 
 }

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DefaultContentHandler.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DefaultContentHandler.java?rev=616070&r1=616069&r2=616070&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DefaultContentHandler.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DefaultContentHandler.java
Mon Jan 28 13:54:12 2008
@@ -16,21 +16,8 @@
  */
 package org.apache.jackrabbit.commons.xml;
 
-import java.io.IOException;
-import java.io.InputStream;
-
-import javax.jcr.InvalidSerializedDataException;
-import javax.jcr.RepositoryException;
-import javax.xml.parsers.FactoryConfigurationError;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.parsers.SAXParser;
-import javax.xml.parsers.SAXParserFactory;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.xml.sax.Attributes;
 import org.xml.sax.ContentHandler;
-import org.xml.sax.InputSource;
 import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.DefaultHandler;
@@ -44,12 +31,6 @@
 public class DefaultContentHandler extends DefaultHandler {
 
     /**
-     * Logger instance.
-     */
-    private static final Logger logger =
-        LoggerFactory.getLogger(DefaultContentHandler.class);
-
-    /**
      * The adapted content handler instance.
      */
     private final ContentHandler handler;
@@ -62,42 +43,6 @@
      */
     public DefaultContentHandler(ContentHandler handler) {
         this.handler = handler;
-    }
-
-    /**
-     * Utility method that parses the given input stream using this handler.
-     *
-     * @param in XML input stream
-     * @throws IOException if an I/O error occurs
-     * @throws RepositoryException if another error occurs
-     */
-    public void parse(InputStream in) throws IOException, RepositoryException {
-        try {
-            SAXParserFactory factory = SAXParserFactory.newInstance();
-            factory.setNamespaceAware(true);
-            factory.setFeature(
-                    "http://xml.org/sax/features/namespace-prefixes", false);
-
-            SAXParser parser = factory.newSAXParser();
-            // JCR-984 & JCR-985: Log the name of the SAXParser class
-            logger.debug("Using SAX parser " + parser.getClass().getName());
-            parser.parse(new InputSource(in), this);
-        } catch (FactoryConfigurationError e) {
-            throw new RepositoryException(
-                    "SAX parser implementation not available", e);
-        } catch (ParserConfigurationException e) {
-            throw new RepositoryException("SAX parser configuration error", e);
-        } catch (SAXException e) {
-            Exception exception = e.getException();
-            if (exception instanceof RepositoryException) {
-                throw (RepositoryException) exception;
-            } else if (exception instanceof IOException) {
-                throw (IOException) exception;
-            } else {
-                throw new InvalidSerializedDataException(
-                        "Error parsing XML import", e);
-            }
-        }
     }
 
     //------------------------------------------------------< ContentHandler >

Copied: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/ParsingContentHandler.java
(from r616047, jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DefaultContentHandler.java)
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/ParsingContentHandler.java?p2=jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/ParsingContentHandler.java&p1=jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DefaultContentHandler.java&r1=616047&r2=616070&rev=616070&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DefaultContentHandler.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/ParsingContentHandler.java
Mon Jan 28 13:54:12 2008
@@ -19,40 +19,21 @@
 import java.io.IOException;
 import java.io.InputStream;
 
-import javax.jcr.InvalidSerializedDataException;
-import javax.jcr.RepositoryException;
-import javax.xml.parsers.FactoryConfigurationError;
 import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.xml.sax.Attributes;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.InputSource;
-import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.DefaultHandler;
 
 /**
- * Adapter class for exposing a {@link ContentHandler} instance as
- * {@link DefaultHandler} object.
+ * Utility class that decorates a {@link ContentHandler} instance with
+ * simple XML parsing capability.
  *
  * @since Jackrabbit JCR Commons 1.5
  */
-public class DefaultContentHandler extends DefaultHandler {
-
-    /**
-     * Logger instance.
-     */
-    private static final Logger logger =
-        LoggerFactory.getLogger(DefaultContentHandler.class);
-
-    /**
-     * The adapted content handler instance.
-     */
-    private final ContentHandler handler;
+public class ParsingContentHandler extends DefaultContentHandler {
 
     /**
      * Creates a {@link DefaultHandler} adapter for the given content
@@ -60,172 +41,26 @@
      *
      * @param handler content handler
      */
-    public DefaultContentHandler(ContentHandler handler) {
-        this.handler = handler;
+    public ParsingContentHandler(ContentHandler handler) {
+        super(handler);
     }
 
     /**
      * Utility method that parses the given input stream using this handler.
+     * The parser is namespace-aware.
      *
      * @param in XML input stream
      * @throws IOException if an I/O error occurs
-     * @throws RepositoryException if another error occurs
+     * @throws SAXException if an XML parsing error occurs
      */
-    public void parse(InputStream in) throws IOException, RepositoryException {
+    public void parse(InputStream in) throws IOException, SAXException {
         try {
             SAXParserFactory factory = SAXParserFactory.newInstance();
             factory.setNamespaceAware(true);
-            factory.setFeature(
-                    "http://xml.org/sax/features/namespace-prefixes", false);
-
-            SAXParser parser = factory.newSAXParser();
-            // JCR-984 & JCR-985: Log the name of the SAXParser class
-            logger.debug("Using SAX parser " + parser.getClass().getName());
-            parser.parse(new InputSource(in), this);
-        } catch (FactoryConfigurationError e) {
-            throw new RepositoryException(
-                    "SAX parser implementation not available", e);
+            factory.newSAXParser().parse(new InputSource(in), this);
         } catch (ParserConfigurationException e) {
-            throw new RepositoryException("SAX parser configuration error", e);
-        } catch (SAXException e) {
-            Exception exception = e.getException();
-            if (exception instanceof RepositoryException) {
-                throw (RepositoryException) exception;
-            } else if (exception instanceof IOException) {
-                throw (IOException) exception;
-            } else {
-                throw new InvalidSerializedDataException(
-                        "Error parsing XML import", e);
-            }
+            throw new SAXException("SAX parser configuration error", e);
         }
-    }
-
-    //------------------------------------------------------< ContentHandler >
-
-    /**
-     * Delegated to {@link #handler}.
-     *
-     * @param ch passed through
-     * @param start passed through
-     * @param length passed through
-     * @throws SAXException if an error occurs
-     */
-    public void characters(char[] ch, int start, int length)
-            throws SAXException {
-        handler.characters(ch, start, length);
-    }
-
-    /**
-     * Delegated to {@link #handler}.
-     *
-     * @throws SAXException if an error occurs
-     */
-    public void endDocument() throws SAXException {
-        handler.endDocument();
-    }
-
-    /**
-     * Delegated to {@link #handler}.
-     *
-     * @param namespaceURI passed through
-     * @param localName passed through
-     * @param qName passed through
-     * @throws SAXException if an error occurs
-     */
-    public void endElement(
-            String namespaceURI, String localName, String qName)
-            throws SAXException {
-        handler.endElement(namespaceURI, localName, qName);
-    }
-
-    /**
-     * Delegated to {@link #handler}.
-     *
-     * @param prefix passed through
-     * @throws SAXException if an error occurs
-     */
-    public void endPrefixMapping(String prefix) throws SAXException {
-        handler.endPrefixMapping(prefix);
-    }
-
-    /**
-     * Delegated to {@link #handler}.
-     *
-     * @param ch passed through
-     * @param start passed through
-     * @param length passed through
-     * @throws SAXException if an error occurs
-     */
-    public void ignorableWhitespace(char[] ch, int start, int length)
-            throws SAXException {
-        handler.ignorableWhitespace(ch, start, length);
-    }
-
-    /**
-     * Delegated to {@link #handler}.
-     *
-     * @param target passed through
-     * @param data passed through
-     * @throws SAXException if an error occurs
-     */
-    public void processingInstruction(String target, String data)
-            throws SAXException {
-        handler.processingInstruction(target, data);
-    }
-
-    /**
-     * Delegated to {@link #handler}.
-     *
-     * @param locator passed through
-     */
-    public void setDocumentLocator(Locator locator) {
-        handler.setDocumentLocator(locator);
-    }
-
-    /**
-     * Delegated to {@link #handler}.
-     *
-     * @param name passed through
-     * @throws SAXException if an error occurs
-     */
-    public void skippedEntity(String name) throws SAXException {
-        handler.skippedEntity(name);
-    }
-
-    /**
-     * Delegated to {@link #handler}.
-     *
-     * @throws SAXException if an error occurs
-     */
-    public void startDocument() throws SAXException {
-        handler.startDocument();
-    }
-
-    /**
-     * Delegated to {@link #handler}.
-     *
-     * @param namespaceURI passed through
-     * @param localName passed through
-     * @param qName passed through
-     * @param atts passed through
-     * @throws SAXException if an error occurs
-     */
-    public void startElement(
-            String namespaceURI, String localName, String qName,
-            Attributes atts) throws SAXException {
-        handler.startElement(namespaceURI, localName, qName, atts);
-    }
-
-    /**
-     * Delegated to {@link #handler}.
-     *
-     * @param prefix passed through
-     * @param uri passed through
-     * @throws SAXException if an error occurs
-     */
-    public void startPrefixMapping(String prefix, String uri)
-            throws SAXException {
-        handler.startPrefixMapping(prefix, uri);
     }
 
 }

Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/xml/ParsingContentHandlerTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/xml/ParsingContentHandlerTest.java?rev=616070&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/xml/ParsingContentHandlerTest.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/xml/ParsingContentHandlerTest.java
Mon Jan 28 13:54:12 2008
@@ -0,0 +1,61 @@
+/*
+ * 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.jackrabbit.commons.xml;
+
+import java.io.ByteArrayInputStream;
+import java.io.StringWriter;
+
+import javax.xml.transform.stream.StreamResult;
+
+import junit.framework.TestCase;
+
+import org.xml.sax.ContentHandler;
+
+public class ParsingContentHandlerTest extends TestCase {
+
+    public void testSerializingContentHandler() throws Exception {
+        String source =
+            "<p:a xmlns:p=\"uri\"><b p:foo=\"bar\">abc</b><c/>xyz</p:a>";
+        StringWriter writer = new StringWriter();
+
+        ContentHandler handler =
+            SerializingContentHandler.getSerializer(new StreamResult(writer));
+        new ParsingContentHandler(handler).parse(
+                new ByteArrayInputStream(source.getBytes("UTF-8")));
+
+        String xml = writer.toString();
+        assertContains(xml, "<p:a");
+        assertContains(xml, "xmlns:p");
+        assertContains(xml, "=");
+        assertContains(xml, "uri");
+        assertContains(xml, ">");
+        assertContains(xml, "<b");
+        assertContains(xml, "p:foo");
+        assertContains(xml, "bar");
+        assertContains(xml, "abc");
+        assertContains(xml, "</b>");
+        assertContains(xml, "<c/>");
+        assertContains(xml, "xyz");
+        assertContains(xml, "</p:a>");
+    }
+
+    private void assertContains(String haystack, String needle) {
+        if (haystack.indexOf(needle) == -1) {
+            fail("'" + haystack + "' does not contain '" + needle+ "'");
+        }
+    }
+}



Mime
View raw message