KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
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 org.xml.sax.Attributes JavaDoc;
26 import org.xml.sax.SAXException JavaDoc;
27 import org.xml.sax.helpers.DefaultHandler JavaDoc;
28 import org.xquark.schema.Type;
29 import org.xquark.schema.validation.ElementPSVInfoset;
30 import org.xquark.schema.validation.PSVInfoSetProvider;
31 import org.xquark.schema.validation.PSVInfoset;
32 import org.xquark.xpath.datamodel.*;
33
34 /**
35  *
36  * This class computes consumes SAX events to produce a DOM tree.
37  * input: document, top-level element
38  * output: top-level element, with some new nodes underneath
39  *
40  * document is needed to grab DOM methods
41  * element is needed to insert the nodes
42  */

43
44 public class SAX2DOM extends DefaultHandler JavaDoc {
45     // **********************************************************************
46
// * VERSIONING
47
// **********************************************************************
48
private static final String JavaDoc RCSRevision = "$Revision: 1.5 $";
49     private static final String JavaDoc RCSName = "$Name: $";
50     // **********************************************************************
51
// * CLASS VARIABLES
52
// **********************************************************************
53
private TypedDocumentImpl doc = null ;
54     // Element top;
55
private TypedNode currentNode = null ;
56     //private Node root = null ;
57
//private int toKeep = -1 ;
58

59     //private ArrayList steps = null ;
60
private Tuple tuple ;
61     //private PathStack pathstack = null ;
62
private int index = 0;
63     private int depth = 0;
64     private PSVInfoSetProvider psvip = null;
65     private StringBuffer JavaDoc ongoingchars = new StringBuffer JavaDoc();
66     // ************************************************************************
67
// * INITIALIZATION
68
// ************************************************************************
69
/**
70      *
71      */

72     //new ValidatingSchemaHandler(
73
public SAX2DOM(PSVInfoSetProvider psvip) { this.psvip = psvip; doc = new TypedDocumentImpl() ; }
74     
75     public void init(Tuple tuple) { this.tuple = tuple ; index = 0; }
76     
77     // ************************************************************************
78
// * SAX Implementation
79
// ************************************************************************
80
/**
81      *
82      */

83     public void characters(char[] ch, int start, int length) throws SAXException JavaDoc {
84         // we create a TEXT node and add it to the current node
85
/*
86         Node n = doc.createTextNode(new String(ch, start, length));
87         if (depth == 2) tuple.addNodeAtIndex(index, n) ;
88         else currentNode.appendChild(n);
89         */

90         ongoingchars.append(new String JavaDoc(ch, start, length));
91     }
92     
93     //public void endDocument() throws SAXException { return; }
94

95     public void endElement(String JavaDoc uri, String JavaDoc local, String JavaDoc raw) throws SAXException JavaDoc {
96         boolean onlytext = false;
97         if (depth >= 2) {
98             ElementPSVInfoset epsvis = psvip.getCurrentInfoset();
99             Type type = epsvis.getType();
100             if (ongoingchars.length() > 0) {
101                 TypedValueImpl tv = (TypedValueImpl)doc.createTextNode(ongoingchars.toString());
102                 if (type != null && type.getValueType() != null) {
103                         tv.setType(type.getValueType());
104                         tv.setTypedValue(epsvis.getActualValue());
105                         tv.setNodeValue(epsvis.getNormalizedValue());
106                 }
107                 else {
108                     tv.setType(this.psvip.getSchemaManager().getAnySimpleType());
109                     tv.setTypedValue(ongoingchars.toString());
110                 }
111                 if (currentNode == null) { currentNode = tv; depth++; onlytext = true;}
112                 else currentNode.appendChild(tv);
113                 ongoingchars.setLength(0);
114             }
115             if (currentNode != null && !onlytext) {
116                 currentNode.setType(epsvis.getType());
117                 currentNode.setDeclaration(epsvis.getDeclaration());
118             }
119         }
120         depth--;
121         switch (depth) {
122             case 0 : index = 0; break;
123             case 1 : index++; break;
124             case 2 :
125                 // setting type of element
126
//currentNode.setType
127
// add node to current tuple
128
tuple.addNodeAtIndex(index, currentNode) ;
129                 if (onlytext) { depth--; index++; }
130                 currentNode = null;
131                 break;
132             default : currentNode = (TypedNode)currentNode.getParentNode();
133         }
134     }
135     
136     //public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { return; }
137

138 // public void processingInstruction(String target, String data) throws SAXException {
139
// // we create a PI node and add it to the current node
140
// Node n = doc.createProcessingInstruction(target, data);
141
// /*
142
// if (currentNode == null) {
143
// tuple.addNodeAtIndex(index, n) ;
144
// }
145
// else currentNode.appendChild(n);
146
// return;
147
// */
148
// if (depth == 2) tuple.addNodeAtIndex(index, n) ;
149
// else currentNode.appendChild(n);
150
// }
151

152     //public void setDocumentLocator(Locator locator) { return; }
153

154     public void startDocument() throws SAXException JavaDoc {
155         /*pathstack = new PathStack() ;*/
156         depth = 0;
157         index = 0;
158     }
159     
160     public void startElement(String JavaDoc uri, String JavaDoc local, String JavaDoc raw, Attributes JavaDoc atts) throws SAXException JavaDoc {
161         if (depth >= 2) {
162             TypedElement el = (TypedElement)doc.createElementNS(uri,local);
163             ElementPSVInfoset epsvis = psvip.getCurrentInfoset();
164             for (int i=0; i<epsvis.getAttributeCount();i++) {
165                 PSVInfoset infoset = epsvis.getAttributePSVInfoset(i);
166                 TypedAttribute attr = (TypedAttribute)doc.createAttributeNS(infoset.getNamespaceURI(),infoset.getLocalName());
167                 attr.setNodeValue(infoset.getNormalizedValue());
168                 attr.setTypedValue(infoset.getActualValue());
169                 attr.setType(infoset.getType());
170                 attr.setDeclaration(infoset.getDeclaration());
171                 el.setAttributeNode(attr);
172             }
173             if (ongoingchars.length() > 0) {
174                 TypedValueImpl tv = (TypedValueImpl)doc.createTextNode(ongoingchars.toString());
175                 tv.setType(this.psvip.getSchemaManager().getAnySimpleType());
176                 tv.setTypedValue(ongoingchars.toString());
177                 if (currentNode == null) throw new SAXException JavaDoc("Incorrect result!");
178                 currentNode.appendChild(tv);
179                 ongoingchars.setLength(0);
180             }
181             if (depth > 2) currentNode.appendChild(el);
182             currentNode = el;
183         }
184         depth++;
185         
186     }
187 }
188
189
Popular Tags