KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml > 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.xml;
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 java.io.IOException JavaDoc;
39
40 /**
41  * XMLWriter to create a SAX events.
42  */

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

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

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

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

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

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

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

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