KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.xml.binding;
8
9 import org.xml.sax.ContentHandler JavaDoc;
10 import org.xml.sax.Locator JavaDoc;
11 import org.xml.sax.SAXException JavaDoc;
12 import org.xml.sax.Attributes JavaDoc;
13
14 import java.io.Writer JavaDoc;
15 import java.io.IOException JavaDoc;
16
17 /**
18  * org.xml.sax.ContentHandler implementation that serializes an instance of org.jboss.xml.binding.Content
19  * to a java.io.Writer.
20  *
21  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
22  * @version <tt>$Revision: 1.2.6.6 $</tt>
23  */

24 public class ContentWriter
25    implements ContentHandler JavaDoc
26 {
27    final boolean useIndent;
28    private String JavaDoc indent = " ";
29    private int depth = 0;
30    private boolean started = false;
31
32    private final Writer JavaDoc writer;
33
34    public ContentWriter(Writer JavaDoc writer, boolean indent)
35    {
36       this.writer = writer;
37       this.useIndent = indent;
38    }
39
40    public void setDocumentLocator(Locator JavaDoc locator)
41    {
42       throw new UnsupportedOperationException JavaDoc();
43    }
44
45    public void startDocument()
46       throws SAXException JavaDoc
47    {
48    }
49
50    public void endDocument()
51       throws SAXException JavaDoc
52    {
53    }
54
55    public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
56       throws SAXException JavaDoc
57    {
58       throw new UnsupportedOperationException JavaDoc();
59    }
60
61    public void endPrefixMapping(String JavaDoc prefix)
62       throws SAXException JavaDoc
63    {
64       throw new UnsupportedOperationException JavaDoc();
65    }
66
67    public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
68       throws SAXException JavaDoc
69    {
70       if(useIndent)
71       {
72          write(writer, '\n');
73          for(int j = 0; j < depth; ++j)
74          {
75             write(writer, indent);
76          }
77       }
78
79       if(!started)
80       {
81          started = true;
82       }
83
84       ++depth;
85
86       write(writer, '<');
87       write(writer, qName);
88
89       if(atts != null && atts.getLength() > 0)
90       {
91          for(int i = 0; i < atts.getLength(); ++i)
92          {
93             write(writer, ' ');
94             write(writer, atts.getQName(i));
95             write(writer, "=\"");
96             write(writer, atts.getValue(i));
97             write(writer, '\"');
98          }
99       }
100
101       if(namespaceURI != null && namespaceURI.length() > 1)
102       {
103          int colon = qName.indexOf(':');
104          /*
105          if(colon < 0)
106          {
107             throw new IllegalStateException(
108                "Namespace URI specified (" + namespaceURI + ") but no qName found in qualified name '" + qName
109             );
110          }
111          */

112
113          if(colon >= 0)
114          {
115             String JavaDoc prefix = qName.substring(0, colon);
116             if(useIndent)
117             {
118                write(writer, '\n');
119                for(int i = 0; i < depth + 1; ++i)
120                {
121                   write(writer, indent);
122                }
123             }
124             else
125             {
126                write(writer, ' ');
127             }
128
129             write(writer, "xmlns:");
130             write(writer, prefix);
131             write(writer, "=\"");
132             write(writer, namespaceURI);
133             write(writer, "\"");
134          }
135       }
136
137       write(writer, '>');
138    }
139
140    public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
141                           String JavaDoc qName)
142       throws SAXException JavaDoc
143    {
144       --depth;
145       if(!started)
146       {
147          if(useIndent)
148          {
149             write(writer, '\n');
150             for(int j = 0; j < depth; ++j)
151             {
152                write(writer, indent);
153             }
154          }
155       }
156       else
157       {
158          started = false;
159       }
160
161       write(writer, "</");
162       write(writer, qName);
163       write(writer, '>');
164    }
165
166    public void characters(char ch[], int start, int length)
167       throws SAXException JavaDoc
168    {
169       write(writer, ch, start, length);
170    }
171
172    public void ignorableWhitespace(char ch[], int start, int length)
173       throws SAXException JavaDoc
174    {
175       throw new UnsupportedOperationException JavaDoc();
176    }
177
178    public void processingInstruction(String JavaDoc target, String JavaDoc data)
179       throws SAXException JavaDoc
180    {
181       throw new UnsupportedOperationException JavaDoc();
182    }
183
184    public void skippedEntity(String JavaDoc name)
185       throws SAXException JavaDoc
186    {
187       throw new UnsupportedOperationException JavaDoc();
188    }
189
190    // Private
191

192    private static void write(Writer JavaDoc writer, String JavaDoc str) throws SAXException JavaDoc
193    {
194       try
195       {
196          writer.write(str);
197       }
198       catch(IOException JavaDoc e)
199       {
200          throw new SAXException JavaDoc("Writting failed: " + e.getMessage(), e);
201       }
202    }
203
204    private static void write(Writer JavaDoc writer, int ch) throws SAXException JavaDoc
205    {
206       try
207       {
208          writer.write(ch);
209       }
210       catch(IOException JavaDoc e)
211       {
212          throw new SAXException JavaDoc("Writting failed: " + e.getMessage(), e);
213       }
214    }
215
216    private static void write(Writer JavaDoc writer, char[] ch, int start, int length) throws SAXException JavaDoc
217    {
218       try
219       {
220          writer.write(ch, start, length);
221       }
222       catch(IOException JavaDoc e)
223       {
224          throw new SAXException JavaDoc("Writting failed: " + e.getMessage(), e);
225       }
226    }
227 }
228
Popular Tags