1 28 29 package com.caucho.xsl; 30 31 import com.caucho.java.LineMap; 32 import com.caucho.vfs.IOExceptionWrapper; 33 import com.caucho.vfs.WriteStream; 34 import com.caucho.xml.CauchoNode; 35 import com.caucho.xml.XmlPrinter; 36 37 import org.w3c.dom.Node ; 38 import org.xml.sax.SAXException ; 39 40 import javax.xml.transform.OutputKeys ; 41 import java.io.IOException ; 42 import java.io.InputStream ; 43 import java.util.Properties ; 44 45 48 public class StringTransformerImpl extends TransformerImpl { 49 protected LineMap _lineMap; 50 51 StringTransformerImpl(StylesheetImpl stylesheet) 52 { 53 super(stylesheet); 54 } 55 56 public Object getProperty(String name) 57 { 58 if (name.equals(LINE_MAP)) 59 return _lineMap; 60 else 61 return super.getProperty(name); 62 } 63 64 public String transform(InputStream source) 65 throws SAXException , IOException 66 { 67 return transform(parseDocument(source, null)); 68 } 69 70 public String transform(String systemId) 71 throws SAXException , IOException 72 { 73 return transform(parseDocument(systemId)); 74 } 75 76 public String transformString(String source) 77 throws SAXException , IOException 78 { 79 return transform(parseStringDocument(source, null)); 80 } 81 82 89 public String transform(Node node) 90 throws SAXException , IOException 91 { 92 _lineMap = null; 93 Properties output = _stylesheet.getOutputProperties(); 94 95 com.caucho.vfs.StringWriter sw = new com.caucho.vfs.StringWriter(); 96 WriteStream ws = sw.openWrite(); 97 98 XmlPrinter out = new XmlPrinter(ws); 99 100 out.setMethod((String ) output.get(OutputKeys.METHOD)); 101 out.setEncoding("UTF-8"); 102 out.setMimeType((String ) output.get(OutputKeys.MEDIA_TYPE)); 103 String omit = (String ) output.get(OutputKeys.OMIT_XML_DECLARATION); 104 105 if (omit == null || omit.equals("false") || omit.equals("no")) 106 out.setPrintDeclaration(true); 107 out.setStandalone((String ) output.get(OutputKeys.STANDALONE)); 108 out.setSystemId((String ) output.get(OutputKeys.DOCTYPE_SYSTEM)); 109 out.setPublicId((String ) output.get(OutputKeys.DOCTYPE_PUBLIC)); 110 111 String indent = (String ) output.get(OutputKeys.INDENT); 112 if (indent != null) 113 out.setPretty(indent.equals("true")); 114 out.setVersion((String ) output.get(OutputKeys.VERSION)); 115 if (node instanceof CauchoNode) { 116 String filename = ((CauchoNode) node).getFilename(); 117 out.setLineMap(filename != null ? filename : "anonymous.xsl"); 118 } 119 else 120 out.setLineMap("anonymous.xsl"); 121 122 String includeContentType = (String ) output.get("include-content-type"); 123 if (includeContentType != null) 124 out.setIncludeContentType(includeContentType.equals("true") || 125 includeContentType.equals("yes")); 126 127 try { 128 out.startDocument(); 129 _stylesheet.transform(node, out, this); 130 out.endDocument(); 131 _lineMap = out.getLineMap(); 132 ws.close(); 133 134 return sw.getString(); 135 } catch (Exception e) { 136 throw new IOExceptionWrapper(e); 137 } 138 } 139 } 140 | Popular Tags |