X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fsrc%2Forg%2Fwamblee%2Fxml%2FXSLT.java;h=7ffe63b3ff132d0a8cbaf49dca6610f837acd794;hb=17c55ed4fa1f62885fe813ecaa99d053e1111f48;hp=14789e8649a3f3cf232febed299af1a6c63e9d33;hpb=643d979c351150ace01a8f9682f6c9f223854cd6;p=utils diff --git a/support/src/org/wamblee/xml/XSLT.java b/support/src/org/wamblee/xml/XSLT.java index 14789e86..7ffe63b3 100644 --- a/support/src/org/wamblee/xml/XSLT.java +++ b/support/src/org/wamblee/xml/XSLT.java @@ -17,7 +17,9 @@ package org.wamblee.xml; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.IOException; import javax.xml.transform.Result; import javax.xml.transform.Source; @@ -25,17 +27,62 @@ 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.dom.DOMResult; 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; /** * XSLT utilities. */ public class XSLT { + private TransformerFactory _factory; + + /** + * Constructs the URL resolver. + * + * @param aResolver + * URI resolver to use. + */ + public XSLT(URIResolver aResolver) { + _factory = TransformerFactory.newInstance(); + _factory.setURIResolver(aResolver); + } + + /** + * Constructs the XSLT processor. + * + */ + public XSLT() { + _factory = TransformerFactory.newInstance(); + } + + /** + * 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. + */ + public Source resolve(String aXslt) throws TransformerException { + URIResolver resolver = _factory.getURIResolver(); + if (resolver == null) { + if (new File(aXslt).canRead()) { + try { + return new StreamSource(new FileResource(new File(aXslt)) + .getInputStream()); + } catch (IOException e) { + throw new TransformerException(e.getMessage(), e); + } + } + } + return resolver.resolve(aXslt, ""); + } + /** * Transforms a DOM document into another DOM document using a given XSLT * transformation. @@ -45,43 +92,81 @@ public class XSLT { * @param aXslt * XSLT to use. * @return Transformed document. + * @throws IOException + * In case of problems reading resources. + * @throws TransformerException + * In case transformation fails. */ - public static Document transform( Document aDocument, File aXslt ) { - Source source = new DOMSource( aDocument ); - DOMResult result = new DOMResult( ); - transform( source, result, aXslt ); - return (Document) result.getNode( ); - } - - public static Document transform(byte[] aDocument, File aXslt ) { - Source source = new StreamSource( new ByteArrayInputStream(aDocument) ); - DOMResult result = new DOMResult( ); - transform( source, result, aXslt ); - return (Document) result.getNode( ); + public Document transform(Document aDocument, Source aXslt) + throws IOException, TransformerException { + Source source = new DOMSource(aDocument); + DOMResult result = new DOMResult(); + transform(source, result, aXslt); + return (Document) result.getNode(); } /** - * Transforms a DOM document into another DOM document using a given XSLT - * transformation. + * Transforms a document using XSLT. * * @param aDocument * 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 { + Source source = new StreamSource(new ByteArrayInputStream(aDocument)); + DOMResult result = new DOMResult(); + transform(source, result, aXslt); + return (Document) result.getNode(); + } + + /** + * Transforms a document to a text output. This supports XSLT + * transformations that result in text documents. + * + * @param aDocument + * Document to transform. + * @param aXslt + * XSL transformation. + * @return Transformed document. + */ + public String textTransform(byte[] aDocument, Source aXslt) + 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()); + } + + /** + * Transforms a document using XSLT. + * + * @param aSource + * Document to transform. + * @param aResult + * Result of the transformation. + * @param aXslt + * XSLT to use. + * @throws IOException + * In case of problems reading resources. + * @throws TransformerException + * In case transformation fails. */ - public static void transform( Source aSource, Result aResult, - File aXslt ) { + public void transform(Source aSource, Result aResult, Source aXslt) + throws IOException, TransformerException { try { - Source xslt = new StreamSource( aXslt ); - Transformer transformer = TransformerFactory.newInstance( ) - .newTransformer( xslt ); - transformer.transform( aSource, aResult ); - } catch ( TransformerConfigurationException e ) { + Transformer transformer = _factory.newTransformer(aXslt); + transformer.transform(aSource, aResult); + } catch (TransformerConfigurationException e) { throw new RuntimeException( - "Configuration problem of XSLT transformation", e ); - } catch ( TransformerException e ) { - throw new RuntimeException( "Error transforming document", e ); + "Configuration problem of XSLT transformation", e); } } }