1 /*
2 * Copyright 2007-2008 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.kns.util.documentserializer;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21
22 import org.kuali.rice.kns.bo.BusinessObject;
23 import org.kuali.rice.kns.service.impl.DocumentSerializerServiceImpl;
24
25 /**
26 * This object keeps track of most of the open tags while a document is serialized. Note that instances of this class
27 * may not necessarily hold all open tags of a document while it is being serialized. For example, tags enclosing list elements
28 * and map entries are not contained within here. See {@link DocumentSerializerServiceImpl} to determine when this object's state
29 * is modified.
30 *
31 * This class's manipulators behave much like a stack, but it has random access characteristics like an array.
32 */
33 public class DocumentSerializationState {
34 protected class SerializationPropertyElement {
35 private String elementName;
36 private PropertyType propertyType;
37
38 public SerializationPropertyElement(String elementName, PropertyType propertyType) {
39 this.elementName = elementName;
40 this.propertyType = propertyType;
41 }
42
43 public String getElementName() {
44 return this.elementName;
45 }
46
47 public PropertyType getPropertyType() {
48 return this.propertyType;
49 }
50 }
51
52 private List<SerializationPropertyElement> pathElements;
53
54 public DocumentSerializationState() {
55 pathElements = new ArrayList<SerializationPropertyElement>();
56 }
57
58 /**
59 * The number of property elements in this state object.
60 *
61 * @return
62 */
63 public int numPropertyElements() {
64 return pathElements.size();
65 }
66
67 /**
68 * Adds an additional state element into this object.
69 *
70 * @param elementName
71 * @param propertyType the type of the property when it was serialized
72 */
73 public void addSerializedProperty(String elementName, PropertyType propertyType) {
74 SerializationPropertyElement serializationPropertyElement = new SerializationPropertyElement(elementName, propertyType);
75 pathElements.add(serializationPropertyElement);
76 }
77
78 /**
79 * Removes the last added serialized property
80 *
81 */
82 public void removeSerializedProperty() {
83 pathElements.remove(pathElements.size() - 1);
84 }
85
86 /**
87 * Retrieves the element name of the state element. A parameter value of 0 represents the first element that was added
88 * by calling {@link #addSerializedProperty(String, PropertyType)} that hasn't been removed, and a value of
89 * {@link #numPropertyElements()} - 1 represents the element last added that hasn't been removed.
90 *
91 * @param propertyIndex most be between 0 and the value returned by {@link #numPropertyElements()} - 1
92 * @return
93 */
94 public String getElementName(int propertyIndex) {
95 return pathElements.get(propertyIndex).getElementName();
96 }
97
98 /**
99 * Retrieves the property type of the state element. A parameter value of 0 represents the first element that was added
100 * by calling {@link #addSerializedProperty(String, PropertyType)} that hasn't been removed, and a value of
101 * {@link #numPropertyElements()} - 1 represents the element last added that hasn't been removed.
102 *
103 * @param propertyIndex most be between 0 and the value returned by {@link #numPropertyElements()} - 1
104 * @return
105 */
106 public PropertyType getPropertyType(int propertyIndex) {
107 return pathElements.get(propertyIndex).getPropertyType();
108 }
109 }