001: /*
002: * @(#)StyleTransformer.java
003: *
004: * Copyright (C) 2003-2004 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.codecoverage.v2.ant;
028:
029: import java.io.File;
030: import java.io.FileInputStream;
031: import java.io.FileOutputStream;
032: import java.io.IOException;
033: import java.text.DateFormat;
034: import java.util.Date;
035:
036: import javax.xml.parsers.DocumentBuilder;
037: import javax.xml.parsers.DocumentBuilderFactory;
038: import javax.xml.transform.Result;
039: import javax.xml.transform.Source;
040: import javax.xml.transform.Transformer;
041: import javax.xml.transform.TransformerFactory;
042: import javax.xml.transform.dom.DOMResult;
043: import javax.xml.transform.dom.DOMSource;
044: import javax.xml.transform.stream.StreamResult;
045: import javax.xml.transform.stream.StreamSource;
046:
047: import org.apache.tools.ant.BuildException;
048: import org.apache.tools.ant.Project;
049: import org.w3c.dom.Document;
050: import org.w3c.dom.Node;
051:
052: /**
053: * Transforms XML with XSL. Stolen from the JUnit optional task... kind of.
054: *
055: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
056: * @version $Date: 2004/04/15 05:48:25 $
057: * @since November 22, 2003
058: */
059: public class StyleTransformer {
060: private Project project;
061: private Transformer tformer;
062:
063: public StyleTransformer(Project proj, String stylesheetID,
064: File outDir) throws BuildException {
065: this .project = proj;
066:
067: try {
068: TransformerFactory tfactory = TransformerFactory
069: .newInstance();
070: Source xslSrc = new StreamSource(stylesheetID);
071: this .tformer = tfactory.newTransformer(xslSrc);
072: this .tformer.setParameter("output.dir", outDir
073: .getAbsolutePath());
074: } catch (javax.xml.transform.TransformerConfigurationException e) {
075: throw new BuildException("Error creating XSL transformer: "
076: + e.getMessage(), e);
077: }
078:
079: }
080:
081: public void setParameter(String name, Object val) {
082: this .tformer.setParameter(name, val);
083: }
084:
085: public Document transform(Document in) throws BuildException {
086: DOMResult result = new DOMResult();
087: DOMSource source = new DOMSource(in);
088: execute(source, result);
089: return getDocument(result.getNode());
090: }
091:
092: public void transform(Document in, File outFile)
093: throws BuildException, IOException {
094: checkFileTree(outFile);
095: Source source = new DOMSource(in);
096: FileOutputStream fos = null;
097: try {
098: fos = new FileOutputStream(outFile);
099: Result result = new StreamResult(fos);
100: execute(source, result);
101: } finally {
102: if (fos != null) {
103: fos.close();
104: }
105: }
106: }
107:
108: public Document transform(File in) throws BuildException,
109: IOException {
110: DOMResult result = new DOMResult();
111: FileInputStream fis = null;
112: try {
113: fis = new FileInputStream(in);
114: Source source = new StreamSource(in);
115: execute(source, result);
116: } finally {
117: if (fis != null) {
118: fis.close();
119: }
120: }
121: return getDocument(result.getNode());
122: }
123:
124: public void transform(File in, File outFile) throws BuildException,
125: IOException {
126: checkFileTree(outFile);
127: FileOutputStream fos = null;
128: FileInputStream fis = null;
129: try {
130: fos = new FileOutputStream(outFile);
131: fis = new FileInputStream(in);
132: Source source = new StreamSource(fis);
133: Result result = new StreamResult(fos);
134: execute(source, result);
135: } finally {
136: if (fos != null) {
137: fos.close();
138: }
139: if (fis != null) {
140: fis.close();
141: }
142: }
143: }
144:
145: protected final void execute(Source src, Result res)
146: throws BuildException {
147: final long t0 = System.currentTimeMillis();
148: tformer.setParameter("TSTAMP", DateFormat.getDateTimeInstance()
149: .format(new Date(t0)));
150: try {
151: tformer.transform(src, res);
152: } catch (RuntimeException ex) {
153: throw ex;
154: } catch (Exception e) {
155: throw new BuildException(
156: "Errors while applying transformations: "
157: + e.getMessage(), e);
158: } finally {
159: final long dt = System.currentTimeMillis() - t0;
160: this .project.log("Transform time: " + dt + "ms",
161: Project.MSG_DEBUG);
162: }
163: }
164:
165: private final void checkFileTree(File f) throws IOException {
166: File p = f.getParentFile();
167: p.mkdirs();
168: }
169:
170: private static Document getDocument(Node child) {
171: if (child instanceof Document) {
172: return (Document) child;
173: }
174: Document doc = child.getOwnerDocument();
175: if (doc == null) {
176: doc = getDocumentBuilder().newDocument();
177: Node c = doc.importNode(child, true);
178: doc.appendChild(c);
179: }
180: return doc;
181: }
182:
183: private static DocumentBuilder getDocumentBuilder() {
184: try {
185: return DocumentBuilderFactory.newInstance()
186: .newDocumentBuilder();
187: } catch (Exception exc) {
188: throw new ExceptionInInitializerError(exc);
189: }
190: }
191: }
|