← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 1856: xmlreader improvements

 

Merge authors:
  Bob Jolliffe bobjolliffe@xxxxxxxxx
------------------------------------------------------------
revno: 1856 [merge]
committer: Bob Jolliffe bobjolliffe@xxxxxxxxx
branch nick: trunk
timestamp: Fri 2010-07-16 15:51:53 +0100
message:
  xmlreader improvements
modified:
  dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/xml/XMLPreConverter.java
  dhis-2/dhis-services/dhis-service-xml/pom.xml
  dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/reader/DefaultXMLEventReader.java
  dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/reader/DefaultXMLStreamReader.java
  dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/reader/XMLReader.java
  dhis-2/dhis-services/dhis-service-xml/src/test/java/org/amplecode/staxwax/reader/XMLReaderTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/xml/XMLPreConverter.java'
--- dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/xml/XMLPreConverter.java	2010-07-14 14:36:43 +0000
+++ dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/xml/XMLPreConverter.java	2010-07-16 13:08:20 +0000
@@ -41,12 +41,12 @@
 import javax.xml.transform.Result;
 import javax.xml.transform.Source;
 import javax.xml.transform.stream.StreamSource;
+import org.amplecode.staxwax.factory.XMLFactory;
+import org.amplecode.staxwax.reader.XMLReader;
 
 import org.amplecode.staxwax.transformer.TransformerTask;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.codehaus.stax2.XMLInputFactory2;
-import org.codehaus.stax2.XMLStreamReader2;
 import org.hisp.dhis.importexport.ImportException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
@@ -96,12 +96,10 @@
             // buffer enough space to read root elemen
             xmlDataStream.mark( BUFFER_SIZE );
 
-            XMLInputFactory2 factory = (XMLInputFactory2) XMLInputFactory.newInstance();
-            XMLStreamReader2 streamReader = (XMLStreamReader2) factory.createXMLStreamReader( xmlDataStream );
+            XMLReader reader = XMLFactory.getXMLReader( xmlDataStream );
 
-            // move to document root
-            streamReader.nextTag();
-            rootName = streamReader.getName();
+            reader.moveToStartElement();
+            rootName = reader.getElementQName();
 
             xmlDataStream.reset();
         } catch (Exception ex) {

=== modified file 'dhis-2/dhis-services/dhis-service-xml/pom.xml'
--- dhis-2/dhis-services/dhis-service-xml/pom.xml	2010-04-21 19:45:12 +0000
+++ dhis-2/dhis-services/dhis-service-xml/pom.xml	2010-07-16 13:04:51 +0000
@@ -25,13 +25,6 @@
       <groupId>org.codehaus.woodstox</groupId>
       <artifactId>stax2-api</artifactId>
     </dependency>
-    <!--
-	<dependency>
-	  <groupId>stax</groupId>
-	  <artifactId>stax-api</artifactId>
-	  <version>1.0.1</version>
-	</dependency>
-	-->
     <dependency>
       <groupId>org.codehaus.woodstox</groupId>
       <artifactId>woodstox-core-asl</artifactId>
@@ -41,11 +34,6 @@
       <artifactId>junit</artifactId>
       <scope>test</scope>
     </dependency>
-    <dependency>
-      <groupId>javax.xml.stream</groupId>
-      <artifactId>stax-api</artifactId>
-      <scope>test</scope>
-    </dependency>
 
 	<!--Sun's multi-schema-validator jars are required for relaxng validation-->
     <dependency>

=== modified file 'dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/reader/DefaultXMLEventReader.java'
--- dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/reader/DefaultXMLEventReader.java	2010-06-24 14:38:23 +0000
+++ dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/reader/DefaultXMLEventReader.java	2010-07-16 13:04:51 +0000
@@ -103,8 +103,7 @@
         {
             if ( reader.peek().isCharacters() )
             {
-                currentEvent = reader.nextEvent();
-                return currentEvent.asCharacters().getData();
+                return this.getText();
             }
             else
             {
@@ -139,6 +138,26 @@
     }
 
     @Override
+    public void moveToStartElement( )
+    {
+        try
+        {
+            while ( reader.hasNext() )
+            {
+                currentEvent = reader.nextEvent();
+                if ( currentEvent.isStartElement())
+                {
+                    break;
+                }
+            }
+        }
+        catch ( XMLStreamException ex )
+        {
+            throw new RuntimeException( "Failed to move to start element", ex );
+        }
+    }
+
+    @Override
     public boolean moveToStartElement( String startElementName, String endElementName )
     {
         try
@@ -334,4 +353,20 @@
             throw new RuntimeException( "Failed to close reader", ex );
         }
     }
+    
+    protected String getText() throws XMLStreamException
+    {
+        StringBuffer sb = new StringBuffer();
+        while (reader.peek().isCharacters()) {
+            sb.append( reader.nextEvent().asCharacters().getData());
+        }
+        if (sb.length() == 0)
+        {
+            return null;
+        }
+        else
+        {
+            return sb.toString();
+        }
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/reader/DefaultXMLStreamReader.java'
--- dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/reader/DefaultXMLStreamReader.java	2010-07-02 12:37:38 +0000
+++ dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/reader/DefaultXMLStreamReader.java	2010-07-16 13:04:51 +0000
@@ -100,7 +100,7 @@
         {
             reader.next();
 
-            return reader.getEventType() == CHARACTERS ? reader.getText() : null;
+            return this.getText();
         } catch ( XMLStreamException ex )
         {
             throw new RuntimeException( "Failed to get element value", ex );
@@ -108,6 +108,26 @@
     }
 
     @Override
+    public void moveToStartElement( )
+    {
+        try
+        {
+           while (reader.hasNext())  
+           { 
+               reader.next();
+               if( reader.isStartElement())
+               {
+                   break;
+               }
+           } 
+        }
+          catch ( XMLStreamException ex )
+        {
+            throw new RuntimeException( "Failed to get element value", ex );
+        }
+    }
+
+    @Override
     public void moveToStartElement( String name )
     {
         try
@@ -174,7 +194,7 @@
             return reader.next() != END_DOCUMENT;
         } catch ( XMLStreamException ex )
         {
-            throw new RuntimeException( "Failed to move cursor to next element", ex );
+            throw new RuntimeException( "Failed to move cursor to next event", ex );
         }
     }
 
@@ -186,7 +206,7 @@
             return !( reader.next() == END_ELEMENT && reader.getLocalName().equals( endElementName ) );
         } catch ( XMLStreamException ex )
         {
-            throw new RuntimeException( "Failed to move cursor to next element", ex );
+            throw new RuntimeException( "Failed to move cursor to end element", ex );
         }
     }
 
