Package org.apache.cocoon.sunshine.transformation

Source Code of org.apache.cocoon.sunshine.transformation.InsertTransformer

/*

============================================================================
                   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
    apache@apache.org.

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  <stefano@apache.org>. For more  information on the Apache
Software Foundation, please see <http://www.apache.org/>.

*/
package org.apache.cocoon.sunshine.transformation;

import java.io.File;
import java.io.IOException;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.environment.Source;
import org.apache.cocoon.xml.XMLUtils;
import org.apache.cocoon.sunshine.SunShineConstants;
import org.apache.cocoon.sunshine.connector.ResourceConnector;
import org.apache.cocoon.sunshine.xml.XMLUtil;

/**
* This implementation allows the inserting of an xml fragment into a
* resource which is configured in sunConfig.
*
* <sunshine:insert>
*   <sunshine:resource application="applicationname" name="resourceidentifier"/>
*   <sunshine:path>XPath denoting the position to insert</sunshine:path>
*   <sunshine:fragment>the xml fragment</sunshine:fragment>
* </sunshine:insert>
*
* 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.
* <sunshine:insert>
*   <sunshine:resource application="applicationname" name="resourceidentifier"/>
*   <sunshine:path>XPath denoting the position to insert</sunshine:path>
*   <sunshine:fragment>the xml fragment</sunshine:fragment>
*   <sunshine:replace>XPath denoting a criteria for which node will be replaced</sunshine:replace>
* </sunshine:insert>
*
* 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:
* <sunshine:insert>
*   <sunshine:resource application="system" name="sitemap"/>
*   <sunshine:path>/*[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"]</sunshine:path>
*   <sunshine:fragment>
*      <generator name="file" xmln="http://apache.org/cocoon/sitemap/1.0">
*      <test/>
*    </generator>
*   </sunshine:fragment>
*   <sunshine:replace>*[namespace-uri()="http://apache.org/cocoon/sitemap/1.0" and local-name()="generator" and attribute::name="file"]</sunshine:replace>
</sunshine:insert>
*
* This insert replace (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 <sunshine:reinsert> option can be used to
* reinsert a replaced node at a given path in the new fragment.
*
* @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
* @version CVS $Id: InsertTransformer.java,v 1.4 2002/04/02 14:23:45 cziegeler Exp $
*/
public class InsertTransformer
extends AbstractSunShineTransformer {

   /**
     * Elements
     */
    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 FILE_ELEMENT = "file";

    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;

    /**
     * Default constructor
     */
    public InsertTransformer() {
        this.namespaceURI = SunShineConstants.SUNSHINE_NAMESPACE_URI;
    }

    public void setupTransforming()
    throws IOException, ProcessingException, SAXException {
        super.setupTransforming();
        this.state = InsertTransformer.STATE_OUTSIDE;
    }

    /**
     * This is the real implementation of the startElement event for the Insert Transformer
     * The event is checked for a valid element and the corresponding command
     * is executed.
     */
    public void startTransformingElement(String uri,
                                       String name,
                                       String raw,
                                       Attributes attr)
    throws ProcessingException, IOException, SAXException {
        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) == true && this.state == InsertTransformer.STATE_OUTSIDE) {
            state = InsertTransformer.STATE_INSERT;
            if (attr.getValue(InsertTransformer.INSERT_CREATE_ATTRIBUTE) != null
                && attr.getValue(InsertTransformer.INSERT_CREATE_ATTRIBUTE).equals("false") == true) {
                stack.push("false");
            } else {
                stack.push("true");
            }
            if (attr.getValue(InsertTransformer.INSERT_OVERWRITE_ATTRIBUTE) != null
                && attr.getValue(InsertTransformer.INSERT_OVERWRITE_ATTRIBUTE).equals("false") == true) {
                stack.push("false");
            } else {
                stack.push("true");
            }
            stack.push("INSERT");


        // Element: file
        } else if (name.equals(FILE_ELEMENT) == true && this.state == InsertTransformer.STATE_INSERT) {
            state = InsertTransformer.STATE_FILE;
            this.startTextRecording();

        // Element: path
        } else if (name.equals(PATH_ELEMENT) == true && this.state == InsertTransformer.STATE_INSERT) {
            state = InsertTransformer.STATE_PATH;
            this.startTextRecording();

        // Element: replace
        } else if (name.equals(REPLACE_ELEMENT) == true && this.state == InsertTransformer.STATE_INSERT) {
            state = InsertTransformer.STATE_REPLACE;
            this.startTextRecording();

        // Element: fragment
        } else if (name.equals(FRAGMENT_ELEMENT) == true && this.state == InsertTransformer.STATE_INSERT) {
            state = InsertTransformer.STATE_FRAGMENT;
            this.startRecording();

        // Element: reinsert
        } else if (name.equals(REINSERT_ELEMENT) == true
                   && this.state == InsertTransformer.STATE_INSERT) {
            state = InsertTransformer.STATE_REINSERT;
            this.startTextRecording();

        // default
        } else {
            super.startTransformingElement(uri, name, raw, attr);
        }

        if (this.getLogger().isDebugEnabled() == true) {
            this.getLogger().debug("END startTransformingElement");
        }
    }

    public void endTransformingElement(String uri,
                                     String name,
                                     String raw)
    throws ProcessingException ,IOException, SAXException {
        if (this.getLogger().isDebugEnabled() == true) {
            this.getLogger().debug("BEGIN endTransformingElement uri=" + uri +
                              ", name=" + name +
                              ", raw=" + raw);
        }
        if (name.equals(INSERT_ELEMENT) == true && this.state == InsertTransformer.STATE_INSERT) {
            state = InsertTransformer.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)stack.pop();
                if (tag.equals("PATH") == true) {
                    path = (String)stack.pop();
                } else if (tag.equals("FILE") == true) {
                    fileName = (String)stack.pop();
                } else if (tag.equals("FRAGMENT") == true) {
                    fragment = (DocumentFragment)stack.pop();
                } else if (tag.equals("REPLACE") == true) {
                    replacePath = (String)stack.pop();
                } else if (tag.equals("REINSERT") == true) {
                    reinsert = (String)stack.pop();
                }
            } while (tag.equals("INSERT") == false);
            final boolean overwrite = stack.pop().equals("true");
            final boolean create = stack.pop().equals("true");

            this.insertFragment(fileName,
                                    path,
                                    fragment,
                                    replacePath,
                                    create,
                                    overwrite,
                                    reinsert);

      // Element: file
        } else if (name.equals(FILE_ELEMENT) == true && this.state == InsertTransformer.STATE_FILE) {
            state = InsertTransformer.STATE_INSERT;
            stack.push(this.endTextRecording());
            stack.push("FILE");

        // Element: path
        } else if (name.equals(PATH_ELEMENT) == true && this.state == InsertTransformer.STATE_PATH) {
            state = InsertTransformer.STATE_INSERT;
            stack.push(this.endTextRecording());
            stack.push("PATH");

        // Element: replace
        } else if (name.equals(REPLACE_ELEMENT) == true && this.state == InsertTransformer.STATE_REPLACE) {
            state = InsertTransformer.STATE_INSERT;
            stack.push(this.endTextRecording());
            stack.push("REPLACE");

        // Element: fragment
        } else if (name.equals(FRAGMENT_ELEMENT) == true && this.state == InsertTransformer.STATE_FRAGMENT) {
            state = InsertTransformer.STATE_INSERT;
            stack.push(this.endRecording());
            stack.push("FRAGMENT");

        // Element: reinsert
        } else if (name.equals(REINSERT_ELEMENT) == true
                   && this.state == InsertTransformer.STATE_REINSERT) {
            state = InsertTransformer.STATE_INSERT;
            stack.push(this.endTextRecording());
            stack.push("REINSERT");

        // default
        } else {
            super.endTransformingElement(uri, name, raw);
        }

        if (this.getLogger().isDebugEnabled() == true) {
            this.getLogger().debug("END endTransformingElement");
        }
    }

    /**
     * Insert a fragment into a file.
     * The file is loaded by the resource connector.
     *
     * @param fileName 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.
     */
    public void insertFragment(String fileName,
                               String path,
                               DocumentFragment fragment,
                               String replacePath,
                               boolean create,
                               boolean overwrite,
                               String  reinsertPath)
    throws SAXException, IOException, ProcessingException {
        // no sync req
        if (this.getLogger().isDebugEnabled() == true) {
            this.getLogger().debug("BEGIN insertFragment fileName="+fileName+
                ", path="+path+
                ", replace="+replacePath+
                ", create="+create+
                ", overwrite="+overwrite+
                ", resinsert="+reinsertPath+
                ", fragment="+(fragment == null ? "null" : XMLUtils.serializeNodeToXML(fragment)));
        }
        // test parameter
        if (fileName == null) {
            throw new ProcessingException("insertFragment: file name is required.");
        }
        if (path == null) {
            throw new ProcessingException("insertFragment: path is required.");
        }
        if (fragment == null) {
            throw new ProcessingException("insertFragment: fragment is required.");
        }

        Source fileSource = null;
        String systemId = null;
        try {
            fileSource = this.resolver.resolve( fileName );
            systemId = fileSource.getSystemId();
            if (systemId.startsWith("file:") == false) {
                throw new ProcessingException("insertFragment: this is not a file: " + systemId);
            }
        } finally {
            if (fileSource != null) fileSource.recycle();
        }
        if (path.startsWith("/") == true) path = path.substring(1);

        File file = new File(systemId.substring(5));
        DocumentFragment resource = null;
        if (file.exists() == true) {
            resource = this.getResourceConnector().loadXML(ResourceConnector.RESOURCE_TYPE_FILE, null,
                                                           fileName, null);

            // import the fragment
            Node importNode = resource.getOwnerDocument().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) {
                Document doc = XMLUtil.createDocument();
                resource = doc.createDocumentFragment();
                // import the fragment
                Node importNode = resource.getOwnerDocument().importNode(fragment, true);
                // get the node
                Node parent = XMLUtil.selectSingleNode(resource, path);
                // add fragment
                parent.appendChild(importNode);
            }
        }

        if (resource != null) {
            // finally: save resource
            resource.normalize();
            this.getResourceConnector().saveXML(ResourceConnector.RESOURCE_TYPE_FILE, null,
                                                fileName, null,
                                                resource);
        }

        if (this.getLogger().isDebugEnabled() == true) {
            this.getLogger().debug("END insertFragment");
        }
    }


}
TOP

Related Classes of org.apache.cocoon.sunshine.transformation.InsertTransformer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.