changeset 11:3cb753564552

Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase gnv/trunk@90 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 15 Sep 2009 14:19:55 +0000
parents e6c47838eee6
children 4ebe57b170d3
files gnv/Changelog gnv/src/main/java/de/intevation/gnv/action/SelectArtifactFactoryAction.java gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java gnv/src/main/java/de/intevation/gnv/util/XMLUtils.java gnv/src/main/java/de/intevation/gnv/util/XSLTransformer.java gnv/src/main/webapp/WEB-INF/config/templates/describe-ui.xsl gnv/src/main/webapp/WEB-INF/jsp/index.jsp gnv/src/test/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClientTestCase.java gnv/src/test/java/de/intevation/gnv/util/XSLTransformerTestCase.java gnv/src/test/ressources/describe-ui-test.xml
diffstat 10 files changed, 237 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/gnv/Changelog	Tue Sep 15 10:12:56 2009 +0000
+++ b/gnv/Changelog	Tue Sep 15 14:19:55 2009 +0000
@@ -1,3 +1,23 @@
+2009-09-15  Tim Englich  <tim.englich@intevation.de>
+
+    * src/test/java/de/intevation/gnv/util/XSLTransformerTestCase.java: 
+      New Testcase for testing the usability of XSL-Transformations
+    * src/test/ressources/describe-ui-test.xml Added:
+      An Sample-Document for testing XSL-Transformations 
+    * src/main/webapp/WEB-INF/jsp/index.jsp Edited:
+      Added the html-Fragment of the transformated Describe-Methode-response to the document. 
+    * src/main/webapp/WEB-INF/config/templates/describe-ui.xsl Added:
+      XSL-Template for transforming an Xform-Element to a HTML-Select-Element 
+    * src/main/java/de/intevation/gnv/util/XSLTransformer.java Added:
+      XSl-Transformer for doing XSL-Transformations of the ResultDocuments of 
+      the ArtifactDatabase 
+    * src/main/java/de/intevation/gnv/util/XMLUtils.java Edited:
+      Integrate a Method for writing an XML-Node to a String for debugging. 
+    * src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java: 
+      Some Unused imports removed an select the first Child of the ui Node.
+    * src/main/java/de/intevation/gnv/action/SelectArtifactFactoryAction.java (execute) Edited:
+      Integration of the Describe-Stuff for rendering an GUi 
+      
 2009-09-15  Tim Englich  <tim.englich@intevation.de>
 
     * src/test/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClientTestCase.java (testArtifactDatabaseClient) Edited:
--- a/gnv/src/main/java/de/intevation/gnv/action/SelectArtifactFactoryAction.java	Tue Sep 15 10:12:56 2009 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/action/SelectArtifactFactoryAction.java	Tue Sep 15 14:19:55 2009 +0000
@@ -17,6 +17,7 @@
 import de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClientFactory;
 import de.intevation.gnv.artifactdatabase.objects.ArtifactDescription;
 import de.intevation.gnv.artifactdatabase.objects.ArtifactObject;
