You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by jb...@apache.org on 2010/10/03 00:02:38 UTC
svn commit: r1003898 -
/tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/xml/TransformSupport.java
Author: jboynes
Date: Sat Oct 2 22:02:37 2010
New Revision: 1003898
URL: http://svn.apache.org/viewvc?rev=1003898&view=rev
Log:
whitespace police
Modified:
tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/xml/TransformSupport.java
Modified: tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/xml/TransformSupport.java
URL: http://svn.apache.org/viewvc/tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/xml/TransformSupport.java?rev=1003898&r1=1003897&r2=1003898&view=diff
==============================================================================
--- tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/xml/TransformSupport.java (original)
+++ tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/xml/TransformSupport.java Sat Oct 2 22:02:37 2010
@@ -13,7 +13,7 @@
* 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.taglibs.standard.tag.common.xml;
@@ -23,7 +23,6 @@ import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.util.List;
-
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
@@ -66,37 +65,37 @@ public abstract class TransformSupport e
//*********************************************************************
// Protected state
- protected Object xml; // attribute
- protected String xmlSystemId; // attribute
- protected Object xslt; // attribute
- protected String xsltSystemId; // attribute
- protected Result result; // attribute
+ protected Object xml; // attribute
+ protected String xmlSystemId; // attribute
+ protected Object xslt; // attribute
+ protected String xsltSystemId; // attribute
+ protected Result result; // attribute
//*********************************************************************
// Private state
- private String var; // 'var' attribute
- private int scope; // processed 'scope' attr
- private Transformer t; // actual Transformer
- private TransformerFactory tf; // reusable factory
- private DocumentBuilder db; // reusable factory
- private DocumentBuilderFactory dbf; // reusable factory
+ private String var; // 'var' attribute
+ private int scope; // processed 'scope' attr
+ private Transformer t; // actual Transformer
+ private TransformerFactory tf; // reusable factory
+ private DocumentBuilder db; // reusable factory
+ private DocumentBuilderFactory dbf; // reusable factory
//*********************************************************************
// Constructor and initialization
public TransformSupport() {
- super();
- init();
+ super();
+ init();
}
private void init() {
- xml = xslt = null;
- xmlSystemId = xsltSystemId = null;
- var = null;
- result = null;
- tf = null;
+ xml = xslt = null;
+ xmlSystemId = xsltSystemId = null;
+ var = null;
+ result = null;
+ tf = null;
scope = PageContext.PAGE_SCOPE;
}
@@ -105,121 +104,125 @@ public abstract class TransformSupport e
// Tag logic
public int doStartTag() throws JspException {
- /*
- * We can set up our Transformer here, so we do so, and we let
- * it receive parameters directly from subtags (instead of
- * caching them.
- */
- try {
-
- //************************************
- // Initialize
-
- // set up our DocumentBuilderFactory if necessary
- if (dbf == null) {
- dbf = DocumentBuilderFactory.newInstance();
- dbf.setNamespaceAware(true);
- dbf.setValidating(false);
- }
- if (db == null)
- db = dbf.newDocumentBuilder();
-
- // set up the TransformerFactory if necessary
- if (tf == null)
- tf = TransformerFactory.newInstance();
-
- //************************************
- // Produce transformer
-
- Source s;
- if (xslt != null) {
- if (!(xslt instanceof String) && !(xslt instanceof Reader)
- && !(xslt instanceof javax.xml.transform.Source))
- throw new JspTagException(
- Resources.getMessage("TRANSFORM_XSLT_UNRECOGNIZED"));
- s = getSource(xslt, xsltSystemId);
- } else {
- throw new JspTagException(
- Resources.getMessage("TRANSFORM_NO_TRANSFORMER"));
- }
- tf.setURIResolver(new JstlUriResolver(pageContext));
- t = tf.newTransformer(s);
+ /*
+ * We can set up our Transformer here, so we do so, and we let
+ * it receive parameters directly from subtags (instead of
+ * caching them.
+ */
+ try {
+
+ //************************************
+ // Initialize
+
+ // set up our DocumentBuilderFactory if necessary
+ if (dbf == null) {
+ dbf = DocumentBuilderFactory.newInstance();
+ dbf.setNamespaceAware(true);
+ dbf.setValidating(false);
+ }
+ if (db == null)
+ db = dbf.newDocumentBuilder();
- return EVAL_BODY_BUFFERED;
+ // set up the TransformerFactory if necessary
+ if (tf == null)
+ tf = TransformerFactory.newInstance();
+
+ //************************************
+ // Produce transformer
+
+ Source s;
+ if (xslt != null) {
+ if (!(xslt instanceof String) && !(xslt instanceof Reader)
+ && !(xslt instanceof javax.xml.transform.Source))
+ throw new JspTagException(
+ Resources.getMessage("TRANSFORM_XSLT_UNRECOGNIZED"));
+ s = getSource(xslt, xsltSystemId);
+ } else {
+ throw new JspTagException(
+ Resources.getMessage("TRANSFORM_NO_TRANSFORMER"));
+ }
+ tf.setURIResolver(new JstlUriResolver(pageContext));
+ t = tf.newTransformer(s);
+
+ return EVAL_BODY_BUFFERED;
- } catch (SAXException ex) {
- throw new JspException(ex);
- } catch (ParserConfigurationException ex) {
- throw new JspException(ex);
- } catch (IOException ex) {
- throw new JspException(ex);
- } catch (TransformerConfigurationException ex) {
- throw new JspException(ex);
- }
+ } catch (SAXException ex) {
+ throw new JspException(ex);
+ } catch (ParserConfigurationException ex) {
+ throw new JspException(ex);
+ } catch (IOException ex) {
+ throw new JspException(ex);
+ } catch (TransformerConfigurationException ex) {
+ throw new JspException(ex);
+ }
}
// parse 'xml' or body, transform via our Transformer,
// and store as 'var' or through 'result'
+
public int doEndTag() throws JspException {
- try {
+ try {
- //************************************
- // Determine source XML
+ //************************************
+ // Determine source XML
- // if we haven't gotten a source, use the body (which may be empty)
- Object xml = this.xml;
- if (xml == null) // still equal
- if (bodyContent != null && bodyContent.getString() != null)
- xml = bodyContent.getString().trim();
- else
- xml = "";
-
- // let the Source be with you
- Source source = getSource(xml, xmlSystemId);
-
- //************************************
- // Conduct the transformation
-
- // we can assume at most one of 'var' or 'result' is specified
- if (result != null)
- // we can write directly to the Result
- t.transform(source, result);
- else if (var != null) {
- // we need a Document
- Document d = db.newDocument();
- Result doc = new DOMResult(d);
- t.transform(source, doc);
- pageContext.setAttribute(var, d, scope);
- } else {
- Result page =
- new StreamResult(new SafeWriter(pageContext.getOut()));
- t.transform(source, page);
- }
-
- return EVAL_PAGE;
- } catch (SAXException ex) {
- throw new JspException(ex);
- } catch (ParserConfigurationException ex) {
- throw new JspException(ex);
- } catch (IOException ex) {
- throw new JspException(ex);
- } catch (TransformerException ex) {
- throw new JspException(ex);
- }
+ // if we haven't gotten a source, use the body (which may be empty)
+ Object xml = this.xml;
+ if (xml == null) // still equal
+ if (bodyContent != null && bodyContent.getString() != null)
+ xml = bodyContent.getString().trim();
+ else
+ xml = "";
+
+ // let the Source be with you
+ Source source = getSource(xml, xmlSystemId);
+
+ //************************************
+ // Conduct the transformation
+
+ // we can assume at most one of 'var' or 'result' is specified
+ if (result != null)
+ // we can write directly to the Result
+ t.transform(source, result);
+ else if (var != null) {
+ // we need a Document
+ Document d = db.newDocument();
+ Result doc = new DOMResult(d);
+ t.transform(source, doc);
+ pageContext.setAttribute(var, d, scope);
+ } else {
+ Result page =
+ new StreamResult(new SafeWriter(pageContext.getOut()));
+ t.transform(source, page);
+ }
+
+ return EVAL_PAGE;
+ } catch (SAXException ex) {
+ throw new JspException(ex);
+ } catch (ParserConfigurationException ex) {
+ throw new JspException(ex);
+ } catch (IOException ex) {
+ throw new JspException(ex);
+ } catch (TransformerException ex) {
+ throw new JspException(ex);
+ }
}
// Releases any resources we may have (or inherit)
+
public void release() {
- init();
+ init();
}
//*********************************************************************
// Public methods for subtags
- /** Sets (adds) a transformation parameter on our transformer. */
+ /**
+ * Sets (adds) a transformation parameter on our transformer.
+ */
public void addParameter(String name, Object value) {
- t.setParameter(name, value);
+ t.setParameter(name, value);
}
@@ -232,12 +235,12 @@ public abstract class TransformSupport e
* the current directory in the filesystem.
*/
private static String wrapSystemId(String systemId) {
- if (systemId == null)
- return "jstl:";
- else if (ImportSupport.isAbsoluteUrl(systemId))
- return systemId;
- else
- return ("jstl:" + systemId);
+ if (systemId == null)
+ return "jstl:";
+ else if (ImportSupport.isAbsoluteUrl(systemId))
+ return systemId;
+ else
+ return ("jstl:" + systemId);
}
/**
@@ -247,41 +250,41 @@ public abstract class TransformSupport e
* it as a URL. A null input always results in a null output.
*/
private Source getSource(Object o, String systemId)
- throws SAXException, ParserConfigurationException, IOException {
- if (o == null)
- return null;
+ throws SAXException, ParserConfigurationException, IOException {
+ if (o == null)
+ return null;
else if (o instanceof Source) {
- return (Source) o;
+ return (Source) o;
} else if (o instanceof String) {
- // if we've got a string, chain to Reader below
- return getSource(new StringReader((String) o), systemId);
+ // if we've got a string, chain to Reader below
+ return getSource(new StringReader((String) o), systemId);
} else if (o instanceof Reader) {
- // explicitly go through SAX to maintain control
- // over how relative external entities resolve
+ // explicitly go through SAX to maintain control
+ // over how relative external entities resolve
XMLReader xr = XMLReaderFactory.createXMLReader();
xr.setEntityResolver(
- new ParseSupport.JstlEntityResolver(pageContext));
+ new ParseSupport.JstlEntityResolver(pageContext));
InputSource s = new InputSource((Reader) o);
s.setSystemId(wrapSystemId(systemId));
Source result = new SAXSource(xr, s);
result.setSystemId(wrapSystemId(systemId));
- return result;
+ return result;
} else if (o instanceof Node) {
- return new DOMSource((Node) o);
+ return new DOMSource((Node) o);
} else if (o instanceof List) {
- // support 1-item List because our XPath processor outputs them
- List l = (List) o;
- if (l.size() == 1) {
- return getSource(l.get(0), systemId); // unwrap List
- } else {
- throw new IllegalArgumentException(
- Resources.getMessage("TRANSFORM_SOURCE_INVALID_LIST"));
- }
+ // support 1-item List because our XPath processor outputs them
+ List l = (List) o;
+ if (l.size() == 1) {
+ return getSource(l.get(0), systemId); // unwrap List
+ } else {
+ throw new IllegalArgumentException(
+ Resources.getMessage("TRANSFORM_SOURCE_INVALID_LIST"));
+ }
} else {
- throw new IllegalArgumentException(
- Resources.getMessage("TRANSFORM_SOURCE_UNRECOGNIZED")
- + o.getClass());
- }
+ throw new IllegalArgumentException(
+ Resources.getMessage("TRANSFORM_SOURCE_UNRECOGNIZED")
+ + o.getClass());
+ }
}
@@ -289,7 +292,7 @@ public abstract class TransformSupport e
// Tag attributes
public void setVar(String var) {
- this.var = var;
+ this.var = var;
}
public void setScope(String scope) {
@@ -306,52 +309,64 @@ public abstract class TransformSupport e
* toilet in my office similarly...)
*/
private static class SafeWriter extends Writer {
- private Writer w;
- public SafeWriter(Writer w) { this.w = w; }
- public void close() { }
- public void flush() { }
- public void write(char[] cbuf, int off, int len) throws IOException {
- w.write(cbuf, off, len);
- }
- }
+ private Writer w;
+
+ public SafeWriter(Writer w) {
+ this.w = w;
+ }
+
+ public void close() {
+ }
+
+ public void flush() {
+ }
+
+ public void write(char[] cbuf, int off, int len) throws IOException {
+ w.write(cbuf, off, len);
+ }
+ }
//*********************************************************************
// JSTL-specific URIResolver class
- /** Lets us resolve relative external entities. */
+ /**
+ * Lets us resolve relative external entities.
+ */
private static class JstlUriResolver implements URIResolver {
private final PageContext ctx;
+
public JstlUriResolver(PageContext ctx) {
this.ctx = ctx;
}
+
public Source resolve(String href, String base)
- throws TransformerException {
+ throws TransformerException {
// pass if we don't have a systemId
if (href == null)
return null;
- // remove "jstl" marker from 'base'
+ // remove "jstl" marker from 'base'
// NOTE: how 'base' is determined varies among different Xalan
// xsltc implementations
int index;
if (base != null && (index = base.indexOf("jstl:")) != -1) {
base = base.substring(index + 5);
- }
+ }
// we're only concerned with relative URLs
if (ImportSupport.isAbsoluteUrl(href)
- || (base != null && ImportSupport.isAbsoluteUrl(base)))
+ || (base != null && ImportSupport.isAbsoluteUrl(base)))
return null;
- // base is relative; remove everything after trailing '/'
- if (base == null || base.lastIndexOf("/") == -1)
- base = "";
- else
- base = base.substring(0, base.lastIndexOf("/") + 1);
+ // base is relative; remove everything after trailing '/'
+ if (base == null || base.lastIndexOf("/") == -1)
+ base = "";
+ else
+ base = base.substring(0, base.lastIndexOf("/") + 1);
- // concatenate to produce the real URL we're interested in
- String target = base + href;
+ // concatenate to produce the real URL we're interested in
+ String target = base + href;
// for relative URLs, load and wrap the resource.
// don't bother checking for 'null' since we specifically want
@@ -361,19 +376,19 @@ public abstract class TransformSupport e
s = ctx.getServletContext().getResourceAsStream(target);
if (s == null)
throw new TransformerException(
- Resources.getMessage("UNABLE_TO_RESOLVE_ENTITY",
- href));
+ Resources.getMessage("UNABLE_TO_RESOLVE_ENTITY",
+ href));
} else {
String pagePath =
- ((HttpServletRequest) ctx.getRequest()).getServletPath();
+ ((HttpServletRequest) ctx.getRequest()).getServletPath();
String basePath =
- pagePath.substring(0, pagePath.lastIndexOf("/"));
+ pagePath.substring(0, pagePath.lastIndexOf("/"));
s = ctx.getServletContext().getResourceAsStream(
- basePath + "/" + target);
- if (s == null)
- throw new TransformerException(
- Resources.getMessage("UNABLE_TO_RESOLVE_ENTITY",
- href));
+ basePath + "/" + target);
+ if (s == null)
+ throw new TransformerException(
+ Resources.getMessage("UNABLE_TO_RESOLVE_ENTITY",
+ href));
}
return new StreamSource(s);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org