1 16 17 package com.sun.org.apache.xerces.internal.jaxp.validation; 18 19 import java.util.ArrayList ; 20 21 import javax.xml.transform.dom.DOMResult ; 22 23 import com.sun.org.apache.xerces.internal.dom.AttrImpl; 24 import com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl; 25 import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter; 26 import com.sun.org.apache.xerces.internal.dom.DocumentTypeImpl; 27 import com.sun.org.apache.xerces.internal.dom.ElementImpl; 28 import com.sun.org.apache.xerces.internal.dom.ElementNSImpl; 29 import com.sun.org.apache.xerces.internal.dom.EntityImpl; 30 import com.sun.org.apache.xerces.internal.dom.NotationImpl; 31 import com.sun.org.apache.xerces.internal.dom.PSVIAttrNSImpl; 32 import com.sun.org.apache.xerces.internal.dom.PSVIDocumentImpl; 33 import com.sun.org.apache.xerces.internal.dom.PSVIElementNSImpl; 34 import com.sun.org.apache.xerces.internal.impl.Constants; 35 import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType; 36 import com.sun.org.apache.xerces.internal.xni.Augmentations; 37 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; 38 import com.sun.org.apache.xerces.internal.xni.QName; 39 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; 40 import com.sun.org.apache.xerces.internal.xni.XMLLocator; 41 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; 42 import com.sun.org.apache.xerces.internal.xni.XMLString; 43 import com.sun.org.apache.xerces.internal.xni.XNIException; 44 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource; 45 import com.sun.org.apache.xerces.internal.xs.AttributePSVI; 46 import com.sun.org.apache.xerces.internal.xs.ElementPSVI; 47 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition; 48 49 import org.w3c.dom.CDATASection ; 50 import org.w3c.dom.Comment ; 51 import org.w3c.dom.Document ; 52 import org.w3c.dom.DocumentType ; 53 import org.w3c.dom.Element ; 54 import org.w3c.dom.Entity ; 55 import org.w3c.dom.NamedNodeMap ; 56 import org.w3c.dom.Node ; 57 import org.w3c.dom.Notation ; 58 import org.w3c.dom.ProcessingInstruction ; 59 import org.w3c.dom.Text ; 60 61 62 68 final class DOMResultBuilder implements DOMDocumentHandler { 69 70 71 private final static int[] kidOK; 72 73 static { 74 kidOK = new int[13]; 75 kidOK[Node.DOCUMENT_NODE] = 76 1 << Node.ELEMENT_NODE | 1 << Node.PROCESSING_INSTRUCTION_NODE | 77 1 << Node.COMMENT_NODE | 1 << Node.DOCUMENT_TYPE_NODE; 78 kidOK[Node.DOCUMENT_FRAGMENT_NODE] = 79 kidOK[Node.ENTITY_NODE] = 80 kidOK[Node.ENTITY_REFERENCE_NODE] = 81 kidOK[Node.ELEMENT_NODE] = 82 1 << Node.ELEMENT_NODE | 1 << Node.PROCESSING_INSTRUCTION_NODE | 83 1 << Node.COMMENT_NODE | 1 << Node.TEXT_NODE | 84 1 << Node.CDATA_SECTION_NODE | 1 << Node.ENTITY_REFERENCE_NODE ; 85 kidOK[Node.ATTRIBUTE_NODE] = 1 << Node.TEXT_NODE | 1 << Node.ENTITY_REFERENCE_NODE; 86 kidOK[Node.DOCUMENT_TYPE_NODE] = 0; 87 kidOK[Node.PROCESSING_INSTRUCTION_NODE] = 0; 88 kidOK[Node.COMMENT_NODE] = 0; 89 kidOK[Node.TEXT_NODE] = 0; 90 kidOK[Node.CDATA_SECTION_NODE] = 0; 91 kidOK[Node.NOTATION_NODE] = 0; 92 } 94 98 private Document fDocument; 99 private CoreDocumentImpl fDocumentImpl; 100 private boolean fStorePSVI; 101 102 private Node fTarget; 103 private Node fNextSibling; 104 105 private Node fCurrentNode; 106 private Node fFragmentRoot; 107 108 private final ArrayList fTargetChildren = new ArrayList (); 109 110 private boolean fIgnoreChars; 111 112 private final QName fAttributeQName = new QName(); 113 114 public DOMResultBuilder() {} 115 116 119 120 public void setDOMResult(DOMResult result) { 121 fCurrentNode = null; 122 fFragmentRoot = null; 123 fIgnoreChars = false; 124 fTargetChildren.clear(); 125 if (result != null) { 126 fTarget = result.getNode(); 127 fNextSibling = result.getNextSibling(); 128 fDocument = (fTarget.getNodeType() == Node.DOCUMENT_NODE) ? (Document ) fTarget : fTarget.getOwnerDocument(); 129 fDocumentImpl = (fDocument instanceof CoreDocumentImpl) ? (CoreDocumentImpl) fDocument : null; 130 fStorePSVI = (fDocument instanceof PSVIDocumentImpl); 131 return; 132 } 133 fTarget = null; 134 fNextSibling = null; 135 fDocument = null; 136 fDocumentImpl = null; 137 fStorePSVI = false; 138 } 139 140 public void doctypeDecl(DocumentType node) throws XNIException { 141 142 if (fDocumentImpl != null) { 143 DocumentType docType = fDocumentImpl.createDocumentType(node.getName(), node.getPublicId(), node.getSystemId()); 144 final String internalSubset = node.getInternalSubset(); 145 146 if (internalSubset != null) { 147 ((DocumentTypeImpl) docType).setInternalSubset(internalSubset); 148 } 149 150 NamedNodeMap oldMap = node.getEntities(); 151 NamedNodeMap newMap = docType.getEntities(); 152 int length = oldMap.getLength(); 153 for (int i = 0; i < length; ++i) { 154 Entity oldEntity = (Entity ) oldMap.item(i); 155 EntityImpl newEntity = (EntityImpl) fDocumentImpl.createEntity(oldEntity.getNodeName()); 156 newEntity.setPublicId(oldEntity.getPublicId()); 157 newEntity.setSystemId(oldEntity.getSystemId()); 158 newEntity.setNotationName(oldEntity.getNotationName()); 159 newMap.setNamedItem(newEntity); 160 } 161 162 oldMap = node.getNotations(); 163 newMap = docType.getNotations(); 164 length = oldMap.getLength(); 165 for (int i = 0; i < length; ++i) { 166 Notation oldNotation = (Notation ) oldMap.item(i); 167 NotationImpl newNotation = (NotationImpl) fDocumentImpl.createNotation(oldNotation.getNodeName()); 168 newNotation.setPublicId(oldNotation.getPublicId()); 169 newNotation.setSystemId(oldNotation.getSystemId()); 170 newMap.setNamedItem(newNotation); 171 } 172 append(docType); 173 } 174 } 175 176 public void characters(Text node) throws XNIException { 177 178 append(fDocument.createTextNode(node.getNodeValue())); 179 } 180 181 public void cdata(CDATASection node) throws XNIException { 182 183 append(fDocument.createCDATASection(node.getNodeValue())); 184 } 185 186 public void comment(Comment node) throws XNIException { 187 188 append(fDocument.createComment(node.getNodeValue())); 189 } 190 191 public void processingInstruction(ProcessingInstruction node) 192 throws XNIException { 193 194 append(fDocument.createProcessingInstruction(node.getTarget(), node.getData())); 195 } 196 197 public void setIgnoringCharacters(boolean ignore) { 198 fIgnoreChars = ignore; 199 } 200 201 204 205 public void startDocument(XMLLocator locator, String encoding, 206 NamespaceContext namespaceContext, Augmentations augs) 207 throws XNIException {} 208 209 public void xmlDecl(String version, String encoding, String standalone, 210 Augmentations augs) throws XNIException {} 211 212 public void doctypeDecl(String rootElement, String publicId, 213 String systemId, Augmentations augs) throws XNIException {} 214 215 public void comment(XMLString text, Augmentations augs) throws XNIException {} 216 217 public void processingInstruction(String target, XMLString data, 218 Augmentations augs) throws XNIException {} 219 220 public void startElement(QName element, XMLAttributes attributes, 221 Augmentations augs) throws XNIException { 222 Element elem; 223 int attrCount = attributes.getLength(); 224 if (fDocumentImpl == null) { 225 elem = fDocument.createElementNS(element.uri, element.rawname); 226 for (int i = 0; i < attrCount; ++i) { 227 attributes.getName(i, fAttributeQName); 228 elem.setAttributeNS(fAttributeQName.uri, fAttributeQName.rawname, attributes.getValue(i)); 229 } 230 } 231 else { 233 elem = fDocumentImpl.createElementNS(element.uri, element.rawname, element.localpart); 234 for (int i = 0; i < attrCount; ++i) { 235 attributes.getName(i, fAttributeQName); 236 AttrImpl attr = (AttrImpl) fDocumentImpl.createAttributeNS(fAttributeQName.uri, 237 fAttributeQName.rawname, fAttributeQName.localpart); 238 attr.setValue(attributes.getValue(i)); 239 240 AttributePSVI attrPSVI = (AttributePSVI) attributes.getAugmentations(i).getItem (Constants.ATTRIBUTE_PSVI); 242 if (attrPSVI != null) { 243 if (fStorePSVI) { 244 ((PSVIAttrNSImpl) attr).setPSVI(attrPSVI); 245 } 246 Object type = attrPSVI.getMemberTypeDefinition(); 247 if (type == null) { 248 type = attrPSVI.getTypeDefinition(); 249 if (type != null) { 250 attr.setType (type); 251 if (((XSSimpleType) type).isIDType()) { 252 ((ElementImpl) elem).setIdAttributeNode (attr, true); 253 } 254 } 255 } 256 else { 257 attr.setType (type); 258 if (((XSSimpleType) type).isIDType()) { 259 ((ElementImpl) elem).setIdAttributeNode (attr, true); 260 } 261 } 262 } 263 attr.setSpecified(attributes.isSpecified(i)); 264 elem.setAttributeNode(attr); 265 } 266 } 267 append(elem); 268 fCurrentNode = elem; 269 if (fFragmentRoot == null) { 270 fFragmentRoot = elem; 271 } 272 } 273 274 public void emptyElement(QName element, XMLAttributes attributes, 275 Augmentations augs) throws XNIException { 276 startElement(element, attributes, augs); 277 endElement(element, augs); 278 } 279 280 public void startGeneralEntity(String name, 281 XMLResourceIdentifier identifier, String encoding, 282 Augmentations augs) throws XNIException {} 283 284 public void textDecl(String version, String encoding, Augmentations augs) 285 throws XNIException {} 286 287 public void endGeneralEntity(String name, Augmentations augs) 288 throws XNIException {} 289 290 public void characters(XMLString text, Augmentations augs) 291 throws XNIException { 292 if (!fIgnoreChars) { 293 append(fDocument.createTextNode(text.toString())); 294 } 295 } 296 297 public void ignorableWhitespace(XMLString text, Augmentations augs) 298 throws XNIException { 299 characters(text, augs); 300 } 301 302 public void endElement(QName element, Augmentations augs) 303 throws XNIException { 304 if (augs != null && fDocumentImpl != null) { 306 ElementPSVI elementPSVI = (ElementPSVI)augs.getItem(Constants.ELEMENT_PSVI); 307 if (elementPSVI != null) { 308 if (fStorePSVI) { 309 ((PSVIElementNSImpl)fCurrentNode).setPSVI(elementPSVI); 310 } 311 XSTypeDefinition type = elementPSVI.getMemberTypeDefinition(); 312 if (type == null) { 313 type = elementPSVI.getTypeDefinition(); 314 } 315 ((ElementNSImpl)fCurrentNode).setType(type); 316 } 317 } 318 319 if (fCurrentNode == fFragmentRoot) { 321 fCurrentNode = null; 322 fFragmentRoot = null; 323 return; 324 } 325 fCurrentNode = fCurrentNode.getParentNode(); 326 } 327 328 public void startCDATA(Augmentations augs) throws XNIException {} 329 330 public void endCDATA(Augmentations augs) throws XNIException {} 331 332 public void endDocument(Augmentations augs) throws XNIException { 333 final int length = fTargetChildren.size(); 334 if (fNextSibling == null) { 335 for (int i = 0; i < length; ++i) { 336 fTarget.appendChild((Node ) fTargetChildren.get(i)); 337 } 338 } 339 else { 340 for (int i = 0; i < length; ++i) { 341 fTarget.insertBefore((Node ) fTargetChildren.get(i), fNextSibling); 342 } 343 } 344 } 345 346 public void setDocumentSource(XMLDocumentSource source) {} 347 348 public XMLDocumentSource getDocumentSource() { 349 return null; 350 } 351 352 355 356 private void append(Node node) throws XNIException { 357 if (fCurrentNode != null) { 358 fCurrentNode.appendChild(node); 359 } 360 else { 361 362 if ((kidOK[fTarget.getNodeType()] & (1 << node.getNodeType())) == 0) { 363 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null); 364 throw new XNIException(msg); 365 } 366 fTargetChildren.add(node); 367 } 368 } 369 370 } | Popular Tags |