KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > message > SAXOutputter


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.message;
56
57 import org.jboss.axis.encoding.SerializationContext;
58 import org.jboss.logging.Logger;
59 import org.xml.sax.Attributes JavaDoc;
60 import org.xml.sax.SAXException JavaDoc;
61 import org.xml.sax.ext.LexicalHandler JavaDoc;
62 import org.xml.sax.helpers.DefaultHandler JavaDoc;
63
64 import javax.xml.namespace.QName JavaDoc;
65 import java.io.IOException JavaDoc;
66
67 public class SAXOutputter extends DefaultHandler JavaDoc implements LexicalHandler JavaDoc
68 {
69    private static Logger log = Logger.getLogger(SAXOutputter.class.getName());
70
71    SerializationContext context;
72    boolean isCDATA = false;
73
74    public SAXOutputter(SerializationContext context)
75    {
76       this.context = context;
77    }
78
79    public void startDocument() throws SAXException JavaDoc
80    {
81       if (log.isDebugEnabled())
82       {
83          log.debug("SAXOutputter.startDocument");
84       }
85
86       try
87       {
88          context.startDocument();
89       }
90       catch (IOException JavaDoc e)
91       {
92          throw new SAXException JavaDoc(e);
93       }
94    }
95
96    public void endDocument() throws SAXException JavaDoc
97    {
98       if (log.isDebugEnabled())
99       {
100          log.debug("SAXOutputter.endDocument");
101       }
102    }
103
104    public void startPrefixMapping(String JavaDoc p1, String JavaDoc p2) throws SAXException JavaDoc
105    {
106       context.registerPrefixForURI(p1, p2);
107    }
108
109    public void endPrefixMapping(String JavaDoc p1) throws SAXException JavaDoc
110    {
111       // !!!
112
}
113
114    public void characters(char[] p1, int p2, int p3) throws SAXException JavaDoc
115    {
116       if (log.isDebugEnabled())
117       {
118          log.debug("SAXOutputter.characters ['" + new String JavaDoc(p1, p2, p3) + "']");
119       }
120       try
121       {
122          if (!isCDATA)
123          {
124             context.writeChars(p1, p2, p3);
125          }
126          else
127          {
128             context.writeString(new String JavaDoc(p1, p2, p3));
129          }
130       }
131       catch (IOException JavaDoc e)
132       {
133          throw new SAXException JavaDoc(e);
134       }
135    }
136
137    public void ignorableWhitespace(char[] p1, int p2, int p3)
138            throws SAXException JavaDoc
139    {
140       try
141       {
142          context.writeChars(p1, p2, p3);
143       }
144       catch (IOException JavaDoc e)
145       {
146          throw new SAXException JavaDoc(e);
147       }
148    }
149
150    public void skippedEntity(String JavaDoc p1) throws SAXException JavaDoc
151    {
152    }
153
154    public void startElement(String JavaDoc namespace, String JavaDoc localName,
155                             String JavaDoc qName, Attributes JavaDoc attributes)
156            throws SAXException JavaDoc
157    {
158       if (log.isDebugEnabled())
159       {
160          log.debug("SAXOutputter.startElement ['" + namespace + "' " +
161                  localName + "]");
162       }
163
164       try
165       {
166          context.startElement(new QName JavaDoc(namespace, localName), attributes);
167       }
168       catch (IOException JavaDoc e)
169       {
170          throw new SAXException JavaDoc(e);
171       }
172    }
173
174    public void endElement(String JavaDoc namespace, String JavaDoc localName, String JavaDoc qName)
175            throws SAXException JavaDoc
176    {
177       if (log.isDebugEnabled())
178       {
179          log.debug("SAXOutputter.endElement ['" + namespace + "' " +
180                  localName + "]");
181       }
182
183       try
184       {
185          context.endElement();
186       }
187       catch (IOException JavaDoc e)
188       {
189          throw new SAXException JavaDoc(e);
190       }
191    }
192
193    public void startDTD(java.lang.String JavaDoc name,
194                         java.lang.String JavaDoc publicId,
195                         java.lang.String JavaDoc systemId)
196            throws SAXException JavaDoc
197    {
198    }
199
200    public void endDTD()
201            throws SAXException JavaDoc
202    {
203    }
204
205    public void startEntity(java.lang.String JavaDoc name)
206            throws SAXException JavaDoc
207    {
208    }
209
210    public void endEntity(java.lang.String JavaDoc name)
211            throws SAXException JavaDoc
212    {
213    }
214
215    public void startCDATA()
216            throws SAXException JavaDoc
217    {
218       try
219       {
220          isCDATA = true;
221          context.writeString("<![CDATA[");
222       }
223       catch (IOException JavaDoc e)
224       {
225          throw new SAXException JavaDoc(e);
226       }
227    }
228
229    public void endCDATA()
230            throws SAXException JavaDoc
231    {
232       try
233       {
234          isCDATA = false;
235          context.writeString("]]>");
236       }
237       catch (IOException JavaDoc e)
238       {
239          throw new SAXException JavaDoc(e);
240       }
241    }
242
243    public void comment(char[] ch,
244                        int start,
245                        int length)
246            throws SAXException JavaDoc
247    {
248       if (log.isDebugEnabled())
249       {
250          log.debug("SAXOutputter.comment ['" + new String JavaDoc(ch, start, length) + "']");
251       }
252       try
253       {
254          context.writeString("<!--");
255          context.writeChars(ch, start, length);
256          context.writeString("-->");
257       }
258       catch (IOException JavaDoc e)
259       {
260          throw new SAXException JavaDoc(e);
261       }
262    }
263 }
264
Popular Tags