View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.util.documentserializer;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  
22  /**
23   * This object keeps track of most of the open tags while a document is serialized.  Note that instances of this class
24   * may not necessarily hold all open tags of a document while it is being serialized.  For example, tags enclosing list elements
25   * and map entries are not contained within here.  See {@link DocumentSerializerServiceImpl} to determine when this object's state
26   * is modified.
27   *
28   * This class's manipulators behave much like a stack, but it has random access characteristics like an array.
29   */
30  public class SerializationState {
31      protected class SerializationPropertyElement {
32          private String elementName;
33          private PropertyType propertyType;
34  
35          public SerializationPropertyElement(String elementName, PropertyType propertyType) {
36              this.elementName = elementName;
37              this.propertyType = propertyType;
38          }
39  
40          public String getElementName() {
41              return this.elementName;
42          }
43  
44          public PropertyType getPropertyType() {
45              return this.propertyType;
46          }
47      }
48  
49      private List<SerializationPropertyElement> pathElements;
50  
51      public SerializationState(){
52          pathElements = new ArrayList<SerializationPropertyElement>();
53      }
54  
55      /**
56       * The number of property elements in this state object.
57       *
58       * @return
59       */
60      public int numPropertyElements() {
61          return pathElements.size();
62      }
63  
64      /**
65       * Adds an additional state element into this object.
66       *
67       * @param elementName
68       * @param propertyType the type of the property when it was serialized
69       */
70      public void addSerializedProperty(String elementName, PropertyType propertyType) {
71          SerializationPropertyElement serializationPropertyElement = new SerializationPropertyElement(elementName, propertyType);
72          pathElements.add(serializationPropertyElement);
73      }
74  
75      /**
76       * Removes the last added serialized property
77       *
78       */
79      public void removeSerializedProperty() {
80          pathElements.remove(pathElements.size() - 1);
81      }
82  
83      /**
84       * Retrieves the element name of the state element.  A parameter value of 0 represents the first element that was added
85       * by calling {@link #addSerializedProperty(String, PropertyType)} that hasn't been removed, and a value of
86       * {@link #numPropertyElements()} - 1 represents the element last added that hasn't been removed.
87       *
88       * @param propertyIndex most be between 0 and the value returned by {@link #numPropertyElements()} - 1
89       * @return
90       */
91      public String getElementName(int propertyIndex) {
92          return pathElements.get(propertyIndex).getElementName();
93      }
94  
95      /**
96       * Retrieves the property type of the state element.  A parameter value of 0 represents the first element that was added
97       * by calling {@link #addSerializedProperty(String, PropertyType)} that hasn't been removed, and a value of
98       * {@link #numPropertyElements()} - 1 represents the element last added that hasn't been removed.
99       *
100      * @param propertyIndex most be between 0 and the value returned by {@link #numPropertyElements()} - 1
101      * @return
102      */
103     public PropertyType getPropertyType(int propertyIndex) {
104         return pathElements.get(propertyIndex).getPropertyType();
105     }
106 }