KickJava   Java API By Example, From Geeks To Geeks.

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


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.Attributes JavaDoc;
10 import org.xml.sax.ContentHandler JavaDoc;
11 import org.xml.sax.SAXException JavaDoc;
12 import org.jboss.logging.Logger;
13
14 import java.util.LinkedList JavaDoc;
15 import java.io.StringWriter JavaDoc;
16
17 /**
18  * An instance of this class represents XML content.
19  * It is populated on unmarshalling with org.jboss.xml.binding.ContentPopulator and used
20  * to implement content navigation in object model factories.
21  * And on marshalling, first, an instance of this class is created and then it
22  * is serialized into XML content with org.jboss.xml.binding.ContentWriter.
23  *
24  * @version <tt>$Revision: 1.7.2.5 $</tt>
25  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
26  */

27 public class Content
28 {
29    private static Logger log = Logger.getLogger(Content.class);
30
31    private LinkedList JavaDoc content = new LinkedList JavaDoc();
32
33    // Public
34

35    public String JavaDoc getChildContent(String JavaDoc namespaceURI, String JavaDoc qName)
36    {
37       StartElement current = ((StartElement)content.get(index));
38
39       boolean lookingForStart = true;
40       StartElement start = null;
41       boolean childFound = false;
42       StringBuffer JavaDoc value = new StringBuffer JavaDoc();
43
44       int i = index + 1;
45       Object JavaDoc next = content.get(i++);
46       while(!current.isMyEnd(next))
47       {
48          if(lookingForStart)
49          {
50             if(next instanceof StartElement)
51             {
52                start = (StartElement)next;
53                lookingForStart = false;
54
55                if(qName.equals(start.qName) &&
56                   (namespaceURI == null ? start.namespaceURI == null : namespaceURI.equals(start.namespaceURI)))
57                {
58                   childFound = true;
59                }
60             }
61          }
62          else if(next instanceof EndElement)
63          {
64             if(start.isMyEnd(next))
65             {
66                if(childFound)
67                {
68                   break;
69                }
70                else
71                {
72                   lookingForStart = true;
73                }
74             }
75          }
76          else if(childFound && next instanceof Characters)
77          {
78             Characters chars = (Characters)next;
79             value.append(chars.ch, chars.start, chars.length);
80          }
81          next = content.get(i++);
82       }
83       return value.toString().trim();
84    }
85
86    public void handleContent(ContentHandler JavaDoc handler) throws SAXException JavaDoc
87    {
88       handler.startDocument();
89
90       for(int i = 0; i < content.size(); ++i)
91       {
92          Object JavaDoc item = content.get(i);
93          if(item instanceof StartElement)
94          {
95             StartElement start = (StartElement)item;
96             handler.startElement(start.namespaceURI, start.localName, start.qName, start.attrs);
97          }
98          else if(item instanceof EndElement)
99          {
100             EndElement end = (EndElement)item;
101             handler.endElement(end.namespaceURI, end.localName, end.qName);
102          }
103          else if(item instanceof Characters)
104          {
105             Characters ch = (Characters)item;
106             handler.characters(ch.ch, ch.start, ch.length);
107          }
108          else
109          {
110             throw new IllegalStateException JavaDoc("Unexpected element type: " + item);
111          }
112       }
113
114       handler.endDocument();
115    }
116
117    public String JavaDoc toString()
118    {
119       StringWriter JavaDoc writer = new StringWriter JavaDoc();
120       try
121       {
122          ContentWriter contentWriter = new ContentWriter(writer, true);
123          handleContent(contentWriter);
124       }
125       catch(SAXException JavaDoc e)
126       {
127          writer.write(e.getMessage());
128       }
129       return writer.getBuffer().toString();
130    }
131
132    // Methods that populate the content
133

134    public void startDocument()
135    {
136       content.clear();
137    }
138
139    public void endDocument()
140    {
141    }
142
143    public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
144    {
145       StartPrefixMapping node = new StartPrefixMapping(prefix, uri);
146       content.addLast(node);
147    }
148
149    public void endPrefixMapping(String JavaDoc prefix)
150    {
151       EndPrefixMapping node = new EndPrefixMapping(prefix);
152       content.addLast(node);
153    }
154
155    public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
156    {
157       StartElement startElement = new StartElement(namespaceURI, localName, qName, atts);
158       content.addLast(startElement);
159
160       if(log.isTraceEnabled())
161       {
162          log.trace("startElement> uri=" + namespaceURI + ", local=" + localName + ", qn=" + qName + ", attrs=" + atts);
163       }
164    }
165
166    public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
167    {
168       EndElement endElement = new EndElement(namespaceURI, localName, qName);
169       content.addLast(endElement);
170
171       if(log.isTraceEnabled())
172       {
173          log.trace("endElement> uri=" + namespaceURI + ", local=" + localName + ", qn=" + qName);
174       }
175    }
176
177    public void characters(char[] ch, int start, int length)
178    {
179       Characters characters = new Characters(ch, start, length);
180       // ignore whitespace-only characters
181
if (characters.toString().trim().length() > 0)
182       {
183          content.addLast(characters);
184
185          if(log.isTraceEnabled())
186          {
187             log.trace("characters> " + characters);
188          }
189       }
190    }
191
192    // Methods that navigate through the content
193

194    private int index;
195
196    public void append(Content content)
197    {
198       for(int i = 0; i < content.content.size(); ++i)
199       {
200          this.content.addLast(content.content.get(i));
201       }
202    }
203
204    // Inner
205

206    private static interface Node
207    {
208    }
209
210    public static class Characters
211       implements Node
212    {
213       private final char[] ch;
214       private final int start;
215       private final int length;
216
217       public Characters(char[] ch, int start, int length)
218       {
219          /*
220          this.ch = ch;
221          this.start = start;
222          this.length = length;
223          */

224          this.ch = new char[length];
225          System.arraycopy(ch, start, this.ch, 0, length);
226          this.start = 0;
227          this.length = length;
228       }
229
230       public String JavaDoc toString()
231       {
232          return String.valueOf(ch, start, length);
233       }
234    }
235
236    public static class EndElement
237       implements Node
238    {
239       private final String JavaDoc namespaceURI;
240       private final String JavaDoc localName;
241       private final String JavaDoc qName;
242
243       public EndElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
244       {
245          this.namespaceURI = namespaceURI;
246          this.localName = localName;
247          this.qName = qName;
248       }
249
250       public String JavaDoc toString()
251       {
252          return '[' + namespaceURI + ',' + localName + ',' + qName + ']';
253       }
254    }
255
256    public static class StartElement
257       implements Node
258    {
259       private final String JavaDoc namespaceURI;
260       private final String JavaDoc localName;
261       private final String JavaDoc qName;
262       private final Attributes JavaDoc attrs;
263
264       public StartElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attrs)
265       {
266          this.namespaceURI = namespaceURI;
267          this.localName = localName;
268          this.qName = qName;
269          this.attrs = new AttributesImpl(attrs);
270       }
271
272       public boolean isMyEnd(Object JavaDoc element)
273       {
274          boolean itis = false;
275          if(element instanceof EndElement)
276          {
277             EndElement end = (EndElement)element;
278             itis = (namespaceURI == null ? end.namespaceURI == null : namespaceURI.equals(end.namespaceURI))
279                && qName.equals(end.qName);
280          }
281          return itis;
282       }
283
284       public String JavaDoc toString()
285       {
286          return '[' + namespaceURI + ',' + localName + ',' + qName + ']';
287       }
288    }
289
290    public static class StartPrefixMapping implements Node
291    {
292       public final String JavaDoc prefix;
293       public final String JavaDoc uri;
294
295       public StartPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
296       {
297          this.prefix = prefix;
298          this.uri = uri;
299       }
300
301       public void read(ObjectModelBuilder builder)
302       {
303          builder.startPrefixMapping(prefix, uri);
304       }
305
306       public boolean equals(Object JavaDoc o)
307       {
308          if(this == o) return true;
309          if(!(o instanceof StartPrefixMapping)) return false;
310
311          final StartPrefixMapping startPrefixMapping = (StartPrefixMapping) o;
312
313          if(prefix != null ? !prefix.equals(startPrefixMapping.prefix) : startPrefixMapping.prefix != null) return false;
314          if(uri != null ? !uri.equals(startPrefixMapping.uri) : startPrefixMapping.uri != null) return false;
315
316          return true;
317       }
318
319       public int hashCode()
320       {
321          int result;
322          result = (prefix != null ? prefix.hashCode() : 0);
323          result = 29 * result + (uri != null ? uri.hashCode() : 0);
324          return result;
325       }
326    }
327
328    public static class EndPrefixMapping implements Node
329    {
330       public final String JavaDoc prefix;
331
332       public EndPrefixMapping(String JavaDoc prefix)
333       {
334          this.prefix = prefix;
335       }
336
337       public void read(ObjectModelBuilder builder)
338       {
339          builder.endPrefixMapping(prefix);
340       }
341    }
342 }
343
Popular Tags