1 16 19 package org.apache.xalan.lib; 20 21 import java.io.FileNotFoundException ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.util.Properties ; 25 import java.util.Vector ; 26 27 import javax.xml.transform.Templates ; 28 import javax.xml.transform.Transformer ; 29 import javax.xml.transform.TransformerConfigurationException ; 30 import javax.xml.transform.TransformerException ; 31 import javax.xml.transform.TransformerFactory ; 32 import javax.xml.transform.sax.SAXResult ; 33 import javax.xml.transform.sax.SAXTransformerFactory ; 34 import javax.xml.transform.sax.TransformerHandler ; 35 import javax.xml.transform.stream.StreamSource ; 36 37 import org.apache.xalan.extensions.XSLProcessorContext; 38 import org.apache.xalan.templates.AVT; 39 import org.apache.xalan.templates.ElemExtensionCall; 40 import org.apache.xalan.templates.ElemLiteralResult; 41 import org.apache.xalan.transformer.TransformerImpl; 42 import org.apache.xml.utils.SystemIDResolver; 43 import org.apache.xpath.XPathContext; 44 45 import org.w3c.dom.Element ; 46 import org.w3c.dom.Node ; 47 import org.w3c.dom.NodeList ; 48 49 import org.xml.sax.SAXException ; 50 import org.xml.sax.SAXNotRecognizedException ; 51 import org.xml.sax.XMLReader ; 52 import org.xml.sax.helpers.XMLReaderFactory ; 53 55 import org.apache.xml.serializer.Serializer; 57 import org.apache.xml.serializer.SerializerFactory; 58 59 71 public class PipeDocument 72 { 73 106 public void pipeDocument(XSLProcessorContext context, ElemExtensionCall elem) 107 throws TransformerException , TransformerConfigurationException , 108 SAXException , IOException , FileNotFoundException 109 { 110 try 111 { 112 SAXTransformerFactory saxTFactory = (SAXTransformerFactory ) TransformerFactory.newInstance(); 113 114 String source = elem.getAttribute("source", 116 context.getContextNode(), 117 context.getTransformer()); 118 TransformerImpl transImpl = context.getTransformer(); 119 120 String baseURLOfSource = transImpl.getBaseURLOfSource(); 122 String absSourceURL = SystemIDResolver.getAbsoluteURI(source, baseURLOfSource); 124 125 String target = elem.getAttribute("target", 127 context.getContextNode(), 128 context.getTransformer()); 129 130 XPathContext xctxt = context.getTransformer().getXPathContext(); 131 int xt = xctxt.getDTMHandleFromNode(context.getContextNode()); 132 133 String sysId = elem.getSystemId(); 135 136 NodeList ssNodes = null; 137 NodeList paramNodes = null; 138 Node ssNode = null; 139 Node paramNode = null; 140 if (elem.hasChildNodes()) 141 { 142 ssNodes = elem.getChildNodes(); 143 Vector vTHandler = new Vector (ssNodes.getLength()); 145 146 for (int i = 0; i < ssNodes.getLength(); i++) 151 { 152 ssNode = ssNodes.item(i); 153 if (ssNode.getNodeType() == Node.ELEMENT_NODE 154 && ((Element )ssNode).getTagName().equals("stylesheet") 155 && ssNode instanceof ElemLiteralResult) 156 { 157 AVT avt = ((ElemLiteralResult)ssNode).getLiteralResultAttribute("href"); 158 String href = avt.evaluate(xctxt,xt, elem); 159 String absURI = SystemIDResolver.getAbsoluteURI(href, sysId); 160 Templates tmpl = saxTFactory.newTemplates(new StreamSource (absURI)); 161 TransformerHandler tHandler = saxTFactory.newTransformerHandler(tmpl); 162 Transformer trans = tHandler.getTransformer(); 163 164 vTHandler.addElement(tHandler); 166 167 paramNodes = ssNode.getChildNodes(); 168 for (int j = 0; j < paramNodes.getLength(); j++) 169 { 170 paramNode = paramNodes.item(j); 171 if (paramNode.getNodeType() == Node.ELEMENT_NODE 172 && ((Element )paramNode).getTagName().equals("param") 173 && paramNode instanceof ElemLiteralResult) 174 { 175 avt = ((ElemLiteralResult)paramNode).getLiteralResultAttribute("name"); 176 String pName = avt.evaluate(xctxt,xt, elem); 177 avt = ((ElemLiteralResult)paramNode).getLiteralResultAttribute("value"); 178 String pValue = avt.evaluate(xctxt,xt, elem); 179 trans.setParameter(pName, pValue); 180 } 181 } 182 } 183 } 184 usePipe(vTHandler, absSourceURL, target); 185 } 186 } 187 catch (Exception e) 188 { 189 e.printStackTrace(); 190 } 191 } 192 200 public void usePipe(Vector vTHandler, String source, String target) 201 throws TransformerException , TransformerConfigurationException , 202 FileNotFoundException , IOException , SAXException , SAXNotRecognizedException 203 { 204 XMLReader reader = XMLReaderFactory.createXMLReader(); 205 TransformerHandler tHFirst = (TransformerHandler )vTHandler.firstElement(); 206 reader.setContentHandler(tHFirst); 207 reader.setProperty("http://xml.org/sax/properties/lexical-handler", tHFirst); 208 for (int i = 1; i < vTHandler.size(); i++) 209 { 210 TransformerHandler tHFrom = (TransformerHandler )vTHandler.elementAt(i-1); 211 TransformerHandler tHTo = (TransformerHandler )vTHandler.elementAt(i); 212 tHFrom.setResult(new SAXResult (tHTo)); 213 } 214 TransformerHandler tHLast = (TransformerHandler )vTHandler.lastElement(); 215 Transformer trans = tHLast.getTransformer(); 216 Properties outputProps = trans.getOutputProperties(); 217 Serializer serializer = SerializerFactory.getSerializer(outputProps); 218 219 FileOutputStream out = new FileOutputStream (target); 220 try 221 { 222 serializer.setOutputStream(out); 223 tHLast.setResult(new SAXResult (serializer.asContentHandler())); 224 reader.parse(source); 225 } 226 finally 227 { 228 if (out != null) 231 out.close(); 232 } 233 } 234 } 235 | Popular Tags |