jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r641282 - in /jackrabbit/branches/1.4/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 Wed, 26 Mar 2008 12:19:13 GMT
Author: fmeschbe
Date: Wed Mar 26 05:19:01 2008
New Revision: 641282

URL: http://svn.apache.org/viewvc?rev=641282&view=rev
Log:
JCR-1350 Add a serializing content handler
  - Merge Fixes from trunk into 1.4 branch for commons 1.4.1 release

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

Modified: jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java?rev=641282&r1=641281&r2=641282&view=diff
==============================================================================
--- jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java
(original)
+++ jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java
Wed Mar 26 05:19:01 2008
@@ -21,6 +21,7 @@
 import java.io.OutputStream;
 
 import javax.jcr.Credentials;
+import javax.jcr.InvalidSerializedDataException;
 import javax.jcr.Item;
 import javax.jcr.Node;
 import javax.jcr.PathNotFoundException;
@@ -34,6 +35,7 @@
 import javax.xml.transform.sax.TransformerHandler;
 import javax.xml.transform.stream.StreamResult;
 
+import org.apache.jackrabbit.commons.xml.ParsingContentHandler;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;
 
@@ -115,14 +117,27 @@
      * @param in input stream to be parsed as XML and imported
      * @param uuidBehavior passed through
      * @throws IOException if an I/O error occurs
-     * @throws RepositoryException if another error occurs
+     * @throws InvalidSerializedDataException if an XML parsing error occurs
+     * @throws RepositoryException if a repository error occurs
      */
     public void importXML(
             String parentAbsPath, InputStream in, int uuidBehavior)
-            throws IOException, RepositoryException {
-        ContentHandler handler =
-            getImportContentHandler(parentAbsPath, uuidBehavior);
-        new DefaultContentHandler(handler).parse(in);
+            throws IOException, InvalidSerializedDataException,
+            RepositoryException {
+        try {
+            ContentHandler handler =
+                getImportContentHandler(parentAbsPath, uuidBehavior);
+            new ParsingContentHandler(handler).parse(in);
+        } catch (SAXException e) {
+            Throwable exception = e.getException();
+            if (exception instanceof RepositoryException) {
+                throw (RepositoryException) exception;
+            } else if (exception instanceof IOException) {
+                throw (IOException) exception;
+            } else {
+                throw new InvalidSerializedDataException("XML parse error", e);
+            }
+        }
     }
 
     /**

Modified: jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractWorkspace.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractWorkspace.java?rev=641282&r1=641281&r2=641282&view=diff
==============================================================================
--- jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractWorkspace.java
(original)
+++ jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractWorkspace.java
Wed Mar 26 05:19:01 2008
@@ -19,10 +19,13 @@
 import java.io.IOException;
 import java.io.InputStream;
 
+import javax.jcr.InvalidSerializedDataException;
 import javax.jcr.RepositoryException;
 import javax.jcr.Workspace;
 
+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.
@@ -38,14 +41,27 @@
      * @param in input stream to be parsed as XML and imported
      * @param uuidBehavior passed through
      * @throws IOException if an I/O error occurs
-     * @throws RepositoryException if another error occurs
+     * @throws InvalidSerializedDataException if an XML parsing error occurs
+     * @throws RepositoryException if a repository error occurs
      */
     public void importXML(
             String parentAbsPath, InputStream in, int uuidBehavior)
-            throws IOException, RepositoryException {
-        ContentHandler handler =
-            getImportContentHandler(parentAbsPath, uuidBehavior);
-        new DefaultContentHandler(handler).parse(in);
+            throws IOException, InvalidSerializedDataException,
+            RepositoryException {
+        try {
+            ContentHandler handler =
+                getImportContentHandler(parentAbsPath, uuidBehavior);
+            new ParsingContentHandler(handler).parse(in);
+        } catch (SAXException e) {
+            Throwable exception = e.getException();
+            if (exception instanceof RepositoryException) {
+                throw (RepositoryException) exception;
+            } else if (exception instanceof IOException) {
+                throw (IOException) exception;
+            } else {
+                throw new InvalidSerializedDataException("XML parse error", e);
+            }
+        }
     }
 
 }

Copied: jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DefaultContentHandler.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/branches/1.4/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DefaultContentHandler.java?p2=jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DefaultContentHandler.java&p1=jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DefaultContentHandler.java&r1=616047&r2=641282&rev=641282&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DefaultContentHandler.java
(original)
+++ jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DefaultContentHandler.java
Wed Mar 26 05:19:01 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/branches/1.4/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/ParsingContentHandler.java
(from r616070, jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/ParsingContentHandler.java)
URL: http://svn.apache.org/viewvc/jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/ParsingContentHandler.java?p2=jackrabbit/branches/1.4/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/ParsingContentHandler.java&r1=616070&r2=641282&rev=641282&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/ParsingContentHandler.java
(original)
+++ jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/ParsingContentHandler.java
Wed Mar 26 05:19:01 2008
@@ -16,6 +16,7 @@
  */
 package org.apache.jackrabbit.commons.xml;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 
@@ -47,7 +48,8 @@
 
     /**
      * Utility method that parses the given input stream using this handler.
-     * The parser is namespace-aware.
+     * The parser is namespace-aware and will not resolve external entity
+     * references.
      *
      * @param in XML input stream
      * @throws IOException if an I/O error occurs
@@ -61,6 +63,15 @@
         } catch (ParserConfigurationException e) {
             throw new SAXException("SAX parser configuration error", e);
         }
+    }
+
+    /**
+     * Returns an empty stream to prevent the XML parser from attempting
+     * to resolve external entity references.
+     */
+    public InputSource resolveEntity(String publicId, String systemId)
+            throws SAXException {
+        return new InputSource(new ByteArrayInputStream(new byte[0]));
     }
 
 }

