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