ivelin 2002/07/21 18:40:47 Modified: src/java/org/apache/cocoon/transformation CIncludeTransformer.java Log: applied patch by Andy Oliver http://nagoya.apache.org/bugzilla/show_bug.cgi?id=10540 Revision Changes Path 1.8 +56 -4 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.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- CIncludeTransformer.java 7 Jun 2002 13:15:10 -0000 1.7 +++ CIncludeTransformer.java 22 Jul 2002 01:40:47 -0000 1.8 @@ -50,14 +50,24 @@ */ package org.apache.cocoon.transformation; +import org.apache.avalon.framework.component.ComponentException; import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.component.Composable; import org.apache.avalon.framework.parameters.Parameters; import org.apache.cocoon.ProcessingException; +import org.apache.cocoon.components.source.SourceUtil; import org.apache.cocoon.environment.SourceResolver; import org.apache.cocoon.xml.IncludeXMLConsumer; +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.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.ContentHandler; +import org.xml.sax.ext.LexicalHandler; +import org.xml.sax.InputSource; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; @@ -75,6 +85,7 @@ * which surrounds the included content. * * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> + * @author <a href="mailto:[EMAIL PROTECTED]">Andrew C. Oliver</a> * @version CVS $Id$ */ public class CIncludeTransformer extends AbstractTransformer @@ -84,6 +95,7 @@ public static final String CINCLUDE_INCLUDE_ELEMENT = "include"; public static final String CINCLUDE_INCLUDE_ELEMENT_SRC_ATTRIBUTE = "src"; public static final String CINCLUDE_INCLUDE_ELEMENT_ELEMENT_ATTRIBUTE = "element"; + public static final String CINCLUDE_INCLUDE_ELEMENT_SELECT_ATTRIBUTE = "select"; public static final String CINCLUDE_INCLUDE_ELEMENT_NS_ATTRIBUTE = "ns"; public static final String CINCLUDE_INCLUDE_ELEMENT_PREFIX_ATTRIBUTE = "prefix"; @@ -93,6 +105,8 @@ /** The current <code>ComponentManager</code>. */ protected ComponentManager manager = null; + private XPathProcessor processor = null; + /** * Setup the component. */ @@ -107,6 +121,11 @@ */ public final void compose(final ComponentManager manager) { this.manager = manager; + try { + this.processor = (XPathProcessor)this.manager.lookup(XPathProcessor.ROLE); + } catch (Exception e) { + getLogger().error("cannot obtain XPathProcessor", e); + } } /** @@ -125,6 +144,7 @@ this.processCIncludeElement(attr.getValue("",CINCLUDE_INCLUDE_ELEMENT_SRC_ATTRIBUTE), attr.getValue("",CINCLUDE_INCLUDE_ELEMENT_ELEMENT_ATTRIBUTE), + attr.getValue("",CINCLUDE_INCLUDE_ELEMENT_SELECT_ATTRIBUTE), attr.getValue("",CINCLUDE_INCLUDE_ELEMENT_NS_ATTRIBUTE), attr.getValue("",CINCLUDE_INCLUDE_ELEMENT_PREFIX_ATTRIBUTE)); @@ -142,15 +162,17 @@ super.endElement(uri, name, raw); } - protected void processCIncludeElement(String src, String element, String ns, String prefix) + protected void processCIncludeElement(String src, String element, String select, String ns, String prefix) throws SAXException { if (element == null) element=""; + if (select == null) select=""; if (ns == null) ns=""; if (prefix == null) prefix=""; getLogger().debug("Processing CInclude element: src=" + src + ", element=" + element + + ", select=" + select + ", ns=" + ns + ", prefix=" + prefix); @@ -168,9 +190,35 @@ } Source source = null; + Parser parser = null; try { source = this.sourceResolver.resolveURI(src); - this.sourceResolver.toSAX(source, consumer); + + if (!"".equals(select)) { + + + parser = (Parser)manager.lookup(Parser.ROLE); + + InputSource input = SourceUtil.getInputSource(source); + + String xpath = select; + getLogger().debug("XPath is "+xpath); + Document document = parser.parseDocument(input); + NodeList list = processor.selectNodeList(document,xpath); + int length = list.getLength(); + for (int i=0; i<length; i++) { + consumer.includeNode((Node)list.item(i), + (ContentHandler)this, + (LexicalHandler)this); + } + } else { + // do nothing, will result in the inclusion of the whole + // document + } + + this.sourceResolver.toSAX(source, consumer); + + } catch (IOException e) { getLogger().error("CIncludeTransformer", e); throw new SAXException("CIncludeTransformer could not read resource", e); @@ -180,8 +228,12 @@ } catch (ProcessingException e){ getLogger().error("Could not stream input", e); throw new SAXException("Exception in CIncludeTransformer",e); + } catch(ComponentException e) { + getLogger().error("Error in processXIncludeElement", e); + throw new SAXException(e); } finally { - this.sourceResolver.release(source); + this.manager.release(parser); + this.sourceResolver.release(source); } if (!"".equals(element)) {
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]