KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > util > BasicDOMBuilder


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.util;
24
25 import org.w3c.dom.Document JavaDoc;
26 import org.w3c.dom.Element JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28 import org.xml.sax.Attributes JavaDoc;
29 import org.xml.sax.ContentHandler JavaDoc;
30 import org.xml.sax.ext.LexicalHandler JavaDoc;
31 import org.xml.sax.helpers.DefaultHandler JavaDoc;
32
33 /**
34  * Define a SAX ContentHandler or/and LexicalHandler writing a DOM-tree inside a given document.
35  * Such writing doesn't erase previous content but append to it.
36  * So, if the document is not empty, this handler will try to add nodes anyway, and
37  * probably cause a hierarchy error due to try to add root node or doc type in addition
38  * of a previous one... <BR>
39  * <BR>
40  * WARNING: if this class is used for two differents types of handler to build a DOMTree from
41  * the same parsing, the parser must reference the SAME DOMBuilder object for the two types of
42  * handlers unless the DOM-Tree will not correspond to the waited DOM-Tree.
43  *
44  * IMPORTANT: SAX2 XMLReader
45  * <code>http://xml.org/sax/features/namespace-prefixes</code> and
46  * <code>http://xml.org/sax/features/namespace-prefixes</code> features must be
47  * set to true.
48  *
49  * @see org.xml.sax.ContentHandler, org.xml.sax.LexicalHandler, org.w3c.dom.Document
50  */

51 public class BasicDOMBuilder extends DefaultHandler JavaDoc
52 implements ContentHandler JavaDoc, LexicalHandler JavaDoc
53 {
54     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
55     private static final String JavaDoc RCSName = "$Name: $";
56
57     // ***********************************************************************
58
// * Fields
59
// ***********************************************************************
60

61     /**
62      * The document to build
63      */

64     private Document JavaDoc doc;
65     
66     /**
67      * The most recent not closed element
68      */

69     private Node JavaDoc nodeactu;
70     
71     /**
72      * Indicates if the characters must be considered as CDATA sections;
73      */

74     private boolean cs;
75     
76     // ***********************************************************************
77
// * Constructors
78
// ***********************************************************************
79

80     /**
81      * Contruct a DOMBuilder object.
82      *
83      * @param doc the document to fill
84      *
85      * @exception java.lang.NullPointerException the needed document was not provided.
86      */

87     public BasicDOMBuilder(Document JavaDoc doc) throws NullPointerException JavaDoc
88     {
89         setDocument(doc);
90     }
91     
92     /**
93      * Contruct a DOMBuilder object.
94      *
95      * @param element the element on which the DOM fragment will be built.
96      *
97      * @exception java.lang.NullPointerException the needed element was not provided.
98      */

99     public BasicDOMBuilder(Element JavaDoc element) throws NullPointerException JavaDoc
100     {
101         setElement(element);
102     }
103     
104     // ***********************************************************************
105
// * ContentHandler methods
106
// ***********************************************************************
107

108     public void characters(char[] ch, int start, int length)
109     {
110         if (nodeactu != doc) // not allowed at document root
111
{
112             if (cs)
113                 nodeactu.appendChild(doc.createCDATASection(new String JavaDoc(ch,start,length)));
114             else
115                 nodeactu.appendChild(doc.createTextNode(new String JavaDoc(ch,start,length)));
116         }
117     }
118     
119     public void endElement(String JavaDoc namespaceUI, String JavaDoc localName, String JavaDoc qName)
120     {
121         nodeactu=nodeactu.getParentNode();
122     }
123     
124     public void ignorableWhitespace(char[] ch, int start, int length)
125     {
126         if (nodeactu != doc) // not allowed at document root
127
nodeactu.appendChild(doc.createTextNode(new String JavaDoc(ch,start,length)));
128     }
129     
130     public void processingInstruction(String JavaDoc target, String JavaDoc data)
131     {
132         nodeactu.appendChild(doc.createProcessingInstruction(target,data));
133     }
134     
135     public void skippedEntity(String JavaDoc name)
136     {
137         nodeactu.appendChild(doc.createEntityReference(name));
138     }
139     
140     public void startElement
141     (String JavaDoc namespaceURI,
142     String JavaDoc localName,
143     String JavaDoc qName,
144     Attributes JavaDoc atts)
145     {
146         if (namespaceURI.equals(""))
147             namespaceURI = null;
148         
149         Element JavaDoc element=doc.createElementNS(namespaceURI,qName);
150         int nbattr=atts.getLength();
151         for (int i=0; i<nbattr; i++)
152         {
153             namespaceURI=atts.getURI(i);
154             if (namespaceURI.equals(""))
155                 namespaceURI = null;
156             element.setAttributeNS(namespaceURI,atts.getQName(i),atts.getValue(i));
157         }
158         nodeactu.appendChild(element);
159         nodeactu=element;
160     }
161     
162     // ***********************************************************************
163
// * LexicalHandler methods
164
// ***********************************************************************
165

166     public void comment(char[] ch, int start, int length)
167     {
168         nodeactu.appendChild
169         (doc.createComment
170         (new String JavaDoc(ch,start,length)));
171     }
172     
173     public void endCDATA()
174     {
175         cs=false;
176     }
177     
178     public void endDTD()
179     {
180     }
181     
182     public void endEntity(String JavaDoc name)
183     {
184     }
185     
186     public void startCDATA()
187     {
188         cs=true;
189     }
190     
191     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
192     {
193     }
194     
195     public void startEntity(String JavaDoc name)
196     {
197     }
198     
199     // ***********************************************************************
200
// * Public methods
201
// ***********************************************************************
202

203     public void setDocument(Document JavaDoc doc)
204     {
205         if (doc==null)
206             throw new NullPointerException JavaDoc("Couldn't create such an object from a null reference");
207         this.doc=doc;
208         nodeactu=doc;
209         cs=false;
210     }
211     
212     public void setElement(Element JavaDoc root)
213     {
214         if (root==null)
215             throw new NullPointerException JavaDoc("Couldn't create such an object from a null reference");
216         doc=root.getOwnerDocument();
217         nodeactu=root;
218         cs=false;
219     }
220     
221 }
222
Popular Tags