1 package com.icl.saxon.output; 2 import com.icl.saxon.*; 3 import com.icl.saxon.om.Namespace; 4 6 import org.xml.sax.Attributes ; 7 import org.w3c.dom.*; 8 9 import javax.xml.transform.Result ; 10 import javax.xml.transform.TransformerException ; 11 import javax.xml.transform.dom.DOMResult ; 12 13 14 17 18 public class DOMEmitter extends Emitter 19 { 20 protected Node currentNode; 21 protected Document document; 22 private boolean canNormalize = true; 23 24 27 28 public void startDocument () 29 { 30 31 } 32 33 36 37 public void endDocument () 38 { 39 40 } 41 42 43 46 47 public void startElement (int nameCode, Attributes attributes, 48 int[] namespaces, int nscount) throws TransformerException 49 { 50 String name = namePool.getDisplayName(nameCode); 51 try { 52 53 Element element = document.createElement(name); 54 currentNode.appendChild(element); 55 currentNode = element; 56 57 59 for (int n=0; n<nscount; n++) { 60 String prefix = namePool.getPrefixFromNamespaceCode(namespaces[n]); 61 String uri = namePool.getURIFromNamespaceCode(namespaces[n]); 62 if (!(uri.equals(Namespace.XML))) { 63 if (prefix.equals("")) { 64 element.setAttribute("xmlns", uri); 65 } else { 66 element.setAttribute("xmlns:" + prefix, uri); 67 } 68 } 69 } 70 71 73 for (int i=0; i<attributes.getLength(); i++) { 74 element.setAttribute( 75 attributes.getQName(i), 76 attributes.getValue(i)); 77 } 78 79 } catch (DOMException err) { 80 throw new TransformerException (err); 81 } 82 } 83 84 87 88 public void endElement (int nameCode) throws TransformerException 89 { 90 91 if (canNormalize) { 92 try { 93 currentNode.normalize(); 94 } catch (Throwable err) { 95 canNormalize = false; 96 } } 98 99 currentNode = currentNode.getParentNode(); 100 101 } 102 103 104 107 108 public void characters (char[] ch, int start, int length) throws TransformerException 109 { 110 try { 111 Text text = document.createTextNode(new String (ch, start, length)); 112 currentNode.appendChild(text); 113 } catch (DOMException err) { 114 throw new TransformerException (err); 115 } 116 } 117 118 119 122 123 public void processingInstruction (String target, String data) 124 throws TransformerException 125 { 126 try { 127 ProcessingInstruction pi = 128 document.createProcessingInstruction(target, data); 129 currentNode.appendChild(pi); 130 } catch (DOMException err) { 131 throw new TransformerException (err); 132 } 133 } 134 135 138 139 public void comment (char ch[], int start, int length) throws TransformerException 140 { 141 try { 142 Comment comment = document.createComment(new String (ch, start, length)); 143 currentNode.appendChild(comment); 144 } catch (DOMException err) { 145 throw new TransformerException (err); 146 } 147 } 148 149 152 153 public void setNode (Node node) { 154 currentNode = node; 155 if (node instanceof Document) { document = (Document)node; 157 } else { 158 document = currentNode.getOwnerDocument(); 159 } 160 } 161 162 } 163 164 | Popular Tags |