forrest-svn mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thors...@apache.org
Subject svn commit: r694232 - in /forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher: helper/StAX.java helper/XSLContractHelper.java impl/XSLContract.java
Date Thu, 11 Sep 2008 12:53:07 GMT
Author: thorsten
Date: Thu Sep 11 05:53:05 2008
New Revision: 694232

URL: http://svn.apache.org/viewvc?rev=694232&view=rev
Log:
Finishing the implementation of the xslContract. Adding methods to extract the bean properties
and specially the xsl template source.

Modified:
    forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/helper/StAX.java
    forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/helper/XSLContractHelper.java
    forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/impl/XSLContract.java

Modified: forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/helper/StAX.java
URL: http://svn.apache.org/viewvc/forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/helper/StAX.java?rev=694232&r1=694231&r2=694232&view=diff
==============================================================================
--- forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/helper/StAX.java
(original)
+++ forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/helper/StAX.java
Thu Sep 11 05:53:05 2008
@@ -27,6 +27,9 @@
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
+import javax.xml.stream.util.XMLEventAllocator;
+
+import com.ctc.wstx.evt.DefaultEventAllocator;
 
 /**
  * Helper class that eases the usage of StAX in your plugins.
@@ -47,6 +50,7 @@
    */
   public StAX() {
     inputFactory = XMLInputFactory.newInstance();
+    inputFactory.setEventAllocator(DefaultEventAllocator.getDefaultInstance());
     outputFactory = XMLOutputFactory.newInstance();
     eventFactory = XMLEventFactory.newInstance();
   }
@@ -107,11 +111,19 @@
   }
 
   /**
-   * Get the ready to used EventFactory
+   * Get the ready to use EventFactory
    * 
-   * @return ready to used EventFactory
+   * @return ready to use EventFactory
    */
   public XMLEventFactory getEventFactory() {
     return eventFactory;
   }
+  
+  /**
+   *  Get the ready to use Event Allocator
+   * @return ready to use Event Allocator
+   */
+  public XMLEventAllocator getEventAllocator() {
+    return  inputFactory.getEventAllocator();
+  }
 }

Modified: forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/helper/XSLContractHelper.java
URL: http://svn.apache.org/viewvc/forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/helper/XSLContractHelper.java?rev=694232&r1=694231&r2=694232&view=diff
==============================================================================
--- forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/helper/XSLContractHelper.java
(original)
+++ forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/helper/XSLContractHelper.java
Thu Sep 11 05:53:05 2008
@@ -19,27 +19,39 @@
 import java.io.BufferedInputStream;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
-
+import java.io.StringReader;
+import java.util.HashMap;
+import java.util.Iterator;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.stream.XMLEventWriter;
 import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
