KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis.message;
17
18 import org.apache.axis.components.logger.LogFactory;
19 import org.apache.axis.encoding.SerializationContext;
20 import org.apache.axis.utils.XMLUtils;
21 import org.apache.commons.logging.Log;
22 import org.xml.sax.Attributes JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
24 import org.xml.sax.ext.LexicalHandler JavaDoc;
25 import org.xml.sax.helpers.DefaultHandler JavaDoc;
26
27 import javax.xml.namespace.QName JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 public class SAXOutputter extends DefaultHandler JavaDoc implements LexicalHandler JavaDoc
31 {
32     protected static Log log =
33         LogFactory.getLog(SAXOutputter.class.getName());
34     
35     SerializationContext context;
36     boolean isCDATA = false;
37     
38     public SAXOutputter(SerializationContext context)
39     {
40         this.context = context;
41     }
42     
43     public void startDocument() throws SAXException JavaDoc {
44         context.setSendDecl(true);
45     }
46     
47     public void endDocument() throws SAXException JavaDoc {
48         if (log.isDebugEnabled()) {
49             log.debug("SAXOutputter.endDocument");
50         }
51     }
52     
53     public void startPrefixMapping(String JavaDoc p1, String JavaDoc p2) throws SAXException JavaDoc {
54         context.registerPrefixForURI(p1,p2);
55     }
56     
57     public void endPrefixMapping(String JavaDoc p1) throws SAXException JavaDoc {
58         // !!!
59
}
60     
61     public void characters(char[] p1, int p2, int p3) throws SAXException JavaDoc {
62         if (log.isDebugEnabled()) {
63             log.debug("SAXOutputter.characters ['" + new String JavaDoc(p1, p2, p3) + "']");
64         }
65         try {
66             if(!isCDATA) {
67                 context.writeChars(p1, p2, p3);
68             } else {
69                 context.writeString(new String JavaDoc(p1, p2, p3));
70             }
71         } catch (IOException JavaDoc e) {
72             throw new SAXException JavaDoc(e);
73         }
74     }
75     
76     public void ignorableWhitespace(char[] p1, int p2, int p3)
77         throws SAXException JavaDoc
78     {
79         try {
80             context.writeChars(p1, p2, p3);
81         } catch (IOException JavaDoc e) {
82             throw new SAXException JavaDoc(e);
83         }
84     }
85  
86     public void skippedEntity(String JavaDoc p1) throws SAXException JavaDoc {
87     }
88     
89     public void startElement(String JavaDoc namespace, String JavaDoc localName,
90                              String JavaDoc qName, Attributes JavaDoc attributes)
91         throws SAXException JavaDoc
92     {
93         if (log.isDebugEnabled()) {
94             log.debug("SAXOutputter.startElement ['" + namespace + "' " +
95                            localName + "]");
96         }
97
98         try {
99             context.startElement(new QName JavaDoc(namespace,localName), attributes);
100         } catch (IOException JavaDoc e) {
101             throw new SAXException JavaDoc(e);
102         }
103     }
104     
105     public void endElement(String JavaDoc namespace, String JavaDoc localName, String JavaDoc qName)
106         throws SAXException JavaDoc
107     {
108         if (log.isDebugEnabled()) {
109             log.debug("SAXOutputter.endElement ['" + namespace + "' " +
110                            localName + "]");
111         }
112         
113         try {
114             context.endElement();
115         } catch (IOException JavaDoc e) {
116             throw new SAXException JavaDoc(e);
117         }
118     }
119
120     public void startDTD(java.lang.String JavaDoc name,
121                      java.lang.String JavaDoc publicId,
122                      java.lang.String JavaDoc systemId)
123               throws SAXException JavaDoc
124     {
125     }
126     
127     public void endDTD()
128             throws SAXException JavaDoc
129     {
130     }
131     
132     public void startEntity(java.lang.String JavaDoc name)
133                  throws SAXException JavaDoc
134     {
135     }
136     
137     public void endEntity(java.lang.String JavaDoc name)
138                throws SAXException JavaDoc
139     {
140     }
141     
142     public void startCDATA()
143                 throws SAXException JavaDoc
144     {
145         try {
146             isCDATA = true;
147             context.writeString("<![CDATA[");
148         } catch (IOException JavaDoc e) {
149             throw new SAXException JavaDoc(e);
150         }
151     }
152     
153     public void endCDATA()
154               throws SAXException JavaDoc
155     {
156         try {
157             isCDATA = false;
158             context.writeString("]]>");
159         } catch (IOException JavaDoc e) {
160             throw new SAXException JavaDoc(e);
161         }
162     }
163     
164     public void comment(char[] ch,
165                     int start,
166                     int length)
167              throws SAXException JavaDoc
168     {
169         if (log.isDebugEnabled()) {
170             log.debug("SAXOutputter.comment ['" + new String JavaDoc(ch, start, length) + "']");
171         }
172         try {
173             context.writeString("<!--");
174             context.writeChars(ch, start, length);
175             context.writeString("-->");
176         } catch (IOException JavaDoc e) {
177             throw new SAXException JavaDoc(e);
178         }
179     }
180 }
181
Popular Tags