cziegeler    02/05/21 01:29:36

  Modified:    src/java/org/apache/cocoon/components/source SourceUtil.java
               src/java/org/apache/cocoon/webapps/session session-tran.xmap
               src/java/org/apache/cocoon/xml/dom DOMStreamer.java
               src/webapp sitemap.xmap
  Added:       src/java/org/apache/cocoon/transformation
                        SourceWritingTransformer.java
  Removed:     src/java/org/apache/cocoon/webapps/session/transformation
                        SourceWritingTransformer.java
  Log:
  Moved SourceWritingTransformer. One issue is left: XMLUtil and XPath handling...
  
  Revision  Changes    Path
  1.2       +16 -1     
xml-cocoon2/src/java/org/apache/cocoon/components/source/SourceUtil.java
  
  Index: SourceUtil.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/source/SourceUtil.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SourceUtil.java   26 Apr 2002 11:54:11 -0000      1.1
  +++ SourceUtil.java   21 May 2002 08:29:35 -0000      1.2
  @@ -55,17 +55,19 @@
   import org.apache.avalon.framework.component.ComponentException;
   import org.apache.avalon.framework.component.ComponentManager;
   import org.apache.cocoon.ProcessingException;
  +import org.apache.cocoon.xml.dom.DOMBuilder;
   import org.apache.excalibur.source.Source;
   import org.apache.excalibur.source.SourceException;
   import org.apache.excalibur.xmlizer.XMLizer;
   import org.xml.sax.ContentHandler;
   import org.xml.sax.SAXException;
  +import org.w3c.dom.Document;
   
   /**
    * This class contains some utility methods for the source resolving.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Carsten Ziegeler</a>
  - * @version $Id: SourceUtil.java,v 1.1 2002/04/26 11:54:11 cziegeler Exp $
  + * @version $Id: SourceUtil.java,v 1.2 2002/05/21 08:29:35 cziegeler Exp $
    */
   public final class SourceUtil
   {
  @@ -103,5 +105,18 @@
                   manager.release( xmlizer );
               }
           }
  +    }
  +
  +    /**
  +     * Generates a DOM from the given source
  +     * @param  source    the data
  +     * @throws ProcessingException if no suitable converter is found
  +     */
  +    static public Document toDOM( Source source,
  +                                  ComponentManager manager )
  +    throws SAXException, IOException, ProcessingException {
  +        DOMBuilder builder = new DOMBuilder();
  +        toSAX(source, builder, manager);
  +        return builder.getDocument();
       }
   }
  
  
  
  1.1                  
