← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2067: Small change to logging/error handling

 

------------------------------------------------------------
revno: 2067
committer: Bob Jolliffe bobjolliffe@xxxxxxxxx
branch nick: trunk
timestamp: Fri 2010-07-02 13:37:38 +0100
message:
  Small change to logging/error handling
  Added extra test for XMLPipe
modified:
  dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/framework/XMLPipe.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/test/java/org/amplecode/staxwax/framework/XMLPipeTest.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-xml/src/main/java/org/amplecode/staxwax/framework/XMLPipe.java'
--- dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/framework/XMLPipe.java	2010-05-21 15:07:17 +0000
+++ dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/framework/XMLPipe.java	2010-07-02 12:37:38 +0000
@@ -111,8 +111,7 @@
             }
             catch ( InterruptedException ex )
             {
-                log.warn( "XMLpipe read interrupted : " + ex );
-                throw new XMLStreamException( ex.toString() );
+                throw new XMLStreamException( "XMLpipe read interrupted", ex );
             }
         }
 
@@ -133,8 +132,6 @@
         public String getElementText()
             throws XMLStreamException
         {
-            try
-            {
                 // get the text
                 String result = nextEvent().asCharacters().getData();
                 // pop (and test caste) the end element
@@ -142,12 +139,6 @@
 
                 return result;
 
-            }
-            catch ( Exception ex )
-            {
-                log.warn( "XMLpipe getElementText problem : " + ex );
-                throw new XMLStreamException( ex.toString() );
-            }
         }
 
         @Override
@@ -170,7 +161,7 @@
             throws XMLStreamException
         {
             // not a real stream ... no handle to close
-	    return;
+    	    return;
         }
 
         @Override

=== 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-06-24 14:38:23 +0000
+++ dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/reader/DefaultXMLStreamReader.java	2010-07-02 12:37:38 +0000
@@ -54,18 +54,21 @@
 public class DefaultXMLStreamReader
     implements XMLReader
 {
+
     private static final Log log = LogFactory.getLog( DefaultXMLStreamReader.class );
 
-    private static final String[] EVENTS = { "None", "Start Element", "End Element", "Processing Instruction",
+    private static final String[] EVENTS =
+    {
+        "None", "Start Element", "End Element", "Processing Instruction",
         "Characters", "Comment", "Space", "Start Document", "End Document", "Entity Reference", "Attribute", "DTD",
-        "CData", "Namespace", "Notation Declaration", "Entity Declaration" };
+        "CData", "Namespace", "Notation Declaration", "Entity Declaration"
+    };
 
     private XMLStreamReader2 reader;
 
     // -------------------------------------------------------------------------
     // Constructor
     // -------------------------------------------------------------------------
-
     public DefaultXMLStreamReader( XMLStreamReader2 reader )
     {
         this.reader = reader;
@@ -74,7 +77,6 @@
     // -------------------------------------------------------------------------
     // XMLReader implementation
     // -------------------------------------------------------------------------
-
     @Override
     public String getElementName()
     {
@@ -99,8 +101,7 @@
             reader.next();
 
             return reader.getEventType() == CHARACTERS ? reader.getText() : null;
-        }
-        catch ( XMLStreamException ex )
+        } catch ( XMLStreamException ex )
         {
             throw new RuntimeException( "Failed to get element value", ex );
         }
@@ -113,13 +114,14 @@
         {
             while ( reader.next() != END_DOCUMENT )
             {
+                log.debug( "XML Event: " + reader.getEventType() );
+
                 if ( reader.getEventType() == START_ELEMENT && reader.getLocalName().equals( name ) )
                 {
                     break;
                 }
             }
-        }
-        catch ( XMLStreamException ex )
+        } catch ( XMLStreamException ex )
         {
             throw new RuntimeException( "Failed to move to start element", ex );
         }
@@ -132,6 +134,8 @@
         {
             while ( reader.next() != END_DOCUMENT ) // TODO && hasNext ?
             {
+                log.debug( "XML Event: " + reader.getEventType() );
+
                 if ( reader.getEventType() == START_ELEMENT && reader.getLocalName().equals( startElementName ) )
                 {
                     return true;
@@ -144,8 +148,7 @@
             }
 
             return false;
-        }
-        catch ( XMLStreamException ex )
+        } catch ( XMLStreamException ex )
         {
             throw new RuntimeException( "Failed to move to start element", ex );
         }
@@ -169,8 +172,7 @@
         try
         {
             return reader.next() != END_DOCUMENT;
-        }
-        catch ( XMLStreamException ex )
+        } catch ( XMLStreamException ex )
         {
             throw new RuntimeException( "Failed to move cursor to next element", ex );
         }
