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 1999 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  
36  
37  package org.exolab.castor.dsml;
38  
39  
40  import java.util.Enumeration;
41  import org.xml.sax.DocumentHandler;
42  import org.xml.sax.SAXException;
43  import org.xml.sax.AttributeList;
44  import org.xml.sax.Locator;
45  import org.castor.core.util.Messages;
46  
47  
48  /**
49   *
50   *
51   * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
52   * @version $Revision$ $Date: 2006-04-26 13:58:52 -0600 (Wed, 26 Apr 2006) $
53   */
54  public abstract class Consumer implements DocumentHandler {
55  
56  
57    private boolean _insideRoot;
58  
59  
60    private DocumentHandler _redirect;
61  
62  
63    public Consumer() {}
64  
65  
66    public abstract Enumeration getResults();
67  
68  
69    protected abstract DocumentHandler getEntryConsumer();
70  
71  
72    public void startElement(String tagName, AttributeList attr) throws SAXException {
73      if (_redirect != null) {
74        // When redirecting all element creations pass directly to
75        // the other handler, we only look for closure.
76        _redirect.startElement(tagName, attr);
77      } else if (tagName.equals(XML.Namespace.ROOT)) {
78        // Flag when entering (and leaving) the root element.
79        if (_insideRoot)
80          throw new SAXException(Messages.format("dsml.elementNested", XML.Namespace.ROOT));
81        _insideRoot = true;
82      } else {
83        if (!_insideRoot)
84          throw new SAXException(
85              Messages.format("dsml.expectingOpeningTag", XML.Namespace.ROOT, tagName));
86        if (tagName.equals(XML.Schema.ELEMENT) || tagName.equals(XML.Entries.ELEMENT)) {
87          DocumentHandler entry;
88  
89          entry = getEntryConsumer();
90          entry.startElement(tagName, attr);
91          _redirect = entry;
92        } else {
93          throw new SAXException(Messages.format("dsml.openingTagNotRecognized", tagName));
94        }
95      }
96    }
97  
98  
99    public void endElement(String tagName) throws SAXException {
100     if (_redirect == null) {
101       // This is the only case where we expect the root element
102       // to be closed.
103       if (tagName.equals(XML.Namespace.ROOT)) {
104         if (_insideRoot == true)
105           _insideRoot = false;
106         else
107           throw new SAXException(Messages.format("dsml.closingOutsideRoot", tagName));
108       } else {
109         throw new SAXException(
110             Messages.format("dsml.expectingClosingTag", XML.Namespace.ROOT, tagName));
111       }
112     } else {
113       _redirect.endElement(tagName);
114 
115       if (tagName.equals(XML.Schema.ELEMENT) || tagName.equals(XML.Entries.ELEMENT)) {
116         // If we've reached this point we must be inside the
117         // entries/schema redirect.
118         _redirect = null;
119       }
120       // If we've reached this point we must be inside the
121       // entries/schema redirect, we ignore all closing tags.
122     }
123   }
124 
125 
126   public void characters(char[] ch, int offset, int length) throws SAXException {
127     if (_redirect != null) {
128       _redirect.characters(ch, offset, length);
129     }
130   }
131 
132 
133   public void ignorableWhitespace(char[] ch, int offset, int length) throws SAXException {
134     if (_redirect != null) {
135       _redirect.ignorableWhitespace(ch, offset, length);
136     }
137   }
138 
139 
140   public void processingInstruction(String target, String instruction) throws SAXException {
141     if (_redirect != null) {
142       _redirect.processingInstruction(target, instruction);
143     }
144   }
145 
146 
147   public void startDocument() throws SAXException {}
148 
149 
150   public void endDocument() throws SAXException {
151     if (_insideRoot)
152       throw new SAXException(Messages.message("dsml.documentRootStillOpen"));
153   }
154 
155 
156   public void setDocumentLocator(Locator locator) {}
157 
158 
159 }
160