cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject cvs commit: xml-cocoon2/src/java/org/apache/cocoon/transformation ReadDOMSessionTransformer.java
Date Wed, 23 Oct 2002 04:17:56 GMT
vgritsenko    2002/10/22 21:17:56

  Modified:    src/java/org/apache/cocoon/transformation
                        ReadDOMSessionTransformer.java
  Log:
  Rework transformer to use xsp:expr.
  So now it's not about only DOM anymore...
  
  Revision  Changes    Path
  1.9       +77 -79    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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- ReadDOMSessionTransformer.java	10 Sep 2002 12:15:49 -0000	1.8
  +++ ReadDOMSessionTransformer.java	23 Oct 2002 04:17:56 -0000	1.9
  @@ -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,120 +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"/>
  + * <pre>
  + *    &lt;map:transform type="read-session"&gt;
  + *      &lt;map:parameter name="attribute-name" value="companyInfo"/&gt;
    *      &lt;map:parameter name="trigger-element" value="company"/&gt;
    *      &lt;map:parameter name="position" value="after"/&gt;
    *    &lt;/map:transform&gt;
  + * </pre>
    *
  - * 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:
  + * <ul>
  + *  <li><b>attribute-name</b> is the name of the object in the session
  + *  <li><b>trigger-element</b> is the element that we need to insert
the SAX events
  + *  <li><b>postion</b> is the actual place where the stream will be inserted,
ie before, after or in
    *  the trigger-element
  + * </ul>
    *
    * @author <a href="mailto:sven.beauprez@the-ecorp.com">Sven Beauprez</a>
  + * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
    * @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) {
  -        DOMName = parameters.getParameter(ReadDOMSessionTransformer.DOM_NAME,null);
  -        trigger = parameters.getParameter(ReadDOMSessionTransformer.TRIGGER_ELEMENT,null);
  -        position = parameters.getParameter(ReadDOMSessionTransformer.POSITION,"in");
  -        if (this.getLogger().isDebugEnabled()) {
  -            getLogger().debug("ReadSessionTransformer: SessioID="+session.getId());
  -            getLogger().debug("ReadSessionTransformer: Session available, try to read from
it");
  -            getLogger().debug("ReadSessionTransformer: " + ReadDOMSessionTransformer.DOM_NAME
+ "="+
  -                          DOMName + " " + ReadDOMSessionTransformer.TRIGGER_ELEMENT + "="
+
  -                          trigger + " " + ReadDOMSessionTransformer.POSITION + "=" +
  -                          position);
  +    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.");
           }
  -      } else  {
  -        getLogger().error("ReadSessionTransformer: no session object");
  -      }
       }
       /** 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)) {
  -        if (this.getLogger().isDebugEnabled()) {
  -            getLogger().debug("ReadSessionTransformer: trigger encountered");
  +            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);
           }
  -        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);
  -        }
  -      } 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)  {
  -        if (this.getLogger().isDebugEnabled()) {
  -            getLogger().debug("ReadSessionTransformer: start streaming");
  +    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");
           }
  -        EmbeddedXMLPipe pipe = new EmbeddedXMLPipe(super.contentHandler);
  -        streamer = new DOMStreamer(pipe,super.lexicalHandler);
  -        streamer.stream(node);
  -      }else  {
  -        getLogger().error("ReadSessionTransformer: no Document in session");
  -      }
       }
  -
   }
  
  
  

----------------------------------------------------------------------
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


Mime
View raw message