xml-cocoon2/src/java/org/apache/cocoon/transformation/SourceWritingTransformer.java
  
  Index: SourceWritingTransformer.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, 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  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" 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
      [EMAIL PROTECTED]
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   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 (INCLU-
   DING, 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 and was  originally created by
   Stefano Mazzocchi  <[EMAIL PROTECTED]>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.transformation;
  
  import org.apache.avalon.excalibur.xml.Parser;
  import org.apache.avalon.framework.activity.Disposable;
  import org.apache.avalon.framework.component.Component;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.component.ComponentSelector;
  import org.apache.avalon.framework.component.Composable;
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.parameters.Parameters;
  import org.apache.cocoon.serialization.Serializer;
  import org.apache.cocoon.caching.CacheValidity;
  import org.apache.cocoon.caching.Cacheable;
  import org.apache.cocoon.environment.SourceResolver;
  import org.apache.cocoon.components.source.SourceUtil;
  import org.apache.cocoon.components.source.WriteableSource;
  import org.apache.cocoon.components.source.WriteableSAXSource;
  import org.apache.cocoon.ProcessingException;
  import org.apache.cocoon.ResourceNotFoundException;
  import org.apache.cocoon.webapps.session.xml.XMLUtil;
  import org.apache.cocoon.xml.XMLConsumer;
  import org.apache.cocoon.xml.XMLUtils;
  import org.apache.cocoon.xml.dom.DOMStreamer;
  import org.apache.excalibur.source.Source;
  import org.apache.excalibur.source.SourceException;
  import org.w3c.dom.*;
  import org.xml.sax.Attributes;
  import org.xml.sax.ContentHandler;
  import org.xml.sax.SAXException;
  import org.xml.sax.helpers.AttributesImpl;
  import java.io.File;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.util.Map;
  import java.util.Properties;
  
  /**
   * This transformer allows you to output to a WritableSource.
   *
   * <p>Definition:</p>
   * <pre>
   * &lt;map:transformer     name="tofile"     
src="org.apache.cocoon.transformation.SourceWritingTransformer"&gt;
   *   &lt;map:parameter name="serializer" value="xml"/&gt;  &lt;!-- this is the 
default Serializer (if your Source needs one, like for instance FileSource) --&gt;
   * &lt;/map:transformer/&gt;
   * </pre>
   *
   * <p>Invocation:</p>
   * <pre>
   * &lt;map:transform type="tofile"&gt;
   *   &lt;map:parameter name="serializer" value="xml"/&gt;
   * &lt;/map:transform&gt;
   * </pre>
   *
   * <p>Input XML document example:</p>
   * <pre>
   * &lt;page xmlns:source="http://apache.org/cocoon/source/1.0"&gt;
   *   ...
   *   &lt;source:write src="context://doc/editable/my.xml"&gt;
   *     &lt;page&gt;
   *       XML Object body
   *     &lt;/page&gt;
   *   &lt;/source:write&gt;
   *   ...
   * &lt;/page&gt;
   * </pre>
   *
   * <p>Output XML document example:</p>
   * <pre>
   * &lt;page xmlns:source="http://apache.org/cocoon/source/1.0"&gt;
   *   ...
   *   &lt;source:write src="/source/specific/path/to/context/doc/editable/my.xml" 
result="success|failure" action="new"&gt;
   *            source specific error message
   *   &lt;/source:write&gt;
   *   ...
   * &lt;/page&gt;
   * </pre>
   *
   * <P>Inserting of XML fragments:</p>
   * This implementation allows the inserting of an xml fragment into a
   * source.
   *
   * <pre>
   * &lt;page xmlns:source="http://apache.org/cocoon/source/1.0"&gt;
   *   ...
   * &lt;source:insert&gt;
   *   &lt;source:source&gt;The SRC&lt;/source:source&gt;
   *   &lt;source:path&gt;XPath denoting the position to insert&lt;/source:path&gt;
   *   &lt;source:fragment&gt;the xml fragment&lt;/source:fragment&gt;
   * &lt;/source:insert&gt;
   *   ...
   * &lt;/page&gt;
   * </pre>
   *
   * By default, the fragment is always inserted (added). It is possible to specify
   * a node (by an XPath) which will be replaced if it exists.
   * <pre>
   * &lt;page xmlns:source="http://apache.org/cocoon/source/1.0"&gt;
   *   ...
   * &lt;source:insert&gt;
   *   &lt;source:source&gt;The SRC&lt;/source:source&gt;
   *   &lt;source:path&gt;XPath denoting the position to insert&lt;/source:path&gt;
   *   &lt;source:fragment&gt;the xml fragment&lt;/source:fragment&gt;
   *   &lt;source:replace&gt;XPath denoting a criteria for which node will be 
replaced&lt;/source:replace>
   * &lt;/source:insert&gt;
   *   ...
   * &lt;/page&gt;
   * </pre>
   *
   * The information for <code>replace</code> has to be relative to path, but can
   *  specify a subnode of the node the be replaced.
   *
   * The XPath specification is very complicated. So here is an example for the 
sitemap:
   * <pre>
   * &lt;page xmlns:source="http://apache.org/cocoon/source/1.0"&gt;
   *   ...
   * &lt;source:insert&gt;
   *   &lt;source:source&gt;sitemap.xmap&lt;/source:source&gt;
   *   &lt;source:path&gt;/*[namespace-uri()="http://apache.org/cocoon/sitemap/1.0"; 
and local-name()="sitemap"]/*[namespace-uri()="http://apache.org/cocoon/sitemap/1.0"; 
and 
local-name()="components"]/*[namespace-uri()="http://apache.org/cocoon/sitemap/1.0"; 
and local-name()="generators"]&lt;/source:path&gt;
   *   &lt;source:fragment&gt;
   *            <generator name="file" xmln="http://apache.org/cocoon/sitemap/1.0";>
   *                    <test/>
   *            </generator>
   *   &lt;/source:fragment&gt;
   *   &lt;source:replace&gt;*[namespace-uri()="http://apache.org/cocoon/sitemap/1.0"; 
and local-name()="generator" and attribute::name="file"]&lt;/source:replace&gt;
   * &lt;/source:insert&gt;
   *   ...
   * &lt;/page&gt;
   * </pre>
   *
   * This insert replaces (if it exists) the file generator definition with a new one.
   * As the sitemap uses namespaces the XPath for the generator is rather complicated.
   * Due to this it is necessary that the node specified by path exists if namespaces
   * are used! Otherwise a node with the name * would be created...
   *
   *  The create attribute of insert. If this is set
   *  to true (default is true), the file is created if it does not exists.
   *  If it is set to false, it is not created, making insert a real insert.
   *  create is only usable for files!
   *  In addition the overwrite attribute is used to check if replacing is allowed.
   *  If overwrite is true (the default) the node is replaced. If it is false
   *  the node is not inserted if the replace node is available.
   *
   * The &lt;session:reinsert&gt; option can be used to
   * reinsert a replaced node at a given path in the new fragment.
   *
   * TODO: Use the serializer instead of the XMLUtils for inserting of fragments
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Carsten Ziegeler</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jeremy Quinn</a>
   *
   */
  public class SourceWritingTransformer
      extends AbstractSAXTransformer {
  
      private static String SWT_URI = "http://apache.org/cocoon/source/1.0";;
      private static String SWT_ELEMENT = "write";
      private static String SWT_RESULT_ATTRIBUTE= "result";
      private static String SWT_SRC_ATTRIBUTE = "src";
      private static String SWT_ACTION_ATTRIBUTE = "action";
      private static String SWT_SERIALIZER_ATTRIBUTE = "serializer";
      private static String SWT_RESULT_FAILED = "failed";
      private static String SWT_RESULT_SUCCESS = "success";
      private static String SWT_ACTION_NONE = "none";
      private static String SWT_ACTION_NEW = "new";
      private static String SWT_ACTION_OVER = "overwritten";
  
      public static final String INSERT_ELEMENT = "insert";
      public static final String INSERT_CREATE_ATTRIBUTE = "create";
      public static final String INSERT_OVERWRITE_ATTRIBUTE = "overwrite";
  
      public static final String PATH_ELEMENT = "path";
  
      public static final String FRAGMENT_ELEMENT = "fragment";
  
      public static final String REPLACE_ELEMENT = "replace";
  
      public static final String SOURCE_ELEMENT = "source";
  
      public static final String REINSERT_ELEMENT = "reinsert";
  
      /** The current state */
      private static final int STATE_OUTSIDE  = 0;
      private static final int STATE_INSERT   = 1;
      private static final int STATE_RESOURCE = 2;
      private static final int STATE_PATH     = 3;
      private static final int STATE_FRAGMENT = 4;
      private static final int STATE_REPLACE  = 5;
      private static final int STATE_FILE     = 6;
      private static final int STATE_REINSERT = 7;
      private int state;
  
      /** The configured serializer name */
      protected String configuredSerializerName;
  
      /** The ContentHandler instance */
      protected XMLConsumer ch;                   // is XMLConsumer suitable for this 
purpose?
  
  
      /** Are we using a Serializer? */
      private boolean isSerializer = false;
  
      /** Current Serializer name. */
      private String serializer_name = null;
  
      /** Current target name. */
      private String target = null;
  
      /** Current OutputStream. */
      private OutputStream os = null;
  
      /** Current status of outputting the file. */
      private boolean failed = true;
  
      /** Current error message. */
      private String message = null;
  
      /** Does the file exist, before we try to make it? */
      private boolean exists = false;
  
      /** the Source. */
      private Source source = null;
  
          /** the WritableSource. */
      private WriteableSource wsource = null;
  
      /**
       * Constructor
       * Set the namespace
       */
      public SourceWritingTransformer() {
          this.namespaceURI = SWT_URI;
      }
  
      /**
       * Get the current <code>Configuration</code> instance used by this
       * <code>Configurable</code>.
       */
      public void configure(Configuration configuration)
      throws ConfigurationException {
          super.configure( configuration );
          this.configuredSerializerName = 
configuration.getChild(SWT_SERIALIZER_ATTRIBUTE).getValue("xml");
      }
  
      /**
       * Get the <code>Parameter</code> called "serializer" from the
       * <code>Transformer</code> invocation.
       */
      public void setup(SourceResolver resolver, Map objectModel, String src, 
Parameters par)
      throws ProcessingException, SAXException, IOException {
          super.setup(resolver, objectModel, src, par);
          this.serializer_name = par.getParameter(SWT_SERIALIZER_ATTRIBUTE, 
this.configuredSerializerName);
          if (this.serializer_name != null && this.getLogger().isDebugEnabled() ) {
              this.getLogger().debug("Setup, using serializer: " + 
this.serializer_name);
          }
          this.state = STATE_OUTSIDE;
      }
  
      /**
       * Receive notification of the beginning of an element.
       *
       * @param uri The Namespace URI, or the empty string if the element has no
       *            Namespace URI or if Namespace
       *            processing is not being performed.
       * @param loc The local name (without prefix), or the empty string if
       *            Namespace processing is not being performed.
       * @param raw The raw XML 1.0 name (with prefix), or the empty string if
       *            raw names are not available.
       * @param a The attributes attached to the element. If there are no
       *          attributes, it shall be an empty Attributes object.
       */
      public void startTransformingElement(String uri, String name, String raw, 
Attributes attr)
      throws SAXException, IOException, ProcessingException {
          if (this.getLogger().isDebugEnabled() == true) {
              this.getLogger().debug("BEGIN startTransformingElement uri=" + uri +
                                ", name=" + name + ", raw=" + raw + ", attr=" + attr);
          }
          // Element: insert
          if (name.equals(INSERT_ELEMENT)
              && this.state == STATE_OUTSIDE) {
              this.state = STATE_INSERT;
              if (attr.getValue(INSERT_CREATE_ATTRIBUTE) != null
                  && attr.getValue(INSERT_CREATE_ATTRIBUTE).equals("false")) {
                  this.stack.push("false");
              } else {
                  this.stack.push("true");
              }
              if (attr.getValue(INSERT_OVERWRITE_ATTRIBUTE) != null
                  && attr.getValue(INSERT_OVERWRITE_ATTRIBUTE).equals("false")) {
                  this.stack.push("false");
              } else {
                  this.stack.push("true");
              }
              this.stack.push("INSERT");
  
  
          // Element: file
          } else if (name.equals(SOURCE_ELEMENT)
                     && this.state == STATE_INSERT) {
              this.state = STATE_FILE;
              this.startTextRecording();
  
          // Element: path
          } else if (name.equals(PATH_ELEMENT)
                     && this.state == STATE_INSERT) {
              this.state = STATE_PATH;
              this.startTextRecording();
  
          // Element: replace
          } else if (name.equals(REPLACE_ELEMENT)
                     && this.state == STATE_INSERT) {
              this.state = STATE_REPLACE;
              this.startTextRecording();
  
          // Element: fragment
          } else if (name.equals(FRAGMENT_ELEMENT)
                     && this.state == STATE_INSERT) {
              this.state = STATE_FRAGMENT;
              this.startRecording();
  
          // Element: reinsert
          } else if (name.equals(REINSERT_ELEMENT)
                     && this.state == STATE_INSERT) {
              this.state = STATE_REINSERT;
              this.startTextRecording();
  
          } else if (name.equals(SWT_ELEMENT)) {
              this.failed = false;
              this.message = null;
              this.target = "";
  
              // look for the Source
              String src = attr.getValue("",SWT_SRC_ATTRIBUTE);
              try {
                  this.message = "The src attribute could not be resolved";
                  this.source = this.resolver.resolveURI(src);
                  this.target = this.source.getSystemId();
  
                  this.message = "The src attribute doesn't resolve to a writeable 
source";
                  this.wsource = (WriteableSource)this.source;
                  this.exists = this.wsource.exists();
  
                  // has a Serializer been specified?
                  String local_serializer = attr.getValue("",SWT_SERIALIZER_ATTRIBUTE);
                  if (local_serializer != null) this.serializer_name = 
local_serializer;
                  if (this.serializer_name != null) {
                      // Lookup the Serializer
                      this.message = "that Serializer does not exist";
                      ComponentSelector selector = null;
                      try {
                          selector = (ComponentSelector)manager.lookup(Serializer.ROLE 
+ "Selector");
                          this.ch = (Serializer)selector.select(this.serializer_name);
                          this.message = "Could not open the source for writing";
                          this.os = this.wsource.getOutputStream();
                          this.message = "could not write the file";
                          ((Serializer)this.ch).setOutputStream(this.os);              
         // Is there a way to avoid this casting?
                          this.isSerializer = true;
                      } finally {
                          this.manager.release( selector );
                      }
                  } else {
                      this.message = "could not get a ContentHandler";
                      this.ch = 
(XMLConsumer)((WriteableSAXSource)wsource).getContentHandler();
                  }
                  this.addRecorder( this.ch );
                  this.startDocument();
                  this.sendStartPrefixMapping();
  
              } catch (Exception e) {
                  getLogger().warn("failed, " + this.message, e);
                  this.failed = true;
                  try {
                      if (this.isSerializer) {
                          this.wsource.cancel(this.os);
                      } else {
                          ((WriteableSAXSource)this.wsource).cancel(this.ch);
                      }
                  } catch (Exception e2) {
                      getLogger().warn("failed to cancel: " + this.target, e2);
                      this.message += " and failed to cancel";
                  }
              }
          // default
          } else {
              super.startTransformingElement(uri, name, raw, attr);
          }
  
          if (this.getLogger().isDebugEnabled() == true) {
              this.getLogger().debug("END startTransformingElement");
          }
      }
  
  
      /**
       * Receive notification of the end of an element.
       *
       * @param uri The Namespace URI, or the empty string if the element has no
       *            Namespace URI or if Namespace
       *            processing is not being performed.
       * @param loc The local name (without prefix), or the empty string if
       *            Namespace processing is not being performed.
       * @param raw The raw XML 1.0 name (with prefix), or the empty string if
       *            raw names are not available.
       */
      public void endTransformingElement(String uri, String name, String raw)
      throws SAXException, IOException, ProcessingException {
          if (this.getLogger().isDebugEnabled() == true) {
              this.getLogger().debug("BEGIN endTransformingElement uri=" + uri +
                                ", name=" + name +
                                ", raw=" + raw);
          }
          if (name.equals(INSERT_ELEMENT) == true && this.state == STATE_INSERT) {
              this.state = STATE_OUTSIDE;
  
              // get the information from the stack
              String tag;
              String     fileName        = null;
              DocumentFragment fragment  = null;
              String     path            = null;
              String     replacePath     = null;
              String     reinsert        = null;
              do {
                  tag = (String)this.stack.pop();
                  if (tag.equals("PATH") == true) {
                      path = (String)this.stack.pop();
                  } else if (tag.equals("FILE") == true) {
                      fileName = (String)this.stack.pop();
                  } else if (tag.equals("FRAGMENT") == true) {
                      fragment = (DocumentFragment)this.stack.pop();
                  } else if (tag.equals("REPLACE") == true) {
                      replacePath = (String)this.stack.pop();
                  } else if (tag.equals("REINSERT") == true) {
                      reinsert = (String)this.stack.pop();
                  }
              } while (tag.equals("INSERT") == false);
              final boolean overwrite = this.stack.pop().equals("true");
              final boolean create = this.stack.pop().equals("true");
  
              // FIXME (CZ) : Overriding of serializer is not implemented yet!
              this.insertFragment(fileName,
                                      path,
                                      fragment,
                                      replacePath,
                                      create,
                                      overwrite,
                                      reinsert,
                                      null);
  
          // Element: file
          } else if (name.equals(SOURCE_ELEMENT) == true && this.state == STATE_FILE) {
              this.state = STATE_INSERT;
              this.stack.push(this.endTextRecording());
              this.stack.push("FILE");
  
          // Element: path
          } else if (name.equals(PATH_ELEMENT) == true && this.state == STATE_PATH) {
              this.state = STATE_INSERT;
              this.stack.push(this.endTextRecording());
              this.stack.push("PATH");
  
          // Element: replace
          } else if (name.equals(REPLACE_ELEMENT) == true && this.state == 
STATE_REPLACE) {
              this.state = STATE_INSERT;
              this.stack.push(this.endTextRecording());
              this.stack.push("REPLACE");
  
          // Element: fragment
          } else if (name.equals(FRAGMENT_ELEMENT) == true && this.state == 
STATE_FRAGMENT) {
              this.state = STATE_INSERT;
              this.stack.push(this.endRecording());
              this.stack.push("FRAGMENT");
  
          // Element: reinsert
          } else if (name.equals(REINSERT_ELEMENT) == true
                     && this.state == STATE_REINSERT) {
              this.state = STATE_INSERT;
              this.stack.push(this.endTextRecording());
              this.stack.push("REINSERT");
  
          } else if (name.equals(SWT_ELEMENT)) {
              if (!this.failed) {
                  this.sendEndPrefixMapping();
                  this.endDocument();
                  this.removeRecorder();
                  // close the OutputStream
                  try {
                      if (this.os != null) {
                          this.os.close();
                          this.os = null;
                      }
  
                  } catch(Exception e) {
                      getLogger().warn("Failed to close source", e);
                      this.message = "Failed to close source";
                      this.failed = true;
                      try {
                          this.message = "Failed to cancel source";
                          if (this.isSerializer) {
                              this.wsource.cancel(this.os);
                          } else {
                              ((WriteableSAXSource)this.wsource).cancel(this.ch);
                          }
                      } catch (Exception e2) {
                          getLogger().warn("failed to cancel: " + this.target, e2);
                      }
                  } finally {
                      if (this.source != null) {
                          this.resolver.release( this.source );
                          this.source = null;
                      }
                  }
                  if (!this.failed) {
                      this.wsource = null;
                  }
  
                  // Report result
                  String result = (this.failed) ? SWT_RESULT_FAILED : 
SWT_RESULT_SUCCESS;
                  String action = SWT_ACTION_NONE;
                  if (!this.failed){
                      if (this.exists) {
                          action = SWT_ACTION_OVER;
                      } else {
                          action = SWT_ACTION_NEW;
                      }
                  }
                  AttributesImpl attrs = new AttributesImpl();
                  attrs.addAttribute(null, SWT_SRC_ATTRIBUTE, SWT_SRC_ATTRIBUTE, 
"CDATA", this.target);
                  attrs.addAttribute(null, SWT_ACTION_ATTRIBUTE, SWT_ACTION_ATTRIBUTE, 
"CDATA", action);
                  attrs.addAttribute(null, SWT_RESULT_ATTRIBUTE, SWT_RESULT_ATTRIBUTE, 
"CDATA", result);
                  if (this.serializer_name != null) attrs.addAttribute(null, 
SWT_SERIALIZER_ATTRIBUTE, SWT_SERIALIZER_ATTRIBUTE, "CDATA", this.serializer_name);
                  super.startElement(uri, name, raw, attrs);
                  if (this.message != null && this.failed == true) 
super.characters(this.message.toCharArray(), 0, this.message.length());
                  super.endElement(uri, name, raw);
                  getLogger().debug("Source Written");
              }
          // default
          } else {
              super.endTransformingElement(uri, name, raw);
          }
  
          if (this.getLogger().isDebugEnabled() == true) {
              this.getLogger().debug("END endTransformingElement");
          }
      }
  
      public void recycle() {
          if (this.wsource != null) {
              getLogger().error("cancelled by recycle() method");
              if (this.os != null) {
                  try {
                      this.wsource.cancel(this.os);
                  } catch (Exception e) {
                      getLogger().error("failed to cancel in recycle() method: 
OutputStream");
                  }
              } else if (this.ch != null) {
                  try {
                      ((WriteableSAXSource)this.wsource).cancel(this.ch);
                  } catch (Exception e) {
                      getLogger().error("failed to cancel in recycle() method: 
ContentHandler");
                  }
              }
          }
          if (this.source != null) {
              this.resolver.release( this.source );
              this.source = null;
          }
  
          if (isSerializer) this.manager.release((Component)this.ch);
          this.ch = null;
          super.recycle();
      }
  
      /**
       * Insert a fragment into a file.
       * The file is loaded by the resource connector.
       *
       * @param systemID The name of the xml file.
       * @param path   The XPath specifying the node under which the data is inserted
       * @param fragment The data to be inserted.
       * @param replacePath Optional XPath relative to <CODE>path</CODE>. This path
       *                    can specify a node which will be removed if it exists.
       *                    So insertFragment can be used as a replace utility.
       * @param create      If the file does not exists and this is set to
       *                    <CODE>false</CODE> nothing is inserted. If it is set
       *                    to <CODE>true</CODE> the file is created and the data
       *                    is inserted.
       * @param overwrite   If this is set to <CODE>true</CODE> the data is only
       *                    inserted if the node specified by the 
<CODE>replacePath</CODE>
       *                    does not exists.
       * @param reinsertPath If specified and a node is replaced , all children of
       *                     this replaced node will be reinserted at the given path.
       * @param serializer  The serializer used to serialize the XML
       */
      protected void insertFragment(String systemID,
                                    String path,
                                    DocumentFragment fragment,
                                    String replacePath,
                                    boolean create,
                                    boolean overwrite,
                                    String  reinsertPath,
                                    String  localSerializer)
      throws SAXException, IOException, ProcessingException {
          // no sync req
          if (this.getLogger().isDebugEnabled() == true) {
              this.getLogger().debug("BEGIN insertFragment systemID="+systemID+
                  ", path="+path+
                  ", replace="+replacePath+
                  ", create="+create+
                  ", overwrite="+overwrite+
                  ", resinsert="+reinsertPath+
                  ", fragment="+(fragment == null ? "null" : 
XMLUtils.serializeNodeToXML(fragment)));
          }
          // test parameter
          if (systemID == null) {
              throw new ProcessingException("insertFragment: systemID is required.");
          }
          if (path == null) {
              throw new ProcessingException("insertFragment: path is required.");
          }
          if (path.startsWith("/") == true) path = path.substring(1);
          if (fragment == null) {
              throw new ProcessingException("insertFragment: fragment is required.");
          }
  
          // first: read the source as a DOM
          Source source = null;
          Document resource = null;
          try {
              source = this.resolver.resolveURI( systemID );
              if ( ! (source instanceof WriteableSource)) {
                  throw new ProcessingException("Source '"+systemID+"' is not 
writeable.");
              }
              WriteableSource ws = (WriteableSource)source;
              if ( ws.exists() ) {
  
                  resource = SourceUtil.toDOM( source, this.manager );
                  // import the fragment
                  Node importNode = resource.importNode(fragment, true);
  
                  // get the node
                  Node parent = XMLUtil.selectSingleNode(resource, path);
  
                  // replace?
                  if (replacePath != null) {
                      try {
                          Node replaceNode = XMLUtil.getSingleNode(parent, 
replacePath);
  
                          // now get the parent of this node until it is the parent 
node for insertion
                          while (replaceNode != null && 
replaceNode.getParentNode().equals(parent) == false) {
                             replaceNode = replaceNode.getParentNode();
                          }
                          if (replaceNode != null) {
                              if (overwrite == true) {
                                  parent.replaceChild(importNode, replaceNode);
                                  if (reinsertPath != null) {
                                      Node insertAt = 
XMLUtil.getSingleNode(importNode, reinsertPath);
                                      if (insertAt != null) {
                                          while (replaceNode.hasChildNodes() == true) {
                                              
insertAt.appendChild(replaceNode.getFirstChild());
                                          }
                                      }
                                  }
                              }
                          } else {
                              parent.appendChild(importNode);
                          }
                      } catch (javax.xml.transform.TransformerException sax) {
                          throw new ProcessingException("TransformerException: " + 
sax, sax);
                      }
                  } else { // no replace
                      parent.appendChild(importNode);
                  }
              } else if (create == true) {
  
                  Parser parser = (Parser)this.manager.lookup(Parser.ROLE);
                  try {
                      resource = parser.createDocument();
                  } finally {
                      this.manager.release( parser );
                  }
  
                  // import the fragment
                  Node importNode = resource.importNode(fragment, true);
                  // get the node
                  Node parent = XMLUtil.selectSingleNode(resource, path);
                  // add fragment
                  parent.appendChild(importNode);
              }
  
              // write source
              if ( resource != null) {
                  resource.normalize();
  
                  if (source instanceof WriteableSAXSource) {
                      ContentHandler contentHandler = 
((WriteableSAXSource)ws).getContentHandler();
                      DOMStreamer streamer = new DOMStreamer(contentHandler);
                      streamer.stream(resource);
                  } else {
                      // use serializer
                      if (localSerializer == null) localSerializer = 
this.configuredSerializerName;
                      if (localSerializer != null) {
                          // Lookup the Serializer
                          ComponentSelector selector = null;
                          Serializer serializer = null;
                          OutputStream oStream = null;
                          try {
                              selector = 
(ComponentSelector)manager.lookup(Serializer.ROLE + "Selector");
                              serializer = 
(Serializer)selector.select(localSerializer);
                              oStream = ws.getOutputStream();
                              serializer.setOutputStream(oStream);
                              DOMStreamer streamer = new DOMStreamer(serializer);
                              streamer.stream(resource);
                          } finally {
                              if (oStream != null) {
                                  try {
                                      oStream.flush();
                                      oStream.close();
                                  } catch (Exception ignore) {}
                              }
                              if ( selector != null ) {
                                  selector.release( serializer );
                                  this.manager.release( selector );
                              }
                          }
                      } else {
                          throw new ProcessingException("No serializer specified for 
writing to source " + systemID);
                      }
                  }
              }
          } catch (ComponentException ce) {
              throw new ProcessingException("Unable to lookup component.", ce);
          } catch (SourceException se) {
              throw new ProcessingException("Error during resolving "+systemID, se);
          } finally {
              this.resolver.release( source );
          }
  
          if (this.getLogger().isDebugEnabled() == true) {
              this.getLogger().debug("END insertFragment");
          }
      }
  
  
  
  }
  
  
  
  1.4       +0 -2      
xml-cocoon2/src/java/org/apache/cocoon/webapps/session/session-tran.xmap
  
  Index: session-tran.xmap
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/webapps/session/session-tran.xmap,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- session-tran.xmap 8 May 2002 07:55:50 -0000       1.3
  +++ session-tran.xmap 21 May 2002 08:29:36 -0000      1.4
  @@ -12,6 +12,4 @@
       <map:transformer name="session-post"
                        
src="org.apache.cocoon.webapps.session.transformation.SessionPostTransformer"
                        pool-max="32" pool-min="8" pool-grow="4"/>
  -    <map:transformer name="write-source"
  -                     
src="org.apache.cocoon.webapps.session.transformation.SourceWritingTransformer"/>
   </xmap>
  
  
  
  1.5       +4 -1      xml-cocoon2/src/java/org/apache/cocoon/xml/dom/DOMStreamer.java
  
  Index: DOMStreamer.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/xml/dom/DOMStreamer.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- DOMStreamer.java  22 Feb 2002 07:03:59 -0000      1.4
  +++ DOMStreamer.java  21 May 2002 08:29:36 -0000      1.5
  @@ -72,7 +72,7 @@
    * @author <a href="mailto:[EMAIL PROTECTED]";>Carsten Ziegeler</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Pierpaolo Fumagalli</a>
    *         (Apache Software Foundation, Exoffice Technologies)
  - * @version CVS $Id: DOMStreamer.java,v 1.4 2002/02/22 07:03:59 cziegeler Exp $
  + * @version CVS $Id: DOMStreamer.java,v 1.5 2002/05/21 08:29:36 cziegeler Exp $
    */
   public class DOMStreamer extends AbstractXMLProducer {
   
  @@ -101,6 +101,9 @@
        */
       public DOMStreamer(ContentHandler content) {
           this(content,null);
  +        if (content instanceof LexicalHandler) {
  +            this.setLexicalHandler((LexicalHandler)content);
  +        }
       }
   
       /**
  
  
  
  1.53      +2 -1      xml-cocoon2/src/webapp/sitemap.xmap
  
  Index: sitemap.xmap
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/webapp/sitemap.xmap,v
  retrieving revision 1.52
  retrieving revision 1.53
  diff -u -r1.52 -r1.53
  --- sitemap.xmap      20 May 2002 06:53:15 -0000      1.52
  +++ sitemap.xmap      21 May 2002 08:29:36 -0000      1.53
  @@ -120,7 +120,8 @@
      <map:transformer logger="sitemap.transformer.readDOMsession" 
name="readDOMsession" 
src="org.apache.cocoon.transformation.ReadDOMSessionTransformer"/>
      <map:transformer logger="sitemap.transformer.encodeURL" name="encodeURL" 
src="org.apache.cocoon.transformation.EncodeURLTransformer"/>
   
  -  
  +   <map:transformer logger="sitemap.transformer.write-source" name="write-source" 
src="org.apache.cocoon.transformation.SourceWritingTransformer"/>
  +
       <map:transformer name="xt" 
src="org.apache.cocoon.transformation.XTTransformer"/>
   </map:transformers>
   
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to