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 * This object keeps track of most of the open tags while a document is serialized.  Note that instances of this class
023 * may not necessarily hold all open tags of a document while it is being serialized.  For example, tags enclosing list elements
024 * and map entries are not contained within here.  See DocumentSerializerServiceImpl, in krad-service-impl, to determine when
025 * this object's state is modified.
026 *
027 * This class's manipulators behave much like a stack, but it has random access characteristics like an array.
028 */
029public class DocumentSerializationState {
030    protected class SerializationPropertyElement {
031        private String elementName;
032        private PropertyType propertyType;
033
034        public SerializationPropertyElement(String elementName, PropertyType propertyType) {
035            this.elementName = elementName;
036            this.propertyType = propertyType;
037        }
038
039        public String getElementName() {
040            return this.elementName;
041        }
042
043        public PropertyType getPropertyType() {
044            return this.propertyType;
045        }
046    }
047
048    private List<SerializationPropertyElement> pathElements;
049
050    public DocumentSerializationState() {
051        pathElements = new ArrayList<SerializationPropertyElement>();
052    }
053
054    /**
055     * The number of property elements in this state object.
056     *
057     * @return
058     */
059    public int numPropertyElements() {
060        return pathElements.size();
061    }
062
063    /**
064     * Adds an additional state element into this object.
065     *
066     * @param elementName
067     * @param propertyType the type of the property when it was serialized
068     */
069    public void addSerializedProperty(String elementName, PropertyType propertyType) {
070        SerializationPropertyElement serializationPropertyElement = new SerializationPropertyElement(elementName, propertyType);
071        pathElements.add(serializationPropertyElement);
072    }
073
074    /**
075     * Removes the last added serialized property
076     *
077     */
078    public void removeSerializedProperty() {
079        pathElements.remove(pathElements.size() - 1);
080    }
081
082    /**
083     * Retrieves the element name of the state element.  A parameter value of 0 represents the first element that was added
084     * by calling {@link #addSerializedProperty(String, PropertyType)} that hasn't been removed, and a value of
085     * {@link #numPropertyElements()} - 1 represents the element last added that hasn't been removed.
086     *
087     * @param propertyIndex most be between 0 and the value returned by {@link #numPropertyElements()} - 1
088     * @return
089     */
090    public String getElementName(int propertyIndex) {
091        return pathElements.get(propertyIndex).getElementName();
092    }
093
094    /**
095     * Retrieves the property type of the state element.  A parameter value of 0 represents the first element that was added
096     * by calling {@link #addSerializedProperty(String, PropertyType)} that hasn't been removed, and a value of
097     * {@link #numPropertyElements()} - 1 represents the element last added that hasn't been removed.
098     *
099     * @param propertyIndex most be between 0 and the value returned by {@link #numPropertyElements()} - 1
100     * @return
101     */
102    public PropertyType getPropertyType(int propertyIndex) {
103        return pathElements.get(propertyIndex).getPropertyType();
104    }
105}