KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mediator > DOMUtils > TypedDOMReader


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2004 XQuark Group.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.mediator.DOMUtils;
24
25 import java.util.Iterator JavaDoc;
26
27 import org.w3c.dom.*;
28 import org.xml.sax.SAXException JavaDoc;
29 import org.xquark.schema.*;
30 import org.xquark.schema.validation.PSVInfoSetProvider;
31 import org.xquark.util.DOMReader;
32 import org.xquark.xpath.datamodel.TypedAttribute;
33 import org.xquark.xpath.datamodel.TypedElement;
34 import org.xquark.xpath.datamodel.TypedNode;
35 import org.xquark.xquery.parser.XQueryException;
36 import org.xquark.xquery.typing.QAtomicSerializer;
37
38
39 /**
40  * Define a typed DOM-tree walker that produces SAX events.
41  *
42  * <p><B> WARNING : DOM must support namespaces !!!</p>
43  * <p>Special Feature:</p>
44  * <code>http://wwww.xquark.org/sax/features/fragment</code> disables SAX
45  * startDocument() and endDocument() events triggering
46  *
47  * @see org.xml.sax.XMLReader, org.w3c.dom.Element
48  */

49 public class TypedDOMReader extends DOMReader {
50     private static final String JavaDoc RCSRevision = "$Revision: 1.7 $";
51     private static final String JavaDoc RCSName = "$Name: $";
52
53     protected PSVInfoSetProvider psvisp = null;
54     protected QAtomicSerializer qAtomicSerializer = null;
55
56     public TypedDOMReader() {
57         super();
58     }
59     
60     public TypedDOMReader(Document doc) {
61         super(doc);
62     }
63     
64     public TypedDOMReader(Element root) {
65         super(root);
66     }
67
68     public void setPSVInfoSetProvider(PSVInfoSetProvider psvisp) {
69         this.psvisp = psvisp;
70     }
71
72     public void setQAtomicSerializer(QAtomicSerializer qAtomicSerializer) {
73         this.qAtomicSerializer = qAtomicSerializer;
74     }
75     
76     protected void generateText(Node node) throws SAXException JavaDoc {
77         if (psvisp != null && node.getNodeValue() != null && node instanceof TypedNode) {
78             psvisp.setActualValue(-1,((TypedNode)node).getTypedValue());
79             psvisp.setNormalizedValue(-1,((TypedNode)node).getNormalizedStringValue());
80         }
81         //super.generateText(node);
82
String JavaDoc value = null;
83         if (qAtomicSerializer != null) {
84             Object JavaDoc typedValue = ((TypedNode)node).getTypedValue();
85             try {
86                 value = qAtomicSerializer.serialize((SimpleType)((TypedNode)node).getType(), typedValue);
87             } catch (XQueryException e) {
88                 throw new SAXException JavaDoc(e);
89             }
90         }
91         if (value == null) {
92             value = node.getNodeValue();
93         }
94         if (value != null) {
95             contentHandler.characters(value.toCharArray(), 0, value.length());
96         }
97     }
98
99
100     protected void generateStartElement(Node node) throws SAXException JavaDoc {
101         
102         // TODO do it using inheritance
103
String JavaDoc uri = node.getNamespaceURI();
104         if (uri == null) uri = "";
105         String JavaDoc local = node.getLocalName();
106         if (local == null) local = "";
107         String JavaDoc prefix = node.getPrefix();
108         String JavaDoc qname = prefix == null ? local : prefix + ":" +local;
109         
110         wAtts.setDOMAttributes(node.getAttributes());
111         Iterator JavaDoc it = wAtts.getPrefixIterator(); // OPTIM SR 23/05/2001
112
if (it != null)
113             while (it.hasNext()) {
114                 Attr a = (Attr)it.next();
115                 contentHandler.startPrefixMapping(
116                 a.getLocalName().equals(XMLNS_PREFIX) ? "": a.getLocalName(), // default prefix
117
a.getValue());
118             }
119             
120         if (psvisp != null) {
121             psvisp.pushElementInfoset(uri, local);
122             if (node instanceof TypedElement) {
123                 TypedElement elt = (TypedElement)node;
124                 psvisp.initElementValidationInfo((ElementDeclaration)elt.getDeclaration(),elt.getType(),false,SchemaConstants.LAX);
125                 for (int i = 0; i < wAtts.getLength(); i++) {
126                     String JavaDoc namespace = wAtts.getURI(i);
127                     String JavaDoc localName = wAtts.getLocalName(i);
128                     String JavaDoc attrValue = wAtts.getValue(i);
129                     TypedAttribute attr = (TypedAttribute)elt.getAttributeNodeNS(namespace, localName);
130                     int index = psvisp.addAttributePSVI(namespace, localName, (AttributeDeclaration)attr.getDeclaration(), true);
131                     psvisp.setActualValue(index, attr.getTypedValue());
132                     psvisp.setNormalizedValue(index, attr.getNormalizedStringValue());
133                 }
134             }
135         }
136         
137         contentHandler.startElement(uri, local, qname, wAtts);
138  
139 // super.generateStartElement(node);
140
}
141     
142     protected void generateEndElement(Node node) throws SAXException JavaDoc {
143         super.generateEndElement(node);
144         if (psvisp != null) {
145             psvisp.popElementInfoset();
146         }
147     }
148
149 }
150
151
152
Popular Tags