cziegeler 2002/09/25 04:45:35 Modified: src/java/org/apache/cocoon/components/source SourceUtil.java src/java/org/apache/cocoon/webapps/session/connector ResourceConnectorImpl.java src/java/org/apache/cocoon/xml IncludeXMLConsumer.java src/java/org/apache/cocoon/transformation CIncludeTransformer.java Removed: src/java/org/apache/cocoon/webapps/session/connector URLRewriter.java Log: Start of merging cinclude transformer with session transformer include capabilities. Revision Changes Path 1.6 +122 -1 xml-cocoon2/src/java/org/apache/cocoon/components/source/SourceUtil.java Index: SourceUtil.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/source/SourceUtil.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- SourceUtil.java 13 Jun 2002 11:25:48 -0000 1.5 +++ SourceUtil.java 25 Sep 2002 11:45:34 -0000 1.6 @@ -51,15 +51,26 @@ package org.apache.cocoon.components.source; import java.io.IOException; +import java.util.Iterator; +import java.util.Map; + import org.apache.avalon.excalibur.xml.XMLizable; import org.apache.avalon.framework.component.ComponentException; import org.apache.avalon.framework.component.ComponentManager; +import org.apache.avalon.framework.parameters.Parameters; import org.apache.cocoon.ProcessingException; import org.apache.cocoon.ResourceNotFoundException; +import org.apache.cocoon.environment.ObjectModelHelper; +import org.apache.cocoon.environment.Request; +import org.apache.cocoon.environment.SourceResolver; +import org.apache.cocoon.xml.IncludeXMLConsumer; +import org.apache.cocoon.xml.XMLConsumer; import org.apache.cocoon.xml.dom.DOMBuilder; import org.apache.excalibur.source.Source; import org.apache.excalibur.source.SourceException; import org.apache.excalibur.source.SourceNotFoundException; +import org.apache.excalibur.source.SourceParameters; +import org.apache.excalibur.source.impl.URLSource; import org.apache.excalibur.xmlizer.XMLizer; import org.xml.sax.ContentHandler; import org.xml.sax.InputSource; @@ -113,6 +124,36 @@ } /** + * Generates SAX events from the given source + * <b>NOTE</b> : if the implementation can produce lexical events, care should be taken + * that <code>handler</code> can actually + * directly implement the LexicalHandler interface! + * + * @param source the data + * @throws ProcessingException if no suitable converter is found + */ + static public void toSAX( Source source, + ContentHandler handler, + ComponentManager manager, + Parameters typeParameters, + boolean filterDocumentEvent) + throws SAXException, IOException, ProcessingException { + + + // Test for url rewriting + if (typeParameters != null + && typeParameters.getParameter(URLRewriter.PARAMETER_MODE, null) != null) { + handler = new URLRewriter(typeParameters, handler); + } + if (filterDocumentEvent) { + IncludeXMLConsumer filter = new IncludeXMLConsumer(handler); + toSAX(source, filter, manager); + } else { + toSAX(source, handler, manager); + } + } + + /** * Generates a DOM from the given source * @param source the data * @throws ProcessingException if no suitable converter is found @@ -160,5 +201,85 @@ } catch (SourceException se) { throw handle(se); } + } + + /** + * Get a <code>Source</code> object + */ + static public Source getSource(String uri, + Parameters typeParameters, + SourceParameters resourceParameters, + SourceResolver resolver, + Map objectModel) + throws IOException, SAXException, SourceException { + + // Test: local uri (= same servlet/cocoon) ? + if (uri.startsWith("/") == true) { + // server-absolute url is transformed to absolute url + Request request = ObjectModelHelper.getRequest(objectModel); + uri = request.getScheme() + "://" + + request.getServerName() + ":" + request.getServerPort() + uri; + } + + // first step: encode parameters which are already appended to the url + int queryPos = uri.indexOf('?'); + if (queryPos != -1) { + String queryString = uri.substring(queryPos+1); + SourceParameters queries = new SourceParameters(queryString); + if (queries.hasParameters()) { + StringBuffer buffer; + buffer = new StringBuffer(uri.substring(0, queryPos)); + String current; + Iterator iter = queries.getParameterNames(); + char separator = '?'; + Iterator values; + while (iter.hasNext() == true) { + current = (String)iter.next(); + values = queries.getParameterValues(current); + while (values.hasNext()) { + buffer.append(separator) + .append(current) + .append('=') + .append(org.apache.excalibur.source.SourceUtil.encode((String)values.next())); + separator = '&'; + } + } + uri = buffer.toString(); + } + } + boolean followRedirects = (typeParameters != null ? + typeParameters.getParameterAsBoolean("followRedirects", true) + : true); + String method = (typeParameters != null ? typeParameters.getParameter("method", "GET") + : "GET"); + if (method.equalsIgnoreCase("POST") == true + && (resourceParameters == null || resourceParameters.hasParameters() == false)) { + method = "GET"; + } + if ((method.equalsIgnoreCase("POST") == false + || uri.startsWith("cocoon:") == true) + && resourceParameters != null + && resourceParameters.hasParameters()) { + + int pos = uri.indexOf(";jsessionid="); + if (uri.startsWith("cocoon:") == false) { + if (pos != -1) uri = uri.substring(0, pos); + uri = org.apache.excalibur.source.SourceUtil.appendParameters(uri, resourceParameters); + } else { + StringBuffer buf; + if (pos == -1) { + buf = new StringBuffer(uri); + } else { + buf = new StringBuffer(uri.substring(0, pos)); + } + buf.append((uri.indexOf('?') == -1 ? '?' : '&')); + buf.append(resourceParameters.getEncodedQueryString()); + uri = buf.toString(); + } + } + Map resolverParameters = new java.util.HashMap(); + resolverParameters.put(URLSource.HTTP_METHOD, method); + resolverParameters.put(URLSource.REQUEST_PARAMETERS, resourceParameters); + return resolver.resolveURI(uri, null, resolverParameters); } } 1.7 +4 -84 xml-cocoon2/src/java/org/apache/cocoon/webapps/session/connector/ResourceConnectorImpl.java Index: ResourceConnectorImpl.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/webapps/session/connector/ResourceConnectorImpl.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- ResourceConnectorImpl.java 31 Jul 2002 13:13:32 -0000 1.6 +++ ResourceConnectorImpl.java 25 Sep 2002 11:45:34 -0000 1.7 @@ -76,6 +76,7 @@ import org.apache.cocoon.Processor; import org.apache.cocoon.ResourceNotFoundException; import org.apache.cocoon.components.RequestLifecycleComponent; +import org.apache.cocoon.components.source.URLRewriter; import org.apache.cocoon.environment.Context; import org.apache.cocoon.environment.Environment; import org.apache.cocoon.environment.ObjectModelHelper; @@ -169,87 +170,6 @@ } /** - * Get a <code>Source</code> object - */ - private Source getSource(String uri, - Parameters typeParameters, - SourceParameters resourceParameters) - throws IOException, SAXException, SourceException { - - // Test: local uri (= same servlet/cocoon) ? - if (uri.startsWith("/") == true) { - // server-absolute url is transformed to absolute url - Request request = ObjectModelHelper.getRequest(objectModel); - uri = request.getScheme() + "://" + - request.getServerName() + ":" + request.getServerPort() + uri; - } - - // first step: encode parameters which are already appended to the url - int queryPos = uri.indexOf('?'); - if (queryPos != -1) { - String queryString = uri.substring(queryPos+1); - SourceParameters queries = new SourceParameters(queryString); - if (queries.hasParameters()) { - StringBuffer buffer; - buffer = new StringBuffer(uri.substring(0, queryPos)); - String current; - Iterator iter = queries.getParameterNames(); - char separator = '?'; - Iterator values; - while (iter.hasNext() == true) { - current = (String)iter.next(); - values = queries.getParameterValues(current); - while (values.hasNext()) { - buffer.append(separator) - .append(current) - .append('=') - .append(SourceUtil.encode((String)values.next())); - separator = '&'; - } - } - uri = buffer.toString(); - } - } - boolean followRedirects = (typeParameters != null ? - typeParameters.getParameterAsBoolean("followRedirects", true) - : true); - String method = (typeParameters != null ? typeParameters.getParameter("method", "GET") - : "GET"); - if (method.equalsIgnoreCase("POST") == true - && (resourceParameters == null || resourceParameters.hasParameters() == false)) { - method = "GET"; - } - if ((method.equalsIgnoreCase("POST") == false - || uri.startsWith("cocoon:") == true) - && resourceParameters != null - && resourceParameters.hasParameters()) { - - int pos = uri.indexOf(";jsessionid="); - if (uri.startsWith("cocoon:") == false) { - if (pos != -1) uri = uri.substring(0, pos); - uri = SourceUtil.appendParameters(uri, resourceParameters); - } else { - StringBuffer buf; - if (pos == -1) { - buf = new StringBuffer(uri); - } else { - buf = new StringBuffer(uri.substring(0, pos)); - } - buf.append((uri.indexOf('?') == -1 ? '?' : '&')); - buf.append(resourceParameters.getEncodedQueryString()); - uri = buf.toString(); - } - } - if (this.getLogger().isInfoEnabled() == true) { - this.getLogger().info("Getting content of '"+uri+"' using method " + method); - } - Map resolverParameters = new java.util.HashMap(); - resolverParameters.put(URLSource.HTTP_METHOD, method); - resolverParameters.put(URLSource.REQUEST_PARAMETERS, resourceParameters); - return this.resolver.resolveURI(uri, null, resolverParameters); - } - - /** * Load XML */ private DocumentFragment loadXMLFromURI(Parameters typeParameters, @@ -287,7 +207,7 @@ } Source input = null; try { - input = this.getSource(uri, typeParameters, resourceParameters); + input = org.apache.cocoon.components.source.SourceUtil.getSource(uri, typeParameters, resourceParameters, this.resolver, this.objectModel); if (input != null) { this.resolver.toSAX(input, filter); @@ -696,7 +616,7 @@ Source input = null; try { - input = this.getSource(uri, typeParameters, resourceParameters); + input = org.apache.cocoon.components.source.SourceUtil.getSource(uri, typeParameters, resourceParameters, this.resolver, this.objectModel); this.resolver.toSAX( input, filter ); } finally { this.resolver.release(input); 1.9 +2 -2 xml-cocoon2/src/java/org/apache/cocoon/xml/IncludeXMLConsumer.java Index: IncludeXMLConsumer.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/xml/IncludeXMLConsumer.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- IncludeXMLConsumer.java 2 Jul 2002 22:01:45 -0000 1.8 +++ IncludeXMLConsumer.java 25 Sep 2002 11:45:35 -0000 1.9 @@ -105,7 +105,7 @@ */ public IncludeXMLConsumer (ContentHandler contentHandler) { this.contentHandler = contentHandler; - this.lexicalHandler = null; + this.lexicalHandler = (contentHandler instanceof LexicalHandler ? (LexicalHandler)contentHandler : null); } /** 1.11 +163 -13 xml-cocoon2/src/java/org/apache/cocoon/transformation/CIncludeTransformer.java Index: CIncludeTransformer.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/transformation/CIncludeTransformer.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- CIncludeTransformer.java 25 Sep 2002 10:40:25 -0000 1.10 +++ CIncludeTransformer.java 25 Sep 2002 11:45:35 -0000 1.11 @@ -59,10 +59,12 @@ import org.apache.cocoon.components.source.SourceUtil; import org.apache.cocoon.environment.SourceResolver; import org.apache.cocoon.xml.IncludeXMLConsumer; +import org.apache.cocoon.xml.XMLUtils; import org.apache.avalon.excalibur.xml.Parser; import org.apache.avalon.excalibur.xml.xpath.XPathProcessor; import org.apache.excalibur.source.Source; import org.apache.excalibur.source.SourceException; +import org.apache.excalibur.source.SourceParameters; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @@ -89,7 +91,7 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Andrew C. Oliver</a> * @version CVS $Id$ */ -public class CIncludeTransformer extends AbstractTransformer +public class CIncludeTransformer extends AbstractSAXTransformer implements Composable { public static final String CINCLUDE_NAMESPACE_URI = "http://apache.org/cocoon/include/1.0"; @@ -100,6 +102,27 @@ public static final String CINCLUDE_INCLUDE_ELEMENT_NS_ATTRIBUTE = "ns"; public static final String CINCLUDE_INCLUDE_ELEMENT_PREFIX_ATTRIBUTE = "prefix"; + public static final String CINCLUDE_INCLUDEXML_ELEMENT = "includexml"; + public static final String CINCLUDE_INCLUDEXML_ELEMENT_IGNORE_ERRORS_ATTRIBUTE = "ignoreErrors"; + public static final String CINCLUDE_SRC_ELEMENT = "src"; + public static final String CINCLUDE_CONFIGURATION_ELEMENT = "configuration"; + public static final String CINCLUDE_PARAMETERS_ELEMENT = "parameters"; + public static final String CINCLUDE_PARAMETER_ELEMENT = "parameter"; + public static final String CINCLUDE_NAME_ELEMENT = "name"; + public static final String CINCLUDE_VALUE_ELEMENT = "value"; + + private static final int STATE_OUTSIDE = 0; + private static final int STATE_INCLUDE = 1; + + /** The configuration of includexml */ + protected Parameters configurationParameters; + + /** The parameters for includexml */ + protected SourceParameters resourceParameters; + + /** The current state: STATE_ */ + protected int state; + /** The <code>SourceResolver</code> */ protected SourceResolver sourceResolver; @@ -108,12 +131,21 @@ /** + * Constructor + * Set the namespace + */ + public CIncludeTransformer() { + this.namespaceURI = CINCLUDE_NAMESPACE_URI; + } + + /** * Setup the component. */ public void setup(SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws ProcessingException, SAXException, IOException { this.sourceResolver = resolver; + this.state = STATE_OUTSIDE; } /** @@ -129,13 +161,13 @@ public void recycle() { super.recycle(); this.sourceResolver = null; + this.configurationParameters = null; + this.resourceParameters = null; } - public void startElement(String uri, String name, String raw, Attributes attr) - throws SAXException { - if (uri != null && name != null - && uri.equals(CINCLUDE_NAMESPACE_URI) - && name.equals(CINCLUDE_INCLUDE_ELEMENT)) { + public void startTransformingElement(String uri, String name, String raw, Attributes attr) + throws ProcessingException ,IOException, SAXException { + if (name.equals(CINCLUDE_INCLUDE_ELEMENT)) { this.processCIncludeElement(attr.getValue("",CINCLUDE_INCLUDE_ELEMENT_SRC_ATTRIBUTE), attr.getValue("",CINCLUDE_INCLUDE_ELEMENT_ELEMENT_ATTRIBUTE), @@ -143,18 +175,136 @@ attr.getValue("",CINCLUDE_INCLUDE_ELEMENT_NS_ATTRIBUTE), attr.getValue("",CINCLUDE_INCLUDE_ELEMENT_PREFIX_ATTRIBUTE)); + // Element: include + } else if (name.equals(CINCLUDE_INCLUDEXML_ELEMENT) + && this.state == STATE_OUTSIDE) { + this.state = STATE_INCLUDE; + String ignoreErrors = attr.getValue("", CINCLUDE_INCLUDEXML_ELEMENT_IGNORE_ERRORS_ATTRIBUTE); + if (ignoreErrors == null || ignoreErrors.length() == 0) { + ignoreErrors = "false"; + } + this.stack.push(new Boolean(this.ignoreEmptyCharacters)); + this.stack.push(new Boolean(this.ignoreWhitespaces)); + this.stack.push(ignoreErrors); + + this.ignoreEmptyCharacters = false; + this.ignoreWhitespaces = true; + + // target + } else if (name.equals(CINCLUDE_SRC_ELEMENT) + && this.state == STATE_INCLUDE) { + this.startTextRecording(); + + // configparameters + } else if (name.equals(CINCLUDE_CONFIGURATION_ELEMENT) + && this.state == STATE_INCLUDE) { + stack.push("end"); + + // parameters + } else if (name.equals(CINCLUDE_PARAMETERS_ELEMENT) + && this.state == STATE_INCLUDE) { + stack.push("end"); + + // parameter + } else if (name.equals(CINCLUDE_PARAMETER_ELEMENT) + && this.state == STATE_INCLUDE) { + + // parameter name + } else if (name.equals(CINCLUDE_NAME_ELEMENT) + && this.state == STATE_INCLUDE) { + this.startTextRecording(); + + // parameter value + } else if (name.equals(CINCLUDE_VALUE_ELEMENT) + && this.state == STATE_INCLUDE) { + this.startSerializedXMLRecording(XMLUtils.defaultSerializeToXMLFormat(true)); + } else { - super.startElement(uri, name, raw, attr); + super.startTransformingElement(uri, name, raw, attr); } } - public void endElement(String uri, String name, String raw) throws SAXException { - if (uri != null && name != null - && uri.equals(CINCLUDE_NAMESPACE_URI) - && name.equals(CINCLUDE_INCLUDE_ELEMENT)) { + public void endTransformingElement(String uri, String name, String raw) + throws ProcessingException, IOException, SAXException { + if (name.equals(CINCLUDE_INCLUDE_ELEMENT)) { + // do nothing return; + + // Element: includexml + } else if (name.equals(CINCLUDE_INCLUDEXML_ELEMENT) + && this.state == STATE_INCLUDE) { + + this.state = STATE_OUTSIDE; + + final String resource = (String)stack.pop(); + + final boolean ignoreErrors = ((String)stack.pop()).equals("true"); + + Source source = null; + try { + source = SourceUtil.getSource(resource, + this.configurationParameters, + this.resourceParameters, + this.resolver, + this.objectModel); + + SourceUtil.toSAX(source, this.xmlConsumer, manager, this.configurationParameters, true); + + } catch (SourceException se) { + throw SourceUtil.handle(se); + } finally { + this.sourceResolver.release(source); + } + + // restore values + this.ignoreWhitespaces = ((Boolean)stack.pop()).booleanValue(); + this.ignoreEmptyCharacters = ((Boolean)stack.pop()).booleanValue(); + + // src element + } else if (name.equals(CINCLUDE_SRC_ELEMENT) + && this.state == STATE_INCLUDE) { + + this.stack.push(this.endTextRecording()); + + } else if (name.equals(CINCLUDE_PARAMETERS_ELEMENT) + && this.state == STATE_INCLUDE) { + this.resourceParameters = new SourceParameters(); + // Now get the parameters off the stack + String label = (String)stack.pop(); + String value; + while (!label.equals("end")) { + value = (String)stack.pop(); + this.resourceParameters.setParameter(label, value); + label = (String)stack.pop(); + } + + } else if (name.equals(CINCLUDE_CONFIGURATION_ELEMENT) == true + && this.state == STATE_INCLUDE) { + this.configurationParameters = new Parameters(); + // Now get the parameters off the stack + String label = (String)stack.pop(); + String value; + while (!label.equals("end")) { + value = (String)stack.pop(); + this.configurationParameters.setParameter(label, value); + label = (String)stack.pop(); + } + + } else if (name.equals(CINCLUDE_PARAMETER_ELEMENT) == true + && this.state == STATE_INCLUDE) { + + } else if (name.equals(CINCLUDE_NAME_ELEMENT) == true + && this.state == STATE_INCLUDE) { + stack.push(this.endTextRecording()); + + // parameter value + } else if (name.equals(CINCLUDE_VALUE_ELEMENT) == true + && this.state == STATE_INCLUDE) { + stack.push(this.endSerializedXMLRecording()); + + } else { + super.endTransformingElement(uri, name, raw); } - super.endElement(uri, name, raw); } protected void processCIncludeElement(String src, String element,
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]