You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by an...@apache.org on 2004/06/27 19:40:10 UTC

cvs commit: cocoon-2.1/src/webapp/samples/flow/jxcalc/screens getNumberA.xml

antonio     2004/06/27 10:40:10

  Modified:    .        status.xml
               src/java/org/apache/cocoon/generation
                        JXTemplateGenerator.java
               src/webapp/samples/flow/jxcalc/screens getNumberA.xml
  Log:
  Fix bug #25083: Add new <jx:comment/> tag. XML comments are no more parsed.
  
  Revision  Changes    Path
  1.375     +9 -1      cocoon-2.1/status.xml
  
  Index: status.xml
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/status.xml,v
  retrieving revision 1.374
  retrieving revision 1.375
  diff -u -r1.374 -r1.375
  --- status.xml	27 Jun 2004 01:14:17 -0000	1.374
  +++ status.xml	27 Jun 2004 17:40:10 -0000	1.375
  @@ -204,6 +204,14 @@
   
     <changes>
    <release version="@version@" date="@date@">
  +   <action dev="AG" type="fix" fixes-bug="25083">
  +     Add new &lt;jx:comment&gt; tag. XML comments are no more parsed. Sample:
  +     
  +     &lt;!-- This text will not be parsed as it is a comment node. --&gt;
  +
  +     &lt;jx:comment&gt;This text will be parsed as it is a text node. It will end as
  +     &lt;!-- --&gt; in the result.&lt;/jx:comment&gt;
  +   </action>
      <action dev="AG" type="fix" fixes-bug="29369" due-to="Juan Jose Pablos">
        Move commons-logging-1.0.4.jar to lib/core as jcs.jar depends on it
      </action>
  
  
  
  1.48      +74 -153   cocoon-2.1/src/java/org/apache/cocoon/generation/JXTemplateGenerator.java
  
  Index: JXTemplateGenerator.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/generation/JXTemplateGenerator.java,v
  retrieving revision 1.47
  retrieving revision 1.48
  diff -u -r1.47 -r1.48
  --- JXTemplateGenerator.java	26 Jun 2004 08:41:18 -0000	1.47
  +++ JXTemplateGenerator.java	27 Jun 2004 17:40:10 -0000	1.48
  @@ -37,6 +37,7 @@
   import java.util.List;
   import java.util.Locale;
   import java.util.Map;
  +import java.util.Properties;
   import java.util.Stack;
   import java.util.TimeZone;
   
  @@ -55,6 +56,7 @@
   import org.apache.cocoon.transformation.ServiceableTransformer;
   import org.apache.cocoon.xml.XMLConsumer;
   import org.apache.cocoon.xml.IncludeXMLConsumer;
  +import org.apache.cocoon.xml.XMLUtils;
   import org.apache.cocoon.xml.dom.DOMBuilder;
   import org.apache.cocoon.xml.dom.DOMStreamer;
   import org.apache.commons.jexl.Expression;
  @@ -132,6 +134,7 @@
                   return null;
               }
               public void remove() {
  +                // EMPTY
               }
           };
   
  @@ -143,6 +146,7 @@
                   return null;
               }
               public void remove() {
  +                // EMPTY
               }
       };
   
  @@ -537,8 +541,7 @@
           }
       }
   
  -    static class MyJexlContext
  -        extends HashMap implements JexlContext {
  +    static class MyJexlContext extends HashMap implements JexlContext {
   
           private MyJexlContext closure;
   
  @@ -560,16 +563,6 @@
   
           public boolean containsKey(Object key) {
               return this.get(key) !=null;
  -            /* if (key.equals("this")) {
  -                return true;
  -            }
  -            boolean result = super.containsKey(key);
  -            if (!result) {
  -                if (closure != null) {
  -                    result = closure.containsKey(key);
  -                }
  -            }
  -            return result; */
           }
   
           public Object get(Object key) {
  @@ -577,10 +570,8 @@
                   return this;
               }
               Object result = super.get(key);
  -            if (result == null) {
  -                if (closure != null) {
  -                    result = closure.get(key);
  -                }
  +            if (result == null && closure != null) {
  +                result = closure.get(key);
               }
               return result;
           }
  @@ -713,6 +704,7 @@
       final static String PARAMETER = "parameter";
       final static String FORMAT_NUMBER = "formatNumber";
       final static String FORMAT_DATE = "formatDate";
  +    final static String COMMENT = "comment";
       final static String CACHE_KEY = "cache-key";
       final static String VALIDITY = "cache-validity";
   
  @@ -973,8 +965,11 @@
           final Locator location;
           Event next; // in document order
           Event(Locator locator) {
  -            this.location =
  -                locator == null ? NULL_LOCATOR : new LocatorImpl(locator);
  +            if (locator != null) {
  +                this.location = new LocatorImpl(locator);
  +            } else {
  +                this.location = NULL_LOCATOR;
  +            }
           }
   
           public String locationString() {
  @@ -992,7 +987,6 @@
           }
       }
   
  -
       static class TextEvent extends Event {
           TextEvent(Locator location, char[] chars, int start, int length)
                                                             throws SAXException {
  @@ -1364,13 +1358,6 @@
           final String uri;
       }
   
  -    static class Comment extends TextEvent {
  -        Comment(Locator location, char[] chars, int start, int length)
  -                    throws SAXException {
  -            super(location, chars, start, length);
  -        }
  -    }
  -
       static class EndCDATA extends Event {
           EndCDATA(Locator location) {
               super(location);
  @@ -1608,6 +1595,13 @@
           final JXTExpression value;
       }
   
  +    
  +     static class StartComment extends StartInstruction {
  +        StartComment(StartElement raw) {
  +            super(raw);
  +        }
  +    }
  +
       // formatNumber tag (borrows from Jakarta taglibs JSTL)
   
       private static Locale parseLocale(String locale, String variant) {
  @@ -2064,24 +2058,24 @@
           }
   
           private void addEvent(Event ev) throws SAXException {
  -            if (ev == null) {
  -                throw new NullPointerException("null event");
  -            }
  -            if (lastEvent == null) {
  -                lastEvent = startEvent = new StartDocument(locator);
  +            if (ev != null) {
  +                if (lastEvent == null) {
  +                    lastEvent = startEvent = new StartDocument(locator);
  +                } else {
  +                    flushChars();
  +                }
  +                lastEvent.next = ev;
  +                lastEvent = ev;    
               } else {
  -                flushChars();
  +                throw new NullPointerException("null event");
               }
  -            lastEvent.next = ev;
  -            lastEvent = ev;
           }
   
           void flushChars() throws SAXException {
               if (charBuf != null) {
                   char[] chars = new char[charBuf.length()];
                   charBuf.getChars(0, charBuf.length(), chars, 0);
  -                Characters ev = new Characters(charLocation,
  -                                               chars, 0, chars.length);
  +                Characters ev = new Characters(charLocation, chars, 0, chars.length);
                   lastEvent.next = ev;
                   lastEvent = ev;
                   charLocation = null;
  @@ -2092,7 +2086,7 @@
           public void characters(char[] ch, int start, int length)
               throws SAXException {
               if (charBuf == null) {
  -                charBuf = new StringBuffer();
  +                charBuf = new StringBuffer(length);
                   if (locator != null) {
                       charLocation = new LocatorImpl(locator);
                   } else {
  @@ -2450,6 +2444,10 @@
                       StartTemplate startTemplate =
                           new StartTemplate(startElement);
                       newEvent = startTemplate;
  +                } else if (localName.equals(COMMENT)) {
  +                    // <jx:comment>This will be parsed</jx:comment>
  +                    StartComment startJXComment = new StartComment(startElement);
  +                    newEvent = startJXComment;
                   } else {
                       throw new SAXParseException("unrecognized tag: " + localName, locator, null);
                   }
  @@ -2467,7 +2465,7 @@
   
           public void comment(char ch[], int start, int length)
               throws SAXException {
  -            addEvent(new Comment(locator, ch, start, length));
  +            // DO NOTHING
           }
   
           public void endCDATA() throws SAXException {
  @@ -2804,95 +2802,6 @@
           }
       }
   
  -/*    private void executeRaw(final XMLConsumer consumer,
  -                            Event startEvent, Event endEvent)
  -    throws SAXException {
  -        Event ev = startEvent;
  -        LocatorFacade loc = new LocatorFacade(ev.location);
  -        consumer.setDocumentLocator(loc);
  -        while (ev != endEvent) {
  -            loc.setDocumentLocator(ev.location);
  -            if (ev instanceof Characters) {
  -                TextEvent text = (TextEvent)ev;
  -                consumer.characters(text.raw, 0, text.raw.length);
  -            } else if (ev instanceof EndDocument) {
  -                consumer.endDocument();
  -            } else if (ev instanceof StartElement) {
  -                StartElement startElement =
  -                    (StartElement)ev;
  -                consumer.startElement(startElement.namespaceURI,
  -                                      startElement.localName,
  -                                      startElement.raw,
  -                                      startElement.attributes);
  -            } else if (ev instanceof EndElement) {
  -                EndElement endElement = (EndElement)ev;
  -                StartElement startElement = endElement.startElement;
  -                consumer.endElement(startElement.namespaceURI,
  -                                    startElement.localName,
  -                                    startElement.raw);
  -            } else if (ev instanceof EndPrefixMapping) {
  -                EndPrefixMapping endPrefixMapping =
  -                    (EndPrefixMapping)ev;
  -                consumer.endPrefixMapping(endPrefixMapping.prefix);
  -            } else if (ev instanceof IgnorableWhitespace) {
  -                TextEvent text = (TextEvent)ev;
  -                consumer.ignorableWhitespace(text.raw, 0, text.raw.length);
  -            } else if (ev instanceof ProcessingInstruction) {
  -                ProcessingInstruction pi = (ProcessingInstruction)ev;
  -                consumer.processingInstruction(pi.target, pi.data);
  -            } else if (ev instanceof SkippedEntity) {
  -                SkippedEntity skippedEntity = (SkippedEntity)ev;
  -                consumer.skippedEntity(skippedEntity.name);
  -            } else if (ev instanceof StartDocument) {
  -                StartDocument startDoc = (StartDocument)ev;
  -                if (startDoc.endDocument != null) {
  -                    // if this isn't a document fragment
  -                    consumer.startDocument();
  -                }
  -            } else if (ev instanceof StartPrefixMapping) {
  -                StartPrefixMapping startPrefixMapping =
  -                    (StartPrefixMapping)ev;
  -                consumer.startPrefixMapping(startPrefixMapping.prefix,
  -                                            startPrefixMapping.uri);
  -            } else if (ev instanceof Comment) {
  -                TextEvent text = (TextEvent)ev;
  -                consumer.comment(text.raw, 0, text.raw.length);
  -            } else if (ev instanceof EndCDATA) {
  -                consumer.endCDATA();
  -            } else if (ev instanceof EndDTD) {
  -                consumer.endDTD();
  -            } else if (ev instanceof EndEntity) {
  -                consumer.endEntity(((EndEntity)ev).name);
  -            } else if (ev instanceof StartCDATA) {
  -                consumer.startCDATA();
  -            } else if (ev instanceof StartDTD) {
  -                StartDTD startDTD = (StartDTD)ev;
  -                consumer.startDTD(startDTD.name,
  -                                  startDTD.publicId,
  -                                  startDTD.systemId);
  -            } else if (ev instanceof StartEntity) {
  -                consumer.startEntity(((StartEntity)ev).name);
  -            } else if (ev instanceof StartInstruction) {
  -                StartInstruction startInstruction = (StartInstruction)ev;
  -                StartElement startElement = startInstruction.startElement;
  -                consumer.startElement(startElement.namespaceURI,
  -                                      startElement.localName,
  -                                      startElement.raw,
  -                                      startElement.attributes);
  -            } else if (ev instanceof EndInstruction) {
  -                EndInstruction endInstruction = (EndInstruction)ev;
  -                StartInstruction startInstruction =
  -                    endInstruction.startInstruction;
  -                StartElement startElement = startInstruction.startElement;
  -                consumer.endElement(startElement.namespaceURI,
  -                                    startElement.localName,
  -                                    startElement.raw);
  -            }
  -            ev = ev.next;
  -        }
  -    }
  -*/
  -
       private void executeDOM(final XMLConsumer consumer,
                               MyJexlContext jexlContext,
                               JXPathContext jxpathContext,
  @@ -3032,9 +2941,6 @@
                                      consumer.ignorableWhitespace(ch, offset, len);
                                  }
                              });
  -            } else if (ev instanceof ProcessingInstruction) {
  -                ProcessingInstruction pi = (ProcessingInstruction)ev;
  -                consumer.processingInstruction(pi.target, pi.data);
               } else if (ev instanceof SkippedEntity) {
                   SkippedEntity skippedEntity = (SkippedEntity)ev;
                   consumer.skippedEntity(skippedEntity.name);
  @@ -3192,12 +3098,12 @@
                       execute(consumer, localJexlContext, localJXPathContext,
                               macroCall, startForEach.next,
                               startForEach.endInstruction);
  -                    /* Skip rows */
  +                    // Skip rows
                       skipCounter = step;
                       while (--skipCounter > 0 && iter.hasNext()) {
                           iter.next();
                       }
  -                    /* Increase index */
  +                    // Increase index
                       i += step;
                       count++;
                   }
  @@ -3264,9 +3170,8 @@
                       builder.endDocument();
                       Node node = builder.getDocument().getDocumentElement();
                       NodeList nodeList = node.getChildNodes();
  +                    // JXPath doesn't handle NodeList, so convert it to an array
                       int len = nodeList.getLength();
  -                    // JXPath doesn't handle NodeList, so convert
  -                    // it to an array
                       Node[] nodeArr = new Node[len];
                       for (int i = 0; i < len; i++) {
                           nodeArr[i] = nodeList.item(i);
  @@ -3476,19 +3381,36 @@
                   StartPrefixMapping startPrefixMapping = (StartPrefixMapping)ev;
                   consumer.startPrefixMapping(startPrefixMapping.prefix,
                                               startPrefixMapping.uri);
  -            } else if (ev instanceof Comment) {
  -                TextEvent text = (TextEvent)ev;
  +            } else if (ev instanceof StartComment) {
  +                StartComment startJXComment = (StartComment)ev;
  +                // Parse the body of the comment
  +                DOMBuilder builder = new DOMBuilder();
  +                builder.startDocument();
  +                builder.startElement(NS, "comment", "comment", EMPTY_ATTRS);
  +                execute(builder, jexlContext, jxpathContext, macroCall,
  +                        startJXComment.next, startJXComment.endInstruction);
  +                builder.endElement(NS, "comment", "comment");
  +                builder.endDocument();
  +                Node node = builder.getDocument().getDocumentElement();
  +                NodeList nodeList = node.getChildNodes();
  +                // JXPath doesn't handle NodeList, so convert
  +                // it to an array
  +                int len = nodeList.getLength();
                   final StringBuffer buf = new StringBuffer();
  -                characters(jexlContext, jxpathContext, text,
  -                           new CharHandler() {
  -                               public void characters(char[] ch, int offset,
  -                                              int len) throws SAXException {
  -                                   buf.append(ch, offset, len);
  -                               }
  -                           });
  +                Properties omit = XMLUtils.createPropertiesForXML(true);
  +                for (int i = 0; i < len; i++) {
  +                    try {
  +                        String str = XMLUtils.serializeNode(nodeList.item(i), omit);  
  +                        buf.append(StringUtils.substringAfter(str, ">")); // cut the XML header
  +                    } catch (ProcessingException e) {
  +                        throw new SAXParseException(e.getMessage(), startJXComment.location, e);
  +                    }
  +                }
                   char[] chars = new char[buf.length()];
                   buf.getChars(0, chars.length, chars, 0);
                   consumer.comment(chars, 0, chars.length);
  +                ev = startJXComment.endInstruction.next;
  +                continue;
               } else if (ev instanceof EndCDATA) {
                   consumer.endCDATA();
               } else if (ev instanceof EndDTD) {
  @@ -3652,7 +3574,7 @@
                       if (doc == null) {
                           Parser parser = new Parser();
                           // call getValidity before using the stream is faster if the source is a SitemapSource
  -                        if ( validity == null ) {
  +                        if (validity == null) {
                               validity = input.getValidity();
                           }
                           SourceUtil.parse(this.manager, input, parser);
  @@ -3674,10 +3596,8 @@
                   MyJexlContext selectJexl = jexlContext;
                   if (startImport.select != null) {
                       try {
  -                        Object obj = getValue(startImport.select,
  -                                              jexlContext, jxpathContext);
  -                        selectJXPath =
  -                            jxpathContextFactory.newContext(null, obj);
  +                        Object obj = getValue(startImport.select, jexlContext, jxpathContext);
  +                        selectJXPath = jxpathContextFactory.newContext(null, obj);
                           selectJXPath.setVariables(variables);
                           selectJexl = new MyJexlContext(jexlContext);
                           fillContext(obj, selectJexl);
  @@ -3691,8 +3611,7 @@
                       }
                   }
                   try {
  -                    execute(consumer, selectJexl, selectJXPath, macroCall,
  -                            doc.next, doc.endDocument);
  +                    execute(consumer, selectJexl, selectJXPath, macroCall, doc.next, doc.endDocument);
                   } catch (Exception exc) {
                           throw new SAXParseException(
                                   "Exception occurred in imported template "
  @@ -3702,13 +3621,15 @@
                   ev = startImport.endInstruction.next;
                   continue;
               } else if (ev instanceof StartDocument) {
  -                StartDocument startDoc = (StartDocument)ev;
  -                if (startDoc.endDocument != null) {
  +                if (((StartDocument)ev).endDocument != null) {
                       // if this isn't a document fragment
                       consumer.startDocument();
                   }
               } else if (ev instanceof EndDocument) {
                   consumer.endDocument();
  +            } else if (ev instanceof ProcessingInstruction) {
  +                ProcessingInstruction pi = (ProcessingInstruction)ev;
  +                consumer.processingInstruction(pi.target, pi.data);
               }
               ev = ev.next;
           }
  
  
  
  1.5       +10 -2     cocoon-2.1/src/webapp/samples/flow/jxcalc/screens/getNumberA.xml
  
  Index: getNumberA.xml
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/webapp/samples/flow/jxcalc/screens/getNumberA.xml,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- getNumberA.xml	6 Mar 2004 02:26:06 -0000	1.4
  +++ getNumberA.xml	27 Jun 2004 17:40:10 -0000	1.5
  @@ -21,12 +21,20 @@
       | Date: March 23, 2002
       +-->
   
  -<page>
  +<page xmlns:jx="http://apache.org/cocoon/templates/jx/1.0">
       <resources>
         <resource type="file" href="../calc/calc.js">Flowscript</resource>
       </resources>
       <title>Calculator</title>
  +    <!--+ XML comments are not parsed.
  +        |ie: This expresion don't break the Generator: #{$.[4.]$..cocoon/continuation/id}">
  +        +-->
       <content>
  +      <jx:comment>
  +      	The "comment" tag allows to insert comments in the result file.
  +      	JXTemplate Expresions are parsed. Sample:
  +	This is the continuation id: #{$cocoon/continuation/id}
  +      </jx:comment>
         <form method="post" action="continue.#{$cocoon/continuation/id}">
           <para>Enter value of <strong>a</strong>: <input type="text" name="a"/></para>
           <input type="submit" name="submit" value="Enter"/>