001/**
002 * Copyright 2005-2015 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.util.documentserializer;
017
018import java.util.ArrayList;
019import java.util.List;
020
021
022/**
023 * This object keeps track of most of the open tags while a document is serialized.  Note that instances of this class
024 * may not necessarily hold all open tags of a document while it is being serialized.  For example, tags enclosing list elements
025 * and map entries are not contained within here.  See DocumentSerializerServiceImpl, in krad-service-impl, to determine when
026 * this object's state is modified.
027 *
028 * This class's manipulators behave much like a stack, but it has random access characteristics like an array.
029 */
030public class SerializationState {
031    protected class SerializationPropertyElement {
032        private String elementName;
033        private PropertyType propertyType;
034
035        public SerializationPropertyElement(String elementName, PropertyType propertyType) {
036            this.elementName = elementName;
037            this.propertyType = propertyType;
038        }
039
040        public String getElementName() {
041            return this.elementName;
042        }
043
044        public PropertyType getPropertyType() {
045            return this.propertyType;
046        }
047    }
048
049    private List<SerializationPropertyElement> pathElements;
050
051    public SerializationState(){
052        pathElements = new ArrayList<SerializationPropertyElement>();
053    }
054
055    /**
056     * The number of property elements in this state object.
057     *
058     * @return
059     */
060    public int numPropertyElements() {
061        return pathElements.size();
062    }
063
064    /**
065     * Adds an additional state element into this object.
066     *
067     * @param elementName
068     * @param propertyType the type of the property when it was serialized
069     */
070    public void addSerializedProperty(String elementName, PropertyType propertyType) {
071        SerializationPropertyElement serializationPropertyElement = new SerializationPropertyElement(elementName, propertyType);
072        pathElements.add(serializationPropertyElement);
073    }
074
075    /**
076     * Removes the last added serialized property
077     *
078     */
079    public void removeSerializedProperty() {
080        pathElements.remove(pathElements.size() - 1);
081    }
082
083    /**
084     * Retrieves the element name of the state element.  A parameter value of 0 represents the first element that was added
085     * by calling {@link #addSerializedProperty(String, PropertyType)} that hasn't been removed, and a value of
086     * {@link #numPropertyElements()} - 1 represents the element last added that hasn't been removed.
087     *
088     * @param propertyIndex most be between 0 and the value returned by {@link #numPropertyElements()} - 1
089     * @return
090     */
091    public String getElementName(int propertyIndex) {
092        return pathElements.get(propertyIndex).getElementName();
093    }
094
095    /**
096     * Retrieves the property type of the state element.  A parameter value of 0 represents the first element that was added
097     * by calling {@link #addSerializedProperty(String, PropertyType)} that hasn't been removed, and a value of
098     * {@link #numPropertyElements()} - 1 represents the element last added that hasn't been removed.
099     *
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}