1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.xerces.internal.jaxp.validation; 23 24 import javax.xml.transform.dom.DOMResult; 25 26 import com.sun.org.apache.xerces.internal.dom.AttrImpl; 27 import com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl; 28 import com.sun.org.apache.xerces.internal.dom.ElementImpl; 29 import com.sun.org.apache.xerces.internal.dom.ElementNSImpl; 30 import com.sun.org.apache.xerces.internal.dom.PSVIAttrNSImpl; 31 import com.sun.org.apache.xerces.internal.dom.PSVIDocumentImpl; 32 import com.sun.org.apache.xerces.internal.dom.PSVIElementNSImpl; 33 import com.sun.org.apache.xerces.internal.impl.Constants; 34 import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType; 35 import com.sun.org.apache.xerces.internal.xni.Augmentations; 36 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; 37 import com.sun.org.apache.xerces.internal.xni.QName; 38 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; 39 import com.sun.org.apache.xerces.internal.xni.XMLLocator; 40 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; 41 import com.sun.org.apache.xerces.internal.xni.XMLString; 42 import com.sun.org.apache.xerces.internal.xni.XNIException; 43 import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource; 44 import com.sun.org.apache.xerces.internal.xs.AttributePSVI; 45 import com.sun.org.apache.xerces.internal.xs.ElementPSVI; 46 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition; 47 import org.w3c.dom.CDATASection; 48 import org.w3c.dom.Comment; 49 import org.w3c.dom.Document; 50 import org.w3c.dom.DocumentType; 51 import org.w3c.dom.Element; 52 import org.w3c.dom.NamedNodeMap; 53 import org.w3c.dom.Node; 54 import org.w3c.dom.ProcessingInstruction; 55 import org.w3c.dom.Text; 56 57 /** 58 * <p>DOM result augmentor.</p> 59 * 60 * @author Michael Glavassevich, IBM 61 */ 62 final class DOMResultAugmentor implements DOMDocumentHandler { 63 64 // 65 // Data 66 // 67 68 private DOMValidatorHelper fDOMValidatorHelper; 69 70 private Document fDocument; 71 private CoreDocumentImpl fDocumentImpl; 72 private boolean fStorePSVI; 73 74 private boolean fIgnoreChars; 75 76 private final QName fAttributeQName = new QName(); 77 DOMResultAugmentor(DOMValidatorHelper helper)78 public DOMResultAugmentor(DOMValidatorHelper helper) { 79 fDOMValidatorHelper = helper; 80 } 81 setDOMResult(DOMResult result)82 public void setDOMResult(DOMResult result) { 83 fIgnoreChars = false; 84 if (result != null) { 85 final Node target = result.getNode(); 86 fDocument = (target.getNodeType() == Node.DOCUMENT_NODE) ? (Document) target : target.getOwnerDocument(); 87 fDocumentImpl = (fDocument instanceof CoreDocumentImpl) ? (CoreDocumentImpl) fDocument : null; 88 fStorePSVI = (fDocument instanceof PSVIDocumentImpl); 89 return; 90 } 91 fDocument = null; 92 fDocumentImpl = null; 93 fStorePSVI = false; 94 } 95 doctypeDecl(DocumentType node)96 public void doctypeDecl(DocumentType node) throws XNIException {} 97 characters(Text node)98 public void characters(Text node) throws XNIException {} 99 cdata(CDATASection node)100 public void cdata(CDATASection node) throws XNIException {} 101 comment(Comment node)102 public void comment(Comment node) throws XNIException {} 103 processingInstruction(ProcessingInstruction node)104 public void processingInstruction(ProcessingInstruction node) 105 throws XNIException {} 106 setIgnoringCharacters(boolean ignore)107 public void setIgnoringCharacters(boolean ignore) { 108 fIgnoreChars = ignore; 109 } 110 startDocument(XMLLocator locator, String encoding, NamespaceContext namespaceContext, Augmentations augs)111 public void startDocument(XMLLocator locator, String encoding, 112 NamespaceContext namespaceContext, Augmentations augs) 113 throws XNIException {} 114 xmlDecl(String version, String encoding, String standalone, Augmentations augs)115 public void xmlDecl(String version, String encoding, String standalone, 116 Augmentations augs) throws XNIException {} 117 doctypeDecl(String rootElement, String publicId, String systemId, Augmentations augs)118 public void doctypeDecl(String rootElement, String publicId, 119 String systemId, Augmentations augs) throws XNIException {} 120 comment(XMLString text, Augmentations augs)121 public void comment(XMLString text, Augmentations augs) throws XNIException {} 122 processingInstruction(String target, XMLString data, Augmentations augs)123 public void processingInstruction(String target, XMLString data, 124 Augmentations augs) throws XNIException {} 125 startElement(QName element, XMLAttributes attributes, Augmentations augs)126 public void startElement(QName element, XMLAttributes attributes, 127 Augmentations augs) throws XNIException { 128 final Element currentElement = (Element) fDOMValidatorHelper.getCurrentElement(); 129 final NamedNodeMap attrMap = currentElement.getAttributes(); 130 131 final int oldLength = attrMap.getLength(); 132 // If it's a Xerces DOM store type information for attributes, set idness, etc.. 133 if (fDocumentImpl != null) { 134 AttrImpl attr; 135 for (int i = 0; i < oldLength; ++i) { 136 attr = (AttrImpl) attrMap.item(i); 137 138 // write type information to this attribute 139 AttributePSVI attrPSVI = (AttributePSVI) attributes.getAugmentations(i).getItem (Constants.ATTRIBUTE_PSVI); 140 if (attrPSVI != null) { 141 if (processAttributePSVI(attr, attrPSVI)) { 142 ((ElementImpl) currentElement).setIdAttributeNode (attr, true); 143 } 144 } 145 } 146 } 147 148 final int newLength = attributes.getLength(); 149 // Add default/fixed attributes 150 if (newLength > oldLength) { 151 if (fDocumentImpl == null) { 152 for (int i = oldLength; i < newLength; ++i) { 153 attributes.getName(i, fAttributeQName); 154 currentElement.setAttributeNS(fAttributeQName.uri, fAttributeQName.rawname, attributes.getValue(i)); 155 } 156 } 157 // If it's a Xerces DOM store type information for attributes, set idness, etc.. 158 else { 159 for (int i = oldLength; i < newLength; ++i) { 160 attributes.getName(i, fAttributeQName); 161 AttrImpl attr = (AttrImpl) fDocumentImpl.createAttributeNS(fAttributeQName.uri, 162 fAttributeQName.rawname, fAttributeQName.localpart); 163 attr.setValue(attributes.getValue(i)); 164 165 // write type information to this attribute 166 AttributePSVI attrPSVI = (AttributePSVI) attributes.getAugmentations(i).getItem (Constants.ATTRIBUTE_PSVI); 167 if (attrPSVI != null) { 168 if (processAttributePSVI(attr, attrPSVI)) { 169 ((ElementImpl) currentElement).setIdAttributeNode (attr, true); 170 } 171 } 172 attr.setSpecified(false); 173 currentElement.setAttributeNode(attr); 174 } 175 } 176 } 177 } 178 emptyElement(QName element, XMLAttributes attributes, Augmentations augs)179 public void emptyElement(QName element, XMLAttributes attributes, 180 Augmentations augs) throws XNIException { 181 startElement(element, attributes, augs); 182 endElement(element, augs); 183 } 184 startGeneralEntity(String name, XMLResourceIdentifier identifier, String encoding, Augmentations augs)185 public void startGeneralEntity(String name, 186 XMLResourceIdentifier identifier, String encoding, 187 Augmentations augs) throws XNIException {} 188 textDecl(String version, String encoding, Augmentations augs)189 public void textDecl(String version, String encoding, Augmentations augs) 190 throws XNIException {} 191 endGeneralEntity(String name, Augmentations augs)192 public void endGeneralEntity(String name, Augmentations augs) 193 throws XNIException {} 194 characters(XMLString text, Augmentations augs)195 public void characters(XMLString text, Augmentations augs) 196 throws XNIException { 197 if (!fIgnoreChars) { 198 final Element currentElement = (Element) fDOMValidatorHelper.getCurrentElement(); 199 currentElement.appendChild(fDocument.createTextNode(text.toString())); 200 } 201 } 202 ignorableWhitespace(XMLString text, Augmentations augs)203 public void ignorableWhitespace(XMLString text, Augmentations augs) 204 throws XNIException { 205 characters(text, augs); 206 } 207 endElement(QName element, Augmentations augs)208 public void endElement(QName element, Augmentations augs) 209 throws XNIException { 210 final Node currentElement = fDOMValidatorHelper.getCurrentElement(); 211 // Write type information to this element 212 if (augs != null && fDocumentImpl != null) { 213 ElementPSVI elementPSVI = (ElementPSVI)augs.getItem(Constants.ELEMENT_PSVI); 214 if (elementPSVI != null) { 215 if (fStorePSVI) { 216 ((PSVIElementNSImpl) currentElement).setPSVI(elementPSVI); 217 } 218 XSTypeDefinition type = elementPSVI.getMemberTypeDefinition(); 219 if (type == null) { 220 type = elementPSVI.getTypeDefinition(); 221 } 222 ((ElementNSImpl) currentElement).setType(type); 223 } 224 } 225 } 226 startCDATA(Augmentations augs)227 public void startCDATA(Augmentations augs) throws XNIException {} 228 endCDATA(Augmentations augs)229 public void endCDATA(Augmentations augs) throws XNIException {} 230 endDocument(Augmentations augs)231 public void endDocument(Augmentations augs) throws XNIException {} 232 setDocumentSource(XMLDocumentSource source)233 public void setDocumentSource(XMLDocumentSource source) {} 234 getDocumentSource()235 public XMLDocumentSource getDocumentSource() { 236 return null; 237 } 238 239 /** Returns whether the given attribute is an ID type. **/ processAttributePSVI(AttrImpl attr, AttributePSVI attrPSVI)240 private boolean processAttributePSVI(AttrImpl attr, AttributePSVI attrPSVI) { 241 if (fStorePSVI) { 242 ((PSVIAttrNSImpl) attr).setPSVI (attrPSVI); 243 } 244 Object type = attrPSVI.getMemberTypeDefinition (); 245 if (type == null) { 246 type = attrPSVI.getTypeDefinition (); 247 if (type != null) { 248 attr.setType(type); 249 return ((XSSimpleType) type).isIDType(); 250 } 251 } 252 else { 253 attr.setType(type); 254 return ((XSSimpleType) type).isIDType(); 255 } 256 return false; 257 } 258 259 } // DOMResultAugmentor 260