Author: thorsten Date: Tue Dec 9 05:58:11 2008 New Revision: 724703 URL: http://svn.apache.org/viewvc?rev=724703&view=rev Log: Adding SAX implementation again to the dispatcher. Next step is profiling and further testing.
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=724703&r1=724702&r2=724703&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 Dec 9 05:58:11 2008 @@ -17,7 +17,6 @@ package org.apache.forrest.dispatcher.transformation; import java.io.BufferedInputStream; -import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -51,29 +50,27 @@ import org.apache.axiom.om.OMNamespace; import org.apache.axiom.om.OMNode; import org.apache.axiom.om.impl.builder.StAXOMBuilder; -import org.apache.commons.io.output.ByteArrayOutputStream; 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.cocoon.util.TraxErrorHandler; +import org.apache.cocoon.xml.IncludeXMLConsumer; +import org.apache.cocoon.xml.RedundantNamespacesFilter; +import org.apache.cocoon.xml.StringXMLizable; import org.apache.cocoon.xml.XMLUtils; +import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.excalibur.source.Source; import org.apache.excalibur.source.SourceException; import org.apache.excalibur.source.SourceValidity; import org.apache.excalibur.xml.sax.SAXParser; -import org.apache.excalibur.xml.sax.XMLizable; import org.apache.forrest.dispatcher.api.Contract; import org.apache.forrest.dispatcher.api.Resolver; -import org.apache.forrest.dispatcher.api.Structurer; -import org.apache.forrest.dispatcher.config.DispatcherBean; import org.apache.forrest.dispatcher.config.WritableDispatcherBean; import org.apache.forrest.dispatcher.exception.ContractException; import org.apache.forrest.dispatcher.exception.DispatcherException; import org.apache.forrest.dispatcher.factories.ContractFactory; import org.apache.forrest.dispatcher.impl.CocoonResolver; -import org.apache.forrest.dispatcher.impl.XMLStructurer; -import org.apache.forrest.dispatcher.impl.XMLStructurerAxiom; import org.apache.forrest.dispatcher.impl.helper.AXIOMXPathCreate; import org.apache.forrest.dispatcher.impl.helper.Captions; import org.apache.forrest.dispatcher.impl.helper.StAX; @@ -248,6 +245,8 @@ private String prefixString; + private HashMap storedPrefixMap; + /* * @see * org.apache.cocoon.transformation.AbstractSAXTransformer#configure(org.apache @@ -328,6 +327,8 @@ */ // setup our super class super.setup(resolver, objectModel, src, par); + storedPrefixMap = new HashMap(); + // get the id of this request this.requestId = parameters .getParameter(DISPATCHER_REQUEST_ATTRIBUTE, null); @@ -393,7 +394,6 @@ throws SAXException { // Process start element event // Are we inside of properties? If so we need to record the elements. - if (this.insideProperties && this.includeNodes) { // just start the recording super.startElement(uri, name, raw, attr); @@ -418,16 +418,17 @@ contractProcessingStart(attr); } else if (Captions.PROPERTY_ELEMENT.equals(name)) { // we are inside a property element - this.insideProperties = true; + //this.insideProperties = true; propertyProcessingStart(uri, name, raw, attr); - } + } } } catch (Exception e) { throw new SAXException(e); } } else { + // Do we want to allow to have structurer definitions as nested elements? if (!this.insideProperties && this.includeNodes) { - super.startElement(uri, name, raw, attr); + //super.startElement(uri, name, raw, attr); } } @@ -440,18 +441,18 @@ */ public void endElement(String uri, String name, String raw) throws SAXException { - System.out.println(); getLogger().debug("Ending element: " + raw); if (Captions.NS.equals(uri)) { // we are in the dispatcher ns try { if (Captions.STRUCTURE_ELEMENT.equals(name)) { // we are in a structurer end element - structurerProcessingEnd(raw); + if (includeNodes) { + includeNodes=false; + } } else if (Captions.HOOK_ELEMENT.equals(name) && this.includeNodes) { // we are inside a hook end element - // FIXME Implement - System.out.println("FIXME Implement "); + pathNode = (OMElement) pathNode.getParent(); } else if (Captions.CONTRACT_ELEMENT.equals(name) && this.includeNodes) { // we are inside a contract end element contractProcessingEnd(); @@ -460,27 +461,19 @@ if (allowXml) { String property = null; try { - property = prefixString+super.endSerializedXMLRecording()+"</forrest:property>"; + //XMLizable endSAXRecording = super.endSAXRecording(); + property = prefixString+endSerializedXMLRecording()+"</forrest:property>"; insideProperties = false; - System.out.println("super.endSerializedXMLRecording(): "+property); - } catch (ProcessingException e) { + getLogger().debug("super.endSerializedXMLRecording(): "+property); + } catch (Exception e) { throw new SAXException(e); } if (null != property) { localParams.put(currentProperty, property.getBytes()); } - } else { - // FIXME Implement - System.out.println("FIXME Implement "); } } - } catch (ContractException e) { - throw new SAXException(e); - } catch (IOException e) { - throw new SAXException(e); - } catch (JaxenException e) { - throw new SAXException(e); - } catch (XMLStreamException e) { + } catch (Exception e) { throw new SAXException(e); } } else if (this.insideProperties && this.includeNodes) { @@ -488,6 +481,51 @@ } } + public void ignorableWhitespace(char c[], int start, int len) + throws SAXException { + // do nothing here! + } + + + public void characters(char c[], int start, int len) + throws SAXException { + /* + * only if we in properties mode we want to record + * the characters. + */ + if (includeNodes && insideProperties){ + super.characters(c, start, len); + } + } + + public void startDocument() throws SAXException { + // Add the namespace filter to our own output. + RedundantNamespacesFilter nsPipe = new RedundantNamespacesFilter(); + if (this.xmlConsumer != null) { + nsPipe.setConsumer(this.xmlConsumer); + } else { + nsPipe.setContentHandler(this.contentHandler); + } + setConsumer(nsPipe); + super.startDocument(); + } + + public void endDocument() + throws SAXException { + structurerProcessingEnd(); + super.endDocument(); + } + + /* + * copy 'n paste + */ + + public void startCDATA() throws SAXException { + } + + public void endCDATA() throws SAXException { + } + /** * Will execute the contract and process the result. * @@ -607,13 +645,24 @@ * Finish the processing of the structurer * * @param raw + * @throws SAXException */ - private void structurerProcessingEnd(String raw) { - if (includeNodes) { - // FIXME: implement - System.out.println("FIXME Implement "); + private void structurerProcessingEnd() throws SAXException { + try { + // get the result of the structurer as stream + ByteArrayOutputStream out = new ByteArrayOutputStream(); + OMElement firstElement = root.getFirstElement(); + if (null!=firstElement){ + firstElement.serialize(out); + }else{ + root.serialize(out); + } + StringXMLizable xml = new StringXMLizable(out.toString()); + xml.toSAX(new IncludeXMLConsumer(super.xmlConsumer)); + } catch (Exception e) { + throw new SAXException(e); } - + } /** @@ -641,12 +690,13 @@ value = localValue; } } + currentProperty = id; // if we allow xml properties we will just record them if (allowXml) { // just start the recording prefixString = "<"+raw+attributesString+">"; - startSerializedXMLRecording(XMLUtils.createPropertiesForXML(true)); - currentProperty = id; + startSerializedXMLRecording((XMLUtils.createPropertiesForXML(true))); + insideProperties = true; } else { // if we do not allow xml we will use a simple key/value approach if (id != null && value != null) { @@ -714,6 +764,7 @@ element.addAttribute(attribute); } pathNode.addChild(element); + pathNode = element; } /** @@ -784,9 +835,6 @@ // release source - you need to ALWAYS do this! release(propsSource); } - // creating in/out stream objects - BufferedInputStream stream = null; - ByteArrayOutputStream out = new ByteArrayOutputStream(); /* * Preparing the model by creating a root document/element. Afterward we * need to strip the root element again. @@ -817,91 +865,6 @@ } - /* - * Here we pick up the recoreder result and start the processing on it. - * - * @see org.apache.cocoon.transformation.AbstractSAXTransformer#endDocument() - */ - public void endDocument() throws SAXException { - - // 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 { - // get the source representation of the propertyURI - Source propsSource = m_resolver.resolveURI(propertyURI); - if (propsSource != null) { - // get the stream - InputStream stream = new BufferedInputStream(propsSource - .getInputStream()); - /* - * we need either just the bytes of the stream (if we allow xml - * properties) or we need to process it extracting the properties. - */ - if (config.isAllowXmlProperties()) { - // get the bytes from the stream - byte[] properties = IOUtils.getStreamAsByteArray(stream); - /* - * add the bytes to the properties map later on they will be picked up - * and parsed to a dom node. - */ - map.put(DEFAULT_VARIABLES, properties); - } else { - // extract the properties of the in coming stream - XMLProperties.parseProperties(stream, map); - } - // release source - you need to ALWAYS do this! - release(propsSource); - } - } catch (Exception e) { - throw new SAXException(e); - } - // which implementation do we want - if (xpathSupport.equals("enhanced")) { - /* - * The axiom implementation is an object model approach to StAX. It allows - * you a dom like navigation (allocate xpath nodes), adding of child - * elements in this xpath statement and many more. - */ - structurer = new XMLStructurerAxiom((DispatcherBean) config, map); - } else { - /* - * The stax implementationis 100% StAX even the generation of the - * resulting document is done with StAX. However it does not offer real - * support of xpath expressions. - */ - structurer = new XMLStructurer((DispatcherBean) config, map); - } - - try { - // the recorded document we just streamed - String document = null; - try { - // request the information from the recorder - document = super.endSerializedXMLRecording(); - } catch (ProcessingException e) { - throw new SAXException(e); - } - // get the result of the structurer as stream - InputStream result = structurer.execute(new BufferedInputStream( - new ByteArrayInputStream(document.getBytes())), requestedFormat); - // requesting a parser - parser = (SAXParser) manager.lookup(SAXParser.ROLE); - // adding the result to the consumer - parser.parse(new InputSource(result), super.xmlConsumer); - } catch (DispatcherException e) { - throw new SAXException(e); - } catch (Exception e) { - throw new SAXException(e); - } finally { - if (null != parser) { - // release parser - you need to ALWAYS do this! - manager.release(parser); - } - } - } /** * Generate the unique key. This key must be unique inside the space of this @@ -971,7 +934,16 @@ * Recycle the component */ public void recycle() { + this.includeNodes = false; + this.insideProperties= false; + this.requestId= ""; this.validity = null; + this.root=null; + this.pathNode=null; + this.contract=null; + this.map=null; + this.localParams=null; + this.currentProperty=null; super.recycle(); }