KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > util > Dom2Sax


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.util;
23
24 import java.util.List JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import org.jboss.xb.binding.Constants;
28 import org.jboss.xb.binding.NamespaceRegistry;
29 import org.w3c.dom.Attr JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.NamedNodeMap JavaDoc;
32 import org.w3c.dom.NodeList JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.xml.sax.ContentHandler JavaDoc;
35 import org.xml.sax.SAXException JavaDoc;
36 import org.xml.sax.helpers.AttributesImpl JavaDoc;
37
38 /**
39  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
40  * @version <tt>$Revision: 1958 $</tt>
41  */

42 public class Dom2Sax
43 {
44    public static void dom2sax(Element JavaDoc e, ContentHandler JavaDoc ch) throws SAXException JavaDoc
45    {
46       if(e == null)
47       {
48          throw new IllegalArgumentException JavaDoc("The element argument must not be null!");
49       }
50       
51       ch.startDocument();
52
53       process(e, ch, new NamespaceRegistry());
54
55       ch.endDocument();
56    }
57
58    private static void process(Element JavaDoc e, ContentHandler JavaDoc ch, NamespaceRegistry nsReg) throws SAXException JavaDoc
59    {
60       AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
61
62       List JavaDoc startedPrefixes = Collections.EMPTY_LIST;
63
64       NamedNodeMap JavaDoc domAttrs = e.getAttributes();
65       if(domAttrs != null && domAttrs.getLength() > 0)
66       {
67          // run through xmlns first to declare all the namespaces
68
for(int i = 0; i < domAttrs.getLength(); ++i)
69          {
70             Attr JavaDoc attr = (Attr JavaDoc)domAttrs.item(i);
71             String JavaDoc attrNs = attr.getNamespaceURI();
72             String JavaDoc attrLocal = attr.getLocalName();
73             if(attrLocal == null)
74             {
75                attrLocal = attr.getNodeName();
76             }
77
78             if(attrNs != null && isXmlns(attrNs))
79             {
80                String JavaDoc prefix;
81                String JavaDoc attrPrefix;
82                if("xmlns".equals(attrLocal))
83                {
84                   prefix = "";
85                   attrPrefix = "";
86                }
87                else
88                {
89                   prefix = attrLocal;
90                   attrPrefix = "xmlns";
91                }
92
93                String JavaDoc attrVal = attr.getValue();
94                nsReg.addPrefixMapping(prefix, attrVal);
95                ch.startPrefixMapping(prefix, attrVal);
96                startedPrefixes = add(startedPrefixes, prefix);
97
98                attrs.addAttribute(attrNs, attrLocal, buildQName(attrPrefix, attrLocal), null, attrVal);
99             }
100          }
101
102          for(int i = 0; i < domAttrs.getLength(); ++i)
103          {
104             Attr JavaDoc attr = (Attr JavaDoc)domAttrs.item(i);
105             String JavaDoc attrNs = attr.getNamespaceURI();
106             String JavaDoc attrLocal = attr.getLocalName();
107             if(attrLocal == null)
108             {
109                attrLocal = attr.getNodeName();
110             }
111
112             if(attrNs == null)
113             {
114                attrNs = "";
115             }
116
117             if(!isXmlns(attrNs))
118             {
119                String JavaDoc prefix = nsReg.getPrefix(attrNs);
120                if(prefix == null && attrNs.length() > 0)
121                {
122                   prefix = attrLocal + "_ns";
123                   nsReg.addPrefixMapping(prefix, attrNs);
124                   ch.startPrefixMapping(prefix, attrNs);
125                   startedPrefixes = add(startedPrefixes, prefix);
126                   attrs.addAttribute(Constants.NS_XML_SCHEMA, prefix, "xmlns:" + prefix, null, attrNs);
127                }
128
129                attrs.addAttribute(attrNs, attrLocal, buildQName(prefix, attrLocal), null, attr.getValue());
130             }
131          }
132       }
133
134       String JavaDoc localName = e.getLocalName();
135       if(localName == null)
136       {
137          localName = e.getNodeName();
138       }
139
140       String JavaDoc ns = e.getNamespaceURI();
141       if(ns == null)
142       {
143          ns = "";
144       }
145
146       String JavaDoc prefix = nsReg.getPrefix(ns);
147       if(prefix == null && ns.length() > 0)
148       {
149          prefix = localName + "_ns";
150          nsReg.addPrefixMapping(prefix, ns);
151          ch.startPrefixMapping(prefix, ns);
152          startedPrefixes = add(startedPrefixes, prefix);
153          attrs.addAttribute(Constants.NS_XML_SCHEMA, prefix, "xmlns:" + prefix, null, ns);
154       }
155
156       String JavaDoc qName = buildQName(prefix, localName);
157       ch.startElement(ns, localName, qName, attrs);
158
159       NodeList JavaDoc childNodes = e.getChildNodes();
160       if(childNodes != null && childNodes.getLength() > 0)
161       {
162          for(int i = 0; i < childNodes.getLength(); ++i)
163          {
164             Node JavaDoc node = childNodes.item(i);
165             switch(node.getNodeType())
166             {
167                case Node.ELEMENT_NODE:
168                   process((Element JavaDoc)node, ch, nsReg);
169                   break;
170                case Node.CDATA_SECTION_NODE:
171                case Node.TEXT_NODE:
172                   String JavaDoc value = node.getNodeValue();
173                   ch.characters(value.toCharArray(), 0, value.length());
174                   break;
175                case Node.ENTITY_REFERENCE_NODE:
176                   String JavaDoc ref = '&' + node.getNodeName() + ';';
177                   ch.characters(ref.toCharArray(), 0, ref.length());
178                   break;
179                case Node.ENTITY_NODE:
180                   ch.skippedEntity(node.getNodeName());
181                   break;
182             }
183          }
184       }
185
186       ch.endElement(ns, localName, qName);
187
188       if(startedPrefixes.size() > 0)
189       {
190          for(int i = startedPrefixes.size() - 1; i >= 0; --i)
191          {
192            String JavaDoc pref = (String JavaDoc)startedPrefixes.get(i);
193            nsReg.removePrefixMapping(pref);
194            ch.endPrefixMapping(pref);
195          }
196       }
197    }
198
199    private static boolean isXmlns(String JavaDoc ns)
200    {
201       return ns.startsWith("http://www.w3.org/2000/xmlns");
202    }
203
204    private static String JavaDoc buildQName(String JavaDoc prefix, String JavaDoc localName)
205    {
206       return prefix == null || prefix.length() == 0 ?
207          localName :
208          prefix + ':' + localName;
209    }
210
211    private static List JavaDoc add(List JavaDoc list, Object JavaDoc o)
212    {
213       switch(list.size())
214       {
215          case 0:
216             list = Collections.singletonList(o);
217             break;
218          case 1:
219             list = new ArrayList JavaDoc(list);
220          default:
221             list.add(o);
222       }
223       return list;
224    }
225 }
226
Popular Tags