cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject cvs commit: cocoon-2.0/src/java/org/apache/cocoon/serialization TextSerializer.java
Date Sun, 27 Apr 2003 17:59:34 GMT
vgritsenko    2003/04/27 10:59:33

  Modified:    src/java/org/apache/cocoon/serialization TextSerializer.java
  Log:
  Fix bug 10242: Text serializer adds root element when one is missing.
  Thanks to Peter Hunsberger for patch suggestion.
  
  Revision  Changes    Path
  1.2       +54 -7     cocoon-2.0/src/java/org/apache/cocoon/serialization/TextSerializer.java
  
  Index: TextSerializer.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.0/src/java/org/apache/cocoon/serialization/TextSerializer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TextSerializer.java	9 Mar 2003 00:03:15 -0000	1.1
  +++ TextSerializer.java	27 Apr 2003 17:59:33 -0000	1.2
  @@ -53,27 +53,46 @@
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.ConfigurationException;
   import org.apache.cocoon.CascadingIOException;
  +import org.xml.sax.SAXException;
  +import org.xml.sax.Attributes;
  +import org.xml.sax.helpers.AttributesImpl;
  +
   import javax.xml.transform.OutputKeys;
   import javax.xml.transform.sax.TransformerHandler;
   import javax.xml.transform.stream.StreamResult;
   import java.io.IOException;
   import java.io.OutputStream;
   
  -
   /**
  + * Text serializer converts XML into plain text.
  + * It omits all XML tags and writes only character events to the output.
  + * Internally, text serializer uses XML serializer with {@link OutputKeys#METHOD}
  + * set to <code>text</code>.
  + *
  + * <p>Input document must have at least one element - root element - which
  + * should wrap all the text inside it.
  + *
    * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
    * @version CVS $Id$
    */
  -
   public class TextSerializer extends AbstractTextSerializer {
   
       /**
  +     * Set to true after first XML element
  +     */
  +    private boolean hasRootElement;
  +
  +    /**
  +     * Set to true after first XML element
  +     */
  +    private boolean hadNoRootElement;
  +
  +    /**
        * Set the configurations for this serializer.
        */
  -    public void configure(Configuration conf)
  -    throws ConfigurationException {
  -        super.configure( conf );
  -        this.format.put(OutputKeys.METHOD,"text");
  +    public void configure(Configuration conf) throws ConfigurationException {
  +        super.configure(conf);
  +        this.format.put(OutputKeys.METHOD, "text");
       }
   
       /**
  @@ -94,4 +113,32 @@
           }
       }
   
  +    public void startElement(String uri, String loc, String raw, Attributes a)
  +            throws SAXException {
  +        this.hasRootElement = true;
  +        super.startElement(uri, loc, raw, a);
  +    }
  +
  +    public void characters(char c[], int start, int len)
  +            throws SAXException {
  +        if (!this.hasRootElement) {
  +            this.hasRootElement = this.hadNoRootElement = true;
  +            getLogger().warn("Encountered text before root element. Creating <text>
wrapper element.");
  +            super.startElement("", "text", "text", new AttributesImpl());
  +        }
  +        super.characters(c, start, len);
  +    }
  +
  +    public void endDocument() throws SAXException {
  +        if (this.hadNoRootElement) {
  +            super.endElement("", "text", "text");
  +        }
  +        super.endDocument();
  +    }
  +
  +    public void recycle() {
  +        super.recycle();
  +        this.hasRootElement = false;
  +        this.hadNoRootElement = false;
  +    }
   }
  
  
  

Mime
View raw message