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 22package com.sun.org.apache.xerces.internal.jaxp.validation; 23 24import javax.xml.transform.dom.DOMResult; 25 26import com.sun.org.apache.xerces.internal.dom.AttrImpl; 27import com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl; 28import com.sun.org.apache.xerces.internal.dom.ElementImpl; 29import com.sun.org.apache.xerces.internal.dom.ElementNSImpl; 30import com.sun.org.apache.xerces.internal.dom.PSVIAttrNSImpl; 31import com.sun.org.apache.xerces.internal.dom.PSVIDocumentImpl; 32import com.sun.org.apache.xerces.internal.dom.PSVIElementNSImpl; 33import com.sun.org.apache.xerces.internal.impl.Constants; 34import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType; 35import com.sun.org.apache.xerces.internal.xni.Augmentations; 36import com.sun.org.apache.xerces.internal.xni.NamespaceContext; 37import com.sun.org.apache.xerces.internal.xni.QName; 38import com.sun.org.apache.xerces.internal.xni.XMLAttributes; 39import com.sun.org.apache.xerces.internal.xni.XMLLocator; 40import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; 41import com.sun.org.apache.xerces.internal.xni.XMLString; 42import com.sun.org.apache.xerces.internal.xni.XNIException; 43import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource; 44import com.sun.org.apache.xerces.internal.xs.AttributePSVI; 45import com.sun.org.apache.xerces.internal.xs.ElementPSVI; 46import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition; 47import org.w3c.dom.CDATASection; 48import org.w3c.dom.Comment; 49import org.w3c.dom.Document; 50import org.w3c.dom.DocumentType; 51import org.w3c.dom.Element; 52import org.w3c.dom.NamedNodeMap; 53import org.w3c.dom.Node; 54import org.w3c.dom.ProcessingInstruction; 55import org.w3c.dom.Text; 56 57/** 58 * <p>DOM result augmentor.</p> 59 * 60 * @author Michael Glavassevich, IBM 61 */ 62final 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 78 public DOMResultAugmentor(DOMValidatorHelper helper) { 79 fDOMValidatorHelper = helper; 80 } 81 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 96 public void doctypeDecl(DocumentType node) throws XNIException {} 97 98 public void characters(Text node) throws XNIException {} 99 100 public void cdata(CDATASection node) throws XNIException {} 101 102 public void comment(Comment node) throws XNIException {} 103 104 public void processingInstruction(ProcessingInstruction node) 105 throws XNIException {} 106 107 public void setIgnoringCharacters(boolean ignore) { 108 fIgnoreChars = ignore; 109 } 110 111 public void startDocument(XMLLocator locator, String encoding, 112 NamespaceContext namespaceContext, Augmentations augs) 113 throws XNIException {} 114 115 public void xmlDecl(String version, String encoding, String standalone, 116 Augmentations augs) throws XNIException {} 117 118 public void doctypeDecl(String rootElement, String publicId, 119 String systemId, Augmentations augs) throws XNIException {} 120 121 public void comment(XMLString text, Augmentations augs) throws XNIException {} 122 123 public void processingInstruction(String target, XMLString data, 124 Augmentations augs) throws XNIException {} 125 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 179 public void emptyElement(QName element, XMLAttributes attributes, 180 Augmentations augs) throws XNIException { 181 startElement(element, attributes, augs); 182 endElement(element, augs); 183 } 184 185 public void startGeneralEntity(String name, 186 XMLResourceIdentifier identifier, String encoding, 187 Augmentations augs) throws XNIException {} 188 189 public void textDecl(String version, String encoding, Augmentations augs) 190 throws XNIException {} 191 192 public void endGeneralEntity(String name, Augmentations augs) 193 throws XNIException {} 194 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 203 public void ignorableWhitespace(XMLString text, Augmentations augs) 204 throws XNIException { 205 characters(text, augs); 206 } 207 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 227 public void startCDATA(Augmentations augs) throws XNIException {} 228 229 public void endCDATA(Augmentations augs) throws XNIException {} 230 231 public void endDocument(Augmentations augs) throws XNIException {} 232 233 public void setDocumentSource(XMLDocumentSource source) {} 234 235 public XMLDocumentSource getDocumentSource() { 236 return null; 237 } 238 239 /** Returns whether the given attribute is an ID type. **/ 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