KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
20
21 import javax.xml.transform.dom.DOMResult JavaDoc;
22
23 import org.apache.xerces.dom.AttrImpl;
24 import org.apache.xerces.dom.CoreDocumentImpl;
25 import org.apache.xerces.dom.DOMMessageFormatter;
26 import org.apache.xerces.dom.DocumentTypeImpl;
27 import org.apache.xerces.dom.ElementImpl;
28 import org.apache.xerces.dom.ElementNSImpl;
29 import org.apache.xerces.dom.EntityImpl;
30 import org.apache.xerces.dom.NotationImpl;
31 import org.apache.xerces.dom.PSVIAttrNSImpl;
32 import org.apache.xerces.dom.PSVIDocumentImpl;
33 import org.apache.xerces.dom.PSVIElementNSImpl;
34 import org.apache.xerces.impl.Constants;
35 import org.apache.xerces.impl.dv.XSSimpleType;
36 import org.apache.xerces.xni.Augmentations;
37 import org.apache.xerces.xni.NamespaceContext;
38 import org.apache.xerces.xni.QName;
39 import org.apache.xerces.xni.XMLAttributes;
40 import org.apache.xerces.xni.XMLLocator;
41 import org.apache.xerces.xni.XMLResourceIdentifier;
42 import org.apache.xerces.xni.XMLString;
43 import org.apache.xerces.xni.XNIException;
44 import org.apache.xerces.xni.parser.XMLDocumentSource;
45 import org.apache.xerces.xs.AttributePSVI;
46 import org.apache.xerces.xs.ElementPSVI;
47 import org.apache.xerces.xs.XSTypeDefinition;
48
49 import org.w3c.dom.CDATASection JavaDoc;
50 import org.w3c.dom.Comment JavaDoc;
51 import org.w3c.dom.Document JavaDoc;
52 import org.w3c.dom.DocumentType JavaDoc;
53 import org.w3c.dom.Element JavaDoc;
54 import org.w3c.dom.Entity JavaDoc;
55 import org.w3c.dom.NamedNodeMap JavaDoc;
56 import org.w3c.dom.Node JavaDoc;
57 import org.w3c.dom.Notation JavaDoc;
58 import org.w3c.dom.ProcessingInstruction JavaDoc;
59 import org.w3c.dom.Text JavaDoc;
60
61
62 /**
63  * <p>DOM result builder.</p>
64  *
65  * @author Michael Glavassevich, IBM
66  * @version $Id: DOMResultBuilder.java,v 1.1 2005/05/22 20:08:07 mrglavas Exp $
67  */

68 final class DOMResultBuilder implements DOMDocumentHandler {
69
70     /** Table for quick check of child insertion. */
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     } // static
93

94     //
95
// Data
96
//
97

98     private Document JavaDoc fDocument;
99     private CoreDocumentImpl fDocumentImpl;
100     private boolean fStorePSVI;
101     
102     private Node JavaDoc fTarget;
103     private Node JavaDoc fNextSibling;
104     
105     private Node JavaDoc fCurrentNode;
106     private Node JavaDoc fFragmentRoot;
107     
108     private final ArrayList JavaDoc fTargetChildren = new ArrayList JavaDoc();
109     
110     private boolean fIgnoreChars;
111     
112     private final QName fAttributeQName = new QName();
113     
114     public DOMResultBuilder() {}
115
116     /*
117      * DOMDocumentHandler methods
118      */

