You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2003/03/08 11:13:35 UTC

cvs commit: jakarta-commons/jelly/src/java/org/apache/commons/jelly/util SafeContentHandler.java

jstrachan    2003/03/08 02:13:34

  Modified:    jelly/src/java/org/apache/commons/jelly/tags/core
                        FileTag.java
  Added:       jelly/src/java/org/apache/commons/jelly/util
                        SafeContentHandler.java
  Log:
  Patch to avoid multiple start/end document events occuring when outputting files
  
  Revision  Changes    Path
  1.11      +5 -1      jakarta-commons/jelly/src/java/org/apache/commons/jelly/tags/core/FileTag.java
  
  Index: FileTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jelly/src/java/org/apache/commons/jelly/tags/core/FileTag.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- FileTag.java	29 Jan 2003 15:45:36 -0000	1.10
  +++ FileTag.java	8 Mar 2003 10:13:34 -0000	1.11
  @@ -67,10 +67,10 @@
   import org.apache.commons.jelly.JellyTagException;
   import org.apache.commons.jelly.TagSupport;
   import org.apache.commons.jelly.XMLOutput;
  +import org.apache.commons.jelly.util.SafeContentHandler;
   import org.dom4j.io.HTMLWriter;
   import org.dom4j.io.OutputFormat;
   import org.dom4j.io.XMLWriter;
  -
   import org.xml.sax.SAXException;
   
   /** 
  @@ -199,6 +199,10 @@
   
           XMLOutput newOutput = createXMLOutput(writer);
           try {
  +            // we need to avoid multiple start/end document events
  +            newOutput.setContentHandler(
  +            	new SafeContentHandler(newOutput.getContentHandler())
  +            );
               newOutput.startDocument();
               invokeBody(newOutput);
               newOutput.endDocument();
  
  
  
  1.1                  jakarta-commons/jelly/src/java/org/apache/commons/jelly/util/SafeContentHandler.java
  
  Index: SafeContentHandler.java
  ===================================================================
  /*
   * 
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.jelly.util;
  
  import org.xml.sax.Attributes;
  import org.xml.sax.ContentHandler;
  import org.xml.sax.Locator;
  import org.xml.sax.SAXException;
  
  /** 
   * Ensures that only one start and end document event is passed onto the underlying
   * ContentHandler. This object can only be used once and then discarded.
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   */
  public class SafeContentHandler implements ContentHandler {
      private ContentHandler handler;
      private boolean documentStarted;   
      private boolean documentEnded;
      
      public SafeContentHandler(ContentHandler handler) {
          this.handler = handler;
      }
  
      /**
       * @throws org.xml.sax.SAXException
       */
      public void startDocument() throws SAXException {
          if (! documentStarted) {
              handler.startDocument();
              documentStarted = true;
          }
      }
  
      /**
       * @throws org.xml.sax.SAXException
       */
      public void endDocument() throws SAXException {
          if (! documentEnded) {
              handler.endDocument();
              documentEnded = true;
          }
      }
  
      /**
       * @param arg0
       * @param arg1
       * @param arg2
       * @throws org.xml.sax.SAXException
       */
      public void characters(char[] arg0, int arg1, int arg2)
          throws SAXException {
          handler.characters(arg0, arg1, arg2);
      }
  
      /**
       * @param arg0
       * @param arg1
       * @param arg2
       * @throws org.xml.sax.SAXException
       */
      public void endElement(String arg0, String arg1, String arg2)
          throws SAXException {
          handler.endElement(arg0, arg1, arg2);
      }
  
      /**
       * @param arg0
       * @throws org.xml.sax.SAXException
       */
      public void endPrefixMapping(String arg0) throws SAXException {
          handler.endPrefixMapping(arg0);
      }
  
      /**
       * @param arg0
       * @param arg1
       * @param arg2
       * @throws org.xml.sax.SAXException
       */
      public void ignorableWhitespace(char[] arg0, int arg1, int arg2)
          throws SAXException {
          handler.ignorableWhitespace(arg0, arg1, arg2);
      }
  
      /**
       * @param arg0
       * @param arg1
       * @throws org.xml.sax.SAXException
       */
      public void processingInstruction(String arg0, String arg1)
          throws SAXException {
          handler.processingInstruction(arg0, arg1);
      }
  
      /**
       * @param arg0
       */
      public void setDocumentLocator(Locator arg0) {
          handler.setDocumentLocator(arg0);
      }
  
      /**
       * @param arg0
       * @throws org.xml.sax.SAXException
       */
      public void skippedEntity(String arg0) throws SAXException {
          handler.skippedEntity(arg0);
      }
  
      /**
       * @param arg0
       * @param arg1
       * @param arg2
       * @param arg3
       * @throws org.xml.sax.SAXException
       */
      public void startElement(
          String arg0,
          String arg1,
          String arg2,
          Attributes arg3)
          throws SAXException {
          handler.startElement(arg0, arg1, arg2, arg3);
      }
  
      /**
       * @param arg0
       * @param arg1
       * @throws org.xml.sax.SAXException
       */
      public void startPrefixMapping(String arg0, String arg1)
          throws SAXException {
          handler.startPrefixMapping(arg0, arg1);
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org