View Javadoc
1   /*
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 2001 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$ Date Author Changes 04/06/2001 Arnaud Blandin Created
34   */
35  package org.exolab.castor.xml.util;
36  
37  import java.util.HashSet;
38  
39  import org.exolab.castor.types.AnyNode;
40  import org.exolab.castor.xml.EventProducer;
41  import org.exolab.castor.xml.Namespaces;
42  import org.exolab.castor.xml.NamespacesStack;
43  import org.xml.sax.DocumentHandler;
44  import org.xml.sax.SAXException;
45  import org.xml.sax.helpers.AttributeListImpl;
46  
47  /**
48   * A class for converting an AnyNode to SAX events
49   * 
50   * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
51   * @version $Revision$ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $
52   */
53  public class AnyNode2SAX implements EventProducer {
54  
55    /** The AnyNode we are firing events for. */
56    private AnyNode _node;
57    /** The Document Handler. */
58    private DocumentHandler _handler;
59    /** The stack to store the elements. */
60    private HashSet _elements;
61  
62    /**
63     * Represents the namespace stack.
64     */
65    private NamespacesStack namespacesStack;
66  
67    /**
68     * No-arg constructor.
69     */
70    public AnyNode2SAX() {
71      this(null, null);
72    }
73  
74    /**
75     * Creates a AnyNode2SAX for the given node.
76     * 
77     * @param node the AnyNode to create AnyNode2SAX for.
78     */
79    public AnyNode2SAX(final AnyNode node) {
80      this(node, null);
81    }
82  
83    /**
84     * Creates a AnyNode2SAX for the given node and the namespace context.
85     * 
86     * @param node the AnyNode to create AnyNode2SAX for.
87     * @param namespacesStack the namespace stack
88     */
89    public AnyNode2SAX(final AnyNode node, final NamespacesStack namespacesStack) {
90      _elements = new HashSet();
91      _node = node;
92      this.namespacesStack = namespacesStack != null ? namespacesStack : new NamespacesStack();
93    }
94  
95    /**
96     * Set the Document Handler
97     * 
98     * @param handler the document handler to set
99     */
100   public void setDocumentHandler(final DocumentHandler handler) {
101     if (handler == null) {
102       throw new IllegalArgumentException("AnyNode2SAX#setDocumentHandler 'null' value for handler");
103     }
104     _handler = handler;
105   }
106 
107   public static void fireEvents(final AnyNode node, final DocumentHandler handler)
108       throws SAXException {
109     fireEvents(node, handler, null);
110   }
111 
112   public static void fireEvents(final AnyNode node, final DocumentHandler handler,
113       final NamespacesStack namespacesStack) throws SAXException {
114     AnyNode2SAX eventProducer = new AnyNode2SAX(node, namespacesStack);
115     eventProducer.setDocumentHandler(handler);
116     eventProducer.start();
117   }
118 
119   public void start() throws org.xml.sax.SAXException {
120     if (_node == null || _handler == null) {
121       return;
122     }
123     processAnyNode(_node, _handler);
124   }
125 
126   private void processAnyNode(final AnyNode node, final DocumentHandler handler)
127       throws SAXException {
128 
129     if (node == null || handler == null) {
130       throw new IllegalArgumentException();
131     }
132 
133     // -- so we don't potentially get into an endlessloop
134     if (!_elements.add(node)) {
135       return;
136     }
137 
138     if (node.getNodeType() == AnyNode.ELEMENT) {
139       String name = node.getLocalName();
140 
141       // -- retrieve the attributes and handle them
142       AttributeListImpl atts = new AttributeListImpl();
143       AnyNode tempNode = node.getFirstAttribute();
144       String xmlName = null;
145       String value = null;
146       String attUri = null;
147       String attPrefix = null;
148 
149       while (tempNode != null) {
150         xmlName = tempNode.getLocalName();
151         // --retrieve a prefix?
152         attUri = tempNode.getNamespaceURI();
153         if (attUri != null) {
154           attPrefix = namespacesStack.getNamespacePrefix(attUri);
155         }
156         if (attPrefix != null && attPrefix.length() > 0) {
157           xmlName = attPrefix + ':' + xmlName;
158         }
159         value = tempNode.getStringValue();
160         atts.addAttribute(xmlName, "CDATA", value);
161         tempNode = tempNode.getNextSibling();
162       } // attributes
163 
164       // -- namespace management
165       namespacesStack.addNewNamespaceScope();
166       String nsPrefix = node.getNamespacePrefix();
167       String nsURI = node.getNamespaceURI();
168 
169       // -- retrieve the namespaces declaration and handle them
170       tempNode = node.getFirstNamespace();
171       String prefix = null;
172       while (tempNode != null) {
173         prefix = tempNode.getNamespacePrefix();
174         value = tempNode.getNamespaceURI();
175         if (value != null && value.length() > 0) {
176           namespacesStack.addNamespace(prefix, value);
177         }
178         tempNode = tempNode.getNextSibling();
179       } // namespaceNode
180 
181       String qName = null;
182       // maybe the namespace is already bound to a prefix in the
183       // namespace context
184       if (nsURI != null && nsURI.length() > 0) {
185         String tempPrefix = namespacesStack.getNamespacePrefix(nsURI);
186         if (tempPrefix != null) {
187           nsPrefix = tempPrefix;
188         } else {
189           namespacesStack.addNamespace(nsPrefix, nsURI);
190         }
191       }
192 
193       if (nsPrefix != null) {
194         int len = nsPrefix.length();
195         if (len > 0) {
196           qName = nsPrefix + ':' + name;
197         } else {
198           qName = name;
199         }
200       } else {
201         qName = name;
202       }
203 
204       try {
205         namespacesStack.declareAsAttributes(atts, true);
206         handler.startElement(qName, atts);
207       } catch (SAXException sx) {
208         throw new SAXException(sx);
209       }
210 
211       // -- handle child&daughter elements
212       tempNode = node.getFirstChild();
213       while (tempNode != null) {
214         processAnyNode(tempNode, handler);
215         tempNode = tempNode.getNextSibling();
216       }
217 
218       // -- finish element
219       try {
220         handler.endElement(qName);
221         namespacesStack.removeNamespaceScope();
222       } catch (org.xml.sax.SAXException sx) {
223         throw new SAXException(sx);
224       }
225     } else {
226       // ELEMENTS
227       if (node.getNodeType() == AnyNode.TEXT) {
228         String value = node.getStringValue();
229         if (value != null && value.length() > 0) {
230           char[] chars = value.toCharArray();
231           try {
232             handler.characters(chars, 0, chars.length);
233           } catch (org.xml.sax.SAXException sx) {
234             throw new SAXException(sx);
235           }
236         }
237       }
238     }
239   }
240 
241 }