X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fxml%2FXslTransformer.java;h=b3ab9eda04e516ca0afc4532dbe99826140db9ae;hb=4a575582a5c2999bd816b197d9cf274b4b3ddcd7;hp=fb3d305219946216d1a6092af5c5d2aa95d3c1dc;hpb=32a62ca2c752e33a7873ac868a7a1f289caedcd4;p=utils diff --git a/support/general/src/main/java/org/wamblee/xml/XslTransformer.java b/support/general/src/main/java/org/wamblee/xml/XslTransformer.java index fb3d3052..b3ab9eda 100644 --- a/support/general/src/main/java/org/wamblee/xml/XslTransformer.java +++ b/support/general/src/main/java/org/wamblee/xml/XslTransformer.java @@ -1,21 +1,24 @@ /* - * Copyright 2005 the original author or authors. + * Copyright 2005-2010 the original author or authors. * - * Licensed 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 + * Licensed 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 + * 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. + * 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.wamblee.xml; +import org.w3c.dom.Document; + +import org.wamblee.io.FileResource; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; @@ -33,17 +36,13 @@ import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; -import org.w3c.dom.Document; -import org.wamblee.io.FileResource; - /** * XSL transformer for simplified usage of XSL transformations. - * + * * @author Erik Brakkee */ public class XslTransformer { - - private TransformerFactory _factory; + private TransformerFactory factory; /** * Constructs the URL resolver. @@ -52,8 +51,8 @@ public class XslTransformer { * URI resolver to use. */ public XslTransformer(URIResolver aResolver) { - _factory = TransformerFactory.newInstance(); - _factory.setURIResolver(aResolver); + factory = TransformerFactory.newInstance(); + factory.setURIResolver(aResolver); } /** @@ -61,29 +60,35 @@ public class XslTransformer { * */ public XslTransformer() { - _factory = TransformerFactory.newInstance(); + factory = TransformerFactory.newInstance(); } /** - * Resolves an XSLT based on URI. - * @param aXslt XSLT to resolve, + * Resolves an XSLT based on URI. + * + * @param aXslt + * XSLT to resolve, + * * @return Source for the XSLT - * @throws TransformerException In case the XSLT cannot be found. + * + * @throws TransformerException + * In case the XSLT cannot be found. */ public Source resolve(String aXslt) throws TransformerException { - URIResolver resolver = _factory.getURIResolver(); + URIResolver resolver = factory.getURIResolver(); + if (resolver == null) { if (new File(aXslt).canRead()) { try { return new StreamSource(new FileResource(new File(aXslt)) - .getInputStream()); + .getInputStream()); } catch (IOException e) { throw new TransformerException(e.getMessage(), e); } - } else { - throw new TransformerException("Cannot read '" + aXslt + "'"); } + throw new TransformerException("Cannot read '" + aXslt + "'"); } + return resolver.resolve(aXslt, ""); } @@ -95,17 +100,20 @@ public class XslTransformer { * Document to transform. * @param aXslt * XSLT to use. + * * @return Transformed document. + * * @throws IOException * In case of problems reading resources. * @throws TransformerException * In case transformation fails. */ public Document transform(Document aDocument, Source aXslt) - throws IOException, TransformerException { + throws IOException, TransformerException { Source source = new DOMSource(aDocument); DOMResult result = new DOMResult(); transform(source, result, aXslt); + return (Document) result.getNode(); } @@ -116,17 +124,20 @@ public class XslTransformer { * Document to transform. * @param aXslt * XSLT to use. + * * @return Transformed document. + * * @throws IOException * In case of problems reading resources. * @throws TransformerException * In case transformation fails. */ public Document transform(byte[] aDocument, Source aXslt) - throws IOException, TransformerException { + throws IOException, TransformerException { Source source = new StreamSource(new ByteArrayInputStream(aDocument)); DOMResult result = new DOMResult(); transform(source, result, aXslt); + return (Document) result.getNode(); } @@ -138,14 +149,17 @@ public class XslTransformer { * Document to transform. * @param aXslt * XSL transformation. + * * @return Transformed document. + * */ public String textTransform(byte[] aDocument, Source aXslt) - throws IOException, TransformerException { + throws IOException, TransformerException { Source source = new StreamSource(new ByteArrayInputStream(aDocument)); ByteArrayOutputStream os = new ByteArrayOutputStream(); StreamResult result = new StreamResult(os); transform(source, result, aXslt); + return new String(os.toByteArray()); } @@ -158,19 +172,20 @@ public class XslTransformer { * Result of the transformation. * @param aXslt * XSLT to use. + * * @throws IOException * In case of problems reading resources. * @throws TransformerException * In case transformation fails. */ public void transform(Source aSource, Result aResult, Source aXslt) - throws IOException, TransformerException { + throws IOException, TransformerException { try { - Transformer transformer = _factory.newTransformer(aXslt); + Transformer transformer = factory.newTransformer(aXslt); transformer.transform(aSource, aResult); } catch (TransformerConfigurationException e) { throw new TransformerException( - "Configuration problem of XSLT transformation", e); + "Configuration problem of XSLT transformation", e); } } }