Copied: jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/SerializingContentHandler.java
(from r616047, jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/SerializingContentHandler.java)
URL: http://svn.apache.org/viewvc/jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/SerializingContentHandler.java?p2=jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/SerializingContentHandler.java&p1=jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/SerializingContentHandler.java&r1=616047&r2=641282&rev=641282&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/SerializingContentHandler.java
(original)
+++ jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/SerializingContentHandler.java
Wed Mar 26 05:19:01 2008
@@ -16,7 +16,9 @@
  */
 package org.apache.jackrabbit.commons.xml;
 
+import java.io.OutputStream;
 import java.io.StringWriter;
+import java.io.Writer;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -55,6 +57,30 @@
 
     /** The URI for xml namespaces */
     private static final String XML = "http://www.w3.org/XML/1998/namespace";
+
+    /**
+     * Creates a serializing content handler that writes to the given stream.
+     *
+     * @param stream serialization target
+     * @return serializing content handler
+     * @throws SAXException if the content handler could not be initialized
+     */
+    public static DefaultHandler getSerializer(OutputStream output)
+            throws SAXException {
+        return getSerializer(new StreamResult(output));
+    }
+
+    /**
+     * Creates a serializing content handler that writes to the given writer.
+     *
+     * @param writer serialization target
+     * @return serializing content handler
+     * @throws SAXException if the content handler could not be initialized
+     */
+    public static DefaultHandler getSerializer(Writer writer)
+            throws SAXException {
+        return getSerializer(new StreamResult(writer));
+    }
 
     /**
      * Creates a serializing content handler that writes to the given result.

Copied: jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/xml/ParsingContentHandlerTest.java
(from r616070, jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/xml/ParsingContentHandlerTest.java)
URL: http://svn.apache.org/viewvc/jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/xml/ParsingContentHandlerTest.java?p2=jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/xml/ParsingContentHandlerTest.java&p1=jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/xml/ParsingContentHandlerTest.java&r1=616070&r2=641282&rev=641282&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/xml/ParsingContentHandlerTest.java
(original)
+++ jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/xml/ParsingContentHandlerTest.java
Wed Mar 26 05:19:01 2008
@@ -19,21 +19,20 @@
 import java.io.ByteArrayInputStream;
 import java.io.StringWriter;
 
-import javax.xml.transform.stream.StreamResult;
-
 import junit.framework.TestCase;
 
 import org.xml.sax.ContentHandler;
+import org.xml.sax.helpers.DefaultHandler;
 
 public class ParsingContentHandlerTest extends TestCase {
 
-    public void testSerializingContentHandler() throws Exception {
+    public void testParsingContentHandler() 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));
+            SerializingContentHandler.getSerializer(writer);
         new ParsingContentHandler(handler).parse(
                 new ByteArrayInputStream(source.getBytes("UTF-8")));
 
@@ -51,6 +50,22 @@
         assertContains(xml, "<c/>");
         assertContains(xml, "xyz");
         assertContains(xml, "</p:a>");
+    }
+
+    /**
+     * Test case for JCR-1355.
+     * 
+     * @see https://issues.apache.org/jira/browse/JCR-1355
+     */
+    public void testExternalEntities() {
+        try {
+            String source =
+                "<!DOCTYPE foo SYSTEM \"http://invalid.address/\"><foo/>";
+            new ParsingContentHandler(new DefaultHandler()).parse(
+                    new ByteArrayInputStream(source.getBytes("UTF-8")));
+        } catch (Exception e) {
+            fail("JCR-1355: XML import should not access external entities");
+        }
     }
 
     private void assertContains(String haystack, String needle) {

Copied: jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/xml/SerializingContentHandlerTest.java
(from r616047, jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/xml/SerializingContentHandlerTest.java)
URL: http://svn.apache.org/viewvc/jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/xml/SerializingContentHandlerTest.java?p2=jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/xml/SerializingContentHandlerTest.java&p1=jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/xml/SerializingContentHandlerTest.java&r1=616047&r2=641282&rev=641282&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/xml/SerializingContentHandlerTest.java
(original)
+++ jackrabbit/branches/1.4/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/commons/xml/SerializingContentHandlerTest.java
Wed Mar 26 05:19:01 2008
@@ -18,20 +18,18 @@
 
 import java.io.StringWriter;
 
-import javax.xml.transform.stream.StreamResult;
+import junit.framework.TestCase;
 
 import org.xml.sax.ContentHandler;
 import org.xml.sax.helpers.AttributesImpl;
 
-import junit.framework.TestCase;
-
 public class SerializingContentHandlerTest extends TestCase {
 
     public void testSerializingContentHandler() throws Exception {
         StringWriter writer = new StringWriter();
 
         ContentHandler handler =
-            SerializingContentHandler.getSerializer(new StreamResult(writer));
+            SerializingContentHandler.getSerializer(writer);
         handler.startDocument();
         handler.startPrefixMapping("p", "uri");
         handler.startElement("uri", "a", "p:a", new AttributesImpl());



Mime
View raw message