cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From stef...@locus.apache.org
Subject cvs commit: xml-cocoon/src/org/apache/cocoon/processor/xsp/library/fp fp.xsl fpError.java fpLibrary.java fpResource.java
Date Fri, 21 Jul 2000 20:12:54 GMT
stefano     00/07/21 13:12:54

  Added:       src/org/apache/cocoon/processor/xsp/library/fp fp.xsl
                        fpError.java fpLibrary.java fpResource.java
  Log:
  added FP taglib
  
  Revision  Changes    Path
  1.1                  xml-cocoon/src/org/apache/cocoon/processor/xsp/library/fp/fp.xsl
  
  Index: fp.xsl
  ===================================================================
  <?xml version="1.0"?>
  <!--
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) @year@ 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 "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/>.
  
  -->
  
  <!-- Written by Jeremy Quinn "sharkbait@mac.com" -->
  
  <xsl:stylesheet version="1.0"
  	xmlns:fp="http://apache.org/cocoon/XSP/FP/1.0"
  	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  	xmlns:xsp="http://www.apache.org/1999/XSP/Core"
  >
  
  	
  <xsl:template match="xsp:page">
  	<xsp:page>
  		<xsl:copy>
  			<xsl:apply-templates select="@*"/>
  		</xsl:copy>
  		<xsp:structure>
  			<xsp:include>org.apache.cocoon.processor.xsp.library.fp.*</xsp:include>
  		</xsp:structure>
  		<xsl:apply-templates/>
  	</xsp:page>
  </xsl:template>
  	
  <xsl:template match="xsp:page/*">
  	<xsl:copy>
  		<xsl:apply-templates select="@*"/>
  		<xsp:logic>
  			Hashtable fpResources = new Hashtable();
  			Hashtable fpErrors = new Hashtable();
  			String fpRedirect = new String();
  			Document fpDocument = null;
  		</xsp:logic>
  		<xsl:apply-templates/>
  		<xsp:attribute name="fp-errors">
  			<xsp:expr>fpErrors.size()</xsp:expr>
  		</xsp:attribute>
  		<xsp:logic>
  			/* if no errors and post, use redirect */
  			if ( fpErrors.size() == 0 ) {
  				/* serialise any changed resources */
  				fpLibrary.saveResources(xspCurrentNode, fpResources, fpErrors);
  				if ("POST".equalsIgnoreCase(request.getMethod())) {
  					/* send redirect */
  					if (!fpRedirect.equals("")) {
  						response.sendRedirect(fpRedirect);
  					}
  				}
  			} else {
  				// output errors
  				fpLibrary.getErrors(xspCurrentNode, fpErrors);
  			}
  		</xsp:logic>
  	</xsl:copy>
  </xsl:template>
  
  <xsl:template match="fp:write">
  	<xsl:variable name="select">
  		<xsl:if test="select"><xsl:value-of select="normalize-space(select)"/></xsl:if>
  		<xsl:if test="@select"><xsl:value-of select="@select"/></xsl:if>
  	</xsl:variable>
  	<xsl:variable name="to">
  		<xsl:if test="to"><xsl:value-of select="normalize-space(to)"/></xsl:if>
  		<xsl:if test="@to"><xsl:value-of select="@to"/></xsl:if>
  	</xsl:variable>
  	<xsl:variable name="mode">
  		<xsl:if test="mode"><xsl:value-of select="normalize-space(mode)"/></xsl:if>
  		<xsl:if test="@mode"><xsl:value-of select="@mode"/></xsl:if>
  	</xsl:variable>
  	<xsl:variable name="value">
  		<xsl:call-template name="get-nested-content">
  			<xsl:with-param name="content" select="."/>
  		</xsl:call-template>
  	</xsl:variable>
  	<xsl:variable name="as">
  		<xsl:call-template name="value-for-as">
          	<xsl:with-param name="default" select="'string'"/>
        </xsl:call-template>
      </xsl:variable>
  	<xsp:logic>
  		fpLibrary.handleWrite(
  			xspCurrentNode,
  			fpResources, 
  	    	fpErrors, 
  	    	"<xsl:copy-of select="$select"/>", 
  	    	"<xsl:copy-of select="$to"/>", 
  	    	<xsl:copy-of select="$value"/>,
  	    	"<xsl:value-of select="$as"/>", 
  	    	"<xsl:copy-of select="$mode"/>"
  	    );
  	</xsp:logic>
  </xsl:template>
  
  <xsl:template match="fp:read">
  	<xsl:variable name="select">
  		<xsl:if test="select"><xsl:value-of select="normalize-space(select)"/></xsl:if>
  		<xsl:if test="@select"><xsl:value-of select="@select"/></xsl:if>
  	</xsl:variable>
  	<xsl:variable name="from">
  		<xsl:if test="from"><xsl:value-of select="normalize-space(from)"/></xsl:if>
  		<xsl:if test="@from"><xsl:value-of select="@from"/></xsl:if>
  	</xsl:variable>
  	<xsl:variable name="as">
  		<xsl:call-template name="value-for-as">
          	<xsl:with-param name="default" select="'string'"/>
      	</xsl:call-template>
      </xsl:variable>
      <xsl:choose>
      	<xsl:when test="name(..) = 'fp:write'">
  			<xsp:logic>
  				fpLibrary.handleRead(
  					xspCurrentNode,
  					fpResources, 
  					fpErrors, 
  					"<xsl:value-of select="$select"/>", 
  					"<xsl:value-of select="$from"/>",
  					"<xsl:value-of select="$as"/>")
  			</xsp:logic>
      	</xsl:when>
      	<xsl:otherwise>
  			<xsp:expr>
  				fpLibrary.handleRead(
  					xspCurrentNode,
  					fpResources, 
  					fpErrors, 
  					"<xsl:value-of select="$select"/>", 
  					"<xsl:value-of select="$from"/>",
  					"<xsl:value-of select="$as"/>")
  			</xsp:expr>
      	</xsl:otherwise>
      </xsl:choose>
  </xsl:template>
  	
  <xsl:template match="fp:resource">
  	<xsl:variable name="file">
  		<xsl:call-template name="get-nested-string">
  			<xsl:with-param name="content" select="fp:resource-file"/>
  		</xsl:call-template>
  	</xsl:variable>
  	<xsl:variable name="node">
  		<xsl:call-template name="get-nested-string">
  			<xsl:with-param name="content" select="fp:resource-node"/>
  		</xsl:call-template>
  	</xsl:variable>
  	<xsl:variable name="mode">
  		<xsl:call-template name="get-nested-string">
  			<xsl:with-param name="content" select="fp:default-mode"/>
  		</xsl:call-template>
  	</xsl:variable>
  	<xsp:logic>
  		fpLibrary.handleFileResource(
  			xspCurrentNode,
  			request, 
  			(javax.servlet.ServletContext)context, 
  			fpResources, 
  			fpErrors,
  			String.valueOf("<xsl:value-of select="@id"/>"), 
  			String.valueOf(<xsl:copy-of select="$file"/>), 
  			String.valueOf(<xsl:copy-of select="$node"/>), 
  			String.valueOf(<xsl:copy-of select="$mode"/>)
  		);
  	</xsp:logic>
  </xsl:template>
  
  <xsl:template match="fp:resource-file"></xsl:template>
  <xsl:template match="fp:resource-node"></xsl:template>
  <xsl:template match="fp:write-mode"></xsl:template>
  
  <xsl:template match="fp:redirect">
  	<xsl:variable name="value">
  		<xsl:call-template name="get-nested-string">
  			<xsl:with-param name="content" select="."/>
  		</xsl:call-template>
  	</xsl:variable>
  	<xsp:logic>
  		fpRedirect = <xsl:copy-of select="$value"/>;
  	</xsp:logic>
  </xsl:template>
  
  <xsl:template match="fp:if-post">
  	<xsp:logic>
  		if ("POST".equalsIgnoreCase(request.getMethod())) {</xsp:logic>
  			<xsl:apply-templates/>
  		<xsp:logic>}; // end if-post
  	</xsp:logic>
  </xsl:template>
  
  <xsl:template match="fp:if-get">
  	<xsp:logic>
  		if ("GET".equalsIgnoreCase(request.getMethod())) {</xsp:logic>
  			<xsl:apply-templates/>
  		<xsp:logic>}; // end if-get
  	</xsp:logic>
  </xsl:template>
  
  <xsl:template match="@*|node()" priority="-1">
  	<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
  </xsl:template>
  
  <xsl:template name="value-for-as">
  	<xsl:param name="default"/>
  	<xsl:choose>
  		<xsl:when test="@as"><xsl:value-of select="@as"/></xsl:when>
  		<xsl:otherwise><xsl:value-of select="$default"/></xsl:otherwise>
  	</xsl:choose>
  </xsl:template>
  
   <xsl:template name="value-for-name">
  	<xsl:choose>
  		<xsl:when test="@name">"<xsl:value-of select="@name"/>"</xsl:when>
  		<xsl:when test="name">
  			<xsl:call-template name="get-nested-content">
  				<xsl:with-param name="content" select="name"/>
  			</xsl:call-template>
  		</xsl:when>
  	</xsl:choose>
  </xsl:template>
  
  <xsl:template name="get-nested-content">
  	<xsl:param name="content"/>
  	<xsl:choose>
  		<xsl:when test="$content/*"><xsl:apply-templates select="$content/*"/></xsl:when>
  		<xsl:otherwise>"<xsl:value-of select="$content"/>"</xsl:otherwise>
  	</xsl:choose>
  </xsl:template>
  
  <xsl:template name="get-nested-string">
  	<xsl:param name="content"/>
  	<xsl:choose>
  		<xsl:when test="$content/*">
  			""
  			<xsl:for-each select="$content/node()">
  				<xsl:choose>
  					<xsl:when test="name(.)">
  						+ <xsl:apply-templates select="."/>
  					</xsl:when>
  					<xsl:otherwise>
  						+ "<xsl:value-of select="translate(.,'&#9;&#10;&#13;','		')"/>"
  					</xsl:otherwise>
  				</xsl:choose>
  			</xsl:for-each>
  		</xsl:when>
  		<xsl:otherwise>"<xsl:value-of select="$content"/>"</xsl:otherwise>
  	</xsl:choose>
  </xsl:template>
  
  </xsl:stylesheet>
  
  
  
  1.1                  xml-cocoon/src/org/apache/cocoon/processor/xsp/library/fp/fpError.java
  
  Index: fpError.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
   
   Copyright (C) @year@ 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 "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.processor.xsp.library.fp;
  
  import java.io.*;
  import java.net.*;
  import java.util.*;
  import java.text.*;
  
  import org.w3c.dom.*;
  
  import javax.servlet.*;
  import javax.servlet.http.*;
  
  import org.apache.cocoon.processor.xsp.*;
  import org.apache.cocoon.framework.XObject;
  
  public class fpError {
  
  	Node cNode = null;
  	Node eNode = null;
  	String eMessage = null;
  	String eLabel = null;
  	
    	public fpError (Node cNode, Node eNode, String eMessage, String eLabel) {
    		this.cNode = cNode;
    		this.eNode = eNode;
    		this.eMessage = eMessage;
    		this.eLabel = eLabel;
    		System.out.println(getErrorString());
    	}
  
  	public String getErrorString () {
  		StringBuffer sb = new StringBuffer();
  		sb.append("fp error on:					" + ((cNode != null) ? cNode.getNodeName(): "unkown") + "\r");
  		sb.append("fp error-message:		" + eMessage + "\r");
  		sb.append("fp error-node-name:	" + ((eNode != null) ? eNode.getNodeName(): "unkown") +
"\r");
  		sb.append("fp error-node-type:	" + ((eNode != null) ? String.valueOf(eNode.getNodeType()):
"unkown") + "\r");
  		return sb.toString();
  	}
  
  	public Node getErrorNode(Node rNode) {
  		Element errors = null, msg;
  		try {
  			Document doc = rNode.getOwnerDocument();
  			errors = doc.createElement("fp-error");
  			errors.setAttribute("id", "fp-" + eLabel);
  			msg = doc.createElement("fp-error-message");
  			errors.appendChild(doc.createTextNode("\n\t"));
  			msg.appendChild(doc.createTextNode(eMessage));
  			errors.appendChild(msg);
  			msg = doc.createElement("fp-error-node-name");
  			msg.appendChild(doc.createTextNode("\n\t"));
  			msg.appendChild(doc.createTextNode(eNode.getNodeName()));
  			errors.appendChild(msg);
  			msg = doc.createElement("fp-error-node-type");
  			msg.appendChild(doc.createTextNode("\n\t"));
  			msg.appendChild(doc.createTextNode(String.valueOf(eNode.getNodeType())));
  			errors.appendChild(msg);
  		} catch (Exception e) {
  			e.printStackTrace();
  			System.out.println(e.getMessage());
  		}
  		return errors;
  	}
  }
  
  
  
  1.1                  xml-cocoon/src/org/apache/cocoon/processor/xsp/library/fp/fpLibrary.java
  
  Index: fpLibrary.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
   
   Copyright (C) @year@ 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 "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.processor.xsp.library.fp;
  
  import java.io.*;
  import java.net.*;
  import java.util.*;
  import java.text.*;
  import java.security.*;
  
  import org.w3c.dom.*;
  
  import javax.servlet.*;
  import javax.servlet.http.*;
  
  import org.apache.cocoon.processor.xsp.*;
  import org.apache.cocoon.framework.XObject;
  
  public class fpLibrary {
  
  
  	public static void getErrors(Node cNode, Hashtable errors) {
  		Enumeration e = errors.elements();
  		while (e.hasMoreElements()) {
  			cNode.appendChild(((fpError)e.nextElement()).getErrorNode(cNode));
  		}
  		
  	}
  
  	public static void reportError(Node cNode, Node eNode, Hashtable errors, String message
) {
  		String label = String.valueOf(errors.size() + 1);
  		//System.out.println("error:"+ label);
  		errors.put(label, new fpError(cNode, eNode, message, label));
  		((Element)cNode).setAttribute("fp-error", "fp-" + label);
  	}
  
  	public static Node[] NodeList2Array(NodeList nodes) {
  		int len = nodes.getLength();
  		Node n[]  = new Node[len];
  		if (len > 0) {
  			for(int i = 0; i < len; i++) {
  				n[i] = nodes.item(i);
  			}
  			return n;
  		}
  		return null;
  	}
  
  	public static void handleFileResource(Node cNode, HttpServletRequest request, ServletContext
context, Hashtable resources,  Hashtable errors, String key, String file, String xpath, String
mode) {
  		//try {//
  		File f = null;
  		try {
  			f = new File(XSPUtil.relativeFilename(file, request, context));
  		} catch (Exception e) {
  			reportError(cNode, cNode, errors, "Resource Error: Could not locate file: " + f.toString()
+ " : " + e.getMessage());
  		}
  		fpResource res = new fpResource(cNode, f, xpath, mode, key);
  		resources.put(key, res);
  		/*} catch(Exception ex) {
  			System.out.println(ex.getMessage());
  		}*/
  			//System.out.println("request [" + request.getParameter("body") + "]");
  		}
   
  	public static Object handleRead(Node cNode, Hashtable resources, Hashtable errors, String
select, String key, String type) {
  		Object e = null;
  		fpResource res = (fpResource)resources.get(key);
  		if (res == null) {
  			reportError(cNode, cNode, errors, "Resource not defined: " + key);
  			return e;
  		}
  		if (type.equalsIgnoreCase("node")) {
  			e = res.readAsNode(cNode, errors, select);
  		} else {
  			e = res.readAsString(cNode, errors, select);
  		}
  		return e;
  	}
  
  	public static Object handleWrite(Node cNode, Hashtable resources, Hashtable errors, String
select, String key, Object value, String type, String mode) {
  		Object e = null;
  		fpResource res = (fpResource)resources.get(key);
  		if (res == null) {
  			reportError(cNode, cNode, errors, "Resource not defined: " + key);
  			return e;
  		}
  		if (type.equalsIgnoreCase("node".trim())) {
  			e = res.writeAsXMLNode(cNode, errors, select, value, mode);
  		} else {
  			e = res.writeAsTextNode(cNode, errors, select, value, mode);
  		}
  		return e;
  	}
  	
  	public static String getAsMarkup(Node node) {
  		if (!node.hasChildNodes()) {
  			//System.out.println("getAsMarkup:" + node.getNodeValue());
  			return node.getNodeValue();
  		} else {
  			StringBuffer buffer = new StringBuffer();
  			NodeList n = node.getChildNodes();
  			int childCount = n.getLength();
  			for (int i = 0; i < childCount; i++) {
  				doMarkup(n.item(i), buffer);
  			}
  			//System.out.println("getAsMarkup:" + buffer.toString());
  			return buffer.toString();
  		}
  	}
  
  		public static void saveResources(Node cNode, Hashtable resources, Hashtable errors) {
  			Enumeration e = resources.elements();
  			while (e.hasMoreElements()) {
  				fpResource res = (fpResource)e.nextElement();
  				if (res.hasChanged()) {
  					res.save(cNode, errors);
  				}
  			}
  		}
  
  	protected static void doMarkup(Node node, StringBuffer buffer) {
  		switch (node.getNodeType()) {
  			case Node.CDATA_SECTION_NODE:
  				buffer.append("<![CDATA[\n");
  				buffer.append(node.getNodeValue());
  				buffer.append("]]>\n");
  				break;
  			case Node.DOCUMENT_NODE:
  			case Node.DOCUMENT_FRAGMENT_NODE: {
  				NodeList nodeList = node.getChildNodes();
  				int childCount = nodeList.getLength();
  				for (int i = 0; i < childCount; i++) {
  					doMarkup(nodeList.item(i), buffer);
  				}
  				break;
  			}
  			case Node.ELEMENT_NODE: {
  				Element element = (Element) node;
  				buffer.append("<" + element.getTagName());
  				NamedNodeMap attributes = element.getAttributes();
  				int attributeCount = attributes.getLength();
  				for (int i = 0; i < attributeCount; i++) {
  					Attr attribute = (Attr) attributes.item(i);
  					buffer.append(" ");
  					buffer.append(attribute.getName());
  					buffer.append("=\"");
  					buffer.append(attribute.getValue());
  					buffer.append("\"");
  				}
  				NodeList nodeList = element.getChildNodes();
  				int childCount = nodeList.getLength();
  				if (childCount == 0) {
  					buffer.append("/>\n");
  				} else {
  					buffer.append(">");
  					for (int i = 0; i < childCount; i++) {
  						doMarkup(nodeList.item(i), buffer);
  					}
  					buffer.append("</");
  					buffer.append(element.getTagName());
  					buffer.append(">");
  				}
  				break;
  			}
  			case Node.COMMENT_NODE:
  				buffer.append("<!-- ");
  				buffer.append(node.getNodeValue());
  				buffer.append(" -->\n");
  				break;
  			case Node.PROCESSING_INSTRUCTION_NODE:
  				ProcessingInstruction pi = (ProcessingInstruction) node;
  				buffer.append("<?");
  				buffer.append(pi.getTarget());
  				buffer.append(" ");
  				buffer.append(pi.getData());
  				buffer.append("?>\n");
  				break;
  			case Node.TEXT_NODE:
  				buffer.append(node.getNodeValue());
  				// buffer.append("\n");
  				break;
  			default:
  				break;
  		}
  	}
  
  }
  
  
  
  1.1                  xml-cocoon/src/org/apache/cocoon/processor/xsp/library/fp/fpResource.java
  
  Index: fpResource.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
   
   Copyright (C) @year@ 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 "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.processor.xsp.library.fp;
  
  import java.io.*;
  import java.net.*;
  import java.util.*;
  import java.text.*;
  
  import org.w3c.dom.*;
  import org.apache.xerces.dom.DocumentImpl;
  import org.apache.xerces.parsers.*;
  import org.apache.xml.serialize.*;
  import javax.servlet.*;
  import javax.servlet.http.*;
  
  import org.apache.cocoon.processor.xsp.*;
  import org.apache.cocoon.xml.util.XPathAPI;
  import org.apache.cocoon.framework.XObject;
  import org.xml.sax.InputSource;
  
  
  public class fpResource {
  
  	//RevalidatingDOMParser parser = null;
  	protected static SerializerFactory serializer_factory = SerializerFactory.getSerializerFactory(Method.XML);
  	Document workDoc = null;
  	String name = "";
  	Node readNode = null;
  	Node writeNode = null;
  	Node errorNode = null;
  	File workFile = null;
  	String workEncoding = "UTF-8";
  	HttpServletRequest request;
  	boolean changed = false;
  	
  	String xpath = "";
  	public String mode = "";
  	
  	
  	public fpResource (Node errorNode, File workFile, String xpath, String mode, String name)
{
  		this.errorNode = errorNode;
  		this.workFile = workFile;
  		this.xpath = xpath;
  		this.mode = mode;
  		this.name = name;
  	}
  
  	public void save(Node cNode, Hashtable errors) {
  		OutputFormat format = new OutputFormat(workDoc, workEncoding, true);
  		format.setVersion("1.0");
  		format.setPreserveSpace(true);
  		format.setIndent(1);
  		format.setIndenting(true);
  		format.setLineWidth(0);
  		format.setLineSeparator("\n");
  	
  		try {
  			FileWriter writer = new FileWriter(workFile);
  			Serializer serializer = serializer_factory.makeSerializer(writer,format);
  			serializer.asDOMSerializer().serialize(workDoc);
  			writer.close();
  		} catch (Exception ex) {
  			fpLibrary.reportError(cNode, cNode, errors, "Cannot save the file: " + workFile);
  			ex.printStackTrace();
  		}
  	}
  		
  	private void init(Hashtable errors) {
  		if (notLoaded()) {
  			Node workNode = null;
  			Node parent = null;
  			NodeList nodes = null;
  			//System.out.println("about to call LoadDocument");
  			workDoc = loadDocument(workFile, errors);
  			//System.out.println("LoadDocument called ");
  			if (workDoc == null) {
  			//System.out.println("Document was null");
  			}
  			try {
  				//System.out.println("about to call XPathAPI.selectNodeList [" + xpath + "]");
  				nodes = XPathAPI.selectNodeList(workDoc.getDocumentElement(), xpath);
  				//System.out.println("XPathAPI.selectNodeList called");
  			} catch (Exception ex) {
  				fpLibrary.reportError(errorNode, errorNode, errors, "Problem using the supplied XPath
expression: " + xpath + " : " + ex.getMessage());
  				ex.printStackTrace();
  			}
  			if (nodes != null && nodes.getLength() == 0) {
  				fpLibrary.reportError(errorNode, errorNode, errors, "(init) No nodes match the given
XPath expression: " + xpath);
  			}
  			//System.out.println("OK");
  			try {
  				readNode = nodes.item(0);
  				writeNode = readNode;
  				parent = readNode.getParentNode();
  				//System.out.println("Starting mode, Got :" + parent.getNodeName() + "/" + readNode.getNodeName());
  				//System.out.println("there are nodes here :" + String.valueOf(readNode.hasChildNodes()));
  				if (mode.equalsIgnoreCase("remove")) {
  					parent.removeChild(readNode);
  					changed = true;
  				} else if (mode.equalsIgnoreCase("insert-before")) {
  					workNode = workDoc.createElement(readNode.getNodeName());
  					parent.insertBefore(workDoc.createTextNode("\r\t"), readNode);
  					parent.insertBefore(workNode, readNode);
  					writeNode = workNode;
  				} else if (mode.equalsIgnoreCase("insert-after")) {
  					workNode = workDoc.createElement(readNode.getNodeName());
  					Node brother = parent.getNextSibling();
  					if (brother == null) {
  						parent.appendChild(workDoc.createTextNode("\r\t"));
  						parent.appendChild(workNode);
  					} else {
  						parent.insertBefore(workDoc.createTextNode("\r\t"), brother);
  						parent.insertBefore(workNode, brother);
  					}
  					writeNode = workNode;
  				}
  			} catch (Exception ex) {
  				ex.printStackTrace();
  			}
  			//System.out.println("Ending mode, Got :" + parent.getNodeName() + "/" + readNode.getNodeName());
  			//System.out.println("there are nodes here :" + String.valueOf(readNode.hasChildNodes()));
  			}
  	}
  
  	public Object readAsNode(Node cNode, Hashtable errors, String select) {
  		init(errors);
  		NodeList nodes = null;
  		((Element)cNode).setAttribute("fp-type", "text/xml");
  		((Element)cNode).setAttribute("fp-action", "read");
  		((Element)cNode).setAttribute("fp-select", select);
  		((Element)cNode).setAttribute("fp-from", name);
  		try {
  			try {
  				nodes = XPathAPI.selectNodeList(readNode, select, null);
  			} catch (Exception ex) {
  				fpLibrary.reportError(cNode, readNode, errors, "XPath expression error on:" + select
+ " : " + ex.getMessage());
  				ex.printStackTrace();
  				return null;
  			}
  			if (nodes == null || nodes.getLength() == 0) {
  				fpLibrary.reportError(cNode, readNode, errors, "No nodes match the given XPath expression:"
+ select);
  				return null;
  			} 
  				//e.appendChild((Element)XSPUtil.cloneNode(nodes.item(0), outDoc));
  				//e.appendChild((Element)XSPUtil.cloneNode((Node)nodes, outDoc));
  				//for (int i = 0; i < nodes.getLength(); i ++) {
  				//	e.appendChild((Element)XSPUtil.cloneNode(nodes.item(i), outDoc));
  				//}
  		} catch (Exception ex) {
  			System.out.println(ex.getMessage());
  			ex.printStackTrace();
  		}
  		return fpLibrary.NodeList2Array(nodes);
  	}
  
  	public Object readAsString(Node cNode, Hashtable errors, String select) {
  		init(errors);
  		NodeList nodes = null;
  		StringBuffer temp = new StringBuffer("");
  		((Element)cNode).setAttribute("fp-type", "text/plain");
  		((Element)cNode).setAttribute("fp-action", "read");
  		((Element)cNode).setAttribute("fp-select", select);
  		((Element)cNode).setAttribute("fp-from", name);
  		try {
  			try {
  				nodes = XPathAPI.selectNodeList(readNode, select, null);
  				//System.out.println("Found " + nodes.getLength() + " nodes");
  			} catch (Exception ex) {
  				fpLibrary.reportError(cNode, readNode, errors, "XPath expression error on:" + select
+ " : " + ex.getMessage());
  				ex.printStackTrace();
  			}
  			if (nodes == null || nodes.getLength() == 0) {
  				fpLibrary.reportError(cNode, readNode, errors, "No nodes match the given XPath expression:"
+ select);
  			} else {
  				//System.out.println("extracting TextNodes from: " + nodes.getLength());
  				// for each of our results
  				for (int i = 0; i < nodes.getLength(); i ++) {
  					Node n = nodes.item(i);
  					NodeList nl = n.getChildNodes();
  					if (nodes.getLength() == 0) { return n.getNodeValue(); }// if this is a TextNode, get
just it
  					for (int ix = 0; ix < nl.getLength(); ix++) 
  						if (nl.item(ix).getNodeType() == Node.TEXT_NODE) { // only get the Text Nodes
  							//System.out.println("extracting: [" + nl.item(ix).getNodeValue() + "]");
  							//temp.append(fpLibrary.getAsMarkup((Node)nodes.item(i)));
  							temp.append(nl.item(ix).getNodeValue());
  						}
  					}
  				}
  				/*
  				 = fpLibrary.getAsMarkup((Node)nodes);
  				System.out.println("got : [" + temp + "]");*/
  				//e.appendChild(outDoc.createTextNode(temp.toString()));
  
  			} catch (Exception ex) {
  				ex.printStackTrace();
  			}
  			return temp.toString();
  	}
  
  	public Object writeAsTextNode(Node cNode, Hashtable errors, String select, Object value,
String customMode) {
  		init(errors);
  		//Object result = null;
  		((Element)cNode).setAttribute("fp-type", "text/plain");
  		((Element)cNode).setAttribute("fp-action", "write");
  		((Element)cNode).setAttribute("fp-select", select);
  		((Element)cNode).setAttribute("fp-to", name);
  		try {
  			Object fileNode = sureNode(select); // construct path to node
  			if (fileNode instanceof Node[]) {
  				int len = ((Node[])fileNode).length;
  				if ( len > 1 ) {  // FIXME ?? is there an alternative to this?
  					fpLibrary.reportError(cNode, readNode, errors, "Cannot write to the multiple Nodes
returned by this XPath expression:" + select);
  					return null;
  				} else if ( len == 1 ) {
  					fileNode = (Node)((Node[])fileNode)[0];
  				}
  			}
  			NodeList kids = ((Node)fileNode).getChildNodes();
  			for (int i = kids.getLength()-1; i>-1	 ; i --) { // strip out all existing TextNodes
from fileNode
  				if (kids.item(i).getNodeType() == Node.TEXT_NODE) {
  					((Element)fileNode).removeChild(kids.item(i));
  				}
  			}
  			if (value instanceof Node[]) { // from embedded fp:read tag having multiple hits with
it's XPath
  				Node[] v = (Node[])value;
  				for (int i = 0; i < v.length ; i ++) { // adopt all TextNode elements
  					if (v[i].getNodeType() == Node.TEXT_NODE  ) {
  						((Element)fileNode).appendChild(workDoc.createTextNode(v[i].getNodeValue())); // send
to file
  					}
  				}
  			//result = fileNode; // send to form
  			} else if (value instanceof Element) { // from embedded fp:tag
  				Node v = (Node)value;
  				kids = v.getChildNodes();
  				if (kids.getLength() == 0) { // it's a Text Node (?!?)
  					((Element)fileNode).appendChild(workDoc.createTextNode(v.getNodeValue()));
  				} else {
  					for (int i = 0; i < kids.getLength() ; i ++) {
  						if (kids.item(i).getNodeType() == Node.TEXT_NODE) {
  							((Element)fileNode).appendChild(workDoc.createTextNode(kids.item(i).getNodeValue()));
// clone the TextNode kids to file
  						}
  					}
  				}
  				//result = fileNode; // send to form
  			} else { // make a TextNode of the String value
  				Node tn = workDoc.createTextNode((String)value);
  				((Element)fileNode).appendChild(tn); // send to file
  				//result = tn;
  			}
  			// removed till Xerces 1.1.2 is working
  			/*Node errorNode = parser.validate(workNode);
  			if (errorNode != null) {
  				fpLibrary.reportError(currentNode, errorNode, "Cannot Validate from: " + errorNode.getNodeName()
+ " with XPath: " + select);
  				errors = true;
  			} else {
  				changed = true;
  			}*/
  			changed = true;
  					
  		} catch (Exception ex) {
  			ex.printStackTrace();
  		}
  		return "";  // no longer returning write data
  	}
  
  	public Object writeAsXMLNode(Node cNode, Hashtable errors, String select, Object value,
String customMode) {
  		init(errors);
  		//Object result = null;
  		((Element)cNode).setAttribute("fp-type", "text/xml");
  		((Element)cNode).setAttribute("fp-action", "write");
  		((Element)cNode).setAttribute("fp-select", select);
  		((Element)cNode).setAttribute("fp-to", name);
  		try {
  			Object fileNode = sureNode(select);
  			if (fileNode instanceof Node[]) {
  				int len = ((Node[])fileNode).length;
  				if ( len > 1 ) {
  					fpLibrary.reportError(cNode, readNode, errors, "Cannot write to the multiple Nodes
returned by this XPath expression:" + select);
  					return value;
  				} else if ( len == 1 ) {
  					fileNode = (Node)((Node[])fileNode)[0];
  				}
  			}
  			Node newNode = workDoc.createElement(((Node)fileNode).getNodeName()); // node to insert
to file
  			if (value instanceof Node[]) { // from embedded fp:read tag having multiple hits with
it's XPath
  				Node[] v = (Node[])value;
  				for (int i = 0; i < v.length ; i ++) { // adopt all Node elements
  					newNode.appendChild(XSPUtil.cloneNode(v[i], workDoc)); // send to file
  				}
  			} else if (value instanceof Element) { // from embedded fp:tag
  				Node v = (Node)value;
  				NodeList kids = v.getChildNodes();
  				for (int i = 0; i < kids.getLength() ; i ++) { // adopt all childNodes
  					newNode.appendChild(XSPUtil.cloneNode(kids.item(i), workDoc)); // send to file
  				}
  			} else {
  				// make a TextNode of the parsed String value
  				newNode = parseStringInto(workDoc, cNode, String.valueOf(value), errors); // send to
file
  			}
  			if (newNode != null) {
  				((Node)fileNode).getParentNode().replaceChild(newNode, (Node)fileNode);// replace copy
in file
  				//result = newNode; // send it to form
  			}
  			// removed till Xerces 1.1.2 is working
  			/*Node errorNode = parser.validate(fileNode);
  			if (errorNode != null) {
  				fpLibrary.reportError(cNode, errorNode, errors, "Cannot Validate from: " + errorNode.getNodeName()
+ " with XPath: " + select);
  				errors = true;
  			}*/			
  			changed = true;
  		} catch (Exception ex) {
  			ex.printStackTrace();
  		}
  		return "";  // no longer returning write data
  	}
  
  	/* get the Element we need to change, building any required on the way */
  	public Object sureNode(String select) {
  		NodeList nodes = null;
  		boolean found = false;
  		Element e = null;
  		//System.out.println("sureNode select:" + select + " from:" + writeNode.getNodeName());
  		try {
  			nodes = XPathAPI.selectNodeList(writeNode, select);
  		} catch (Exception ex) {
  			//fpLibrary.reportError(currentNode, rootNode, "Cannot construct XPath: " + select +
" : " + ex.getMessage());
  			//errors = true;
  		}
  		/*if (nodes != null) {
  			System.out.println("Selected Nodes:" + nodes.getLength());
  		} else {
  			System.out.println("Selected Nodes:0");
  		}*/
  		if (nodes != null && nodes.getLength() > 0) {
  			// I've got hits, return an Array of Nodes
  			return fpLibrary.NodeList2Array(nodes);
  		} else {
  			/* make it */
  			//System.out.println("Making Nodes");
  			StringTokenizer st = new StringTokenizer(select,"/");
  			Node workNode = writeNode;
  			NodeList kids;
  			String part = "";
  			int len;
  			while (st.hasMoreTokens()) {
  				part = st.nextToken();
  				//System.out.println("Looking for:" + part);
  				kids = workNode.getChildNodes();
  				found = false;
  				len = kids.getLength();
  				//System.out.println("Children:" + len);
  				for (int i = 0; i < len ; i++) {
  					//System.out.println("OK 6");
  					Node n = kids.item(i);
  					//System.out.println("Looking at:"+ n.getNodeName());
  					if (n.getNodeName().equals(part)) {
  						workNode = n;
  						//System.out.println("found = true");
  						found = true;
  						break;
  					}
  				}
  			}
  			if (found == false) {
  				//System.out.println("createElement");
  				//System.out.println((workDoc!=null) ? "OK": "SHIT!");
  				part = (part=="")?select:part;
  				e = workDoc.createElement(part);
  				//System.out.println("appendChild");
  				workNode.appendChild(workDoc.createTextNode("\n\t"));
  				workNode.appendChild(e);
  			}
  		}
  		//System.out.println("Done");
  			return e;
  		}
  
  	public Node parseStringInto(Document doc, Node cNode, String val, Hashtable errors) {
  		DOMParser p = new DOMParser();
  		try {
  			//System.out.println("Writing:" + val);
  			p.parse(new InputSource(new StringReader(val)));
  		} catch (Exception ex) {
  			fpLibrary.reportError(cNode, errorNode, errors, "Cannot Parse: "  + ex.getMessage());
  			ex.printStackTrace();
  			return null;
  		}
  		return XSPUtil.cloneNode(p.getDocument().getDocumentElement(), doc);
  	}
  
  	public Document loadDocument(File f, Hashtable errors) {
  		// removed till Xerces 1.1.2 is working
  		//RevalidatingDOMParser parser = new RevalidatingDOMParser();
  		DOMParser parser = new DOMParser();
  		try {
  			//System.out.println("about to parse Document");
  			InputSource is = new InputSource(f.toString());
  			String enc = is.getEncoding();
  			if (enc != "") {
  				workEncoding = enc;
  			}
  			parser.parse(is);
  			//System.out.println("parsed Document");
  		} catch (Exception ex) {
  			//System.out.println("about to call reportError");
  			fpLibrary.reportError(errorNode, errorNode, errors, "Cannot Parse: " + f.toString() +
" : " + ex.getMessage());
  			//System.out.println("reportError called");
  			ex.printStackTrace();
  			return null;
  		}
  		return parser.getDocument();
  	}
  
  	public boolean notLoaded() {
  		return (readNode == null);
  	}
  	public boolean hasChanged() {
  		return (changed);
  	}
  }
  
  
  

Mime
View raw message