View Javadoc
1   /*
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Intalio, Inc.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2001-2003 (C) Intalio, Inc. All Rights Reserved.
42   *
43   * $Id$
44   * Date         Author              Changes
45   * 04/06/2001   Arnaud Blandin      Created
46   */
47  package org.exolab.castor.xml.util;
48  
49  import java.util.Stack;
50  
51  import org.exolab.castor.types.AnyNode;
52  import org.exolab.castor.xml.Namespaces;
53  import org.exolab.castor.xml.NamespacesStack;
54  import org.xml.sax.AttributeList;
55  import org.xml.sax.Attributes;
56  import org.xml.sax.ContentHandler;
57  import org.xml.sax.DocumentHandler;
58  import org.xml.sax.ErrorHandler;
59  import org.xml.sax.Locator;
60  import org.xml.sax.SAXException;
61  import org.xml.sax.SAXParseException;
62  
63  /**
64   * This class is a SAX Content Handler that
65   * build an AnyNode from a stream of SAX events (either SAX1 for compatibility or SAX2)
66   * @author <a href="blandin@intalio.com>Arnaud Blandin</a>
67   * @version $Revision$ $Date: 2006-04-29 09:44:19 -0600 (Sat, 29 Apr 2006) $
68   */
69  public class SAX2ANY implements ContentHandler, DocumentHandler, ErrorHandler {
70     /**
71      * Prefix used by namespace declaration.
72      */
73      private final static String XMLNS_PREFIX        = "xmlns";
74      private final static int    XMLNS_PREFIX_LENGTH = XMLNS_PREFIX.length() + 1; // prefix + ':'
75  
76      /**
77       * The starting node.
78       */
79      private AnyNode _startingNode;
80  
81      /**
82       * The current AnyNode we are building
83       */
84      private AnyNode _node;
85  
86      /**
87       * A stack to store all the nodes we are creating
88       */
89      private Stack _nodeStack = new Stack();
90  
91      /**
92       * A stack to store the namespaces declaration
93       */
94      private Stack _namespaces = new Stack();
95  
96      /**
97       * A flag indicating if the SAX2 Parser is processing the
98       * namespace or not. 'true' will indicate that the code of this
99       * Content Handler will have to deal with Namespaces.This is the default
100      * value.
101      */
102     private boolean _processNamespace = true;
103 
104     /**
105      * A flag that indicates we are in a character section.
106      */
107     private boolean _character = false;
108 
109     /**
110      * Represents the namespaces stack.
111      */
112     private NamespacesStack namespacesStack;
113 
114     private boolean _wsPreserve = false;
115 
116     /**
117      * Default constructor
118      */
119     public SAX2ANY() {
120         super();
121         init();
122     }
123 
124     /**
125      * Constructs a SAX2ANY given a namespace context.
126      *
127      * @param namespacesStack the namespace stack
128      * @param wsPreserve if white spaces whould be preserved
129      */
130     public SAX2ANY(NamespacesStack namespacesStack, boolean wsPreserve) {
131         this.namespacesStack = namespacesStack;
132         _wsPreserve = wsPreserve;
133         init();
134     }
135 
136     private void init() {
137         if (this.namespacesStack == null)
138             this.namespacesStack = new NamespacesStack();
139     }
140 
141     /**
142      * Sets the document locator of the current parsed inputsource
143      * @param locator the Locator of the current parsed inputsource
144      */
145     public void setDocumentLocator(final Locator locator) { }
146 
147     //----------------- NOT IMPLEMENTED --------------
148     //we don't need to implement these methods since
149     //we are only dealing with xml fragments
150     public void startDocument() throws SAXException {
151     }
152 
153     public void endDocument() throws SAXException {
154     }
155 
156     public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
157         return;
158     }
159 
160     public void processingInstruction(String target, String data) throws SAXException {
161         return;
162     }
163 
164     public void skippedEntity(String name) throws SAXException {
165         return;
166     }
167     //-------------------------------------------------
168 
169     //--Namespace related (SAX2 Events)
170     public void startPrefixMapping(String prefix, String uri) throws SAXException {
171         AnyNode temp = new AnyNode(AnyNode.NAMESPACE, null, prefix, uri, null);
172        _namespaces.push(temp);
173        if (_processNamespace) {
174            namespacesStack.addNewNamespaceScope();
175            _processNamespace = true;
176        }
177        namespacesStack.addNamespace(prefix, uri);
178     }
179 
180     public void endPrefixMapping(String prefix) throws SAXException {
181         namespacesStack.removeNamespace(prefix);
182     }
183 
184     //--startElement methods SAX1 and SAX2
185     /**
186      * Implementation of {@link org.xml.sax.DocumentHandler#startElement}
187      */
188     public void startElement(String name, AttributeList atts)
189            throws SAXException {
190         _character = false;
191         String qName;
192         String value;
193         AnyNode tempNode = null;
194 
195         //Namespace handling code to be moved once we integrate
196         //the new event API
197         /////////////////NAMESPACE HANDLING/////////////////////
198         namespacesStack.addNewNamespaceScope();
199         String prefix = "";
200         String namespaceURI = null;
201         int idx = name.indexOf(':');
202         if (idx >= 0) {
203              prefix = name.substring(0,idx);
204         }
205         namespaceURI = namespacesStack.getNamespaceURI(prefix);
206         //--Overhead here since we process attributes twice
207         for (int i=0; i<atts.getLength(); ++i) {
208             qName = atts.getName(i);
209             value = atts.getValue(i);
210             String nsPrefix = null;
211 
212             if (qName.startsWith(XMLNS_PREFIX)) {
213                 //handles namespace declaration
214                 // Extract the prefix if any
215                 nsPrefix = (qName.equals(XMLNS_PREFIX))?null:qName.substring(XMLNS_PREFIX_LENGTH);
216                 tempNode = new AnyNode(AnyNode.NAMESPACE, getLocalPart(qName), nsPrefix, value, null);
217                 namespacesStack.addNamespace(nsPrefix, value);
218                 _namespaces.push(tempNode);
219                 if (prefix.equals(nsPrefix))
220                     namespaceURI = value;
221             }
222         }
223         ////////////////////////END OF NAMESPACE HANDLING///////////////
224 
225         createNodeElement(namespaceURI, getLocalPart(name), name);
226         while (!_namespaces.empty()) {
227            tempNode = (AnyNode)_namespaces.pop();
228            _node.addNamespace(tempNode);
229         }
230 
231         //process attributes
232         for (int i=0; i<atts.getLength(); ++i) {
233 
234             qName = atts.getName(i);
235             value = atts.getValue(i);
236 
237             //Namespace handling already done
238             if (!qName.startsWith(XMLNS_PREFIX)) {
239                 tempNode = new AnyNode(AnyNode.ATTRIBUTE, getLocalPart(qName), null, null, value);
240                 _node.addAttribute(tempNode);
241             }
242         }
243         tempNode = null;
244     }
245 
246     /**
247      * Implementation of {@link org.xml.sax.ContentHandler#startElement}
248      */
249     public void startElement(String namespaceURI,  String localName,
250                             String qName, Attributes atts) throws SAXException {
251         AnyNode tempNode;
252 
253         //--SAX2 Parser has not processed the namespaces so we need to do it.
254         if (_processNamespace) {
255             //Namespace handling code to be moved once we integrate
256             //the new event API
257             /////////////////NAMESPACE HANDLING/////////////////////
258             namespacesStack.addNewNamespaceScope();
259             String prefix = "";
260             int idx = qName.indexOf(':');
261             if (idx >= 0) {
262                  prefix = qName.substring(0,idx);
263             }
264             namespaceURI = namespacesStack.getNamespaceURI(prefix);
265             //--Overhead here since we process attributes twice
266             for (int i=0; i<atts.getLength(); ++i) {
267                 String attrqName = atts.getQName(i);
268                 String value = atts.getValue(i);
269                 String nsPrefix = null;
270                 //handles namespace declaration
271                 if (attrqName.startsWith(XMLNS_PREFIX)) {
272                     // Extract the prefix if any
273                     nsPrefix = (attrqName.equals(XMLNS_PREFIX))?null:attrqName.substring(XMLNS_PREFIX_LENGTH);
274                     tempNode = new AnyNode(AnyNode.NAMESPACE, getLocalPart(attrqName), nsPrefix, value, null);
275                     namespacesStack.addNamespace(nsPrefix, value);
276                     _namespaces.push(tempNode);
277                     if (prefix.equals(nsPrefix))
278                         namespaceURI = value;
279                 }
280             }
281             ////////////////////////END OF NAMESPACE HANDLING///////////////
282         }
283 
284         //create element
285         createNodeElement(namespaceURI, localName, qName);
286 
287         //process attributes
288         for (int i=0; i<atts.getLength(); ++i) {
289 
290             String uri       = atts.getURI(i);
291             String attqName  = atts.getQName(i);
292             String value     = atts.getValue(i);
293             String prefix    = null;
294 
295             //-- skip namespace declarations? (handled above)
296             if (_processNamespace )
297                 if(attqName.startsWith(XMLNS_PREFIX))
298                     continue;
299 
300             //--attribute namespace prefix?
301             if ((attqName.length() != 0) && (attqName.indexOf(':') != -1 ))
302                 prefix = attqName.substring(0,attqName.indexOf(':'));
303 
304             //--namespace not yet processed?
305             if (_processNamespace ) {
306                 // attribute namespace
307                 if(prefix!=null)
308                     uri = namespacesStack.getNamespaceURI(prefix);
309             }
310             //--add attribute
311             tempNode = new AnyNode(AnyNode.ATTRIBUTE, getLocalPart(attqName), prefix, uri, value);
312             _node.addAttribute(tempNode);
313         }
314 
315         //--empty the namespace stack and add
316         //--the namespace nodes to the current node.
317         while (!_namespaces.empty()) {
318             tempNode = (AnyNode)_namespaces.pop();
319             _node.addNamespace(tempNode);
320         }
321         tempNode = null;
322     }
323 
324     //--endElement methods SAX1 and SAX2
325     public void endElement(String name) throws SAXException {
326         int idx = name.indexOf(':');
327         String prefix = (idx >= 0) ? name.substring(0,idx) : "";
328         String namespaceURI = namespacesStack.getNamespaceURI(prefix);
329         endElement(namespaceURI,getLocalPart(name), name);
330         namespacesStack.removeNamespaceScope();
331     }
332 
333     public void endElement(String namespaceURI, String localName, String qName)
334            throws SAXException {
335         _character = false;
336         String name = null;
337         //-- if namespace processing is disabled then the localName might be null, in that case
338         //-- we use the QName
339         if (localName != null && localName.length() > 0) {
340             name = localName;
341         } else {
342             name = getLocalPart(qName);
343         }
344 
345         //--if it is the starting element just returns
346         if (_startingNode.getLocalName().equals(name) && _nodeStack.empty())
347            return;
348 
349         //--else just add the node we have built to the previous node
350         _node = (AnyNode)_nodeStack.pop();
351 
352         //-- if the stack is empty, we have a new child for the root node
353         //-- or a new sibling for the first child of the root node
354         if (_nodeStack.empty()) {
355             _startingNode.addChild(_node);
356             _node = _startingNode;
357         } else {
358             AnyNode previousNode = (AnyNode) _nodeStack.peek();
359             previousNode.addChild(_node);
360             //--the node processing is finished -> come back to the previous node
361             _node = previousNode;
362          }
363     }
364 
365     public void characters(char[] ch, int start, int length) throws SAXException {
366         //create a Text Node
367         String temp = new String(ch, start, length);
368         //skip whitespaces
369         if (isWhitespace(temp) && !_wsPreserve && !_character) return;
370         AnyNode tempNode = new AnyNode(AnyNode.TEXT, null, null, null, temp);
371         _node.addChild(tempNode);
372         _character = true;
373     }
374 
375 
376     /**************************************************************************/
377     // implementation of ErrorHandler
378     public void warning(SAXParseException e) throws SAXException {
379         String err = "SAX2ANY warning\n" + "Line : " + e.getLineNumber() + '\n'
380                 + "URI : " + e.getSystemId() + '\n' + e.getMessage();
381         throw new SAXException(err, e);
382     } // warning
383 
384     public void error(SAXParseException e) throws SAXException {
385         String err = "SAX2ANY Error \n" + "Line : " + e.getLineNumber() + '\n'
386                 + "URI : " + e.getSystemId() + '\n' + e.getMessage();
387         throw new SAXException(err, e);
388     } // error
389 
390     public void fatalError(SAXParseException e) throws SAXException {
391         String err = "SAX2ANY Fatal Error \n" + "Line : " + e.getLineNumber()
392                 + '\n' + "URI : " + e.getSystemId() + '\n' + e.getMessage();
393         throw new SAXException(err, e);
394     } //fatalError
395     /*************************************************************************/
396 
397     //Utility methods
398     public AnyNode getStartingNode() {
399         return _startingNode;
400     }
401 
402     /**
403      * Checks the given String to determine if it only
404      * contains whitespace.
405      *
406      * @param string the String to check
407      * @return true if the only whitespace characters were
408      * found in the given StringBuffer
409      */
410     private boolean isWhitespace(String string) {
411         for (int i = 0; i < string.length(); i++) {
412             char ch = string.charAt(i);
413             switch (ch) {
414                 case ' ':
415                 case '\n':
416                 case '\t':
417                 case '\r':
418                     break;
419                 default:
420                     return false;
421             }
422         }
423         return true;
424     } //-- isWhitespace
425 
426     /**
427      * Returns the local part of the given NCName. The local part is anything
428      * following the namespace prefix. If there is no namespace prefix
429      * the returned name will be the same as the given name.
430      * @return the local part of the given NCName.
431      */
432     private String getLocalPart(String ncName) {
433         int idx = ncName.indexOf(':');
434         if (idx >= 0) return ncName.substring(idx+1);
435         return ncName;
436     } //-- getLocalPart
437 
438     private void createNodeElement(String namespaceURI, String localName,
439                                    String qName) {
440 
441         String prefix = null;
442         //retrieves the prefix if any
443         if (namespaceURI != null) {
444             prefix = namespacesStack.getNamespacePrefix(namespaceURI);
445         }
446         else if (qName != null) {
447             if ((qName.length() != 0) && (qName.indexOf(':') != -1 ))
448                 prefix = qName.substring(0,qName.indexOf(':'));
449         }
450 
451         String name = null;
452         //-- if namespace processing is disabled then the localName might be null, in that case
453         //-- we use the localpart of the QName
454         if (localName != null && localName.length() > 0)
455             name = localName;
456         else
457              name = getLocalPart(qName);
458 
459         //creates the starting ELEMENT node
460         //or a default ELEMENT node
461         if ( (_nodeStack.empty()) && (_startingNode == null)) {
462            _startingNode = new AnyNode(AnyNode.ELEMENT, name, prefix, namespaceURI, null);
463            _node = _startingNode;
464         } else {
465           _node = new AnyNode(AnyNode.ELEMENT, name, prefix, namespaceURI, null);
466           //push the node in the stack
467           _nodeStack.push(_node);
468         }
469     }
470 
471 }