KickJava   Java API By Example, From Geeks To Geeks.

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


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.StringWriter JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import org.jboss.logging.Logger;
29 import org.xml.sax.Attributes JavaDoc;
30 import org.xml.sax.ContentHandler JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32
33 /**
34  * An instance of this class represents XML content.
35  * It is populated on unmarshalling with org.jboss.xb.binding.ContentPopulator and used
36  * to implement content navigation in object model factories.
37  * And on marshalling, first, an instance of this class is created and then it
38  * is serialized into XML content with org.jboss.xb.binding.ContentWriter.
39  *
40  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
41  * @version <tt>$Revision: 1958 $</tt>
42  */

43 public class Content
44 {
45    private static Logger log = Logger.getLogger(Content.class);
46
47    private List JavaDoc content = new ArrayList JavaDoc();
48    private final boolean trace = log.isTraceEnabled();
49
50    // Public
51

52    public String JavaDoc getChildContent(String JavaDoc namespaceURI, String JavaDoc qName)
53    {
54       StartElement current = ((StartElement)content.get(index));
55
56       boolean lookingForStart = true;
57       StartElement start = null;
58       boolean childFound = false;
59       StringBuffer JavaDoc value = new StringBuffer JavaDoc();
60
61       int i = index + 1;
62       Object JavaDoc next = content.get(i++);
63       while(!current.isMyEnd(next))
64       {
65          if(lookingForStart)
66          {
67             if(next instanceof StartElement)
68             {
69                start = (StartElement)next;
70                lookingForStart = false;
71
72                if(qName.equals(start.qName) &&
73                   (namespaceURI == null ? start.namespaceURI == null : namespaceURI.equals(start.namespaceURI)))
74                {
75                   childFound = true;
76                }
77             }
78          }
79          else if(next instanceof EndElement)
80          {
81             if(start.isMyEnd(next))
82             {
83                if(childFound)
84                {
85                   break;
86                }
87                else
88                {
89                   lookingForStart = true;
90                }
91             }
92          }
93          else if(childFound && next instanceof Characters)
94          {
95             Characters chars = (Characters)next;
96             value.append(chars.ch, chars.start, chars.length);
97          }
98          next = content.get(i++);
99       }
100       return value.toString().trim();
101    }
102
103    public void handleContent(ContentHandler JavaDoc handler) throws SAXException JavaDoc
104    {
105       handler.startDocument();
106
107       for(Iterator JavaDoc i = content.iterator(); i.hasNext();)
108       {
109          Object JavaDoc item = i.next();
110          if(item instanceof StartElement)
111          {
112             StartElement start = (StartElement)item;
113             handler.startElement(start.namespaceURI, start.localName, start.qName, start.attrs);
114          }
115          else if(item instanceof EndElement)
116          {
117             EndElement end = (EndElement)item;
118             handler.endElement(end.namespaceURI, end.localName, end.qName);
119          }
120          else if(item instanceof Characters)
121          {
122             Characters ch = (Characters)item;
123             handler.characters(ch.ch, ch.start, ch.length);
124          }
125          else if(item instanceof StartPrefixMapping)
126          {
127 /*
128             if(trace)
129             {
130                StartPrefixMapping startPrefix = (StartPrefixMapping)item;
131                log.trace("start prefix mapping: " + startPrefix.prefix + "=" + startPrefix.uri);
132             }
133 */

134          }
135          else if(item instanceof EndPrefixMapping)
136          {
137 /*
138             if(trace)
139             {
140                EndPrefixMapping endPrefix = (EndPrefixMapping)item;
141                log.trace("end prefix mapping: " + endPrefix.prefix);
142             }
143 */

144          }
145          else
146          {
147             throw new IllegalStateException JavaDoc("Unexpected element type: " + item);
148          }
149       }
150
151       handler.endDocument();
152    }
153
154    public String JavaDoc toString()
155    {
156       StringWriter JavaDoc writer = new StringWriter JavaDoc();
157       try
158       {
159          ContentWriter contentWriter = new ContentWriter(writer, true);
160          handleContent(contentWriter);
161       }
162       catch(SAXException JavaDoc e)
163       {
164          writer.write(e.getMessage());
165       }
166       return writer.getBuffer().toString();
167    }
168
169    // Methods that populate the content
170

171    public void startDocument()
172    {
173       content.clear();
174    }
175
176    public void endDocument()
177    {
178    }
179
180    public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
181    {
182       StartPrefixMapping node = new StartPrefixMapping(prefix, uri);
183       content.add(node);
184    }
185
186    public void endPrefixMapping(String JavaDoc prefix)
187    {
188       EndPrefixMapping node = new EndPrefixMapping(prefix);
189       content.add(node);
190    }
191
192    public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
193    {
194       StartElement startElement = new StartElement(namespaceURI, localName, qName, atts);
195       content.add(startElement);
196
197       if(trace)
198       {
199          log.trace("startElement> uri=" + namespaceURI + ", local=" + localName + ", qn=" + qName + ", attrs=" + atts);
200       }
201    }
202
203    public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
204    {
205       EndElement endElement = new EndElement(namespaceURI, localName, qName);
206       content.add(endElement);
207
208       if(trace)
209       {
210          log.trace("endElement> uri=" + namespaceURI + ", local=" + localName + ", qn=" + qName);
211       }
212    }
213
214    public void characters(char[] ch, int start, int length)
215    {
216       Characters characters = new Characters(ch, start, length);
217       // ignore whitespace-only characters
218
if(characters.toString().trim().length() > 0)
219       {
220          content.add(characters);
221
222          if(trace)
223          {
224             log.trace("characters> " + characters);
225          }
226       }
227    }
228
229    // Methods that navigate through the content
230

231    private int index;
232
233    public void append(Content content)
234    {
235       for(Iterator JavaDoc i = content.content.iterator(); i.hasNext();)
236       {
237          this.content.add(i.next());
238       }
239    }
240
241    // Inner
242

243    private static interface Node
244    {
245    }
246
247    public static class Characters
248       implements Node
249    {
250       private final char[] ch;
251       private final int start;
252       private final int length;
253
254       public Characters(char[] ch, int start, int length)
255       {
256          /*
257          this.ch = ch;
258          this.start = start;
259          this.length = length;
260          */

261          this.ch = new char[length];
262          System.arraycopy(ch, start, this.ch, 0, length);
263          this.start = 0;
264          this.length = length;
265       }
266
267       public String JavaDoc toString()
268       {
269          return String.valueOf(ch, start, length);
270       }
271    }
272
273    public static class EndElement
274       implements Node
275    {
276       private final String JavaDoc namespaceURI;
277       private final String JavaDoc localName;
278       private final String JavaDoc qName;
279
280       public EndElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
281       {
282          this.namespaceURI = namespaceURI;
283          this.localName = localName;
284          this.qName = qName;
285       }
286
287       public String JavaDoc toString()
288       {
289          return '[' + namespaceURI + ',' + localName + ',' + qName + ']';
290       }
291    }
292
293    public static class StartElement
294       implements Node
295    {
296       private final String JavaDoc namespaceURI;
297       private final String JavaDoc localName;
298       private final String JavaDoc qName;
299       private final Attributes JavaDoc attrs;
300
301       public StartElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attrs)
302       {
303          this.namespaceURI = namespaceURI;
304          this.localName = localName;
305          this.qName = qName;
306          this.attrs = new AttributesImpl(attrs);
307       }
308
309       public boolean isMyEnd(Object JavaDoc element)
310       {
311          boolean itis = false;
312          if(element instanceof EndElement)
313          {
314             EndElement end = (EndElement)element;
315             itis = (namespaceURI == null ? end.namespaceURI == null : namespaceURI.equals(end.namespaceURI))
316                && qName.equals(end.qName);
317          }
318          return itis;
319       }
320
321       public String JavaDoc toString()
322       {
323          return '[' + namespaceURI + ',' + localName + ',' + qName + ']';
324       }
325    }
326
327    public static class StartPrefixMapping
328       implements Node
329    {
330       public final String JavaDoc prefix;
331       public final String JavaDoc uri;
332
333       public StartPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
334       {
335          this.prefix = prefix;
336          this.uri = uri;
337       }
338
339       public void read(ObjectModelBuilder builder)
340       {
341          builder.startPrefixMapping(prefix, uri);
342       }
343
344       public boolean equals(Object JavaDoc o)
345       {
346          if(this == o)
347          {
348             return true;
349          }
350          if(!(o instanceof StartPrefixMapping))
351          {
352             return false;
353          }
354
355          final StartPrefixMapping startPrefixMapping = (StartPrefixMapping)o;
356
357          if(prefix != null ? !prefix.equals(startPrefixMapping.prefix) : startPrefixMapping.prefix != null)
358          {
359             return false;
360          }
361          if(uri != null ? !uri.equals(startPrefixMapping.uri) : startPrefixMapping.uri != null)
362          {
363             return false;
364          }
365
366          return true;
367       }
368
369       public int hashCode()
370       {
371          int result;
372          result = (prefix != null ? prefix.hashCode() : 0);
373          result = 29 * result + (uri != null ? uri.hashCode() : 0);
374          return result;
375       }
376    }
377
378    public static class EndPrefixMapping
379       implements Node
380    {
381       public final String JavaDoc prefix;
382
383       public EndPrefixMapping(String JavaDoc prefix)
384       {
385          this.prefix = prefix;
386       }
387
388       public void read(ObjectModelBuilder builder)
389       {
390          builder.endPrefixMapping(prefix);
391       }
392    }
393 }
394
Popular Tags