You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2008/07/29 00:52:14 UTC
svn commit: r680556 - in /myfaces/tomahawk/trunk:
core/src/main/java/org/apache/myfaces/custom/template/
core/src/main/java/org/apache/myfaces/custom/transform/
examples/simple/src/main/webapp/template/
Author: lu4242
Date: Mon Jul 28 15:52:12 2008
New Revision: 680556
URL: http://svn.apache.org/viewvc?rev=680556&view=rev
Log:
change name t:xmlTemplate to t:xmlTransform according to vote
Added:
myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/transform/
- copied from r680539, myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/template/
myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/transform/AbstractXmlTransform.java (with props)
myfaces/tomahawk/trunk/examples/simple/src/main/webapp/template/home.jsp (with props)
Removed:
myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/template/
myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/transform/AbstractXmlTemplate.java
Modified:
myfaces/tomahawk/trunk/examples/simple/src/main/webapp/template/index.jsp
Added: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/transform/AbstractXmlTransform.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/transform/AbstractXmlTransform.java?rev=680556&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/transform/AbstractXmlTransform.java (added)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/transform/AbstractXmlTransform.java Mon Jul 28 15:52:12 2008
@@ -0,0 +1,223 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.custom.transform;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Writer;
+import java.net.URI;
+import java.net.URL;
+
+import javax.faces.component.UIComponentBase;
+import javax.faces.context.FacesContext;
+import javax.faces.el.ValueBinding;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+/**
+ * Transforms XML using and XSL stylesheet.
+ *
+ * Used to transform XML (from either a String or URL) using either XSLT or
+ * Velocity.
+ *
+ * @JSFComponent
+ * name = "t:xmlTransform"
+ * class = "org.apache.myfaces.custom.transform.XmlTransform"
+ * tagClass = "org.apache.myfaces.custom.transform.XmlTransformTag"
+ *
+ * @author Sean Schofield
+ */
+public abstract class AbstractXmlTransform extends UIComponentBase
+{
+ public static final String COMPONENT_TYPE = "org.apache.myfaces.tomahawk.XmlTransform";
+ public static final String COMPONENT_FAMILY = "org.apache.myfaces.tomahawk.Transform";
+
+ private Object contentStream;
+ private Object styleStream;
+
+ // see superclass for documentation
+ public String getFamily()
+ {
+ return COMPONENT_FAMILY;
+ }
+
+ public void encodeBegin(FacesContext context)
+ throws IOException
+ {
+ InputStream xmlStream = (InputStream)getContentStream();
+ String xml = getContent();
+ String xmlLocation = getContentLocation();
+
+ InputStream xslStream = (InputStream)getStyleStream();
+ String xsl = getStylesheet();
+ String xslLocation = getStylesheetLocation();
+
+ if (context == null) throw new NullPointerException("context");
+ if (!isRendered()) return;
+
+ if (getContent() == null && getContentLocation() == null && getContentStream() == null)
+ throw new NullPointerException("content/contentLocation/contentStream cannot all be null");
+
+ //TODO - handle all cases
+ if (xmlLocation != null)
+ {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ if (loader == null)
+ {
+ loader = AbstractXmlTransform.class.getClassLoader();
+ }
+
+ URL url = loader.getResource(xmlLocation);
+ xmlStream = new FileInputStream(new File(URI.create(url.toString())));
+ }
+
+ if (xslLocation != null)
+ {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ if (loader == null)
+ {
+ loader = AbstractXmlTransform.class.getClassLoader();
+ }
+
+ URL url = loader.getResource(xslLocation);
+ xslStream = new FileInputStream(new File(URI.create(url.toString())));
+ }
+
+ if (xml != null)
+ {
+ xmlStream = new ByteArrayInputStream(xml.getBytes());
+ }
+
+ if (xsl != null)
+ {
+ xslStream = new ByteArrayInputStream(xsl.getBytes());
+ }
+
+ if (xmlStream != null && xslStream != null)
+ {
+ transformContent(xmlStream, xslStream);
+ }
+ }
+
+ /**
+ * Transforms an XML string using the stylesheet string provided.
+ *
+ * @param content The XML to transform
+ * @param stylesheet The stylesheet to use in the transformation
+ * @throws IOException
+ */
+ private void transformContent(InputStream content, InputStream stylesheet)
+ throws IOException
+ {
+ try
+ {
+ TransformerFactory tFactory = TransformerFactory.newInstance();
+ Transformer transformer = tFactory.newTransformer(new StreamSource(stylesheet));
+
+ Writer responseWriter = FacesContext.getCurrentInstance().getResponseWriter();
+ transformer.transform(new StreamSource(content), new StreamResult(responseWriter));
+ }
+ catch (TransformerException te)
+ {
+ throw new IOException("Error while transforming XML: " + te.getMessage());
+ }
+ }
+
+ // component does not need to manage its own children (its not allowed to have any)
+ public void encodeChildren(FacesContext context)
+ throws IOException
+ {}
+
+ // nothing special to do here
+ public void encodeEnd(FacesContext context)
+ throws IOException
+ {}
+
+ /**
+ * String containing the XML content to be transformed.
+ *
+ * @JSFProperty
+ */
+ public abstract String getContent();
+
+ /**
+ * String containing the location of the XML content to be transformed.
+ *
+ * @JSFProperty
+ */
+ public abstract String getContentLocation();
+
+ /**
+ * String containing the XSL information to use in the transformation.
+ *
+ * @JSFProperty
+ */
+ public abstract String getStylesheet();
+
+ public void setContentStream(Object contentStream)
+ {
+ this.contentStream = contentStream;
+ }
+
+ /**
+ * Value binding expression referencing an InputStream from which the XML content is to be read.
+ *
+ * @JSFProperty
+ */
+ public Object getContentStream()
+ {
+ if (contentStream != null) return contentStream;
+
+ ValueBinding vb = getValueBinding("contentStream");
+ return (vb != null) ? vb.getValue(getFacesContext()) : null;
+ }
+
+
+ /**
+ * String containing the location of the XSL stylesheet to use in the transformation.
+ *
+ * @JSFProperty
+ */
+ public abstract String getStylesheetLocation();
+
+ public void setStyleStream(Object styleStream)
+ {
+ this.styleStream = styleStream;
+ }
+
+ /**
+ * Value binding expression referencing an InputStream from which the XSL stylesheet is to be read.
+ *
+ * @JSFProperty
+ */
+ public Object getStyleStream()
+ {
+ if (styleStream != null) return styleStream;
+
+ ValueBinding vb = getValueBinding("styleStream");
+ return (vb != null) ? vb.getValue(getFacesContext()) : null;
+ }
+
+}
Propchange: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/transform/AbstractXmlTransform.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/custom/transform/AbstractXmlTransform.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: myfaces/tomahawk/trunk/examples/simple/src/main/webapp/template/home.jsp
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/examples/simple/src/main/webapp/template/home.jsp?rev=680556&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/examples/simple/src/main/webapp/template/home.jsp (added)
+++ myfaces/tomahawk/trunk/examples/simple/src/main/webapp/template/home.jsp Mon Jul 28 15:52:12 2008
@@ -0,0 +1,4 @@
+<%@ page session="false" contentType="text/xml;charset=utf-8"%>
+<%
+response.sendRedirect("../home.jsf");
+%>
\ No newline at end of file
Propchange: myfaces/tomahawk/trunk/examples/simple/src/main/webapp/template/home.jsp
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: myfaces/tomahawk/trunk/examples/simple/src/main/webapp/template/home.jsp
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Modified: myfaces/tomahawk/trunk/examples/simple/src/main/webapp/template/index.jsp
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/examples/simple/src/main/webapp/template/index.jsp?rev=680556&r1=680555&r2=680556&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/examples/simple/src/main/webapp/template/index.jsp (original)
+++ myfaces/tomahawk/trunk/examples/simple/src/main/webapp/template/index.jsp Mon Jul 28 15:52:12 2008
@@ -31,7 +31,7 @@
<f:view>
<h:outputText value="Example 1: Source and Style Using Location"/>
<br/>
- <t:xmlTemplate rendered="true"
+ <t:xmlTransform rendered="true"
contentLocation="org/apache/myfaces/examples/template/foo.xml"
stylesheetLocation="org/apache/myfaces/examples/template/foo.xsl"/>
@@ -39,7 +39,7 @@
<h:outputText value="Example 2: Source and Style Using Streams and Value Binding"/>
<br/>
- <t:xmlTemplate rendered="true"
+ <t:xmlTransform rendered="true"
contentStream="#{templateBacker.contentStream}"
styleStream="#{templateBacker.styleStream}"/>