KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml2 > SAXBuilder


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xml2;
30
31 import com.caucho.util.CharBuffer;
32
33 import org.xml.sax.ContentHandler JavaDoc;
34 import org.xml.sax.Locator JavaDoc;
35 import org.xml.sax.SAXException JavaDoc;
36 import org.xml.sax.XMLReader JavaDoc;
37
38 import javax.xml.namespace.QName JavaDoc;
39 import java.io.IOException JavaDoc;
40
41 /**
42  * XMLWriter to create a SAX events.
43  */

44 public class SAXBuilder implements XMLWriter, Locator JavaDoc {
45   private XMLReader JavaDoc _saxReader;
46   private ContentHandler JavaDoc _contentHandler;
47
48   private QAttributes _attributes;
49   private CharBuffer _text;
50   private boolean _escapeText;
51
52   private boolean _hasElement;
53
54   private String JavaDoc _elementUri;
55   private String JavaDoc _elementLocalName;
56   private String JavaDoc _elementQName;
57
58   private String JavaDoc _filename;
59   private int _line;
60
61   private String JavaDoc _elementSystemId;
62   private int _elementLine;
63
64   private Locator JavaDoc _locator;
65
66   public SAXBuilder()
67   {
68     _text = new CharBuffer();
69     _attributes = new QAttributes();
70   }
71
72   public SAXBuilder(XMLReader JavaDoc saxReader)
73   {
74     this();
75
76     init(saxReader);
77   }
78
79   public void init(XMLReader JavaDoc saxReader)
80   {
81     _saxReader = saxReader;
82
83     _contentHandler = saxReader.getContentHandler();
84
85     init();
86   }
87
88   public void init()
89   {
90     _text.clear();
91     _attributes.clear();
92     _hasElement = false;
93
94     _filename = null;
95     _line = 0;
96   }
97
98   /**
99    * Sets the SAX content handler.
100    */

101   public void setContentHandler(ContentHandler JavaDoc handler)
102   {
103     _contentHandler = handler;
104   }
105
106   public void setDocumentLocator(Locator JavaDoc locator)
107   {
108     _locator = locator;
109   }
110
111   public void startDocument()
112     throws IOException JavaDoc, SAXException JavaDoc
113   {
114     _contentHandler.setDocumentLocator(this);
115     _contentHandler.startDocument();
116   }
117
118   public void endDocument()
119     throws IOException JavaDoc, SAXException JavaDoc
120   {
121     pop();
122
123     _contentHandler.endDocument();
124   }
125   
126   public void setLocation(String JavaDoc filename, int line, int column)
127   {
128     _filename = filename;
129     _line = line;
130   }
131
132   /**
133    * Returns the current filename.
134    */

135   public String JavaDoc getSystemId()
136   {
137     if (_elementSystemId != null)
138       return _elementSystemId;
139     else if (_locator != null)
140       return _locator.getSystemId();
141     else
142       return _filename;
143   }
144
145   /**
146    * Don't really have a public id (?).
147    */

148   public String JavaDoc getPublicId()
149   {
150     if (_locator != null)
151       return _locator.getPublicId();
152     else
153       return null;
154   }
155   
156   /**
157    * Returns the current line.
158    */

159   public int getLineNumber()
160   {
161     if (_elementSystemId != null)
162       return _elementLine;
163     else if (_locator != null)
164       return _locator.getLineNumber();
165     else
166       return 0;
167   }
168
169   /**
170    * The column number is always 0.
171    */

172   public int getColumnNumber()
173   {
174     if (_locator != null)
175       return _locator.getColumnNumber();
176     else
177       return 0;
178   }
179
180   /**
181    * Starts the building of an element.
182    *
183    * @param uri the element's namespace URI
184    * @param localName the element's local name
185    * @param qName the element's fully qualified name
186    */

187   public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
188     throws IOException JavaDoc, SAXException JavaDoc
189   {
190     pop();
191
192     _elementUri = uri;
193     _elementLocalName = localName;
194     _elementQName = qName;
195     _hasElement = true;
196
197     if (_locator != null) {
198       _elementSystemId = _locator.getSystemId();
199       _elementLine = _locator.getLineNumber();
200     }
201   }
202
203   public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
204     throws IOException JavaDoc, SAXException JavaDoc
205   {
206     _contentHandler.startPrefixMapping(prefix, uri);
207   }
208
209   public void endPrefixMapping(String JavaDoc prefix)
210     throws IOException JavaDoc, SAXException JavaDoc
211   {
212     _contentHandler.endPrefixMapping(prefix);
213   }
214
215   public void attribute(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
216                         String JavaDoc value)
217     throws IOException JavaDoc, SAXException JavaDoc
218   {
219     QName JavaDoc name = new QName JavaDoc(qName, uri);
220     
221     _attributes.add(name, value);
222   }
223
224   public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
225     throws IOException JavaDoc, SAXException JavaDoc
226   {
227     pop();
228     
229     if (uri != null)
230       _contentHandler.endElement(uri, localName, qName);
231     else
232       _contentHandler.endElement("", null, qName);
233   }
234
235   public void processingInstruction(String JavaDoc name, String JavaDoc data)
236     throws IOException JavaDoc, SAXException JavaDoc
237   {
238     pop();
239
240     _contentHandler.processingInstruction(name, data);
241   }
242   
243   public void comment(String JavaDoc data)
244     throws IOException JavaDoc, SAXException JavaDoc
245   {
246     pop();
247   }
248
249   public boolean getEscapeText()
250   {
251     return _escapeText;
252   }
253
254   public void setEscapeText(boolean isEscaped)
255   {
256     _escapeText = isEscaped;
257   }
258
259   public void text(String JavaDoc text)
260     throws IOException JavaDoc, SAXException JavaDoc
261   {
262     popElement();
263     
264     _text.append(text);
265   }
266
267   public void text(char []buffer, int offset, int length)
268     throws IOException JavaDoc, SAXException JavaDoc
269   {
270     popElement();
271     
272     _text.append(buffer, offset, length);
273   }
274
275   public void cdata(String JavaDoc text)
276     throws IOException JavaDoc, SAXException JavaDoc
277   {
278     popElement();
279     
280     text(text);
281   }
282
283   public void cdata(char []buffer, int offset, int length)
284     throws IOException JavaDoc, SAXException JavaDoc
285   {
286     popElement();
287     
288     text(buffer, offset, length);
289   }
290
291   private void pop()
292     throws IOException JavaDoc, SAXException JavaDoc
293   {
294     popElement();
295     
296     if (_text.length() == 0)
297       return;
298
299     _contentHandler.characters(_text.getBuffer(), 0, _text.getLength());
300     
301     _text.clear();
302   }
303
304   /**
305    * Completes the open element processing.
306    */

307   private void popElement()
308     throws IOException JavaDoc, SAXException JavaDoc
309   {
310     if (_hasElement) {
311       if (_elementUri == null)
312     _contentHandler.startElement("", null, _elementQName, _attributes);
313       else
314     _contentHandler.startElement(_elementUri, _elementLocalName,
315                      _elementQName, _attributes);
316
317       _hasElement = false;
318       _elementSystemId = null;
319       _attributes.clear();
320     }
321   }
322 }
323
Popular Tags