Return-Path: Delivered-To: apmail-xml-cocoon-cvs-archive@xml.apache.org Received: (qmail 83664 invoked by uid 500); 23 Oct 2002 04:17:58 -0000 Mailing-List: contact cocoon-cvs-help@xml.apache.org; run by ezmlm Precedence: bulk Reply-To: cocoon-dev@xml.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list cocoon-cvs@xml.apache.org Received: (qmail 83643 invoked by uid 500); 23 Oct 2002 04:17:57 -0000 Delivered-To: apmail-xml-cocoon2-cvs@apache.org Date: 23 Oct 2002 04:17:56 -0000 Message-ID: <20021023041756.15755.qmail@icarus.apache.org> From: vgritsenko@apache.org To: xml-cocoon2-cvs@apache.org Subject: cvs commit: xml-cocoon2/src/java/org/apache/cocoon/xml XMLUtils.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N vgritsenko 2002/10/22 21:17:55 Modified: src/java/org/apache/cocoon/transformation Tag: cocoon_2_0_3_branch ReadDOMSessionTransformer.java src/java/org/apache/cocoon/xml Tag: cocoon_2_0_3_branch XMLUtils.java Log: Rework transformer to use xsp:expr. So now it's not about only DOM anymore... Revision Changes Path No revision No revision 1.7.2.1 +79 -75 xml-cocoon2/src/java/org/apache/cocoon/transformation/ReadDOMSessionTransformer.java Index: ReadDOMSessionTransformer.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/transformation/ReadDOMSessionTransformer.java,v retrieving revision 1.7 retrieving revision 1.7.2.1 diff -u -r1.7 -r1.7.2.1 --- ReadDOMSessionTransformer.java 22 Feb 2002 07:03:56 -0000 1.7 +++ ReadDOMSessionTransformer.java 23 Oct 2002 04:17:55 -0000 1.7.2.1 @@ -51,15 +51,12 @@ package org.apache.cocoon.transformation; import org.apache.avalon.framework.parameters.Parameters; -import org.apache.cocoon.Constants; import org.apache.cocoon.ProcessingException; import org.apache.cocoon.environment.ObjectModelHelper; import org.apache.cocoon.environment.Request; import org.apache.cocoon.environment.Session; import org.apache.cocoon.environment.SourceResolver; -import org.apache.cocoon.xml.EmbeddedXMLPipe; -import org.apache.cocoon.xml.dom.DOMStreamer; -import org.w3c.dom.Node; +import org.apache.cocoon.xml.XMLUtils; import org.xml.sax.Attributes; import org.xml.sax.SAXException; @@ -68,114 +65,121 @@ /** - * With this transformer, a DOM-object that is stored in the session, can be inserted - * in the SAX stream at a given position. + * With this transformer, an object that is stored in the session, can be inserted + * in the SAX stream at a given position, using usual <xsp:expr> rules. + * Object can be DOM Node, XMLizable, or any other object supported by <xsp:expr>. * * Usage in sitemap: - * <map:transform type="readDOMsession"> - * <map:parameter name="dom-name" value="companyInfo"/> + *
  + *    <map:transform type="read-session">
  + *      <map:parameter name="attribute-name" value="companyInfo"/>
    *      <map:parameter name="trigger-element" value="company"/>
    *      <map:parameter name="position" value="after"/>
    *    </map:transform>
  + * 
* - * where: - * dom-name is the name of the DOM object in the session - * trigger-element is the element that we need to insert the SAX events - * postion is the actual place where the stream will be inserted, ie before, after or in + * Where: + *
    + *
  • attribute-name is the name of the object in the session + *
  • trigger-element is the element that we need to insert the SAX events + *
  • postion is the actual place where the stream will be inserted, ie before, after or in * the trigger-element + *
