1 5 6 package org.exoplatform.services.xml.transform.impl.trax; 7 8 import java.io.IOException ; 9 import javax.xml.transform.Source ; 10 import javax.xml.transform.Templates ; 11 import javax.xml.transform.TransformerConfigurationException ; 12 import javax.xml.transform.TransformerException ; 13 import javax.xml.transform.sax.SAXSource ; 14 import javax.xml.transform.sax.SAXTransformerFactory ; 15 import javax.xml.transform.sax.TemplatesHandler ; 16 import org.xml.sax.InputSource ; 17 import org.xml.sax.XMLReader ; 18 import org.xml.sax.SAXException ; 19 20 import org.exoplatform.services.xml.transform.impl.trax.TRAXTransformerImpl; 21 import org.exoplatform.services.xml.transform.NotSupportedIOTypeException; 22 import org.exoplatform.services.xml.transform.trax.TRAXTemplates; 23 import org.exoplatform.services.xml.transform.trax.TRAXTransformerService; 24 import org.exoplatform.services.xml.transform.trax.TRAXTransformer; 25 import org.exoplatform.services.xml.resolving.XMLResolvingService; 26 27 34 35 public class TRAXTransformerServiceImpl implements TRAXTransformerService{ 36 37 private XMLResolvingService resolvingService; 38 39 public TRAXTransformerServiceImpl(XMLResolvingService resolvingService){ 40 this.resolvingService = resolvingService; 41 } 42 43 public TRAXTransformer getTransformer() throws 44 TransformerConfigurationException { 45 TRAXTransformerImpl transf = new TRAXTransformerImpl(); 46 transf.resolvingService = resolvingService; 47 return transf; 48 } 49 50 public TRAXTransformer getTransformer(Source source) throws 51 TransformerConfigurationException { 52 TRAXTransformerImpl transf = new TRAXTransformerImpl(source); 53 transf.resolvingService = resolvingService; 54 return transf; 55 } 56 57 public TRAXTemplates getTemplates(Source source) throws 58 TransformerException ,NotSupportedIOTypeException { 59 TRAXTemplatesImpl templates = 60 new TRAXTemplatesImpl(getXSLTemplates(source)); 61 templates.resolvingService = resolvingService; 62 return templates; 63 } 64 65 private Templates getXSLTemplates(Source source) throws 66 TransformerException ,NotSupportedIOTypeException { 67 SAXTransformerFactory saxTFactory = 68 (SAXTransformerFactory ) SAXTransformerFactory. 69 newInstance(); 70 71 TemplatesHandler templateHandler = saxTFactory. 72 newTemplatesHandler(); 73 XMLReader xmlReader; 74 try { 75 xmlReader = TRAXTransformerImpl.getXMLReader(); 78 if (resolvingService != null) { 80 xmlReader.setEntityResolver( 81 resolvingService.getEntityResolver()); 82 } 83 84 } catch (SAXException ex) { 85 throw new TransformerException (ex); 86 } 87 88 xmlReader.setContentHandler(templateHandler); 89 InputSource inputSource = SAXSource.sourceToInputSource(source); 90 if (inputSource == null) { 91 throw new NotSupportedIOTypeException(source); 92 } 93 94 try { 95 xmlReader.parse(inputSource); 96 } catch (SAXException ex) { 97 throw new TransformerException (ex); 98 } catch (IOException ex) { 99 throw new TransformerException (ex); 100 } 101 102 return templateHandler.getTemplates(); 103 104 } 105 106 107 } 108 | Popular Tags |