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