← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 1853: Couldn't resist fixing typo following Lars test commit :-)

 

------------------------------------------------------------
revno: 1853
committer: Bob Jolliffe <bobj@bobj-laptop>
branch nick: trunk
timestamp: Thu 2010-05-13 22:14:53 +0100
message:
  Couldn't resist fixing typo following Lars test commit :-)
  Removed method from XPathFilter
  Added a method to TransformerTask which transforms to a Pipe
modified:
  dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/framework/XPathFilter.java
  dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/transformer/TransformerTask.java
  dhis-2/dhis-services/dhis-service-xml/src/test/java/org/amplecode/staxwax/framework/XPathFilterTest.java
  dhis-2/dhis-services/dhis-service-xml/src/test/resources/dataC.xml


--
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/XPathFilter.java'
--- dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/framework/XPathFilter.java	2010-05-10 18:08:02 +0000
+++ dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/framework/XPathFilter.java	2010-05-13 21:14:53 +0000
@@ -134,33 +134,6 @@
         return result;
     }
 
-    /**
-     * Find numeric data in stream
-     *
-     * @param in
-     * @param xpathExpr
-     * @return
-     */
-    public static Integer findNumber( InputStream in, String xpathExpr )
-    {
-
-        Integer result = null;
-
-        try
-        {
-            XPathExpression expr = compileXPath(xpathExpr);
-
-            Document doc = parseDocument(in);
-
-            result = (Integer) expr.evaluate( doc, XPathConstants.NUMBER );
-
-        } catch ( Exception ex )
-        {
-            log.info( ex );
-        }
-        return result;
-    }
-
     private static synchronized XPathExpression compileXPath(String xpathString)
     {
         XPathFactory factory = XPathFactory.newInstance();

=== modified file 'dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/transformer/TransformerTask.java'
--- dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/transformer/TransformerTask.java	2010-05-13 20:26:51 +0000
+++ dhis-2/dhis-services/dhis-service-xml/src/main/java/org/amplecode/staxwax/transformer/TransformerTask.java	2010-05-13 21:14:53 +0000
@@ -27,9 +27,12 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.io.BufferedInputStream;
+import java.io.InputStream;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Map.Entry;
+import javax.xml.stream.XMLEventWriter;
 
 import javax.xml.transform.Result;
 import javax.xml.transform.Source;
@@ -39,10 +42,16 @@
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.URIResolver;
+import javax.xml.transform.stax.StAXResult;
+import javax.xml.transform.stream.StreamSource;
 import org.amplecode.staxwax.framework.InputPort;
 import org.amplecode.staxwax.framework.OutputPort;
+import org.amplecode.staxwax.framework.XMLPipe;
+import org.amplecode.staxwax.reader.DefaultXMLEventReader;
+import org.amplecode.staxwax.reader.XMLReader;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.codehaus.stax2.XMLEventReader2;
 
 /**
  *
@@ -56,17 +65,17 @@
     /**
      * The source xml stream
      */
-    protected InputPort source;
+    protected InputPort sourcePort;
 
     /**
      * The xslt stylesheet
      */
-    protected InputPort stylesheet;
+    protected InputPort stylesheetPort;
 
     /**
      * The transformation result
      */
-    protected OutputPort result;
+    protected OutputPort resultPort;
 
     /**
      * The stylesheet parameters
@@ -85,21 +94,22 @@
      */
     public TransformerTask( Source stylesheet, Map<String, String> params )
     {
-        this.stylesheet = new InputPort( "Stylesheet", stylesheet );
+        this.stylesheetPort = new InputPort( "Stylesheet", stylesheet );
         this.params = params;
         this.templates = null;
     }
 
     /**
-     * Pre-compile styleshhet to Templates
+     * Pre-compile stylesheet to Templates
      * @throws TransformerConfigurationException
      */
     public void compile() throws TransformerConfigurationException
     {
         TransformerFactory factory = TransformerFactory.newInstance();
-        templates = factory.newTemplates( stylesheet.getSource() );
+        templates = factory.newTemplates( stylesheetPort.getSource() );
     }
 
+
     public void transform( Source source, Result result, URIResolver resolver ) throws TransformerConfigurationException, TransformerException
     {
         log.info( "Transformer running" );
@@ -109,8 +119,8 @@
             compile();
         }
 
-        this.source = new InputPort( "Source", source );
-        this.result = new OutputPort( "Result", result );
+        this.sourcePort = new InputPort( "Source", source );
+        this.resultPort = new OutputPort( "Result", result );
 
         Transformer t = templates.newTransformer();
 
@@ -133,6 +143,42 @@
             }
         }
 
