You are viewing a plain text version of this content. The canonical link for it is here.
Posted to svn@forrest.apache.org by th...@apache.org on 2008/10/07 15:08:05 UTC

svn commit: r702468 - in /forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher: config/ factories/ impl/ impl/helper/

Author: thorsten
Date: Tue Oct  7 06:08:05 2008
New Revision: 702468

URL: http://svn.apache.org/viewvc?rev=702468&view=rev
Log:
Transformer factories need an UriResolver to resolve imports and alike. Extending code to use this new parameter.

Modified:
    forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/config/DispatcherBean.java
    forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/factories/ContractFactory.java
    forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/XSLContract.java
    forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/XSLContractHelper.java

Modified: forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/config/DispatcherBean.java
URL: http://svn.apache.org/viewvc/forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/config/DispatcherBean.java?rev=702468&r1=702467&r2=702468&view=diff
==============================================================================
--- forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/config/DispatcherBean.java (original)
+++ forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/config/DispatcherBean.java Tue Oct  7 06:08:05 2008
@@ -16,6 +16,8 @@
  */
 package org.apache.forrest.dispatcher.config;
 
+import javax.xml.transform.URIResolver;
+
 import org.apache.forrest.dispatcher.api.Resolver;
 
 /**
@@ -28,7 +30,17 @@
 public class DispatcherBean {
   
   private boolean allowXmlProperties = false;
+  private boolean shrink=true;
+  public boolean isShrink() {
+    return shrink;
+  }
+
+  public void setShrink(boolean shrink) {
+    this.shrink = shrink;
+  }
+
   private Resolver resolver = null;
+  private URIResolver uriResolver = null;
 
   private String contractUriPrefix = "";
   private String contractUriSufix = ".contract.xml";
@@ -65,4 +77,12 @@
     this.contractUriSufix = contractUriSufix;
   }
 
+  public void setUriResolver(URIResolver uriResolver) {
+    this.uriResolver = uriResolver;
+  }
+
+  public URIResolver getUriResolver() {
+    return uriResolver;
+  }
+
 }

Modified: forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/factories/ContractFactory.java
URL: http://svn.apache.org/viewvc/forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/factories/ContractFactory.java?rev=702468&r1=702467&r2=702468&view=diff
==============================================================================
--- forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/factories/ContractFactory.java (original)
+++ forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/factories/ContractFactory.java Tue Oct  7 06:08:05 2008
@@ -68,9 +68,9 @@
        * requested contract does not exist. Otherwise it is a xsl based
        * contract.
        */
