1 28 29 package com.caucho.xsl; 30 31 import com.caucho.xml.QElement; 32 import com.caucho.xml.XMLWriter; 33 34 import org.w3c.dom.Node ; 35 import org.xml.sax.SAXException ; 36 37 import javax.xml.transform.TransformerException ; 38 import java.io.IOException ; 39 40 47 public class IdentityStylesheet extends StylesheetImpl { 48 58 public void transform(Node xml, 59 XMLWriter writer, 60 TransformerImpl transformer) 61 throws SAXException , IOException , TransformerException 62 { 63 XslWriter out = new XslWriter(null, this, transformer); 64 out.init(writer); 65 66 applyNode(out, xml); 67 68 out.close(); 69 } 70 71 protected void applyNode(XslWriter out, Node node) 72 throws SAXException , IOException , TransformerException 73 { 74 if (node == null) 75 return; 76 77 switch (node.getNodeType()) { 78 case Node.DOCUMENT_NODE: 79 case Node.DOCUMENT_FRAGMENT_NODE: 80 for (Node child = node.getFirstChild(); 81 child != null; 82 child = child.getNextSibling()) { 83 applyNode(out, child); 84 } 85 break; 86 87 case Node.ELEMENT_NODE: 88 out.pushCopy(node); 89 for (Node child = ((QElement) node).getFirstAttribute(); 90 child != null; 91 child = child.getNextSibling()) { 92 applyNode(out, child); 93 } 94 for (Node child = node.getFirstChild(); 95 child != null; 96 child = child.getNextSibling()) { 97 applyNode(out, child); 98 } 99 out.popCopy(node); 100 break; 101 102 case Node.TEXT_NODE: 103 case Node.CDATA_SECTION_NODE: 104 String value = node.getNodeValue(); 105 out.print(value); 106 return; 107 108 case Node.ATTRIBUTE_NODE: 109 case Node.ENTITY_REFERENCE_NODE: 110 out.pushCopy(node); 111 out.popCopy(node); 112 break; 113 } 114 } 115 116 public OutputFormat getOutputFormat() 117 { 118 return new OutputFormat(); 119 } 120 } 121 122 | Popular Tags |