KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > binding > ContentWriter


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.xb.binding;
23
24 import java.io.IOException JavaDoc;
25 import java.io.Writer JavaDoc;
26 import org.xml.sax.Attributes JavaDoc;
27 import org.xml.sax.ContentHandler JavaDoc;
28 import org.xml.sax.Locator JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30
31 /**
32  * org.xml.sax.ContentHandler implementation that serializes an instance of org.jboss.xb.binding.Content
33  * to a java.io.Writer.
34  *
35  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
36  * @version <tt>$Revision: 1958 $</tt>
37  */

38 public class ContentWriter
39    implements ContentHandler JavaDoc
40 {
41    final boolean useIndent;
42    private String JavaDoc indent = " ";
43    private int depth = 0;
44    private boolean started = false;
45
46    private final Writer JavaDoc writer;
47
48    public ContentWriter(Writer JavaDoc writer, boolean indent)
49    {
50       this.writer = writer;
51       this.useIndent = indent;
52    }
53
54    public void setDocumentLocator(Locator JavaDoc locator)
55    {
56       throw new UnsupportedOperationException JavaDoc();
57    }
58
59    public void startDocument()
60       throws SAXException JavaDoc
61    {
62    }
63
64    public void endDocument()
65       throws SAXException JavaDoc
66    {
67    }
68
69    public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
70       throws SAXException JavaDoc
71    {
72       throw new UnsupportedOperationException JavaDoc();
73    }
74
75    public void endPrefixMapping(String JavaDoc prefix)
76       throws SAXException JavaDoc
77    {
78       throw new UnsupportedOperationException JavaDoc();
79    }
80
81    public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
82       throws SAXException JavaDoc
83    {
84       if(useIndent)
85       {
86          write(writer, '\n');
87          for(int j = 0; j < depth; ++j)
88          {
89             write(writer, indent);
90          }
91       }
92
93       if(!started)
94       {
95          started = true;
96       }
97
98       ++depth;
99
100       write(writer, '<');
101       write(writer, qName);
102
103       if(atts != null && atts.getLength() > 0)
104       {
105          for(int i = 0; i < atts.getLength(); ++i)
106          {
107             write(writer, ' ');
108             write(writer, atts.getQName(i));
109             write(writer, "=\"");
110             writeNormalized(writer, atts.getValue(i));
111             write(writer, '\"');
112          }
113       }
114
115       /*if(namespaceURI != null && namespaceURI.length() > 1)
116       {
117          int colon = qName.indexOf(':');
118          / *
119          if(colon < 0)
120          {
121             throw new IllegalStateException(
122                "Namespace URI specified (" + namespaceURI + ") but no qName found in qualified name '" + qName
123             );
124          }
125          * /
126
127          if(colon >= 0)
128          {
129             String prefix = qName.substring(0, colon);
130             if(useIndent)
131             {
132                write(writer, '\n');
133                for(int i = 0; i < depth + 1; ++i)
134                {
135                   write(writer, indent);
136                }
137             }
138             else
139             {
140                write(writer, ' ');
141             }
142
143             write(writer, "xmlns:");
144             write(writer, prefix);
145             write(writer, "=\"");
146             write(writer, namespaceURI);
147             write(writer, "\"");
148          }
149       }*/

150
151       write(writer, '>');
152    }
153
154    public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
155                           String JavaDoc qName)
156       throws SAXException JavaDoc
157    {
158       --depth;
159       if(!started)
160       {
161          if(useIndent)
162          {
163             write(writer, '\n');
164             for(int j = 0; j < depth; ++j)
165             {
166                write(writer, indent);
167             }
168          }
169       }
170       else
171       {
172          started = false;
173       }
174
175       write(writer, "</");
176       write(writer, qName);
177       write(writer, '>');
178    }
179
180    public void characters(char ch[], int start, int length)
181       throws SAXException JavaDoc
182    {
183       writeNormalized(writer, ch, start, length);
184    }
185
186    public void ignorableWhitespace(char ch[], int start, int length)
187       throws SAXException JavaDoc
188    {
189       throw new UnsupportedOperationException JavaDoc();
190    }
191
192    public void processingInstruction(String JavaDoc target, String JavaDoc data)
193       throws SAXException JavaDoc
194    {
195       throw new UnsupportedOperationException JavaDoc();
196    }
197
198    public void skippedEntity(String JavaDoc name)
199       throws SAXException JavaDoc
200    {
201       throw new UnsupportedOperationException JavaDoc();
202    }
203
204    // Private
205

206    private static void write(Writer JavaDoc writer, String JavaDoc str) throws SAXException JavaDoc
207    {
208       try
209       {
210          writer.write(str);
211       }
212       catch(IOException JavaDoc e)
213       {
214          throw new SAXException JavaDoc("Writting failed: " + e.getMessage(), e);
215       }
216    }
217
218    private static void writeNormalized(Writer JavaDoc writer, String JavaDoc str) throws SAXException JavaDoc
219    {
220       writeNormalized(writer, str.toCharArray(), 0, str.length());
221    }
222
223    private static void write(Writer JavaDoc writer, int ch) throws SAXException JavaDoc
224    {
225       try
226       {
227          writer.write(ch);
228       }
229       catch(IOException JavaDoc e)
230       {
231          throw new SAXException JavaDoc("Writting failed: " + e.getMessage(), e);
232       }
233    }
234
235    private static void writeNormalized(Writer JavaDoc writer, char[] ch, int start, int length) throws SAXException JavaDoc
236    {
237       try
238       {
239          int left = start;
240          int i = start;
241          while(i < start + length)
242          {
243             char c = ch[i++];
244             if(c == '<')
245             {
246                writer.write(ch, left, i - left - 1);
247                writer.write("&lt;");
248                left = i;
249             }
250             else if(c == '>')
251             {
252                writer.write(ch, left, i - left - 1);
253                writer.write("&gt;");
254                left = i;
255             }
256             else if(c == '&')
257             {
258                writer.write(ch, left, i - left - 1);
259                writer.write("&amp;");
260                left = i;
261             }
262             else if(c == '\'')
263             {
264                writer.write(ch, left, i - left - 1);
265                writer.write("&apos;");
266                left = i;
267             }
268             else if(c == '\"')
269             {
270                writer.write(ch, left, i - left - 1);
271                writer.write("&quot;");
272                left = i;
273             }
274          }
275
276          if(left < i)
277          {
278             writer.write(ch, left, i - left);
279          }
280       }
281       catch(IOException JavaDoc e)
282       {
283          throw new SAXException JavaDoc("Writting failed: " + e.getMessage(), e);
284       }
285    }
286 }
287
Popular Tags