1 5 6 package org.exoplatform.services.xml.transform.impl.trax; 7 8 import java.io.IOException ; 9 import java.util.Properties ; 10 import javax.xml.transform.ErrorListener ; 11 import javax.xml.transform.URIResolver ; 12 import javax.xml.transform.Source ; 13 import javax.xml.transform.Result ; 14 import javax.xml.transform.Transformer ; 15 import javax.xml.transform.Templates ; 16 import javax.xml.transform.TransformerException ; 17 import javax.xml.transform.TransformerConfigurationException ; 18 import javax.xml.transform.sax.SAXTransformerFactory ; 19 import javax.xml.transform.sax.TransformerHandler ; 20 import javax.xml.transform.sax.SAXSource ; 21 import javax.xml.transform.sax.SAXResult ; 22 import javax.xml.transform.stream.StreamSource ; 23 import org.xml.sax.helpers.XMLReaderFactory ; 24 import org.xml.sax.InputSource ; 25 import org.xml.sax.XMLReader ; 26 import org.xml.sax.SAXException ; 27 28 import org.exoplatform.services.xml.transform.trax.TRAXTransformer; 29 import org.exoplatform.services.xml.transform.impl.TransformerBase; 30 import org.exoplatform.services.xml.transform.NotSupportedIOTypeException; 31 32 33 35 43 44 public class TRAXTransformerImpl extends TransformerBase 45 implements TRAXTransformer { 46 47 protected TransformerHandler tHandler; 49 50 protected Transformer getTransformer() { 51 return tHandler.getTransformer(); 52 } 53 54 55 public TRAXTransformerImpl() throws TransformerConfigurationException { 56 SAXTransformerFactory saxTFactory = (SAXTransformerFactory ) 57 SAXTransformerFactory.newInstance(); 58 tHandler = saxTFactory.newTransformerHandler(); 59 } 60 61 public TRAXTransformerImpl(Source source) throws 62 TransformerConfigurationException { 63 SAXTransformerFactory saxTFactory = (SAXTransformerFactory ) 64 SAXTransformerFactory.newInstance(); 65 tHandler = saxTFactory.newTransformerHandler(source); 66 } 67 68 public TRAXTransformerImpl(Templates templates) throws 69 TransformerConfigurationException { 70 SAXTransformerFactory saxTFactory = (SAXTransformerFactory ) 71 SAXTransformerFactory.newInstance(); 72 tHandler = saxTFactory.newTransformerHandler(templates); 73 } 74 75 protected void internalTransform(Source src) throws TransformerException , 76 NotSupportedIOTypeException, IllegalStateException { 77 78 XMLReader xmlReader = null; 79 80 try { 81 xmlReader = getXMLReader(); 84 if (resolvingService != null) { 86 xmlReader.setEntityResolver( 87 resolvingService.getEntityResolver()); 88 log.debug("Set entity resolver"); 89 } 90 } catch (SAXException ex) { 91 throw new TransformerException (ex); 92 } 93 94 95 xmlReader.setContentHandler(tHandler); 96 98 InputSource inputSource = SAXSource.sourceToInputSource(src); 100 112 try { 113 xmlReader.parse(inputSource); 114 } catch (SAXException ex) { 115 throw new TransformerException (ex); 116 } catch (IOException ex) { 117 throw new TransformerException (ex); 118 } 119 120 } 121 122 protected void afterInitResult() { 123 tHandler.setResult(getResult()); 124 } 125 126 127 public Result getTransformerAsResult() { 129 return new SAXResult (tHandler); 130 } 131 132 public Object getParameter(String param) { 133 return getTransformer().getParameter(param); 134 } 135 136 public void setParameter(String name, Object value) { 137 getTransformer().setParameter(name, value); 138 } 139 140 public void clearParameters() { 141 getTransformer().clearParameters(); 142 } 143 144 public String getOutputProperty(String prop) { 145 return getTransformer().getOutputProperty(prop); 146 } 147 148 public void setOutputProperty(String name, String value) { 149 getTransformer().setOutputProperty(name, value); 150 } 151 152 public void setOutputProperties(Properties props) { 153 getTransformer().setOutputProperties(props); 154 } 155 156 public Properties getOutputProperties() { 157 return getTransformer().getOutputProperties(); 158 } 159 160 public URIResolver getURIResolver() { 161 return getTransformer().getURIResolver(); 162 } 163 164 public void setURIResolver(URIResolver resolver) { 165 getTransformer().setURIResolver(resolver); 166 } 167 168 public ErrorListener getErrorListener() { 169 return getTransformer().getErrorListener(); 170 } 171 172 public void setErrorListener(ErrorListener listener) { 173 getTransformer().setErrorListener(listener); 174 } 175 176 177 } 178 | Popular Tags |