Author: mszefler
Date: Thu Oct 26 14:15:31 2006
New Revision: 468137
URL: http://svn.apache.org/viewvc?view=rev&rev=468137
Log:
Moved XSL utilities to utils module.
Added:
incubator/ode/trunk/utils/src/main/java/org/apache/ode/utils/xsl/
incubator/ode/trunk/utils/src/main/java/org/apache/ode/utils/xsl/XslTransformException.java
(with props)
incubator/ode/trunk/utils/src/main/java/org/apache/ode/utils/xsl/XslTransformHandler.java
(with props)
Added:
incubator/ode/trunk/utils/src/main/java/org/apache/ode/utils/xsl/XslTransformException.java
URL:
http://svn.apache.org/viewvc/incubator/ode/trunk/utils/src/main/java/org/apache/ode/utils/xsl/XslTransformException.java?view=auto&rev=468137
==============================================================================
---
incubator/ode/trunk/utils/src/main/java/org/apache/ode/utils/xsl/XslTransformException.java
(added)
+++
incubator/ode/trunk/utils/src/main/java/org/apache/ode/utils/xsl/XslTransformException.java
Thu Oct 26 14:15:31 2006
@@ -0,0 +1,38 @@
+/*
+ * 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.ode.utils.xsl;
+
+/**
+ * Thrown when an XSL sheet wasn't parsed property or when the transformation
fails.
+ */
+public class XslTransformException extends RuntimeException {
+
+ public XslTransformException(String message) {
+ super(message);
+ }
+
+ public XslTransformException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public XslTransformException(Throwable cause) {
+ super(cause);
+ }
+}
Propchange:
incubator/ode/trunk/utils/src/main/java/org/apache/ode/utils/xsl/XslTransformException.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
incubator/ode/trunk/utils/src/main/java/org/apache/ode/utils/xsl/XslTransformHandler.java
URL:
http://svn.apache.org/viewvc/incubator/ode/trunk/utils/src/main/java/org/apache/ode/utils/xsl/XslTransformHandler.java?view=auto&rev=468137
==============================================================================
---
incubator/ode/trunk/utils/src/main/java/org/apache/ode/utils/xsl/XslTransformHandler.java
(added)
+++
incubator/ode/trunk/utils/src/main/java/org/apache/ode/utils/xsl/XslTransformHandler.java
Thu Oct 26 14:15:31 2006
@@ -0,0 +1,147 @@
+/*
+ * 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.ode.utils.xsl;
+
+import java.io.StringReader;
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+import javax.xml.transform.ErrorListener;
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.Templates;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.stream.StreamSource;
+
+/**
+ * Singleton wrapping the basic <code>javax.xml.transform</code> operations.
The
+ * transformation is then delegated to a Transformer. Supports both XSL 1.0
and XSL 2.0
+ * depending on the version attribute provided in the XSL stylesheet (see
+ * http://www.w3.org/TR/xslt20/#backwards - 3.8 Backwards-Compatible
Processing).
+ * <br/>
+ * The transform handler also implements a simple cache to avo??d multiple
pre-compilation
+ * of the same XSL sheet.
+ */
+public class XslTransformHandler {
+
+ private static XslTransformHandler __singleton;
+
+ private TransformerFactory _transformerFactory = null;
+ private final HashMap<URI,Templates> _templateCache = new HashMap<URI,
Templates>();
+
+ /**
+ * Singleton access.
+ * @return single XslTransformHandler instance.
+ */
+ public static synchronized XslTransformHandler getInstance() {
+ if (__singleton == null) {
+ __singleton = new XslTransformHandler();
+ }
+ return __singleton;
+ }
+
+ private XslTransformHandler() { }
+
+ /**
+ * Sets the transformer factory for initialization.
+ * @param transformerFactory
+ */
+ public void setTransformerFactory(TransformerFactory transformerFactory) {
+ _transformerFactory = transformerFactory;
+ }
+
+ /**
+ * Always parses the provided stylesheet and stores it in cache from its URI.
+ * @param uri referencing the stylesheet
+ * @param body of the XSL document
+ * @param resolver used to resolve includes and imports
+ */
+ public void parseXSLSheet(URI uri, String body, URIResolver resolver) {
+ Templates tm;
+ try {
+ _transformerFactory.setURIResolver(resolver);
+ tm = _transformerFactory.newTemplates(new StreamSource(new
StringReader(body)));
+ } catch (TransformerConfigurationException e) {
+ throw new XslTransformException(e);
+ }
+ synchronized(_templateCache) {
+ _templateCache.put(uri, tm);
+ }
+ }
+
+ /**
+ * Parses the provided stylesheet and stores it in cache only if it's not
there
+ * already.
+ * @param uri referencing the stylesheet
+ * @param body of the XSL document
+ * @param resolver used to resolve includes and imports
+ */
+ public void cacheXSLSheet(URI uri, String body, URIResolver resolver) {
+ Templates tm;
+ synchronized (_templateCache) {
+ tm = _templateCache.get(uri);
+ }
+ if (tm == null) parseXSLSheet(uri, body, resolver);
+ }
+
+ /**
+ * Transforms a Source document to a result using the XSL stylesheet
referenced
+ * by the provided URI. The stylesheet MUST have been parsed previously.
+ * @param uri referencing the stylesheet
+ * @param source XML document
+ * @param result of the transformation (XSL, HTML or text depending of the
output method specified in stylesheet
+ * @param parameters passed to the stylesheet
+ * @param resolver used to resolve includes and imports
+ */
+ public void transform(URI uri, Source source, Result result,
+ Map<QName, Object> parameters, URIResolver resolver) {
+ Templates tm;
+ synchronized (_templateCache) {
+ tm = _templateCache.get(uri);
+ }
+ if (tm == null)
+ throw new XslTransformException("XSL sheet" + uri + " has not been
parsed before transformation!");
+ try {
+ Transformer tf = tm.newTransformer();
+ tf.setURIResolver(resolver);
+ if (parameters != null) {
+ for (Map.Entry<QName, Object> param : parameters.entrySet()) {
+ tf.setParameter("{" + param.getKey().getNamespaceURI() + "}" +
param.getKey().getLocalPart(), param.getValue());
+ }
+ }
+ tf.transform(source, result);
+ } catch (TransformerConfigurationException e) {
+ throw new XslTransformException(e);
+ } catch (TransformerException e) {
+ throw new XslTransformException("XSL Transformation failed!", e);
+ }
+ }
+
+ public void setErrorListener(ErrorListener l) {
+ _transformerFactory.setErrorListener(l);
+ }
+
+}
Propchange:
incubator/ode/trunk/utils/src/main/java/org/apache/ode/utils/xsl/XslTransformHandler.java
------------------------------------------------------------------------------
svn:eol-style = native