@@ -181,9 +183,8 @@
     {
         try
         {
-            return !(reader.next() == END_ELEMENT && reader.getLocalName().equals( endElementName ));
-        }
-        catch ( XMLStreamException ex )
+            return !( reader.next() == END_ELEMENT && reader.getLocalName().equals( endElementName ) );
+        } catch ( XMLStreamException ex )
         {
             throw new RuntimeException( "Failed to move cursor to next element", ex );
         }
@@ -233,16 +234,14 @@
                     // Read text if any
 
                     elements.put( currentElementName, reader.getEventType() == CHARACTERS ? reader.getText() : null );
-                }
-                else
+                } else
                 {
                     reader.next();
                 }
             }
 
             return elements;
-        }
-        catch ( XMLStreamException ex )
+        } catch ( XMLStreamException ex )
         {
             throw new RuntimeException( "Failed to read elements", ex );
         }
@@ -263,7 +262,7 @@
 
             StringBuffer text = new StringBuffer( "\n" );
 
-            while ( (e = reader.next()) != END_DOCUMENT )
+            while ( ( e = reader.next() ) != END_DOCUMENT )
             {
                 text.append( "EVENT: " + EVENTS[e] + " " );
 
@@ -290,8 +289,7 @@
             }
 
             log.info( text );
-        }
-        catch ( XMLStreamException ex )
+        } catch ( XMLStreamException ex )
         {
             throw new RuntimeException( "Failed to read elements", ex );
         }
@@ -303,8 +301,7 @@
         try
         {
             reader.close();
-        }
-        catch ( XMLStreamException ex )
+        } catch ( XMLStreamException ex )
         {
             throw new RuntimeException( "Failed to close reader", ex );
         }
@@ -314,13 +311,12 @@
     public XMLEventReader2 getXmlEventReader()
     {
         XMLInputFactory2 fac = (XMLInputFactory2) XMLInputFactory.newInstance();
-        
+
         try
         {
             XMLEventReader2 eventReader = (XMLEventReader2) fac.createXMLEventReader( reader );
             return eventReader;
-        }
-        catch ( XMLStreamException ex )
+        } catch ( XMLStreamException ex )
         {
             throw new RuntimeException( "Failed to create XML Event reader", ex );
         }

=== modified file 'dhis-2/dhis-services/dhis-service-xml/src/test/java/org/amplecode/staxwax/framework/XMLPipeTest.java'
--- dhis-2/dhis-services/dhis-service-xml/src/test/java/org/amplecode/staxwax/framework/XMLPipeTest.java	2010-02-19 18:38:02 +0000
+++ dhis-2/dhis-services/dhis-service-xml/src/test/java/org/amplecode/staxwax/framework/XMLPipeTest.java	2010-07-02 12:37:38 +0000
@@ -48,6 +48,8 @@
 {
 
     private InputStream inputStreamB;
+    private InputStream inputStreamDXF;
+    private InputStream inputStreamDXFcopy;
 
     @Override
     protected void setUp() throws Exception
@@ -55,6 +57,17 @@
         super.setUp();
         ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
         inputStreamB = classLoader.getResourceAsStream( "dataB.xml" );
+        inputStreamDXF = classLoader.getResourceAsStream( "Export.xml" );
+        inputStreamDXFcopy = classLoader.getResourceAsStream( "Export.xml" );
+    }
+
+    @Override
+    public void tearDown()
+        throws Exception
+    {
+        inputStreamB.close();
+        inputStreamDXF.close();
+        inputStreamDXFcopy.close();
     }
 
     public synchronized void testReadWrite() throws XMLStreamException
@@ -91,6 +104,36 @@
 
     }
 
+    public void testReadWriteContents() throws XMLStreamException
+    {
+        XMLPipe pipe = new XMLPipe();
+        XMLEventWriter pipeinput = pipe.getInput();
+        XMLEventReader2 pipeoutput = pipe.getOutput();
+
+        XMLEventReader reader = XMLFactory.getXMLEventReader( inputStreamDXF ).getXmlEventReader();
+
+        while ( reader.hasNext() )
+        {
+            XMLEvent ev = reader.nextEvent();
+            pipeinput.add( ev );
+        }
+
+        XMLEventReader dxfCopy = XMLFactory.getXMLEventReader( inputStreamDXFcopy ).getXmlEventReader();
+
+        // read the other end of the pipe and compare contents with original
+        while ( pipeoutput.hasNext() )
+        {
+            XMLEvent ev = pipeoutput.nextEvent();
+            XMLEvent evCopy = dxfCopy.nextEvent();
+            assertEquals(ev.getEventType(), evCopy.getEventType());
+            if (ev.isCharacters()) {
+                assertEquals(ev.asCharacters().getData(), evCopy.asCharacters().getData());
+            }
+            
+        }
+
+    }
+
     public synchronized void testMTReadWrite() throws XMLStreamException
     {