119     
120     public void setDOMResult(DOMResult JavaDoc 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 JavaDoc) 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 JavaDoc node) throws XNIException {
141         /** Create new DocumentType node for the target. */
142         if (fDocumentImpl != null) {
143             DocumentType JavaDoc docType = fDocumentImpl.createDocumentType(node.getName(), node.getPublicId(), node.getSystemId());
144             final String JavaDoc internalSubset = node.getInternalSubset();
145             /** Copy internal subset. */
146             if (internalSubset != null) {
147                 ((DocumentTypeImpl) docType).setInternalSubset(internalSubset);
148             }
149             /** Copy entities. */
150             NamedNodeMap JavaDoc oldMap = node.getEntities();
151             NamedNodeMap JavaDoc newMap = docType.getEntities();
152             int length = oldMap.getLength();
153             for (int i = 0; i < length; ++i) {
154                 Entity JavaDoc oldEntity = (Entity JavaDoc) 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             /** Copy notations. */
162             oldMap = node.getNotations();
163             newMap = docType.getNotations();
164             length = oldMap.getLength();
165             for (int i = 0; i < length; ++i) {
166                 Notation JavaDoc oldNotation = (Notation JavaDoc) 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 JavaDoc node) throws XNIException {
177         /** Create new Text node for the target. */
178         append(fDocument.createTextNode(node.getNodeValue()));
179     }
180     
181     public void cdata(CDATASection JavaDoc node) throws XNIException {
182         /** Create new CDATASection node for the target. */
183         append(fDocument.createCDATASection(node.getNodeValue()));
184     }
185
186     public void comment(Comment JavaDoc node) throws XNIException {
187         /** Create new Comment node for the target. */
188         append(fDocument.createComment(node.getNodeValue()));
189     }
190
191     public void processingInstruction(ProcessingInstruction JavaDoc node)
192             throws XNIException {
193         /** Create new ProcessingInstruction node for the target. */
194         append(fDocument.createProcessingInstruction(node.getTarget(), node.getData()));
195     }
196     
197     public void setIgnoringCharacters(boolean ignore) {
198         fIgnoreChars = ignore;
199     }
200     
201     /*
202      * XMLDocumentHandler methods
203      */

204
205     public void startDocument(XMLLocator locator, String JavaDoc encoding,
206             NamespaceContext namespaceContext, Augmentations augs)
207             throws XNIException {}
208
209     public void xmlDecl(String JavaDoc version, String JavaDoc encoding, String JavaDoc standalone,
210             Augmentations augs) throws XNIException {}
211
212     public void doctypeDecl(String JavaDoc rootElement, String JavaDoc publicId,
213             String JavaDoc systemId, Augmentations augs) throws XNIException {}
214
215     public void comment(XMLString text, Augmentations augs) throws XNIException {}
216
217     public void processingInstruction(String JavaDoc target, XMLString data,
218             Augmentations augs) throws XNIException {}
219
220     public void startElement(QName element, XMLAttributes attributes,
221             Augmentations augs) throws XNIException {
222         Element JavaDoc 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         // If it's a Xerces DOM store type information for attributes, set idness, etc..
232
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                 // write type information to this attribute
241
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 JavaDoc 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 JavaDoc name,
281             XMLResourceIdentifier identifier, String JavaDoc encoding,
282             Augmentations augs) throws XNIException {}
283
284     public void textDecl(String JavaDoc version, String JavaDoc encoding, Augmentations augs)
285             throws XNIException {}
286
287     public void endGeneralEntity(String JavaDoc 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         // write type information to this element
305
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         // adjust current node reference
320
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 JavaDoc) fTargetChildren.get(i));
337             }
338         }
339         else {
340             for (int i = 0; i < length; ++i) {
341                 fTarget.insertBefore((Node JavaDoc) 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     /*
353      * Other methods
354      */

355     
356     private void append(Node JavaDoc node) throws XNIException {
357         if (fCurrentNode != null) {
358             fCurrentNode.appendChild(node);
359         }
360         else {
361             /** Check if this node can be attached to the target. */
362             if ((kidOK[fTarget.getNodeType()] & (1 << node.getNodeType())) == 0) {
363                 String JavaDoc msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null);
364                 throw new XNIException(msg);
365             }
366             fTargetChildren.add(node);
367         }
368     }
369
370 } // DOMResultBuilder
371
Popular Tags