cziegeler 01/12/13 04:35:17 Modified: src/org/apache/cocoon/sitemap ContentAggregator.java Log: Rewrote ContentAggregation. Fixed caching problems (now the elementname, prefix etc are used for the key, too) and hopefully fixed sax event handling. Revision Changes Path 1.21 +140 -136 xml-cocoon2/src/org/apache/cocoon/sitemap/ContentAggregator.java Index: ContentAggregator.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/sitemap/ContentAggregator.java,v retrieving revision 1.20 retrieving revision 1.21 diff -u -r1.20 -r1.21 --- ContentAggregator.java 2001/11/09 12:21:10 1.20 +++ ContentAggregator.java 2001/12/13 12:35:17 1.21 @@ -33,99 +33,85 @@ /** * @author <a href="mailto:[EMAIL PROTECTED]">Giacomo Pati</a> * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> - * @version CVS $Id: ContentAggregator.java,v 1.20 2001/11/09 12:21:10 cziegeler Exp $ + * @version CVS $Id: ContentAggregator.java,v 1.21 2001/12/13 12:35:17 cziegeler Exp $ */ public class ContentAggregator extends ContentHandlerWrapper implements Generator, Cacheable, Recyclable { /** the root element of the aggregated content */ - protected String rootElement; + protected Element rootElement; - /** the namespace of the root element */ - protected String rootElementNS; - - /** the namespace prefix of the root element */ - protected String rootElementNSPrefix; - /** the parts */ protected ArrayList parts = new ArrayList(); - - /** The current <code>SourceResolver</code>. */ - protected SourceResolver resolver; - - /** The current <code>Map</code> objectModel. */ - protected Map objectModel; - /** The current <code>Parameters</code>. */ - protected Parameters parameters; + /** Empty Attributes */ + private AttributesImpl emptyAttrs = new AttributesImpl(); - /** The source URI associated with the request or <b>null</b>. */ - protected String source; - - /** Stacks namespaces during processing */ - private ArrayList currentNS = new ArrayList(); - - /** Stacks prefixes during processing */ - private ArrayList currentPrefix = new ArrayList(); - /** Indicates the position in the stack of the root element of the aggregated content */ private int rootElementIndex; + /** The element used for the current part */ + protected Element currentElement; + /** This object holds the part parts :) */ - private class Part { + protected final class Part { public String uri; - public String element; - public String namespace; - public String prefix; + public Element element; public Source source; boolean stripRootElement; - public Part(String uri, String element, String namespace, String stripRoot, String prefix) { + public Part(String uri, Element element, String stripRoot) { this.uri = uri; this.element = element; + this.stripRootElement = (stripRoot.equals("yes") || stripRoot.equals("true")); + } + } + + /** This object holds an element definition */ + protected final class Element { + public String namespace; + public String prefix; + public String name; + + public Element(String name, String namespace, String prefix) { this.namespace = namespace; this.prefix = prefix; - if (this.prefix == null) this.prefix = ""; - if (stripRoot.equals("yes") || stripRoot.equals("true")) { - this.stripRootElement = true; - } else { - this.stripRootElement = false; - } + this.name = name; } } /** * generates the content */ - public void generate() throws IOException, SAXException, ProcessingException { - getLogger().debug("ContentAggregator: generating aggregated content"); + public void generate() + throws IOException, SAXException, ProcessingException { + if (this.getLogger().isDebugEnabled() == true) { + getLogger().debug("ContentAggregator: generating aggregated content"); + } this.contentHandler.startDocument(); - this.startElem(this.rootElementNS, this.rootElementNSPrefix, this.rootElement); + this.startElem(this.rootElement); try { for (int i = 0; i < this.parts.size(); i++) { - Part part = (Part)this.parts.get(i); + final Part part = (Part)this.parts.get(i); this.rootElementIndex = (part.stripRootElement ? -1 : 0); - String ns = part.namespace; - String prefix = part.prefix; - if (ns.equals("")) { - ns = this.getNS(); - prefix = this.getPrefix(); - } - if (!part.element.equals("")) { - this.startElem(ns, prefix, part.element); + if (part.element != null) { + this.currentElement = part.element; + this.startElem(part.element); + } else { + this.currentElement = this.rootElement; } try { part.source.toSAX(this); } finally { - if (!part.element.equals("")) { - this.endElem(prefix, part.element); + if (part.element != null) { + this.endElem(part.element); } } } } finally { - this.endElem(this.rootElementNSPrefix, this.rootElement); + this.endElem(this.rootElement); this.contentHandler.endDocument(); } getLogger().debug("ContentAggregator: finished aggregating content"); @@ -139,14 +125,31 @@ */ public long generateKey() { try { - long key = 0; + long key = HashUtil.hash("CA(" + + this.rootElement.prefix + + ':' + + this.rootElement.name + + '<' + this.rootElement.namespace + ">)"); Source current; for (int i = 0; i < this.parts.size(); i++) { - current = ((Part)this.parts.get(i)).source; + final Part part = (Part)this.parts.get(i); + current = part.source; if (current.getLastModified() == 0) { return 0; } else { - key += HashUtil.hash(current.getSystemId()); + if (part.element == null) { + key += HashUtil.hash("P=" + + part.stripRootElement + ':' + + current.getSystemId() + ';'); + } else { + key += HashUtil.hash("P=" + + part.element.prefix + + ':' + + part.element.name + + '<' + part.element.namespace + ">:" + + part.stripRootElement + ':' + + current.getSystemId() + ';'); + } } } return key; @@ -181,18 +184,47 @@ } } + /** + * Set the root element. Please make sure that the parameters are not null! + */ public void setRootElement(String element, String namespace, String prefix) { - this.rootElement = element; - this.rootElementNS = namespace; - this.rootElementNSPrefix = prefix; - if (prefix == null) this.rootElementNSPrefix = ""; - getLogger().debug("ContentAggregator: root element='" + element + "' ns='" + namespace + "' prefix='" + prefix + "'"); + this.rootElement = new Element(element, + namespace, + prefix); + if (this.getLogger().isDebugEnabled() == true) { + this.getLogger().debug("ContentAggregator: root element='" + element + + "' ns='" + namespace + "' prefix='" + prefix + "'"); + } } - public void addPart(String uri, String element, String namespace, String stripRootElement, String prefix) { - this.parts.add(new Part(uri, element, namespace, stripRootElement, prefix)); - getLogger().debug("ContentAggregator: part uri='" + uri + "' element='" + element + "' ns='" + namespace + - "' stripRootElement='" + stripRootElement + "' prefix='" + prefix + "'"); + /** + * Add a part. Please make sure that the parameters are not null! + */ + public void addPart(String uri, + String element, + String namespace, + String stripRootElement, + String prefix) { + Element elem = null; + if (!element.equals("")) { + if (namespace.equals("")) { + elem = new Element(element, + this.rootElement.namespace, + this.rootElement.prefix); + } else { + elem = new Element(element, + namespace, + prefix); + } + } + this.parts.add(new Part(uri, + elem, + stripRootElement)); + if (this.getLogger().isDebugEnabled() == true) { + this.getLogger().debug("ContentAggregator: part uri='" + uri + + "' element='" + element + "' ns='" + namespace + + "' stripRootElement='" + stripRootElement + "' prefix='" + prefix + "'"); + } } /** @@ -211,18 +243,11 @@ */ public void recycle() { super.recycle(); - this.resolver = null; - this.objectModel = null; - this.source = null; - this.parameters = null; this.rootElement = null; - this.rootElementNS = null; - this.rootElementNSPrefix = null; for (Iterator i = this.parts.iterator(); i.hasNext();) ((Part)i.next()).source.recycle(); this.parts.clear(); - this.currentNS.clear(); - this.currentPrefix.clear(); + this.currentElement = null; } /** @@ -231,59 +256,35 @@ */ public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) throws ProcessingException, SAXException, IOException { - this.resolver = resolver; - this.objectModel = objectModel; - this.source = src; - this.parameters = par; - - Part current; + // get the Source for each part for(int i=0; i<this.parts.size();i++) { - current = (Part)this.parts.get(i); - current.source = this.resolver.resolve(current.uri); + final Part current = (Part)this.parts.get(i); + current.source = resolver.resolve(current.uri); } } - - private String pushNS(String ns, String prefix) { - this.currentNS.add(ns); - this.currentPrefix.add(prefix); - return ns; - } - - private String popNS() { - int last = currentNS.size() - 1; - String ns = (String)currentNS.get(last); - currentNS.remove(last); - currentPrefix.remove(last); - return ns; - } - private String getNS() { - int last = currentNS.size() - 1; - return (String)currentNS.get(last); - } - - private String getPrefix() { - int last = currentPrefix.size() - 1; - return (String)currentPrefix.get(last); - } - - private void startElem(String namespaceURI, String prefix, String name) throws SAXException { - this.pushNS(namespaceURI, prefix); - AttributesImpl attrs = new AttributesImpl(); - if (!namespaceURI.equals("")) { - this.contentHandler.startPrefixMapping(prefix, namespaceURI); - this.contentHandler.startElement(namespaceURI, name, prefix+':'+name, attrs); - } else { - this.contentHandler.startElement(namespaceURI, name, name, attrs); + /** + * Private method generating startElement event for the aggregated parts + * and the root element + */ + private void startElem(Element element) + throws SAXException { + final String qname = (element.prefix.equals("")) ? element.name : element.prefix + ':' + element.name; + if (!element.namespace.equals("")) { + this.contentHandler.startPrefixMapping(element.prefix, element.namespace); } + this.contentHandler.startElement(element.namespace, element.name, qname, this.emptyAttrs); } - private void endElem(String prefix, String name) throws SAXException { - final String ns = this.popNS(); - final String qname = prefix.equals("") ? name : prefix+':'+name; - this.contentHandler.endElement(ns, name, qname); - if (ns != null && !ns.equals("")) { - this.contentHandler.endPrefixMapping(prefix); + /** + * Private method generating endElement event for the aggregated parts + * and the root element + */ + private void endElem(Element element) throws SAXException { + final String qname = (element.prefix.equals("")) ? element.name : element.prefix + ':' + element.name; + this.contentHandler.endElement(element.namespace, element.name, qname); + if (!element.namespace.equals("")) { + this.contentHandler.endPrefixMapping(element.prefix); } } @@ -293,41 +294,44 @@ public void startDocument() throws SAXException { } + /** + * Ignore start and end document events + */ public void endDocument() throws SAXException { } - public void startElement(String namespaceURI, String localName, String qName, Attributes atts) + /** + * Override startElement() event to add namespace and prefix + */ + public void startElement(String namespaceURI, String localName, String raw, Attributes atts) throws SAXException { this.rootElementIndex++; if (this.rootElementIndex == 0) { getLogger().debug("ContentAggregator: skipping root element start event."); return; } - String ns = namespaceURI; - if (ns == null || ns.equals("")) { - ns = this.getNS(); - final String prefix = this.getPrefix(); - if (!prefix.equals("")) { - qName = prefix + ':' + qName; - } + if (namespaceURI == null || namespaceURI.equals("")) { + final String qname = this.currentElement.prefix.equals("") ? localName : this.currentElement.prefix + ':' + localName; + this.contentHandler.startElement(this.currentElement.namespace, localName, qname, atts); + } else { + this.contentHandler.startElement(namespaceURI, localName, raw, atts); } - this.contentHandler.startElement(ns, localName, qName, atts); } - public void endElement(String namespaceURI, String localName, String qName) throws SAXException { + /** + * Override startElement() event to add namespace and prefix + */ + public void endElement(String namespaceURI, String localName, String raw) throws SAXException { this.rootElementIndex--; if (this.rootElementIndex == -1) { - getLogger().debug("ContentAggregator: ignoring root element end event."); + getLogger().debug("ContentAggregator: skipping root element end event."); return; } - String ns = namespaceURI; - if (ns == null || ns.equals("")) { - ns = this.getNS(); - final String prefix = this.getPrefix(); - if (!prefix.equals("")) { - qName = prefix + ':' + qName; - } + if (namespaceURI == null || namespaceURI.equals("")) { + final String qname = this.currentElement.prefix.equals("") ? localName : this.currentElement.prefix + ':' + localName; + this.contentHandler.endElement(this.currentElement.namespace, localName, qname); + } else { + this.contentHandler.endElement(namespaceURI, localName, raw); } - this.contentHandler.endElement(ns, localName, qName); } }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]