+import javax.xml.stream.util.XMLEventAllocator;
 import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Result;
 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.stream.StreamSource;
 
 import org.apache.forrest.dispatcher.helper.StAX;
 import org.apache.forrest.dispatcher.impl.XSLContract;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
 
 public class XSLContractHelper extends StAX {
 
   private Transformer transformer = null;
 
-  private boolean allowXmlProperties = false;
-
   public static final String NS = "http://apache.org/forrest/templates/1.0";
 
   public static final String CONTRACT_ELEMENT = "contract";
@@ -65,9 +77,12 @@
    * @param allowXmlProperties
    *          whether or not we want to allow xml properties
    * @throws TransformerConfigurationException
+   * @throws ParserConfigurationException 
+   * @throws IOException 
+   * @throws SAXException 
    */
-  public void prepareTransformation(Source xslSource, boolean allowXmlProperties)
-      throws TransformerConfigurationException {
+  public void prepareTransformation(Source xslSource, boolean allowXmlProperties, HashMap<String,
?> params)
+      throws TransformerConfigurationException, ParserConfigurationException, SAXException,
IOException {
     TransformerFactory transFact = TransformerFactory.newInstance();
     // prepare transformation
     transformer = transFact.newTransformer(xslSource);
@@ -75,7 +90,20 @@
     transformer.setOutputProperty(OutputKeys.INDENT, "yes");
     transformer.setOutputProperty(OutputKeys.METHOD, "xml");
     // do we allow xml properties?
-    this.allowXmlProperties = allowXmlProperties;
+    if(!allowXmlProperties){
+      for (Iterator<String> iter = params.keySet().iterator(); iter.hasNext();) {
+        String key = iter.next();
+        String value = (String) params.get(key);
+        transformer.setParameter(key,value);
+      }
+    }else{
+      DocumentBuilder builder = DocumentBuilderFactory.newInstance()
+      .newDocumentBuilder();
+      for (Iterator<String> iter = params.keySet().iterator(); iter.hasNext();) {
+        String key = iter.next();
+        transformer.setParameter(key, builder.parse((InputSource) params.get(key)));
+      }
+    }
   }
 
   /**
@@ -122,12 +150,11 @@
   public void setTemplate(InputStream stream, XSLContract contract)
       throws XMLStreamException {
     XMLStreamReader parser = getParser(stream);
-    ByteArrayOutputStream out = new ByteArrayOutputStream();
-    XMLStreamWriter writer = getStreamWriter(out);
     boolean process = true;
     while (process) {
       int event = parser.next();
       switch (event) {
+
       case XMLStreamConstants.END_DOCUMENT:
         process = false;
         break;
@@ -137,10 +164,123 @@
         if (localName.equals(CONTRACT_ELEMENT)) {
           contract.setName(processContract(parser));
         }
+        if (localName.equals(DESCRIPTION_ELEMENT)) {
+          contract.setDescription(processDescription(parser));
+        }
+        if (localName.equals(USAGE_ELEMENT)) {
+          contract.setUsage(processUsage(parser));
+        }
+
+        if (localName.equals(TEMPLATE_ELEMENT)) {
+          contract.setXslSource(processTemplate(parser));
+        }
+
+      default:
+        break;
       }
     }
   }
 
+  private Source processTemplate(XMLStreamReader parser)
+      throws XMLStreamException {
+    ByteArrayOutputStream out = new ByteArrayOutputStream();
+    XMLEventWriter writer = getWriter(out);
+    XMLEventAllocator allocator = getEventAllocator();
+    String role = "";
+    for (int i = 0; i < parser.getAttributeCount(); i++) {
+      // Get attribute name
+      String localName = parser.getAttributeLocalName(i);
+      if (localName.equals(TEMPLATE_FORMAT_ATT)) {
+        // Return value
+        role = parser.getAttributeValue(i);
+      }
+    }
+    boolean process = true;
+    if (role.equals("xsl")) {
+      while (process) {
+        int event = parser.next();
+        switch (event) {
+
+        case XMLStreamConstants.END_ELEMENT:
+          if (parser.getNamespaceURI() != null) {
+            if (parser.getNamespaceURI().equals(NS)
+                & parser.getLocalName().equals(TEMPLATE_ELEMENT)) {
+              process = false;
+            } else {
+              writer.add(allocator.allocate(parser));
+            }
+          } else {
+            writer.add(allocator.allocate(parser));
+          }
+          break;
+
+        default:
+          writer.add(allocator.allocate(parser));
+          break;
+        }
+      }
+    }
+    writer.flush();
+    //log.debug(out.toString());
+    Source source = new StreamSource(new BufferedInputStream(
+        new ByteArrayInputStream(out.toByteArray())));
+    return source;
+  }
+
+  private String processUsage(XMLStreamReader parser) throws XMLStreamException {
+    boolean process = true;
+    String usage = "";
+    while (process) {
+      int event = parser.next();
+      switch (event) {
+
+      case XMLStreamConstants.CHARACTERS:
+        if (parser.getText().replace(" ", "").length() > 1) {
+          usage = parser.getText().trim();
+        }
+        break;
+
+      case XMLStreamConstants.END_ELEMENT:
+        if (parser.getLocalName().equals(USAGE_ELEMENT)) {
+          process = false;
+        }
+        break;
+
+      default:
+        break;
+      }
+    }
+    return usage;
+  }
+
+  private String processDescription(XMLStreamReader parser)
+      throws XMLStreamException {
+    boolean process = true;
+    String description = "";
+    while (process) {
+      int event = parser.next();
+      switch (event) {
+
+      case XMLStreamConstants.CHARACTERS:
+        if (parser.getText().replace(" ", "").length() > 1) {
+          description = parser.getText().trim();
+        }
+        break;
+
+      case XMLStreamConstants.END_ELEMENT:
+        if (parser.getLocalName().equals(DESCRIPTION_ELEMENT)) {
+          process = false;
+        }
+        break;
+
+      default:
+        break;
+      }
+    }
+
+    return description;
+  }
+
   private String processContract(XMLStreamReader parser) {
     String contractName = "";
     for (int i = 0; i < parser.getAttributeCount(); i++) {
@@ -154,4 +294,9 @@
     }
     return contractName;
   }
+
+  public void transform(InputStream dataStream, Result streamResult) throws TransformerException
{
+    Source dataSource = new StreamSource(dataStream);
+    transformer.transform(dataSource, streamResult);
+  }
 }

Modified: forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/impl/XSLContract.java
URL: http://svn.apache.org/viewvc/forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/impl/XSLContract.java?rev=694232&r1=694231&r2=694232&view=diff
==============================================================================
--- forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/impl/XSLContract.java
(original)
+++ forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/impl/XSLContract.java
Thu Sep 11 05:53:05 2008
@@ -16,13 +16,20 @@
  */
 package org.apache.forrest.dispatcher.impl;
 
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
-import java.io.OutputStream;
 import java.util.HashMap;
 
+import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.stream.XMLStreamException;
+import javax.xml.transform.Result;
 import javax.xml.transform.Source;
 import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.stream.StreamResult;
 
 import org.apache.forrest.dispatcher.DispatcherException;
 import org.apache.forrest.dispatcher.api.Contract;
@@ -42,8 +49,8 @@
     this.allowXmlProperties = allowXmlProperties;
   }
 
-  public OutputStream execute(InputStream dataStream, HashMap properties)
-      throws DispatcherException {
+  public BufferedInputStream execute(InputStream dataStream,
+      HashMap<String, ?> properties) throws DispatcherException {
     if (xslSource == null || helper == null) {
       throw new DispatcherException("Contract \"" + name
           + "\" has produced an exception"
@@ -54,12 +61,17 @@
       /*
        * prepare the transformation in the helper class
        */
-      helper.prepareTransformation(xslSource, allowXmlProperties);
+      helper.prepareTransformation(xslSource, allowXmlProperties, properties);
     } catch (TransformerConfigurationException e) {
       throw new DispatcherException("Contract \"" + name
           + "\" has produced an exception"
           + "Could not setup the transformer for"
-          + "the contract. We cannot proceed without this.",e);
+          + "the contract. We cannot proceed without this.", e);
+    } catch (Exception e) {
+      throw new DispatcherException("Contract \"" + name
+          + "\" has produced an exception"
+          + "Could not setup the DOM Parser for"
+          + "the contract. We cannot proceed without this.", e);
     }
     /*
      * If no dataStream is present we need to create an empty xml doc to be able
@@ -72,11 +84,22 @@
         throw new DispatcherException("Contract \"" + name
             + "\" has produced an exception"
             + "Could not create an empty xml document for"
-            + "the contract. We cannot proceed without this.",e);
+            + "the contract. We cannot proceed without this.", e);
       }
     }
-
-    return null;
+    ByteArrayOutputStream out = new ByteArrayOutputStream();
+    // create a StreamResult and use it for the transformation
+    Result streamResult = new StreamResult(new BufferedOutputStream(out));
+    try {
+      helper.transform(dataStream,streamResult);
+    } catch (TransformerException e) {
+      throw new DispatcherException("Contract \"" + name
+          + "\" has produced an exception"
+          + "Could not invoke the transformation for"
+          + "the contract. We cannot proceed without this.", e);
+    }
+    log.debug(out.toString());
+    return new BufferedInputStream(new ByteArrayInputStream(out.toByteArray()));
   }
 
   public void initializeFromStream(InputStream stream)
@@ -126,7 +149,7 @@
   public void setAllowXmlProperties(boolean allowXmlProperties) {
     this.allowXmlProperties = allowXmlProperties;
   }
-  
+
   public Source getXslSource() {
     return xslSource;
   }



Mime
View raw message