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