Author: thorsten
Date: Tue Oct 7 06:15:23 2008
New Revision: 702473
URL: http://svn.apache.org/viewvc?rev=702473&view=rev
Log:
FOR-1118
Adding old resolver code from the old dispatcher. Instead of using streams for
the xml parameter we now use byte [] since they can be reused as much as we
want. Introducyion of the new configuration parameter
Modified:
forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/transformation/DispatcherTransformer.java
Modified:
forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/transformation/DispatcherTransformer.java
URL:
http://svn.apache.org/viewvc/forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/transformation/DispatcherTransformer.java?rev=702473&r1=702472&r2=702473&view=diff
==============================================================================
---
forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/transformation/DispatcherTransformer.java
(original)
+++
forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/transformation/DispatcherTransformer.java
Tue Oct 7 06:15:23 2008
@@ -18,11 +18,17 @@
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
+import java.util.HashMap;
import java.util.Map;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.stream.StreamSource;
+
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
@@ -30,13 +36,14 @@
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.axiom.attachments.utils.IOUtils;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.caching.CacheableProcessingComponent;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.transformation.AbstractSAXTransformer;
import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceException;
import org.apache.excalibur.source.SourceValidity;
-import org.apache.excalibur.source.impl.validity.AggregatedValidity;
import org.apache.excalibur.xml.sax.SAXParser;
import org.apache.forrest.dispatcher.api.Structurer;
import org.apache.forrest.dispatcher.config.DispatcherBean;
@@ -44,6 +51,7 @@
import org.apache.forrest.dispatcher.impl.XMLStructurer;
import org.apache.forrest.dispatcher.impl.XMLStructurerAxiom;
import org.apache.forrest.dispatcher.impl.helper.Captions;
+import org.apache.forrest.dispatcher.impl.helper.XMLProperties;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -68,24 +76,23 @@
*
*/
public class DispatcherTransformer extends AbstractSAXTransformer implements
- Disposable, CacheableProcessingComponent {
+ Disposable, CacheableProcessingComponent, URIResolver {
/**
* The requested format.
*/
private String requestedFormat;
- // I am not 100% sure whether we really need this information.
- // private String requestId;
+ private String requestId;
/**
* Caching and validity properties
*/
private String cacheKey, validityFile;
- private AggregatedValidity validity;
+ private SourceValidity validity;
- private String validityOverride;
+ private String validityChecking;
private org.apache.excalibur.source.SourceResolver m_resolver;
@@ -137,11 +144,12 @@
*/
private String xpathSupport;
- // I am not 100% sure whether we really need this information.
- /*
- public static final String DISPATCHER_REQUEST_ATTRIBUTE = "request";*/
+ private String contractUriPrefix;
+ public static final String DISPATCHER_REQUEST_ATTRIBUTE = "request";
+ private static final String DEFAULT_VARIABLES = "defaultVariables";
+
/*
* @see
*
org.apache.cocoon.transformation.AbstractSAXTransformer#configure(org.apache
@@ -154,11 +162,14 @@
boolean allowXml = configuration.getChild("allowXml").getValueAsBoolean(
false);
config.setAllowXmlProperties(allowXml);
- String contractUriPrefix = configuration.getChild("contractUriPrefix")
- .getValue("cocoon://resolve.contract");
- config.setContractUriPrefix(contractUriPrefix);
+ contractUriPrefix = configuration.getChild("contractUriPrefix")
+ .getValue("cocoon://resolve.contract.");
config.setContractUriSufix("");
xpathSupport = configuration.getChild("xpathSupport").getValue("basic");
+ boolean shrink = configuration.getChild("shrink").getValueAsBoolean(
+ true);
+ config.setShrink(shrink);
+ config.setUriResolver(this);
}
// we do all the heavy stuff later and only prepare the basics here,
@@ -167,26 +178,22 @@
Parameters par) throws ProcessingException, SAXException, IOException {
super.setup(resolver, objectModel, src, par);
- // I am not 100% sure whether we really need this information.
- /* this.requestId = parameters
- .getParameter(DISPATCHER_REQUEST_ATTRIBUTE, null);*/
+ this.requestId = parameters
+ .getParameter(DISPATCHER_REQUEST_ATTRIBUTE, null);
this.cacheKey = parameters.getParameter(CACHE_PARAMETER, null);
log = getLogger();
if (null == this.cacheKey)
log.warn("Caching not activated! Declare the CACHE_KEY_PARAMETER="
+ CACHE_PARAMETER + " in your sitemap.");
this.validityFile = parameters.getParameter(VALIDITY_PARAMETER, null);
- this.validityOverride = parameters.getParameter(
+ this.validityChecking = parameters.getParameter(
VALIDITY_OVERRIDE_PARAMETER, "");
- this.cacheKey += validityOverride;
- // I am not 100% sure whether we really need this information.
- /*
if (requestId == null) {
String error = "dispatcherError:\n"
+ "You have to set the \"request\" parameter in the sitemap!";
log.error(error);
throw new ProcessingException(error);
- }*/
+ }
this.requestedFormat = parameters.getParameter(Captions.TYPE_ATT, null);
if (requestedFormat == null) {
String error = "dispatcherError:\n"
@@ -194,6 +201,7 @@
log.error(error);
throw new ProcessingException(error);
}
+ this.cacheKey += requestedFormat;
if (null == m_resolver)
try {
m_resolver = (org.apache.excalibur.source.SourceResolver) manager
@@ -218,18 +226,48 @@
} catch (ProcessingException e) {
throw new SAXException(e);
}
+ /*
+ * We need to change now the contract uri prefixes
+ * since in cocoon we need to add the current
+ * format.
+ */
+ String loacalPrefix =contractUriPrefix+requestedFormat+".";
+ config.setContractUriPrefix(loacalPrefix);
+ // start structurer routine
Structurer structurer = null;
+ // need to get the properties for this uri
+ HashMap<String,Object> map = new HashMap<String, Object>();
+ String propertyURI = "cocoon://" + requestId + ".props";
+ try {
+ Source propsSource = m_resolver.resolveURI(propertyURI);
+ if(propsSource!=null){
+ InputStream stream = new
BufferedInputStream(propsSource.getInputStream());
+ // get the bytes from the stream
+ byte [] properties = IOUtils.getStreamAsByteArray(stream);
+ if (config.isAllowXmlProperties()){
+ map.put(DEFAULT_VARIABLES, properties);
+ }else{
+ XMLProperties.parseProperties(stream, map);
+ }
+ release(propsSource);
+ }
+ } catch (Exception e) {
+ throw new SAXException(e);
+ }
+ // which implementation do we want
if (xpathSupport.equals("enhanced")) {
- structurer = new XMLStructurerAxiom(config);
+ structurer = new XMLStructurerAxiom(config,map);
} else {
- structurer = new XMLStructurer(config);
+ structurer = new XMLStructurer(config,map);
}
SAXParser parser = null;
try {
+ // get the result of the structurer as stream
InputStream result = structurer.execute(new BufferedInputStream(
new ByteArrayInputStream(document.getBytes())), requestedFormat);
- // adding the result to the consumer
+ // requesting a parser
parser = (SAXParser) manager.lookup(SAXParser.ROLE);
+ // adding the result to the consumer
parser.parse(new InputSource(result), super.xmlConsumer);
} catch (Exception e) {
throw new SAXException(e);
@@ -257,18 +295,17 @@
* is currently not cacheable.
*/
public SourceValidity getValidity() {
- // You can either request URL?dispatcher.caching=off
+ // You can either request URL?dispatcher.caching=off
// or add this property to forrest.properties.xml
// to force a SourceValidity.INVALID
- if (null != validityFile && !(validityOverride.equals(CACHING_OFF))) {
- this.validity = new AggregatedValidity();
- Source resolveSource = null;
+ if (null != validityFile && !(validityChecking.equals(CACHING_OFF))) {
+ Source resolveSource=null;
try {
- resolveSource = m_resolver.resolveURI(validityFile);
- this.validity.add(resolveSource.getValidity());
+ resolveSource= m_resolver.resolveURI(validityFile);
+ this.validity = resolveSource.getValidity();
} catch (Exception e) {
getLogger().error(e.getMessage());
- } finally {
+ }finally{
release(resolveSource);
}
} else
@@ -313,4 +350,92 @@
super.recycle();
}
+
+//From URIResolver, copied from TraxProcessor
+ public javax.xml.transform.Source resolve(String href, String base)
+ throws TransformerException {
+if (getLogger().isDebugEnabled()) {
+ getLogger().debug(
+ "resolve(href = " + href + ", base = " + base + "); resolver = "
+ + m_resolver);
+}
+
+Source xslSource = null;
+try {
+ if (base == null || href.indexOf(":") > 1) {
+ // Null base - href must be an absolute URL
+ xslSource = m_resolver.resolveURI(href);
+ } else if (href.length() == 0) {
+ // Empty href resolves to base
+ xslSource = m_resolver.resolveURI(base);
+ } else {
+ // is the base a file or a real m_url
+ if (!base.startsWith("file:")) {
+ int lastPathElementPos = base.lastIndexOf('/');
+ if (lastPathElementPos == -1) {
+ // this should never occur as the base should
+ // always be protocol:/....
+ return null; // we can't resolve this
+ } else {
+ xslSource = m_resolver.resolveURI(base.substring(0,
+ lastPathElementPos)
+ + "/" + href);
+ }
+ } else {
+ File parent = new File(base.substring(5));
+ File parent2 = new File(parent.getParentFile(), href);
+ xslSource = m_resolver.resolveURI(parent2.toURL().toExternalForm());
+ }
+ }
+
+ InputSource is = getInputSource(xslSource);
+
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug(
+ "xslSource = " + xslSource + ", system id = " + xslSource.getURI());
+ }
+
+ return new StreamSource(is.getByteStream(), is.getSystemId());
+} catch (SourceException e) {
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug(
+ "Failed to resolve " + href + "(base = " + base + "), return null",
+ e);
+ }
+
+ // CZ: To obtain the same behaviour as when the resource is
+ // transformed by the XSLT Transformer we should return null here.
+ return null;
+} catch (java.net.MalformedURLException mue) {
+ getLogger().debug(
+ "Failed to resolve " + href + "(base = " + base + "), return null",
+ mue);
+
+ return null;
+} catch (IOException ioe) {
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug(
+ "Failed to resolve " + href + "(base = " + base + "), return null",
+ ioe);
+ }
+
+ return null;
+} finally {
+ release(xslSource);
+}
+}
+ /**
+ * Return a new <code>InputSource</code> object that uses the
+ * <code>InputStream</code> and the system ID of the <code>Source</code>
+ * object.
+ *
+ * @throws IOException
+ * if I/O error occured.
+ */
+ private static InputSource getInputSource(final Source source)
+ throws IOException, SourceException {
+ final InputSource newObject = new InputSource(source.getInputStream());
+ newObject.setSystemId(source.getURI());
+ return newObject;
+ }
}