+import de.intevation.gnv.util.XSLTransformer;
 
 /**
  * @author Tim Englich <tim.englich@intevation.de>
@@ -48,8 +49,10 @@
         
         ArtifactDescription artifactdescription = adc.getCurrentStepDescription(sm.getSelectedArtifactFactory(), artifact);
         
-        // TODO Initiales Describe Abrufen
+        XSLTransformer transformer = new XSLTransformer();
+        String ui = transformer.transform(artifactdescription.getCurrentUI(), "UTF-8", request.getRealPath("WEB-INF/config/templates/describe-ui.xsl"));
         
+        request.setAttribute("ui", ui);
         
         // return succsess
         return super.execute(mapping, form, request, response);
--- a/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java	Tue Sep 15 10:12:56 2009 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java	Tue Sep 15 14:19:55 2009 +0000
@@ -5,24 +5,12 @@
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerConfigurationException;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.TransformerFactoryConfigurationError;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
-
 import org.apache.log4j.Logger;
 import org.restlet.Client;
 import org.restlet.data.Method;
@@ -271,6 +259,7 @@
         if (artifact instanceof ArtifactDescription){
             ArtifactDescription ad = (ArtifactDescription)artifact;
             Node uiNode =  xmlUtils.getNodeXPath(document, "/result/ui");
+            uiNode = uiNode.getFirstChild();
             Node outputNode = xmlUtils.getNodeXPath(document, "/result/outputs");
             String currentState = xmlUtils.getStringXPath(document, "/result/state/@name");
             NodeList statesList = xmlUtils.getNodeSetXPath(document, "/result/reachable-states/state/@name");
--- a/gnv/src/main/java/de/intevation/gnv/util/XMLUtils.java	Tue Sep 15 10:12:56 2009 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/util/XMLUtils.java	Tue Sep 15 14:19:55 2009 +0000
@@ -2,6 +2,7 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.StringBufferInputStream;
 import java.io.StringWriter;
 
 import org.w3c.dom.Document;
@@ -175,15 +176,10 @@
         return returnValue;
     }
     
-    public String writeDocument2String(Document document){
+    public String writeNode2String(Node node){
         try {
-            TransformerFactory transformerFactory = TransformerFactory.newInstance();
-            Transformer transformer = transformerFactory.newTransformer();
-            DOMSource source = new DOMSource(document);
-            StringWriter sw = new StringWriter();
-            StreamResult result =  new StreamResult(sw);
-            transformer.transform(source, result);
-            return sw.getBuffer().toString();
+            DOMSource source = new DOMSource(node);
+            return writeDOMSource2String(source);
         } catch (TransformerConfigurationException e) {
             logger.error(e,e);
         } catch (TransformerFactoryConfigurationError e) {
@@ -193,5 +189,44 @@
         }
         return null;
     }
+    
+    public Document reInitDocument(Document document){
+        
+        StringBufferInputStream inputStream = new StringBufferInputStream(this.writeDocument2String(document));
+        return this.readDocument(inputStream);
+    }
+    
+    
+    public String writeDocument2String(Document document){
+        try {
+            DOMSource source = new DOMSource(document);
+            return writeDOMSource2String(source);
+        } catch (TransformerConfigurationException e) {
+            logger.error(e,e);
+        } catch (TransformerFactoryConfigurationError e) {
+            logger.error(e,e);
+        } catch (TransformerException e) {
+            logger.error(e,e);
+        }
+        return null;
+    }
+
+    /**
+     * @param source
+     * @return
+     * @throws TransformerFactoryConfigurationError
+     * @throws TransformerConfigurationException
+     * @throws TransformerException
+     */
+    private String writeDOMSource2String(DOMSource source)
+            throws TransformerFactoryConfigurationError,
+            TransformerConfigurationException, TransformerException {
+        TransformerFactory transformerFactory = TransformerFactory.newInstance();
+        Transformer transformer = transformerFactory.newTransformer();
+        StringWriter sw = new StringWriter();
+        StreamResult result =  new StreamResult(sw);
+        transformer.transform(source, result);
+        return sw.getBuffer().toString();
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/util/XSLTransformer.java	Tue Sep 15 14:19:55 2009 +0000
@@ -0,0 +1,66 @@
+/**
+ *
+ */
+package de.intevation.gnv.util;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.StringBufferInputStream;
+import java.io.UnsupportedEncodingException;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerFactoryConfigurationError;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.apache.log4j.Logger;
+import org.w3c.dom.Node;
+
+/**
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public class XSLTransformer {
+    /**
+     * the logger, used to log exceptions and additonaly information
+     */
+    private static Logger log = Logger.getLogger(XSLTransformer.class);
+    /**
+     * Constructor
+     */
+    public XSLTransformer() {
+        super();
+    }
+    
+    public String transform(Node toTransform, String encoding, String templateFileName){
+        String resultValue = null;
+            try {
+                XMLUtils xmlUtils = new XMLUtils();
+                Source templateSource = new StreamSource(new File(templateFileName));
+                TransformerFactory xformFactory = TransformerFactory.newInstance();
+                Transformer transformer = xformFactory.newTransformer(templateSource);
+                ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                StreamResult scrResult =  new StreamResult(baos);
+                log.debug(xmlUtils.writeNode2String(toTransform));
+                DOMSource source = new DOMSource(toTransform);
+                transformer.transform(source, scrResult);
+                resultValue = new String(baos.toByteArray(),encoding);
+            } catch (TransformerConfigurationException e) {
+                log.error(e,e);
+            } catch (UnsupportedEncodingException e) {
+                log.error(e,e);
+            } catch (TransformerFactoryConfigurationError e) {
+                log.error(e,e);
+            } catch (TransformerException e) {
+                log.error(e,e);
+            }
+        
+        return resultValue;
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv/src/main/webapp/WEB-INF/config/templates/describe-ui.xsl	Tue Sep 15 14:19:55 2009 +0000
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet version="1.0" 
+                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
+                xmlns="http://www.w3.org/2002/xforms" 
+                xmlns:xform="http://www.w3.org/2002/xforms" 
+                xmlns:art="http://www.intevation.de/2009/artifacts">
+    <xsl:output method="html" version="1.0" encoding="UTF-8" omit-xml-declaration="no"/>
+    <xsl:template match="*[local-name() = 'select']">
+    <xsl:variable name="selectName" select="@ref"/>
+        <select name="{$selectName}"> <xsl:apply-templates/></select>
+    </xsl:template>
+    <xsl:template match="*[local-name() = 'item']">
+        <xsl:variable name="optionValue" select="*[local-name() = 'value']"/>
+            <option value="{$optionValue}"><xsl:value-of select="*[local-name() = 'label']"></xsl:value-of></option>
+    </xsl:template>
+    <xsl:template match="*[local-name() = 'label']">
+    </xsl:template>
+</xsl:stylesheet>
\ No newline at end of file
--- a/gnv/src/main/webapp/WEB-INF/jsp/index.jsp	Tue Sep 15 10:12:56 2009 +0000
+++ b/gnv/src/main/webapp/WEB-INF/jsp/index.jsp	Tue Sep 15 14:19:55 2009 +0000
@@ -6,11 +6,15 @@
 <%@page import="java.util.Iterator"%><html>
 <body>
 <form action="selectArtifactFactory.do" method="post">
+
+<%
+  Object ui = request.getAttribute("ui");
+  SessionModel sm = SessionModelFactory.getInstance().getSessionModel(request);
+  Collection<ArtifactObject> artifactFactories = sm.getArtifactFactories();
+  if (artifactFactories != null && ui == null){
+%>
 <select name="artifactFactory">
 <%
-  SessionModel sm = SessionModelFactory.getInstance().getSessionModel(request);
-  Collection<ArtifactObject> artifactFactories = sm.getArtifactFactories();
-  if (artifactFactories != null){
       Iterator<ArtifactObject> it = artifactFactories.iterator();
       while (it.hasNext()){
           ArtifactObject ao = it.next();
@@ -20,9 +24,15 @@
       <option value="<%=ao.getId()%>" <%=(ao.isSelected() ? "selected=\"selected\"" : "")%> ><%=ao.getName()%></option>
 <%
       }
-  }
 %>
     </select>
+<%
+}
+%>
+
+    
+    <%=ui != null ? ui.toString() : "" %>
+    
     <input type="submit" value="Auswählen" />
 </form>
 </body>
--- a/gnv/src/test/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClientTestCase.java	Tue Sep 15 10:12:56 2009 +0000
+++ b/gnv/src/test/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClientTestCase.java	Tue Sep 15 14:19:55 2009 +0000
@@ -10,7 +10,9 @@
 import org.apache.log4j.Logger;
 
 import de.intevation.gnv.artifactdatabase.client.exception.ArtifactDatabaseClientException;
+import de.intevation.gnv.artifactdatabase.objects.ArtifactDescription;
 import de.intevation.gnv.artifactdatabase.objects.ArtifactObject;
+import de.intevation.gnv.util.XSLTransformer;
 
 import junit.framework.TestCase;
 
@@ -54,8 +56,11 @@
                 while(it.hasNext()){
                     ArtifactObject aof = it.next();
                     ArtifactObject artifact = artifactDatabaseClient.createNewArtifact(aof);
-                    artifactDatabaseClient.getCurrentStepDescription(aof, artifact);
+                    ArtifactDescription ad = artifactDatabaseClient.getCurrentStepDescription(aof, artifact);
                     log.debug("Artifact aktualisert.");
+                    XSLTransformer transformer = new XSLTransformer();
+                    String transformedUI = transformer.transform(ad.getCurrentUI(), "UTF-8",("src/main/webapp/WEB-INF/config/templates/describe-ui.xsl"));
+                    log.debug(transformedUI);
                 }
             }
             
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv/src/test/java/de/intevation/gnv/util/XSLTransformerTestCase.java	Tue Sep 15 14:19:55 2009 +0000
@@ -0,0 +1,55 @@
+/**
+ *
+ */
+package de.intevation.gnv.util;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+
+import org.apache.log4j.BasicConfigurator;
+import org.apache.log4j.Logger;
+import org.w3c.dom.Document;
+
+import de.intevation.gnv.artifactdatabase.client.DefaultArtifactDatabaseClient;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public class XSLTransformerTestCase extends TestCase {
+
+    /**
+     * the logger, used to log exceptions and additonaly information
+     */
+    private static Logger log = null;
+    
+    static{
+        BasicConfigurator.configure();
+        log = Logger.getLogger(DefaultArtifactDatabaseClient.class);
+    }
+    
+    public XSLTransformerTestCase() {
+        super();
+    }
+
+    public XSLTransformerTestCase(String name) {
+        super(name);
+    }
+
+    
+    public void testDescribeUI(){
+        
+        try {
+            //System.setProperty("javax.xml.transform.TransformerFactory","com.icl.saxon.TransformerFactoryImpl");
+            Document document = new XMLUtils().readDocument(new FileInputStream(new File("src/test/ressources/describe-ui-test.xml")));
+            XSLTransformer transformer = new XSLTransformer();
+            String value = transformer.transform(document, "UTF-8", "src/main/webapp/WEB-INF/config/templates/describe-ui.xsl");
+            log.debug(value);
+        } catch (FileNotFoundException e) {
+            log.error(e,e);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv/src/test/ressources/describe-ui-test.xml	Tue Sep 15 14:19:55 2009 +0000
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xform:select xmlns:xform="http://www.w3.org/2002/xforms" ref="product">
+	<xform:label>product</xform:label>
+	<xform:choices>
+		<xform:item>
+			<xform:label>timeSeries</xform:label>
+			<xform:value>timeSeries</xform:value>
+		</xform:item>
+	</xform:choices>
+</xform:select>

http://dive4elements.wald.intevation.org