1 17 package org.apache.ws.jaxme.util; 18 19 import javax.xml.XMLConstants ; 20 21 import org.w3c.dom.NamedNodeMap ; 22 import org.w3c.dom.Node ; 23 import org.xml.sax.ContentHandler ; 24 import org.xml.sax.SAXException ; 25 import org.xml.sax.ext.LexicalHandler ; 26 import org.xml.sax.helpers.AttributesImpl ; 27 28 33 public class DOMSerializer { 34 private boolean namespaceDeclarationAttribute; 35 private boolean parentsNamespaceDeclarationDisabled; 36 37 40 public void setNamespaceDeclarationAttribute(boolean pXmlDeclarationAttribute) { 41 namespaceDeclarationAttribute = pXmlDeclarationAttribute; 42 } 43 44 47 public boolean isNamespaceDeclarationAttribute() { 48 return namespaceDeclarationAttribute; 49 } 50 51 57 public void setParentsNamespaceDeclarationDisabled(boolean pParentsXmlDeclarationDisabled) { 58 parentsNamespaceDeclarationDisabled = pParentsXmlDeclarationDisabled; 59 } 60 61 67 public boolean isParentsNamespaceDeclarationDisabled() { 68 return parentsNamespaceDeclarationDisabled; 69 } 70 71 protected void doSerializeChilds(Node pNode, ContentHandler pHandler) 72 throws SAXException { 73 for (Node child = pNode.getFirstChild(); child != null; 74 child = child.getNextSibling()) { 75 doSerialize(child, pHandler); 76 } 77 } 78 79 protected void parentsStartPrefixMappingEvents(Node pNode, ContentHandler pHandler) 80 throws SAXException { 81 if (pNode != null) { 82 parentsStartPrefixMappingEvents(pNode.getParentNode(), pHandler); 83 if (pNode.getNodeType() == Node.ELEMENT_NODE) { 84 startPrefixMappingEvents(pNode, pHandler); 85 } 86 } 87 } 88 89 protected void parentsEndPrefixMappingEvents(Node pNode, ContentHandler pHandler) 90 throws SAXException { 91 if (pNode != null) { 92 if (pNode.getNodeType() == Node.ELEMENT_NODE) { 93 endPrefixMappingEvents(pNode, pHandler); 94 } 95 parentsEndPrefixMappingEvents(pNode.getParentNode(), pHandler); 96 } 97 } 98 99 protected void startPrefixMappingEvents(Node pNode, ContentHandler pHandler) 100 throws SAXException { 101 NamedNodeMap nnm = pNode.getAttributes(); 102 if (nnm != null) { 103 for (int i = 0; i < nnm.getLength(); i++) { 104 Node attr = nnm.item(i); 105 if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attr.getNamespaceURI())) { 106 String prefix; 107 if (XMLConstants.XMLNS_ATTRIBUTE.equals(attr.getPrefix())) { 108 prefix = attr.getLocalName(); 109 } else if (XMLConstants.XMLNS_ATTRIBUTE.equals(attr.getNodeName())) { 110 prefix = ""; 111 } else { 112 throw new IllegalStateException ("Unable to parse namespace declaration: " + attr.getNodeName()); 113 } 114 String uri = attr.getNodeValue(); 115 if (uri == null) { 116 uri = ""; 117 } 118 pHandler.startPrefixMapping(prefix, uri); 119 } 120 } 121 } 122 } 123 124 protected void endPrefixMappingEvents(Node pNode, ContentHandler pHandler) 125 throws SAXException { 126 NamedNodeMap nnm = pNode.getAttributes(); 127 if (nnm != null) { 128 for (int i = nnm.getLength()-1; i >= 0; i--) { 129 Node attr = nnm.item(i); 130 if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attr.getNamespaceURI())) { 131 String prefix = attr.getLocalName(); 132 pHandler.endPrefixMapping(prefix); 133 } 134 } 135 } 136 } 137 138 142 public void serialize(Node pNode, ContentHandler pHandler) 143 throws SAXException { 144 if (!isNamespaceDeclarationAttribute() && 145 !isParentsNamespaceDeclarationDisabled()) { 146 parentsStartPrefixMappingEvents(pNode.getParentNode(), pHandler); 147 } 148 doSerialize(pNode, pHandler); 149 if (!isNamespaceDeclarationAttribute() && 150 !isParentsNamespaceDeclarationDisabled()) { 151 parentsEndPrefixMappingEvents(pNode.getParentNode(), pHandler); 152 } 153 } 154 155 protected void doSerialize(Node pNode, ContentHandler pHandler) 156 throws SAXException { 157 switch (pNode.getNodeType()) { 158 case Node.DOCUMENT_NODE: 159 pHandler.startDocument(); 160 doSerializeChilds(pNode, pHandler); 161 pHandler.endDocument(); 162 break; 163 case Node.DOCUMENT_FRAGMENT_NODE: 164 doSerializeChilds(pNode, pHandler); 165 break; 166 case Node.ELEMENT_NODE: 167 AttributesImpl attr = new AttributesImpl (); 168 boolean isNamespaceDeclarationAttribute = isNamespaceDeclarationAttribute(); 169 if (!isNamespaceDeclarationAttribute) { 170 startPrefixMappingEvents(pNode, pHandler); 171 } 172 NamedNodeMap nnm = pNode.getAttributes(); 173 if (nnm != null) { 174 for (int i = 0; i < nnm.getLength(); i++) { 175 Node a = nnm.item(i); 176 if (isNamespaceDeclarationAttribute || 177 !XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(a.getNamespaceURI())) { 178 String aUri = a.getNamespaceURI(); 179 String aLocalName = a.getLocalName(); 180 String aNodeName = a.getNodeName(); 181 if (aLocalName == null) { 182 if (aUri == null || aUri.length() == 0) { 183 aLocalName = aNodeName; 184 } else { 185 throw new IllegalStateException ("aLocalName is null"); 186 } 187 } 188 attr.addAttribute(aUri == null ? "" : aUri, aNodeName, 189 aLocalName, "CDATA", a.getNodeValue()); 190 } 191 } 192 } 193 String nUri = pNode.getNamespaceURI(); 194 if (nUri == null) { 195 nUri = ""; 196 } 197 pHandler.startElement(nUri, pNode.getLocalName(), 198 pNode.getNodeName(), attr); 199 doSerializeChilds(pNode, pHandler); 200 pHandler.endElement(nUri, pNode.getLocalName(), 201 pNode.getNodeName()); 202 if (!isNamespaceDeclarationAttribute) { 203 endPrefixMappingEvents(pNode, pHandler); 204 } 205 break; 206 case Node.TEXT_NODE: 207 case Node.CDATA_SECTION_NODE: 208 { 209 String s = pNode.getNodeValue(); 210 pHandler.characters(s.toCharArray(), 0, s.length()); 211 } 212 break; 213 case Node.PROCESSING_INSTRUCTION_NODE: 214 pHandler.processingInstruction(pNode.getNodeName(), pNode.getNodeValue()); 215 break; 216 case Node.ENTITY_REFERENCE_NODE: 217 pHandler.skippedEntity(pNode.getNodeName()); 218 break; 219 case Node.COMMENT_NODE: 220 if (pHandler instanceof LexicalHandler ) { 221 String s = pNode.getNodeValue(); 222 ((LexicalHandler ) pHandler).comment(s.toCharArray(), 0, s.length()); 223 } 224 break; 225 default: 226 throw new IllegalStateException ("Unknown node type: " + pNode.getNodeType()); 227 } 228 } 229 } 230 | Popular Tags |