@@ -232,12 +252,13 @@
                     reader.next();
 
                     // Read text if any
-
-                    elements.put( currentElementName, reader.getEventType() == CHARACTERS ? reader.getText() : null );
-                } else
+                    elements.put( currentElementName, this.getText()  );
+                }
+                else
                 {
                     reader.next();
                 }
+
             }
 
             return elements;
@@ -321,4 +342,21 @@
             throw new RuntimeException( "Failed to create XML Event reader", ex );
         }
     }
+
+    protected String getText() throws XMLStreamException
+    {
+        StringBuffer sb = new StringBuffer();
+        while (reader.isCharacters()) {
+            sb.append( reader.getText());
+            reader.next();
+        }
+        if (sb.length() == 0)
+        {
+            return null;
+        }
+        else
+        {
+            return sb.toString();
+        }
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/reader/XMLReader.java'
--- dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/reader/XMLReader.java	2010-06-24 14:38:23 +0000
+++ dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/reader/XMLReader.java	2010-07-16 13:04:51 +0000
@@ -64,6 +64,12 @@
     String getElementValue();
 
     /**
+     * Moves the cursor to the next XML start element in the document.
+     *
+     */
+    void moveToStartElement( );
+
+    /**
      * Moves the cursor to the first XML start element with the given name in the document.
      * 
      * @param name The name of the XML element to move to.
@@ -78,7 +84,7 @@
      * @param endElementName The name of the XML end element to stop moving forward at.
      */
     boolean moveToStartElement( String startElementName, String endElementName );
-    
+
     /**
      * Checks whether the current XML element is a start element with the given name.
      * 
@@ -96,7 +102,7 @@
     boolean isEndElement( String name );
     
     /**
-     * Moves the cursor to the next XML element in the document. Returns true until the cursor points
+     * Moves the cursor to the next XML event in the document. Returns true until the cursor points
      * at the XML document end.
      * 
      * @return False if the cursor points at the XML document end, true otherwise.
@@ -104,7 +110,7 @@
     boolean next();
     
     /**
-     * Moves the cursor to the next XML element in the document. Returns true until the cursor points
+     * Moves the cursor to the next XML event in the document. Returns true until the cursor points
      * at an XML end element with the given name.
      * 
      * @param endElementName The name of the end element.

=== modified file 'dhis-2/dhis-services/dhis-service-xml/src/test/java/org/amplecode/staxwax/reader/XMLReaderTest.java'
--- dhis-2/dhis-services/dhis-service-xml/src/test/java/org/amplecode/staxwax/reader/XMLReaderTest.java	2010-07-15 12:10:45 +0000
+++ dhis-2/dhis-services/dhis-service-xml/src/test/java/org/amplecode/staxwax/reader/XMLReaderTest.java	2010-07-16 13:04:51 +0000
@@ -69,7 +69,7 @@
   private String[] specialChars = {"&", "<", ">", "\"", "'"};
 
 
-  private static int BIGTEXTSIZE = 1987; // fails when set to 1988
+  private static int BIGTEXTSIZE = 5000; 
   // -------------------------------------------------------------------------
   // Fixture
   // -------------------------------------------------------------------------
@@ -122,6 +122,22 @@
   }
 
   @Test
+  public void testMoveToStartElement() {
+    XMLReader reader = XMLFactory.getXMLReader(inputStreamA);
+    reader.moveToStartElement();
+    assertEquals(COLLECTION_NAME,reader.getElementName());
+    reader.closeReader();
+  }
+
+  @Test
+  public void testEventMoveToStartElement() {
+    XMLReader evReader = XMLFactory.getXMLEventReader( inputStreamA );
+    evReader.moveToStartElement();
+    assertEquals(COLLECTION_NAME,evReader.getElementName());
+    evReader.closeReader();
+  }
+
+  @Test
   public void testEventReadElements() {
     XMLReader reader = XMLFactory.getXMLEventReader(inputStreamA);