1 package net.sf.saxon.dom; 2 import net.sf.saxon.event.Emitter; 3 import net.sf.saxon.om.NamespaceConstant; 4 import net.sf.saxon.trans.DynamicError; 5 import net.sf.saxon.trans.XPathException; 6 import org.w3c.dom.*; 7 8 9 12 13 public class DOMEmitter extends Emitter 14 { 15 protected Node currentNode; 16 protected Document document; 17 private boolean canNormalize = true; 18 19 22 23 public void open () {} 24 25 28 29 public void close () {} 30 31 34 35 public void startDocument(int properties) throws XPathException {} 36 37 40 41 public void endDocument() throws XPathException {} 42 43 46 47 public void startElement (int nameCode, int typeCode, int locationId, int properties) throws XPathException { 48 String qname = namePool.getDisplayName(nameCode); 49 String uri = namePool.getURI(nameCode); 50 try { 51 Element element = document.createElementNS(uri, qname); 52 currentNode.appendChild(element); 53 currentNode = element; 54 } catch (DOMException err) { 55 throw new DynamicError(err); 56 } 57 } 58 59 public void namespace (int namespaceCode, int properties) throws XPathException { 60 try { 61 String prefix = namePool.getPrefixFromNamespaceCode(namespaceCode); 62 String uri = namePool.getURIFromNamespaceCode(namespaceCode); 63 Element element = (Element)currentNode; 64 if (!(uri.equals(NamespaceConstant.XML))) { 65 if (prefix.equals("")) { 66 element.setAttributeNS("http://www.w3.org/2000/xmlns/", 67 "xmlns", uri); 68 } else { 69 element.setAttributeNS("http://www.w3.org/2000/xmlns/", 70 "xmlns:" + prefix, uri); 71 72 } 73 } 74 } catch (DOMException err) { 75 throw new DynamicError(err); 76 } 77 } 78 79 public void attribute (int nameCode, int typeCode, CharSequence value, int locationId, int properties) 80 throws XPathException { 81 String qname = namePool.getDisplayName(nameCode); 82 String uri = namePool.getURI(nameCode); 83 try { 84 Element element = (Element)currentNode; 85 element.setAttributeNS(uri, qname, value.toString()); 86 } catch (DOMException err) { 87 throw new DynamicError(err); 88 } 89 } 90 91 public void startContent() throws XPathException {} 92 93 96 97 public void endElement () throws XPathException { 98 if (canNormalize) { 99 try { 100 currentNode.normalize(); 101 } catch (Throwable err) { 102 canNormalize = false; 103 } } 105 106 currentNode = currentNode.getParentNode(); 107 108 } 109 110 111 114 115 public void characters (CharSequence chars, int locationId, int properties) throws XPathException 116 { 117 try { 118 Text text = document.createTextNode(chars.toString()); 119 currentNode.appendChild(text); 120 } catch (DOMException err) { 121 throw new DynamicError(err); 122 } 123 } 124 125 126 129 130 public void processingInstruction (String target, CharSequence data, int locationId, int properties) 131 throws XPathException 132 { 133 try { 134 ProcessingInstruction pi = 135 document.createProcessingInstruction(target, data.toString()); 136 currentNode.appendChild(pi); 137 } catch (DOMException err) { 138 throw new DynamicError(err); 139 } 140 } 141 142 145 146 public void comment (CharSequence chars, int locationId, int properties) throws XPathException 147 { 148 try { 149 Comment comment = document.createComment(chars.toString()); 150 currentNode.appendChild(comment); 151 } catch (DOMException err) { 152 throw new DynamicError(err); 153 } 154 } 155 156 159 160 public void setNode (Node node) { 161 if (node == null) { 162 return; 163 } 164 currentNode = node; 165 if (node.getNodeType() == Node.DOCUMENT_NODE) { 166 document = (Document)node; 167 } else { 168 document = currentNode.getOwnerDocument(); 169 } 170 } 171 172 } 173 174 | Popular Tags |