-        t.transform( this.source.getSource(), this.result.getResult() );
+        t.transform( this.sourcePort.getSource(), this.resultPort.getResult() );
     }
+
+
+    /**
+     * Transforms the foreign xml into dxf
+     *
+     * @param dataStream - the xml data
+     * @param sheetStream - the stylsheet to perform the translation
+     * @param xsltParams - paramaters to pass to the stylesheet
+     * @return a StaxWax XMLEventReader for the readable end of the pipe
+     *
+     * @throws Exception
+     *
+     * TODO: implement XMLPipe as a Result type
+     *
+     */
+    public XMLReader transformToPipe(BufferedInputStream dataStream)
+        throws Exception
+    {
+
+            Source dataSource = new StreamSource( dataStream );
+
+            // make a pipe to capture output of transform
+            XMLPipe pipe = new XMLPipe();
+            XMLEventWriter pipeinput = pipe.getInput();
+            XMLEventReader2 pipeoutput = pipe.getOutput();
+
+            // set result of transform to input of pipe
+            StAXResult result = new StAXResult( pipeinput );
+//            tt.transform( source, result, dhisResolver );
+            transform( dataSource, result, null );
+            log.info( "transform successful - importing dxf" );
+
+            // set reader to output of pipe
+            return new DefaultXMLEventReader( (XMLEventReader2) pipeoutput );
+        }
+
 }

=== modified file 'dhis-2/dhis-services/dhis-service-xml/src/test/java/org/amplecode/staxwax/framework/XPathFilterTest.java'
--- dhis-2/dhis-services/dhis-service-xml/src/test/java/org/amplecode/staxwax/framework/XPathFilterTest.java	2010-04-29 22:42:08 +0000
+++ dhis-2/dhis-services/dhis-service-xml/src/test/java/org/amplecode/staxwax/framework/XPathFilterTest.java	2010-05-13 21:14:53 +0000
@@ -41,6 +41,7 @@
 {
 
     private InputStream inputStreamB;
+    private InputStream inputStreamC;
 
     @Override
     protected void setUp() throws Exception
@@ -48,6 +49,7 @@
         super.setUp();
         ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
         inputStreamB = classLoader.getResourceAsStream( "dataB.xml" );
+        inputStreamC = classLoader.getResourceAsStream( "dataC.xml" );
     }
 
     @Override
@@ -55,6 +57,7 @@
         throws Exception
     {
         inputStreamB.close();
+        inputStreamC.close();
     }
 
     public synchronized void testFindText()
@@ -80,4 +83,12 @@
             "/dataElements/dataElement[(@code='code2') or (@code='code3')]/description" );
         assertEquals( 2, result.getLength() );
     }
+
+    public synchronized void testFindNumber()
+    {
+        String result;
+        result = XPathFilter.findText( inputStreamC,
+            "/root/dataElements/@id" );
+        assertEquals( "42", result );
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-xml/src/test/resources/dataC.xml'
--- dhis-2/dhis-services/dhis-service-xml/src/test/resources/dataC.xml	2010-02-19 18:38:02 +0000
+++ dhis-2/dhis-services/dhis-service-xml/src/test/resources/dataC.xml	2010-05-13 21:14:53 +0000
@@ -1,6 +1,6 @@
 <?xml version='1.0'?>
-<root>
-	<dataElements>
+<root xmlns="dummy">
+	<dataElements id="42">
 		<dataElement name="name1" shortName="shortName1" code="code1"></dataElement>
 		<dataElement name="name2" shortName="shortName2" code="code2"></dataElement>
 		<dataElement name="name3" shortName="shortName3" code="code3"></dataElement>