-      contract = new XSLContract(config.isAllowXmlProperties());
-      InputStream xslStream = config.getResolver().resolve(
-          config.getContractUriPrefix() + name + config.getContractUriSufix());
+      contract = new XSLContract(config.isAllowXmlProperties(),config.getUriResolver());
+      String uri = config.getContractUriPrefix() + name + config.getContractUriSufix();
+      InputStream xslStream = config.getResolver().resolve(uri);
       contract.initializeFromStream(xslStream);
       // closing stream
       if (xslStream != null) {

Modified: forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/XSLContract.java
URL: http://svn.apache.org/viewvc/forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/XSLContract.java?rev=702468&r1=702467&r2=702468&view=diff
==============================================================================
--- forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/XSLContract.java (original)
+++ forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/XSLContract.java Tue Oct  7 06:08:05 2008
@@ -21,13 +21,14 @@
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
-import java.util.HashMap;
+import java.util.Map;
 
 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.URIResolver;
 import javax.xml.transform.stream.StreamResult;
 
 import org.apache.forrest.dispatcher.api.Contract;
@@ -39,17 +40,22 @@
 public class XSLContract extends Loggable implements Contract {
 
   private String name = "", usage = "", description = "";
-  private XSLContractHelper helper = null;
+  private final XSLContractHelper helper;
   private Source xslSource = null;
 
   private boolean allowXmlProperties = false;
 
-  public XSLContract(boolean allowXmlProperties) throws DispatcherException {
+  public XSLContract(boolean allowXmlProperties, URIResolver uriResolver) throws DispatcherException {
     this.allowXmlProperties = allowXmlProperties;
+    /*
+     * get a new instance of the corresponding helper class since the helper is
+     * doing the actual work
+     */
+    helper = new XSLContractHelper(uriResolver);
   }
 
   public BufferedInputStream execute(InputStream dataStream,
-      HashMap<String, ?> properties) throws DispatcherException {
+      Map<String, Object> properties) throws DispatcherException {
     if (xslSource == null || helper == null) {
       throw new DispatcherException("Contract \"" + name
           + "\" has produced an exception. "
@@ -102,11 +108,6 @@
 
   public void initializeFromStream(InputStream stream)
       throws DispatcherException {
-    /*
-     * get a new instance of the corresponding helper class since the helper is
-     * doing the actual work
-     */
-    helper = new XSLContractHelper();
     // invoke the extraction
     try {
       helper.setTemplate(stream, this);

Modified: forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/XSLContractHelper.java
URL: http://svn.apache.org/viewvc/forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/XSLContractHelper.java?rev=702468&r1=702467&r2=702468&view=diff
==============================================================================
--- forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/XSLContractHelper.java (original)
+++ forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/XSLContractHelper.java Tue Oct  7 06:08:05 2008
@@ -16,11 +16,13 @@
  */
 package org.apache.forrest.dispatcher.impl.helper;
 
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.HashMap;
 import java.util.Iterator;
+import java.util.Map;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -38,15 +40,21 @@
 import javax.xml.transform.TransformerConfigurationException;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.URIResolver;
 import javax.xml.transform.stream.StreamSource;
 
+import org.apache.forrest.dispatcher.exception.DispatcherException;
 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 URIResolver uriResolver = null;
+
+  public XSLContractHelper(URIResolver uriResolver) {
+    this.uriResolver = uriResolver;
+  }
 
   /**
    * This method will prepare the transformation from the data and the xsl
@@ -61,9 +69,12 @@
    * @throws IOException 
    * @throws SAXException 
    */
-  public void prepareTransformation(Source xslSource, boolean allowXmlProperties, HashMap<String, ?> params)
+  public void prepareTransformation(Source xslSource, boolean allowXmlProperties, Map<String, Object> params)
       throws TransformerConfigurationException, ParserConfigurationException, SAXException, IOException {
     TransformerFactory transFact = TransformerFactory.newInstance();
+    if(null != uriResolver){
+      transFact.setURIResolver(uriResolver);
+    }
     // prepare transformation
     transformer = transFact.newTransformer(xslSource);
     transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
@@ -75,10 +86,14 @@
       .newDocumentBuilder();
       for (Iterator<String> iter = params.keySet().iterator(); iter.hasNext();) {
         String key = iter.next();
-        Class<InputSource> source = InputSource.class;
+        Class<String> string = String.class;
         Object value = params.get(key);
-        if (source.isInstance(value)){
-          transformer.setParameter(key, builder.parse((InputSource)value));
+        if (!string.isInstance(value)){
+         if(log.isDebugEnabled()){
+           log.debug( new String((byte[])value));
+         }
+          BufferedInputStream valueStream = new BufferedInputStream(new ByteArrayInputStream((byte[]) value));
+          transformer.setParameter(key, builder.parse(valueStream).getFirstChild());
         }else{
           transformer.setParameter(key,value);
         }
@@ -134,10 +149,11 @@
    * @param stream
    * @param contract
    * @throws XMLStreamException
+   * @throws DispatcherException 
    */
   public void setTemplate(InputStream stream, XSLContract contract)
-      throws XMLStreamException {
-    XMLStreamReader reader = getReader(stream);
+      throws XMLStreamException, DispatcherException {
+    XMLStreamReader reader = getNSReader(stream);
     boolean process = true;
     while (process) {
       int event = reader.next();
@@ -169,49 +185,52 @@
     }
   }
 
-  private Source processTemplate(XMLStreamReader reader)
-      throws XMLStreamException {
+  private Source processTemplate(XMLStreamReader reader) throws DispatcherException{
     ByteArrayOutputStream out = new ByteArrayOutputStream();
-    XMLEventWriter writer = getWriter(out);
-    // Making sure we always have all ns 
-    // A wee bit brute force but it works
-    XMLEventAllocator allocator = new NSAllocator(true);
-    String role = "";
-    for (int i = 0; i < reader.getAttributeCount(); i++) {
-      // Get attribute name
-      String localName = reader.getAttributeLocalName(i);
-      if (localName.equals(Captions.TEMPLATE_FORMAT_ATT)) {
-        // Return value
-        role = reader.getAttributeValue(i);
+    try {
+      XMLEventWriter writer = getWriter(out);
+      // Making sure we always have all ns 
+      // A wee bit brute force but it works
+      XMLEventAllocator allocator = getNSEventAllocator();
+      String role = "";
+      for (int i = 0; i < reader.getAttributeCount(); i++) {
+        // Get attribute name
+        String localName = reader.getAttributeLocalName(i);
+        if (localName.equals(Captions.TEMPLATE_FORMAT_ATT)) {
+          // Return value
+          role = reader.getAttributeValue(i);
+        }
       }
-    }
-    boolean process = true;
-    if (role.equals("xsl")) {
-      while (process) {
-        int event = reader.next();
-        switch (event) {
-
-        case XMLStreamConstants.END_ELEMENT:
-          if (reader.getNamespaceURI() != null) {
-            if (reader.getNamespaceURI().equals(Captions.NS)
-                & reader.getLocalName().equals(Captions.TEMPLATE_ELEMENT)) {
-              process = false;
+      boolean process = true;
+      if (role.equals("xsl")) {
+        while (process) {
+          int event = reader.next();
+          switch (event) {
+
+          case XMLStreamConstants.END_ELEMENT:
+            if (reader.getNamespaceURI() != null) {
+              if (reader.getNamespaceURI().equals(Captions.NS)
+                  && reader.getLocalName().equals(Captions.TEMPLATE_ELEMENT)) {
+                process = false;
+              } else {
+                allocator.allocate(reader,writer);
+              }
             } else {
               allocator.allocate(reader,writer);
             }
-          } else {
+            break;
+
+          default:
             allocator.allocate(reader,writer);
+            break;
           }
-          break;
-
-        default:
-          allocator.allocate(reader,writer);
-          break;
         }
       }
+      writer.flush();
+      log.debug(out.toString());
+    } catch (XMLStreamException e) {
+      throw new DispatcherException("Fatal error in contract",e);
     }
-    writer.flush();
-    log.debug(out.toString());
     Source source = new StreamSource(StreamHelper.switchStream(out));
     return source;
   }