1 23 24 package org.mdarad.framework.util; 25 26 import org.mdarad.framework.exception.SystemException; 27 import org.xml.sax.ContentHandler ; 28 import org.xml.sax.InputSource ; 29 import org.xml.sax.SAXException ; 30 import org.xml.sax.XMLReader ; 31 import org.xml.sax.helpers.XMLReaderFactory ; 32 33 import javax.xml.transform.Result ; 34 import javax.xml.transform.Templates ; 35 import javax.xml.transform.TransformerConfigurationException ; 36 import javax.xml.transform.TransformerFactory ; 37 import javax.xml.transform.URIResolver ; 38 import javax.xml.transform.sax.SAXResult ; 39 import javax.xml.transform.sax.SAXSource ; 40 import javax.xml.transform.sax.SAXTransformerFactory ; 41 import javax.xml.transform.sax.TemplatesHandler ; 42 import javax.xml.transform.sax.TransformerHandler ; 43 import java.io.IOException ; 44 45 52 public class SAXHelper { 53 54 public static Templates getTransformationTemplates(InputSource xslSource, URIResolver resolver) throws SystemException, IOException { 55 try { 56 TransformerFactory tFactory = TransformerFactory.newInstance(); 58 SAXTransformerFactory saxTFactory = null; 59 if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)) { 60 saxTFactory = ((SAXTransformerFactory ) tFactory); 61 } 62 if (resolver != null) saxTFactory.setURIResolver(resolver); 64 TemplatesHandler templatesHandler = saxTFactory.newTemplatesHandler(); 66 XMLReader reader = XMLReaderFactory.createXMLReader(); 68 reader.setContentHandler(templatesHandler); 69 70 reader.parse(xslSource); 72 73 Templates templates = templatesHandler.getTemplates(); 75 return templates; 76 } catch (TransformerConfigurationException e) { 77 throw new SystemException(e); 78 } catch (SAXException e) { 79 throw new SystemException(e); 80 } 81 } 82 83 public static TransformerHandler getTransformationContentHandler(Templates templates, Result output) throws SystemException { 84 return getTransformationContentHandler(templates, output, null); 85 } 86 87 public static TransformerHandler getTransformationContentHandler(Templates templates, Result output, URIResolver resolver) throws SystemException { 88 TransformerHandler transformerHandler = null; 89 try { 90 TransformerFactory tFactory = TransformerFactory.newInstance(); 92 SAXTransformerFactory saxTFactory = null; 93 if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)) { 94 saxTFactory = ((SAXTransformerFactory ) tFactory); 95 } 96 transformerHandler = saxTFactory.newTransformerHandler(templates); 98 if (resolver != null) transformerHandler.getTransformer().setURIResolver(resolver); 99 100 transformerHandler.setResult(output); 102 } catch (TransformerConfigurationException e) { 103 throw new SystemException("Could not configure the transformer", e); 104 } 105 return transformerHandler; 106 } 107 108 public static void convertXmlWithXsl(InputSource xmlInputSource, InputSource xslSource, Result output) throws SystemException, IOException , SAXException { 109 110 Templates templates = null; 112 templates = getTransformationTemplates(xslSource, null); 113 114 ContentHandler outputHandler = getTransformationContentHandler(templates, output, null); 116 117 XMLReader reader = XMLReaderFactory.createXMLReader(); 119 reader.setContentHandler(outputHandler); 120 reader.parse(xmlInputSource); 121 } 122 123 } 124 | Popular Tags |