* * @author Sven Beauprez + * @author Vadim Gritsenko * @version CVS $Id$ */ - public class ReadDOMSessionTransformer extends AbstractTransformer { - public static final String DOM_NAME = "dom-name"; + public static final String ATTRIBUTE_NAME = "attribute-name"; public static final String TRIGGER_ELEMENT = "trigger-element"; + /* position where the sax events from the dom should be insterted this can be: 'before', 'after' or 'in' */ public static final String POSITION = "position"; - Node node; - DOMStreamer streamer; Session session; - - String DOMName; + String attributeName; String trigger; String position; /** BEGIN SitemapComponent methods **/ - public void setup(SourceResolver resolver, Map objectModel, - String source, Parameters parameters) - throws ProcessingException, SAXException, IOException { - Request request = ObjectModelHelper.getRequest(objectModel); - session = request.getSession(false); - if (session != null) { - getLogger().debug("ReadSessionTransformer: SessioID="+session.getId()); - getLogger().debug("ReadSessionTransformer: Session available, try to read from it"); - DOMName = parameters.getParameter(ReadDOMSessionTransformer.DOM_NAME,null); - trigger = parameters.getParameter(ReadDOMSessionTransformer.TRIGGER_ELEMENT,null); - position = parameters.getParameter(ReadDOMSessionTransformer.POSITION,"in"); - getLogger().debug("ReadSessionTransformer: " + ReadDOMSessionTransformer.DOM_NAME + "="+ - DOMName + " " + ReadDOMSessionTransformer.TRIGGER_ELEMENT + "=" + - trigger + " " + ReadDOMSessionTransformer.POSITION + "=" + - position); - } else { - getLogger().error("ReadSessionTransformer: no session object"); - } + public void setup(SourceResolver resolver, + Map objectModel, + String source, + Parameters parameters) + throws ProcessingException, SAXException, IOException { + Request request = ObjectModelHelper.getRequest(objectModel); + session = request.getSession(false); + if (session != null) { + if (getLogger().isDebugEnabled()) { + getLogger().debug("Session is available. ID=" + session.getId()); + } + this.attributeName = parameters.getParameter(ATTRIBUTE_NAME, null); + if (this.attributeName == null) { + // Try old syntax + this.attributeName = parameters.getParameter("dom-name", null); + } + + this.trigger = parameters.getParameter(TRIGGER_ELEMENT, null); + this.position = parameters.getParameter(POSITION, "in"); + if (getLogger().isDebugEnabled()) { + getLogger().debug(ATTRIBUTE_NAME + "=" + attributeName + ", " + + TRIGGER_ELEMENT + "=" + trigger + ", " + + POSITION + "=" + position); + } + } else { + getLogger().warn("No session object: Nothing to do."); + } } /** END SitemapComponent methods **/ /** BEGIN SAX ContentHandler handlers **/ public void startElement(String uri, String name, String raw, Attributes attributes) - throws SAXException { - //start streaming after certain startelement is encountered - if (name.equalsIgnoreCase(trigger)) { - getLogger().debug("ReadSessionTransformer: trigger encountered"); - if (position.equalsIgnoreCase("before")) { - streamDOM(); - super.contentHandler.startElement(uri,name,raw,attributes); - } else if (position.equalsIgnoreCase("in")) { - super.contentHandler.startElement(uri,name,raw,attributes); - streamDOM(); - } else if (position.equalsIgnoreCase("after")) { - super.contentHandler.startElement(uri,name,raw,attributes); + throws SAXException { + // Start streaming after certain startelement is encountered + if (name.equalsIgnoreCase(trigger)) { + getLogger().debug("Trigger encountered"); + if ("before".equalsIgnoreCase(position)) { + stream(); + super.contentHandler.startElement(uri,name,raw,attributes); + } else if ("in".equalsIgnoreCase(position)) { + super.contentHandler.startElement(uri,name,raw,attributes); + stream(); + } else if ("after".equalsIgnoreCase(position)) { + super.contentHandler.startElement(uri,name,raw,attributes); + } + } else { + super.contentHandler.startElement(uri,name,raw,attributes); } - } else { - super.contentHandler.startElement(uri,name,raw,attributes); - } } public void endElement(String uri,String name,String raw) - throws SAXException { - super.contentHandler.endElement(uri,name,raw); - if (name.equalsIgnoreCase(trigger)) { - if (position.equalsIgnoreCase("after")) { - streamDOM(); + throws SAXException { + super.contentHandler.endElement(uri,name,raw); + if (name.equalsIgnoreCase(trigger)) { + if ("after".equalsIgnoreCase(position)) { + stream(); + } } - } } /** END SAX ContentHandler handlers **/ /** own methods **/ - private void streamDOM() - throws SAXException { - if (DOMName!=null) { - node = (Node)session.getAttribute(DOMName); - } else { - getLogger().error("ReadSessionTransformer: no "+ ReadDOMSessionTransformer.DOM_NAME - +" parameter specified"); - } - if (node!=null) { - getLogger().debug("ReadSessionTransformer: start streaming"); - EmbeddedXMLPipe pipe = new EmbeddedXMLPipe(super.contentHandler); - streamer = new DOMStreamer(pipe,super.lexicalHandler); - streamer.stream(node); - }else { - getLogger().error("ReadSessionTransformer: no Document in session"); - } + private void stream() throws SAXException { + if (attributeName != null) { + Object node = session.getAttribute(attributeName); + if (node != null) { + getLogger().debug("Start streaming"); + XMLUtils.valueOf(super.xmlConsumer, node); + } else { + getLogger().error("No attribute " + attributeName + " in session"); + } + } else { + getLogger().error("No "+ ATTRIBUTE_NAME + " parameter specified"); + } } - } No revision No revision 1.6.2.3 +11 -8 xml-cocoon2/src/java/org/apache/cocoon/xml/XMLUtils.java Index: XMLUtils.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/xml/XMLUtils.java,v retrieving revision 1.6.2.2 retrieving revision 1.6.2.3 diff -u -r1.6.2.2 -r1.6.2.3 --- XMLUtils.java 23 Oct 2002 03:39:15 -0000 1.6.2.2 +++ XMLUtils.java 23 Oct 2002 04:17:55 -0000 1.6.2.3 @@ -264,7 +264,7 @@ * @param contentHandler the SAX content handler * @param text the value */ - public static void valueOf(ContentHandler contentHandler, String text) + public static void valueOf(ContentHandler contentHandler, String text) throws SAXException { if (text != null) { data(contentHandler, text); @@ -278,10 +278,10 @@ * @param contentHandler the SAX content handler * @param v the XML fragment */ - public static void valueOf(ContentHandler contentHandler, XMLizable v) + public static void valueOf(ContentHandler contentHandler, XMLizable v) throws SAXException { if (v != null) { - try { + try { v.toSAX(contentHandler); } catch(ProcessingException e) { throw new SAXException(e); @@ -296,10 +296,13 @@ * @param contentHandler the SAX content handler * @param v the value */ - public static void valueOf(ContentHandler contentHandler, Node v) + public static void valueOf(ContentHandler contentHandler, Node v) throws SAXException { if (v != null) { DOMStreamer streamer = new DOMStreamer(contentHandler); + if (contentHandler instanceof LexicalHandler) { + streamer.setLexicalHandler((LexicalHandler)contentHandler); + } streamer.stream(v); } } @@ -312,8 +315,8 @@ * @param contentHandler the SAX content handler * @param v the XML fragment */ - public static void valueOf(ContentHandler contentHandler, - Collection v) + public static void valueOf(ContentHandler contentHandler, + Collection v) throws SAXException { if (v != null) { Iterator iterator = v.iterator(); @@ -334,7 +337,7 @@ * @param contentHandler the SAX content handler * @param v the value */ - public static void valueOf(ContentHandler contentHandler, Object v) + public static void valueOf(ContentHandler contentHandler, Object v) throws SAXException { if (v == null) { return; ---------------------------------------------------------------------- In case of troubles, e-mail: webmaster@xml.apache.org To unsubscribe, e-mail: cocoon-cvs-unsubscribe@xml.apache.org For additional commands, e-mail: cocoon-cvs-help@xml.apache.org