Author: thorsten
Date: Wed Sep 10 06:11:47 2008
New Revision: 693831
URL: http://svn.apache.org/viewvc?rev=693831&view=rev
Log:
Starting to implement the code to parse the contract
Added:
forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/DispatcherException.java
- copied unchanged from r693801,
forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/DispatcherException.java
Modified:
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/XSLContractHelper.java
URL:
http://svn.apache.org/viewvc/forrest/trunk/whiteboard/dispatcher/java/org/apache/forrest/dispatcher/helper/XSLContractHelper.java?rev=693831&r1=693830&r2=693831&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
Wed Sep 10 06:11:47 2008
@@ -21,12 +21,69 @@
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
+import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerFactory;
import org.apache.forrest.dispatcher.helper.StAX;
+import org.apache.forrest.dispatcher.impl.XSLContract;
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";
+
+ public static final String CONTRACT_NAME_ATT = "name";
+
+ public static final String DESCRIPTION_ELEMENT = "description";
+
+ public static final String TEMPLATE_ELEMENT = "template";
+
+ public static final String TEMPLATE_FORMAT_ATT = "inputFormat";
+
+ public static final String USAGE_ELEMENT = "usage";
+
+ public static final String RESULT_XPATH = "xpath";
+
+ /**
+ * This method will prepare the transformation from the data and the xsl
+ * stylesheet from the contract.
+ *
+ * @param xslSource
+ * the xsl stylesheet
+ * @param allowXmlProperties
+ * whether or not we want to allow xml properties
+ * @throws TransformerConfigurationException
+ */
+ public void prepareTransformation(Source xslSource, boolean
allowXmlProperties)
+ throws TransformerConfigurationException {
+ TransformerFactory transFact = TransformerFactory.newInstance();
+ // prepare transformation
+ transformer = transFact.newTransformer(xslSource);
+ transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+ transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+ // do we allow xml properties?
+ this.allowXmlProperties = allowXmlProperties;
+ }
+
+ /**
+ * Will create a dummy xml document with only one element.
+ *
+ * @return ByteArrayOutputStream of the dummy xml document.
+ * @throws XMLStreamException
+ */
public ByteArrayOutputStream createEmptyXmlOutput() throws
XMLStreamException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLStreamWriter writer = getStreamWriter(out);
@@ -38,8 +95,63 @@
return out;
}
+ /**
+ * Will create a dummy xml document with only one element contacting
+ * [EMAIL PROTECTED] #createEmptyXmlOutput()} and then switching the
outputStream to an
+ * inputStream
+ *
+ * @return InputStream of the dummy xml document.
+ * @throws XMLStreamException
+ */
public InputStream createEmptyXml() throws XMLStreamException {
ByteArrayOutputStream out = createEmptyXmlOutput();
return new BufferedInputStream(new
ByteArrayInputStream(out.toByteArray()));
}
+
+ /**
+ * This method sets the xslSource, name, description and usage information of
+ * the contract.
+ *
+ * This is been done by parsing the contract and extracting the corresponding
+ * information.
+ *
+ * @param stream
+ * @param contract
+ * @throws XMLStreamException
+ */
+ 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;
+
+ case XMLStreamConstants.START_ELEMENT:
+ String localName = parser.getLocalName();
+ if (localName.equals(CONTRACT_ELEMENT)) {
+ contract.setName(processContract(parser));
+ }
+ }
+ }
+ }
+
+ private String processContract(XMLStreamReader parser) {
+ String contractName = "";
+ for (int i = 0; i < parser.getAttributeCount(); i++) {
+ // Get attribute name
+ String localName = parser.getAttributeLocalName(i);
+ if (localName.equals(CONTRACT_NAME_ATT)) {
+ // Return value
+ contractName = parser.getAttributeValue(i);
+ return contractName;
+ }
+ }
+ return contractName;
+ }
}
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=693831&r1=693830&r2=693831&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
Wed Sep 10 06:11:47 2008
@@ -21,8 +21,10 @@
import java.util.HashMap;
import javax.xml.stream.XMLStreamException;
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerConfigurationException;
-
+import org.apache.forrest.dispatcher.DispatcherException;
import org.apache.forrest.dispatcher.api.Contract;
import org.apache.forrest.dispatcher.helper.Loggable;
@@ -30,32 +32,66 @@
public class XSLContract extends Loggable implements Contract {
- private String name="", usage="", description="";
- private XSLContractHelper helper=null;
-
- public OutputStream execute(InputStream dataStream, HashMap properties) {
- /*
- * get a new instance of the corresponding helper class since the helper
- * is doing the actual work
- */
- helper = new XSLContractHelper();
+ private String name = "", usage = "", description = "";
+ private XSLContractHelper helper = null;
+ private Source xslSource = null;
+
+ private boolean allowXmlProperties = false;
+
+ public XSLContract(boolean allowXmlProperties) throws DispatcherException {
+ this.allowXmlProperties = allowXmlProperties;
+ }
+
+ public OutputStream execute(InputStream dataStream, HashMap properties)
+ throws DispatcherException {
+ if (xslSource == null || helper == null) {
+ throw new DispatcherException("Contract \"" + name
+ + "\" has produced an exception"
+ + "The xsl source have not been initialize from stream"
+ + "the contract. We cannot proceed without this.");
+ }
+ try {
+ /*
+ * prepare the transformation in the helper class
+ */
+ helper.prepareTransformation(xslSource, allowXmlProperties);
+ } 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);
+ }
/*
- * If no dataStream is present we need to create an empty xml doc
- * to be able to invoke the xsl transformation.
+ * If no dataStream is present we need to create an empty xml doc to be
able
+ * to invoke the xsl transformation.
*/
if (null == dataStream) {
try {
dataStream = helper.createEmptyXml();
} catch (XMLStreamException e) {
- log.fatal(e);
+ 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);
}
}
return null;
}
- public void initializeFromStream(InputStream stream) {
- // TODO Auto-generated method stub
+ 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);
+ } catch (XMLStreamException e) {
+ throw new DispatcherException(e);
+ }
}
@@ -82,4 +118,20 @@
public void setUsage(String usage) {
this.usage = usage;
}
+
+ public boolean isAllowXmlProperties() {
+ return allowXmlProperties;
+ }
+
+ public void setAllowXmlProperties(boolean allowXmlProperties) {
+ this.allowXmlProperties = allowXmlProperties;
+ }
+
+ public Source getXslSource() {
+ return xslSource;
+ }
+
+ public void setXslSource(Source xslSource) {
+ this.xslSource = xslSource;
+ }
}