001/**
002 * Copyright 2005-2018 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     * Creates a new SerializationState that is a copy of the given state
057     *
058     * @param stateToCopy the state to copy
059     */
060    public SerializationState(SerializationState stateToCopy) {
061        this();
062        this.pathElements.addAll(stateToCopy.pathElements);
063    }
064
065    /**
066     * The number of property elements in this state object.
067     *
068     * @return
069     */
070    public int numPropertyElements() {
071        return pathElements.size();
072    }
073
074    /**
075     * Adds an additional state element into this object.
076     *
077     * @param elementName
078     * @param propertyType the type of the property when it was serialized
079     */
080    public void addSerializedProperty(String elementName, PropertyType propertyType) {
081        SerializationPropertyElement serializationPropertyElement = new SerializationPropertyElement(elementName, propertyType);
082        pathElements.add(serializationPropertyElement);
083    }
084
085    /**
086     * Removes the last added serialized property
087     *
088     */
089    public void removeSerializedProperty() {
090        pathElements.remove(pathElements.size() - 1);
091    }
092
093    /**
094     * Retrieves the element name of the state element.  A parameter value of 0 represents the first element that was added
095     * by calling {@link #addSerializedProperty(String, PropertyType)} that hasn't been removed, and a value of
096     * {@link #numPropertyElements()} - 1 represents the element last added that hasn't been removed.
097     *
098     * @param propertyIndex most be between 0 and the value returned by {@link #numPropertyElements()} - 1
099     * @return
100     */
101    public String getElementName(int propertyIndex) {
102        return pathElements.get(propertyIndex).getElementName();
103    }
104
105    /**
106     * Retrieves the property type of the state element.  A parameter value of 0 represents the first element that was added
107     * by calling {@link #addSerializedProperty(String, PropertyType)} that hasn't been removed, and a value of
108     * {@link #numPropertyElements()} - 1 represents the element last added that hasn't been removed.
109     *
110     * @param propertyIndex most be between 0 and the value returned by {@link #numPropertyElements()} - 1
111     * @return
112     */
113    public PropertyType getPropertyType(int propertyIndex) {
114        return pathElements.get(propertyIndex).getPropertyType();
115    }
116}