1 26 27 package net.sourceforge.groboutils.codecoverage.v2.ant; 28 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.io.FileOutputStream ; 32 import java.io.IOException ; 33 import java.text.DateFormat ; 34 import java.util.Date ; 35 36 import javax.xml.parsers.DocumentBuilder ; 37 import javax.xml.parsers.DocumentBuilderFactory ; 38 import javax.xml.transform.Result ; 39 import javax.xml.transform.Source ; 40 import javax.xml.transform.Transformer ; 41 import javax.xml.transform.TransformerFactory ; 42 import javax.xml.transform.dom.DOMResult ; 43 import javax.xml.transform.dom.DOMSource ; 44 import javax.xml.transform.stream.StreamResult ; 45 import javax.xml.transform.stream.StreamSource ; 46 47 import org.apache.tools.ant.BuildException; 48 import org.apache.tools.ant.Project; 49 import org.w3c.dom.Document ; 50 import org.w3c.dom.Node ; 51 52 53 60 public class StyleTransformer 61 { 62 private Project project; 63 private Transformer tformer; 64 65 public StyleTransformer( Project proj, String stylesheetID, File outDir ) 66 throws BuildException 67 { 68 this.project = proj; 69 70 try 71 { 72 TransformerFactory tfactory = TransformerFactory.newInstance(); 73 Source xslSrc = new StreamSource ( stylesheetID ); 74 this.tformer = tfactory.newTransformer( xslSrc ); 75 this.tformer.setParameter("output.dir", outDir.getAbsolutePath()); 76 } 77 catch (javax.xml.transform.TransformerConfigurationException e) 78 { 79 throw new BuildException( "Error creating XSL transformer: "+ 80 e.getMessage(), e ); 81 } 82 83 } 84 85 86 public void setParameter( String name, Object val ) 87 { 88 this.tformer.setParameter( name, val ); 89 } 90 91 92 93 public Document transform( Document in ) 94 throws BuildException 95 { 96 DOMResult result = new DOMResult (); 97 DOMSource source = new DOMSource ( in ); 98 execute( source, result ); 99 return getDocument( result.getNode() ); 100 } 101 102 103 public void transform( Document in, File outFile ) 104 throws BuildException, IOException 105 { 106 checkFileTree( outFile ); 107 Source source = new DOMSource ( in ); 108 FileOutputStream fos = null; 109 try 110 { 111 fos = new FileOutputStream ( outFile ); 112 Result result = new StreamResult ( fos ); 113 execute( source, result ); 114 } 115 finally 116 { 117 if (fos != null) 118 { 119 fos.close(); 120 } 121 } 122 } 123 124 125 public Document transform( File in ) 126 throws BuildException, IOException 127 { 128 DOMResult result = new DOMResult (); 129 FileInputStream fis = null; 130 try 131 { 132 fis = new FileInputStream ( in ); 133 Source source = new StreamSource ( in ); 134 execute( source, result ); 135 } 136 finally 137 { 138 if (fis != null) 139 { 140 fis.close(); 141 } 142 } 143 return getDocument( result.getNode() ); 144 } 145 146 147 public void transform( File in, File outFile ) 148 throws BuildException, IOException 149 { 150 checkFileTree( outFile ); 151 FileOutputStream fos = null; 152 FileInputStream fis = null; 153 try 154 { 155 fos = new FileOutputStream ( outFile ); 156 fis = new FileInputStream ( in ); 157 Source source = new StreamSource ( fis ); 158 Result result = new StreamResult ( fos ); 159 execute( source, result ); 160 } 161 finally 162 { 163 if (fos != null) 164 { 165 fos.close(); 166 } 167 if (fis != null) 168 { 169 fis.close(); 170 } 171 } 172 } 173 174 175 protected final void execute( Source src, Result res ) 176 throws BuildException 177 { 178 final long t0 = System.currentTimeMillis(); 179 tformer.setParameter( "TSTAMP", DateFormat.getDateTimeInstance(). 180 format( new Date ( t0 ) ) ); 181 try 182 { 183 tformer.transform( src, res ); 184 } 185 catch (RuntimeException ex) 186 { 187 throw ex; 188 } 189 catch (Exception e) 190 { 191 throw new BuildException( "Errors while applying transformations: " 192 + e.getMessage(), e ); 193 } 194 finally 195 { 196 final long dt = System.currentTimeMillis() - t0; 197 this.project.log( "Transform time: " + dt + "ms", 198 Project.MSG_DEBUG ); 199 } 200 } 201 202 203 private final void checkFileTree( File f ) 204 throws IOException 205 { 206 File p = f.getParentFile(); 207 p.mkdirs(); 208 } 209 210 211 private static Document getDocument( Node child ) 212 { 213 if (child instanceof Document ) 214 { 215 return (Document )child; 216 } 217 Document doc = child.getOwnerDocument(); 218 if (doc == null) 219 { 220 doc = getDocumentBuilder().newDocument(); 221 Node c = doc.importNode( child, true ); 222 doc.appendChild( c ); 223 } 224 return doc; 225 } 226 227 228 private static DocumentBuilder getDocumentBuilder() 229 { 230 try 231 { 232 return DocumentBuilderFactory.newInstance().newDocumentBuilder(); 233 } 234 catch (Exception exc) 235 { 236 throw new ExceptionInInitializerError (exc); 237 } 238 } 239 } 240 241 | Popular Tags |