1 16 17 package com.sun.org.apache.xerces.internal.jaxp.validation; 18 19 import javax.xml.transform.dom.DOMResult ; 20 21 import com.sun.org.apache.xerces.internal.dom.AttrImpl; 22 import com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl; 23 import com.sun.org.apache.xerces.internal.dom.ElementImpl; 24 import com.sun.org.apache.xerces.internal.dom.ElementNSImpl; 25 import com.sun.org.apache.xerces.internal.dom.PSVIAttrNSImpl; 26 import com.sun.org.apache.xerces.internal.dom.PSVIDocumentImpl; 27 import com.sun.org.apache.xerces.internal.dom.PSVIElementNSImpl; 28 import com.sun.org.apache.xerces.internal.impl.Constants; 29 import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType; 30 import com.sun.org.apache.xerces.internal.xni.Augmentations; 31 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; 32 import com.sun.org.apache.xerces.internal.xni.QName; 33 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; 34 import com.sun.org.apache.xerces.internal.xni.XMLLocator; 35 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; 36 import com.sun.org.apache.xerces.internal.xni.XMLString; 37 import com.sun.org.apache.xerces.internal.xni.XNIException; 38 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource; 39 import com.sun.org.apache.xerces.internal.xs.AttributePSVI; 40 import com.sun.org.apache.xerces.internal.xs.ElementPSVI; 41 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition; 42 import org.w3c.dom.CDATASection ; 43 import org.w3c.dom.Comment ; 44 import org.w3c.dom.Document ; 45 import org.w3c.dom.DocumentType ; 46 import org.w3c.dom.Element ; 47 import org.w3c.dom.NamedNodeMap ; 48 import org.w3c.dom.Node ; 49 import org.w3c.dom.ProcessingInstruction ; 50 import org.w3c.dom.Text ; 51 52 58 final class DOMResultAugmentor implements DOMDocumentHandler { 59 60 64 private DOMValidatorHelper fDOMValidatorHelper; 65 66 private Document fDocument; 67 private CoreDocumentImpl fDocumentImpl; 68 private boolean fStorePSVI; 69 70 private boolean fIgnoreChars; 71 72 private final QName fAttributeQName = new QName(); 73 74 public DOMResultAugmentor(DOMValidatorHelper helper) { 75 fDOMValidatorHelper = helper; 76 } 77 78 public void setDOMResult(DOMResult result) { 79 fIgnoreChars = false; 80 if (result != null) { 81 final Node target = result.getNode(); 82 fDocument = (target.getNodeType() == Node.DOCUMENT_NODE) ? (Document ) target : target.getOwnerDocument(); 83 fDocumentImpl = (fDocument instanceof CoreDocumentImpl) ? (CoreDocumentImpl) fDocument : null; 84 fStorePSVI = (fDocument instanceof PSVIDocumentImpl); 85 return; 86 } 87 fDocument = null; 88 fDocumentImpl = null; 89 fStorePSVI = false; 90 } 91 92 public void doctypeDecl(DocumentType node) throws XNIException {} 93 94 public void characters(Text node) throws XNIException {} 95 96 public void cdata(CDATASection node) throws XNIException {} 97 98 public void comment(Comment node) throws XNIException {} 99 100 public void processingInstruction(ProcessingInstruction node) 101 throws XNIException {} 102 103 public void setIgnoringCharacters(boolean ignore) { 104 fIgnoreChars = ignore; 105 } 106 107 public void startDocument(XMLLocator locator, String encoding, 108 NamespaceContext namespaceContext, Augmentations augs) 109 throws XNIException {} 110 111 public void xmlDecl(String version, String encoding, String standalone, 112 Augmentations augs) throws XNIException {} 113 114 public void doctypeDecl(String rootElement, String publicId, 115 String systemId, Augmentations augs) throws XNIException {} 116 117 public void comment(XMLString text, Augmentations augs) throws XNIException {} 118 119 public void processingInstruction(String target, XMLString data, 120 Augmentations augs) throws XNIException {} 121 122 public void startElement(QName element, XMLAttributes attributes, 123 Augmentations augs) throws XNIException { 124 final Element currentElement = (Element ) fDOMValidatorHelper.getCurrentElement(); 125 final NamedNodeMap attrMap = currentElement.getAttributes(); 126 127 final int oldLength = attrMap.getLength(); 128 if (fDocumentImpl != null) { 130 AttrImpl attr; 131 for (int i = 0; i < oldLength; ++i) { 132 attr = (AttrImpl) attrMap.item(i); 133 134 AttributePSVI attrPSVI = (AttributePSVI) attributes.getAugmentations(i).getItem (Constants.ATTRIBUTE_PSVI); 136 if (attrPSVI != null) { 137 if (processAttributePSVI(attr, attrPSVI)) { 138 ((ElementImpl) currentElement).setIdAttributeNode (attr, true); 139 } 140 } 141 } 142 } 143 144 final int newLength = attributes.getLength(); 145 if (newLength > oldLength) { 147 if (fDocumentImpl == null) { 148 for (int i = oldLength; i < newLength; ++i) { 149 attributes.getName(i, fAttributeQName); 150 currentElement.setAttributeNS(fAttributeQName.uri, fAttributeQName.rawname, attributes.getValue(i)); 151 } 152 } 153 else { 155 for (int i = oldLength; i < newLength; ++i) { 156 attributes.getName(i, fAttributeQName); 157 AttrImpl attr = (AttrImpl) fDocumentImpl.createAttributeNS(fAttributeQName.uri, 158 fAttributeQName.rawname, fAttributeQName.localpart); 159 attr.setValue(attributes.getValue(i)); 160 161 AttributePSVI attrPSVI = (AttributePSVI) attributes.getAugmentations(i).getItem (Constants.ATTRIBUTE_PSVI); 163 if (attrPSVI != null) { 164 if (processAttributePSVI(attr, attrPSVI)) { 165 ((ElementImpl) currentElement).setIdAttributeNode (attr, true); 166 } 167 } 168 attr.setSpecified(false); 169 currentElement.setAttributeNode(attr); 170 } 171 } 172 } 173 } 174 175 public void emptyElement(QName element, XMLAttributes attributes, 176 Augmentations augs) throws XNIException { 177 startElement(element, attributes, augs); 178 endElement(element, augs); 179 } 180 181 public void startGeneralEntity(String name, 182 XMLResourceIdentifier identifier, String encoding, 183 Augmentations augs) throws XNIException {} 184 185 public void textDecl(String version, String encoding, Augmentations augs) 186 throws XNIException {} 187 188 public void endGeneralEntity(String name, Augmentations augs) 189 throws XNIException {} 190 191 public void characters(XMLString text, Augmentations augs) 192 throws XNIException { 193 if (!fIgnoreChars) { 194 final Element currentElement = (Element ) fDOMValidatorHelper.getCurrentElement(); 195 currentElement.appendChild(fDocument.createTextNode(text.toString())); 196 } 197 } 198 199 public void ignorableWhitespace(XMLString text, Augmentations augs) 200 throws XNIException { 201 characters(text, augs); 202 } 203 204 public void endElement(QName element, Augmentations augs) 205 throws XNIException { 206 final Node currentElement = fDOMValidatorHelper.getCurrentElement(); 207 if (augs != null && fDocumentImpl != null) { 209 ElementPSVI elementPSVI = (ElementPSVI)augs.getItem(Constants.ELEMENT_PSVI); 210 if (elementPSVI != null) { 211 if (fStorePSVI) { 212 ((PSVIElementNSImpl) currentElement).setPSVI(elementPSVI); 213 } 214 XSTypeDefinition type = elementPSVI.getMemberTypeDefinition(); 215 if (type == null) { 216 type = elementPSVI.getTypeDefinition(); 217 } 218 ((ElementNSImpl) currentElement).setType(type); 219 } 220 } 221 } 222 223 public void startCDATA(Augmentations augs) throws XNIException {} 224 225 public void endCDATA(Augmentations augs) throws XNIException {} 226 227 public void endDocument(Augmentations augs) throws XNIException {} 228 229 public void setDocumentSource(XMLDocumentSource source) {} 230 231 public XMLDocumentSource getDocumentSource() { 232 return null; 233 } 234 235 236 private boolean processAttributePSVI(AttrImpl attr, AttributePSVI attrPSVI) { 237 if (fStorePSVI) { 238 ((PSVIAttrNSImpl) attr).setPSVI (attrPSVI); 239 } 240 Object type = attrPSVI.getMemberTypeDefinition (); 241 if (type == null) { 242 type = attrPSVI.getTypeDefinition (); 243 if (type != null) { 244 attr.setType(type); 245 return ((XSSimpleType) type).isIDType(); 246 } 247 } 248 else { 249 attr.setType(type); 250 return ((XSSimpleType) type).isIDType(); 251 } 252 return false; 253 } 254 255 } | Popular Tags |