KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > jaxp > validation > DOMResultAugmentor


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.jaxp.validation;
18
19 import javax.xml.transform.dom.DOMResult JavaDoc;
20
21 import org.apache.xerces.dom.AttrImpl;
22 import org.apache.xerces.dom.CoreDocumentImpl;
23 import org.apache.xerces.dom.ElementImpl;
24 import org.apache.xerces.dom.ElementNSImpl;
25 import org.apache.xerces.dom.PSVIAttrNSImpl;
26 import org.apache.xerces.dom.PSVIDocumentImpl;
27 import org.apache.xerces.dom.PSVIElementNSImpl;
28 import org.apache.xerces.impl.Constants;
29 import org.apache.xerces.impl.dv.XSSimpleType;
30 import org.apache.xerces.xni.Augmentations;
31 import org.apache.xerces.xni.NamespaceContext;
32 import org.apache.xerces.xni.QName;
33 import org.apache.xerces.xni.XMLAttributes;
34 import org.apache.xerces.xni.XMLLocator;
35 import org.apache.xerces.xni.XMLResourceIdentifier;
36 import org.apache.xerces.xni.XMLString;
37 import org.apache.xerces.xni.XNIException;
38 import org.apache.xerces.xni.parser.XMLDocumentSource;
39 import org.apache.xerces.xs.AttributePSVI;
40 import org.apache.xerces.xs.ElementPSVI;
41 import org.apache.xerces.xs.XSTypeDefinition;
42 import org.w3c.dom.CDATASection JavaDoc;
43 import org.w3c.dom.Comment JavaDoc;
44 import org.w3c.dom.Document JavaDoc;
45 import org.w3c.dom.DocumentType JavaDoc;
46 import org.w3c.dom.Element JavaDoc;
47 import org.w3c.dom.NamedNodeMap JavaDoc;
48 import org.w3c.dom.Node JavaDoc;
49 import org.w3c.dom.ProcessingInstruction JavaDoc;
50 import org.w3c.dom.Text JavaDoc;
51
52 /**
53  * <p>DOM result augmentor.</p>
54  *
55  * @author Michael Glavassevich, IBM
56  * @version $Id: DOMResultAugmentor.java,v 1.1 2005/05/22 20:08:07 mrglavas Exp $
57  */

58 final class DOMResultAugmentor implements DOMDocumentHandler {
59
60     //
61
// Data
62
//
63

64     private DOMValidatorHelper fDOMValidatorHelper;
65     
66     private Document JavaDoc 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 JavaDoc result) {
79         fIgnoreChars = false;
80         if (result != null) {
81             final Node JavaDoc target = result.getNode();
82             fDocument = (target.getNodeType() == Node.DOCUMENT_NODE) ? (Document JavaDoc) 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 JavaDoc node) throws XNIException {}
93
94     public void characters(Text JavaDoc node) throws XNIException {}
95
96     public void cdata(CDATASection JavaDoc node) throws XNIException {}
97
98     public void comment(Comment JavaDoc node) throws XNIException {}
99
100     public void processingInstruction(ProcessingInstruction JavaDoc node)
101             throws XNIException {}
102
103     public void setIgnoringCharacters(boolean ignore) {
104         fIgnoreChars = ignore;
105     }
106
107     public void startDocument(XMLLocator locator, String JavaDoc encoding,
108             NamespaceContext namespaceContext, Augmentations augs)
109             throws XNIException {}
110
111     public void xmlDecl(String JavaDoc version, String JavaDoc encoding, String JavaDoc standalone,
112             Augmentations augs) throws XNIException {}
113
114     public void doctypeDecl(String JavaDoc rootElement, String JavaDoc publicId,
115             String JavaDoc systemId, Augmentations augs) throws XNIException {}
116
117     public void comment(XMLString text, Augmentations augs) throws XNIException {}
118
119     public void processingInstruction(String JavaDoc 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 JavaDoc currentElement = (Element JavaDoc) fDOMValidatorHelper.getCurrentElement();
125         final NamedNodeMap JavaDoc attrMap = currentElement.getAttributes();
126         
127         final int oldLength = attrMap.getLength();
128         // If it's a Xerces DOM store type information for attributes, set idness, etc..
129
if (fDocumentImpl != null) {
130             AttrImpl attr;
131             for (int i = 0; i < oldLength; ++i) {
132                 attr = (AttrImpl) attrMap.item(i);
133                 
134                 // write type information to this attribute
135
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         // Add default/fixed attributes
146
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             // If it's a Xerces DOM store type information for attributes, set idness, etc..
154
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                     // write type information to this attribute
162
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 JavaDoc name,
182             XMLResourceIdentifier identifier, String JavaDoc encoding,
183             Augmentations augs) throws XNIException {}
184
185     public void textDecl(String JavaDoc version, String JavaDoc encoding, Augmentations augs)
186             throws XNIException {}
187
188     public void endGeneralEntity(String JavaDoc name, Augmentations augs)
189             throws XNIException {}
190
191     public void characters(XMLString text, Augmentations augs)
192             throws XNIException {
193         if (!fIgnoreChars) {
194             final Element JavaDoc currentElement = (Element JavaDoc) 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 JavaDoc currentElement = fDOMValidatorHelper.getCurrentElement();
207         // Write type information to this element
208
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     /** Returns whether the given attribute is an ID type. **/
236     private boolean processAttributePSVI(AttrImpl attr, AttributePSVI attrPSVI) {
237         if (fStorePSVI) {
238             ((PSVIAttrNSImpl) attr).setPSVI (attrPSVI);
239         }
240         Object JavaDoc 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 } // DOMResultAugmentor
256
Popular Tags