1 21 22 package nu.xom.xslt; 23 24 import javax.xml.transform.OutputKeys ; 25 import javax.xml.transform.Source ; 26 import javax.xml.transform.Templates ; 27 import javax.xml.transform.Transformer ; 28 import javax.xml.transform.TransformerConfigurationException ; 29 import javax.xml.transform.TransformerException ; 30 import javax.xml.transform.TransformerFactoryConfigurationError ; 31 import javax.xml.transform.TransformerFactory ; 32 33 import org.xml.sax.SAXParseException ; 34 35 import nu.xom.Document; 36 import nu.xom.Element; 37 import nu.xom.NodeFactory; 38 import nu.xom.Nodes; 39 import nu.xom.XMLException; 40 41 116 public final class XSLTransform { 117 118 119 126 private Templates templates; 127 private NodeFactory factory; 128 129 130 134 135 147 private XSLTransform(Source source) throws XSLException { 148 149 try { 150 TransformerFactory factory 151 = TransformerFactory.newInstance(); 152 this.templates = factory.newTemplates(source); 153 } 154 catch (TransformerFactoryConfigurationError error) { 155 throw new XSLException( 156 "Could not locate a TrAX TransformerFactory", error 157 ); 158 } 159 catch (TransformerConfigurationException ex) { 160 throw new XSLException( 161 "Syntax error in stylesheet", ex 162 ); 163 } 164 165 } 166 167 168 179 public XSLTransform(Document stylesheet) throws XSLException { 180 this(stylesheet, new NodeFactory()); 181 } 182 183 184 202 public XSLTransform(Document stylesheet, NodeFactory factory) 203 throws XSLException { 204 205 this(new XOMSource(stylesheet)); 206 if (factory == null) this.factory = new NodeFactory(); 207 else this.factory = factory; 208 209 } 210 211 212 228 public Nodes transform(Document in) throws XSLException { 229 return transform(new XOMSource(in)); 230 } 231 232 233 249 public Nodes transform(Nodes in) throws XSLException { 250 251 if (in.size() == 0) return new Nodes(); 252 XOMSource source = new XOMSource(in); 253 return transform(source); 254 255 } 256 257 258 273 private Nodes transform(Source in) throws XSLException { 274 275 try { 276 XOMResult out = new XOMResult(factory); 277 Transformer transformer = templates.newTransformer(); 278 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); 280 transformer.transform(in, out); 281 return out.getResult(); 282 } 283 catch (Exception ex) { 284 Throwable cause = ex; 286 if (cause instanceof TransformerException ) { 287 TransformerException tex = (TransformerException ) cause; 288 Throwable nested = tex.getException(); 289 if (nested != null) { 290 cause = nested; 291 if (cause instanceof SAXParseException ) { 292 nested = ((SAXParseException ) cause).getException(); 293 if (nested != null) cause = nested; 294 } 295 } 296 } 297 throw new XSLException(ex.getMessage(), cause); 298 } 299 300 } 301 302 303 320 public static Document toDocument(Nodes nodes) { 321 322 Element root = null; 323 int rootPosition = 0; 324 for (int i = 0; i < nodes.size(); i++) { 325 if (nodes.get(i) instanceof Element) { 326 rootPosition = i; 327 root = (Element) nodes.get(i); 328 break; 329 } 330 } 331 332 if (root == null) { 333 throw new XMLException("No root element"); 334 } 335 336 Document result = new Document(root); 337 338 for (int i = 0; i < rootPosition; i++) { 339 result.insertChild(nodes.get(i), i); 340 } 341 342 for (int i = rootPosition+1; i < nodes.size(); i++) { 343 result.appendChild(nodes.get(i)); 344 } 345 346 return result; 347 348 } 349 350 351 359 public String toString() { 360 return "[" + getClass().getName() + ": " + templates + "]"; 361 } 362 363 364 } | Popular Tags |