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-2004 (C) Intalio, Inc. All Rights Reserved.
42   *
43   * $Id$
44   */
45  
46  package org.exolab.castor.xml;
47  
48  import java.util.HashSet;
49  import java.util.Set;
50  
51  import org.apache.commons.lang.StringUtils;
52  
53  /**
54   * The state information class for the UnmarshalHandler.
55   * 
56   * @author <a href="mailto:kvisco-at-intalio.com">Keith Visco</a>
57   * @version $Revision$ $Date: 2004-12-11 02:25:45 -0700 (Sat, 11 Dec 2004) $
58   */
59  public class UnmarshalState {
60      
61      /** Holds on to Constructor arguments. */
62      private UnmarshalHandler.Arguments _args = null;
63      
64      /** Holds the current location path. */
65      private String _location = "";
66      
67      /** Indicates if the xsi:nil='true' attribute was present on the element. */
68      private boolean _nil = false;
69      
70      /** The xml element name of the current object. */
71      private String _elementName = null;
72  
73      /** Characters read in during unmarshalling. */
74      private StringBuffer _buffer = null;
75  
76      /** The key for the object. This may be null if no key
77       *  or identity has been specified. */
78      private Object _key = null;
79      
80      /** The current that we are unmarshalling to. */
81      private Object _object = null;
82      
83      /** The class of the object, mainly used for primitives. */
84      private Class<?> _type = null;
85  
86      /** The field descriptor for the Object. */
87      private XMLFieldDescriptor _fieldDescriptor = null;
88  
89      /** The class descriptor for the Object, in case
90       *  FieldDescriptor#getClassDescriptor returns null. */
91      private XMLClassDescriptor _classDescriptor = null;
92  
93      /** Is the field a primitive or immutable type? */
94      private boolean _primitiveOrImmutable = false;
95  
96      /** The list of *used* field descriptors. */
97      private Set<XMLFieldDescriptor> _markedList = new HashSet<XMLFieldDescriptor>();
98  
99      /** Is this a derived field? */
100     private boolean _derived = false;
101     
102     /** Is this a wrapper state? */
103     private boolean _wrapper = false;
104     
105     /** The whitespace preserve flag. */
106     private boolean _whitespacePreserving = false;
107     
108     private boolean _trailingWhitespaceRemoved = false;
109     
110     /** Index of next expected sequence element; used during validation. */
111     private int _expectedIndex = 0;
112     
113     /** Indicates (during validation) whether the current field descriptor 
114      *  points to a multi-valued element. */
115     private boolean _withinMultivaluedElement = false;
116     
117     /** The {@link UnmarshalState} which contains information about the parent object for the object
118      *  contained within this state. Used when handling element containers/wrappers. */
119     private UnmarshalState _targetState = null;
120     
121     /** A reference to the parent state. */
122     private UnmarshalState _parent = null;
123 
124     /**
125      * Reinitializes all variables
126      */
127     void clear() {
128         
129         setConstructorArguments(null);
130         setLocation("");
131         setElementName(null);
132         setBuffer(null);
133         setKey(null);
134         setNil(false);
135         setObject(null);
136         setType(null);
137         setFieldDescriptor(null);
138         setClassDescriptor(null);
139         setPrimitiveOrImmutable(false);
140         if (_markedList != null) {
141             _markedList.clear();
142         }
143         setDerived(false);
144         setWrapper(false);
145         setTargetState(null);
146         setWhitespacePreserving(false);
147         setParent(null);
148         setTrailingWhitespaceRemoved(false);
149     } //-- clear
150 
151     /**
152      * Marks the given XMLFieldDescriptor as having been used.
153      * 
154      * @param descriptor the XMLFieldDescriptor to mark.
155      */
156     void markAsUsed(XMLFieldDescriptor descriptor) {
157         _markedList.add(descriptor);
158     }
159 
160     void markAsNotUsed(XMLFieldDescriptor descriptor) {
161         if (_markedList == null) { 
162             return; 
163          }
164         _markedList.remove(descriptor);
165     }
166 
167     boolean isUsed(XMLFieldDescriptor descriptor) {
168         if (_markedList.isEmpty()) { 
169             return false; 
170         }
171         return _markedList.contains(descriptor);
172     }
173 
174     void setFieldDescriptor(XMLFieldDescriptor fieldDesc) {
175         _fieldDescriptor = fieldDesc;
176     }
177 
178     XMLFieldDescriptor getFieldDescriptor() {
179         return _fieldDescriptor;
180     }
181 
182     void setObject(Object object) {
183         _object = object;
184     }
185 
186     Object getObject() {
187         return _object;
188     }
189 
190     void setKey(Object key) {
191         _key = key;
192     }
193 
194     Object getKey() {
195         return _key;
196     }
197 
198     void setType(Class<?> type) {
199         _type = type;
200     }
201 
202     Class<?> getType() {
203         return _type;
204     }
205 
206     void setClassDescriptor(XMLClassDescriptor classDesc) {
207         _classDescriptor = classDesc;
208     }
209 
210     XMLClassDescriptor getClassDescriptor() {
211         return _classDescriptor;
212     }
213 
214     void setLocation(String location) {
215         _location = location;
216     }
217 
218     String getLocation() {
219         return _location;
220     }
221 
222     void setElementName(String elementName) {
223         _elementName = elementName;
224     }
225 
226     String getElementName() {
227         return _elementName;
228     }
229 
230     void setBuffer(StringBuffer buffer) {
231         _buffer = buffer;
232     }
233 
234     StringBuffer getBuffer() {
235         return _buffer;
236     }
237 
238     void setDerived(boolean derived) {
239         _derived = derived;
240     }
241 
242     boolean isDerived() {
243         return _derived;
244     }
245 
246     void setWrapper(boolean wrapper) {
247         _wrapper = wrapper;
248     }
249 
250     boolean isWrapper() {
251         return _wrapper;
252     }
253 
254     void setWhitespacePreserving(boolean wsPreserve) {
255         _whitespacePreserving = wsPreserve;
256     }
257 
258     boolean isWhitespacePreserving() {
259         return _whitespacePreserving;
260     }
261 
262     void setPrimitiveOrImmutable(boolean primitiveOrImmutable) {
263         _primitiveOrImmutable = primitiveOrImmutable;
264     }
265 
266     boolean isPrimitiveOrImmutable() {
267         return _primitiveOrImmutable;
268     }
269 
270     void setTrailingWhitespaceRemoved(boolean trailingWhitespaceRemoved) {
271         _trailingWhitespaceRemoved = trailingWhitespaceRemoved;
272     }
273 
274     boolean isTrailingWhitespaceRemoved() {
275         return _trailingWhitespaceRemoved;
276     }
277 
278     void setTargetState(UnmarshalState targetState) {
279         _targetState = targetState;
280     }
281 
282     UnmarshalState getTargetState() {
283         return _targetState;
284     }
285 
286     void setParent(UnmarshalState parent) {
287         _parent = parent;
288     }
289 
290     UnmarshalState getParent() {
291         return _parent;
292     }
293 
294     void setNil(boolean nil) {
295         _nil = nil;
296     }
297 
298     boolean isNil() {
299         return _nil;
300     }
301 
302     public void setExpectedIndex(int expectedIndex) {
303         _expectedIndex = expectedIndex;
304     }
305 
306     public int getExpectedIndex() {
307         return _expectedIndex;
308     }
309 
310     public void setWithinMultivaluedElement(boolean withinMultivaluedElement) {
311         _withinMultivaluedElement = withinMultivaluedElement;
312     }
313 
314     public boolean isWithinMultivaluedElement() {
315         return _withinMultivaluedElement;
316     }
317 
318     void setConstructorArguments(UnmarshalHandler.Arguments args) {
319         _args = args;
320     }
321 
322     UnmarshalHandler.Arguments getConstructorArguments() {
323         return _args;
324     }
325 
326     @Override
327     public String toString() {
328         StringBuffer buffer = new StringBuffer();
329         buffer.append("UnmarshalState [" + _elementName + "("+ _type + ") ");
330         if (!StringUtils.isEmpty(_location)) {
331             buffer.append("rooted at location=" + _location + ", ]");
332         }
333         return buffer.toString();
334         
335     }
336     
337     
338 }