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.Iterator;
20  import java.util.List;
21  
22  /**
23   * A node in the trie.
24   *
25   */
26  public class PropertySerializerTrieNode implements PropertySerializabilityMetadata {
27      private String pathString;
28      private String propertyNameComponent;
29      private PropertySerializability propertySerializability;
30  
31      private List<PropertySerializerTrieNode> childNodes;
32  
33      public PropertySerializerTrieNode(String pathString, String propertyNameComponent) {
34          this.pathString = pathString;
35          this.propertyNameComponent = propertyNameComponent;
36          this.childNodes = null;
37          this.propertySerializability = PropertySerializability.SERIALIZE_OBJECT;
38      }
39  
40      public void addChildNode(PropertySerializerTrieNode child) {
41          if (childNodes == null) {
42              childNodes = new ArrayList<PropertySerializerTrieNode>();
43          }
44          childNodes.add(child);
45      }
46  
47      /**
48       * The name of this property, relative to the parent node (i.e. the child node name relative to its parents).
49       *
50       * @return
51       */
52      public String getPropertyNameComponent() {
53          return propertyNameComponent;
54      }
55  
56      /**
57       * Retrieves the child node with the given name
58       *
59       * @param propertyNameComponent
60       * @return
61       */
62      public PropertySerializerTrieNode getChildNode(String propertyNameComponent) {
63          if (childNodes == null) {
64              return null;
65          }
66          for (int i = 0; i < childNodes.size(); i++) {
67              PropertySerializerTrieNode childNode = childNodes.get(i);
68              if (childNode.getPropertyNameComponent().equals(propertyNameComponent)) {
69                  return childNode;
70              }
71          }
72          return null;
73      }
74  
75      /**
76       * @see org.kuali.rice.krad.util.documentserializer.PropertySerializabilityMetadata#getSerializableChildProperty(java.lang.String)
77       */
78      public PropertySerializabilityMetadata getSerializableChildProperty(String propertyNameComponent) {
79          return getChildNode(propertyNameComponent);
80      }
81  
82      /**
83       * @see org.kuali.rice.krad.util.documentserializer.PropertySerializabilityMetadata#getPathString()
84       */
85      public String getPathString() {
86          return pathString;
87      }
88  
89      /**
90       * @see org.kuali.rice.krad.util.documentserializer.PropertySerializabilityMetadata#getPropertySerializability()
91       */
92      public PropertySerializability getPropertySerializability() {
93          return propertySerializability;
94      }
95  
96      /**
97       * Marks that all primitives of this object will be serialized.
98       */
99      public void setPropertySerializabilityToObjectAndAllPrimitives() {
100         this.propertySerializability = PropertySerializability.SERIALIZE_OBJECT_AND_ALL_PRIMITIVES;
101     }
102 
103     @Override
104     public String toString() {
105         StringBuilder buf = new StringBuilder();
106         buf.append("Path String: ").append(pathString).append(" Name component: ").append(propertyNameComponent);
107         if (childNodes == null || childNodes.isEmpty()) {
108             buf.append(" No child nodes.");
109         }
110         else {
111             buf.append(" Child nodes: ");
112             for (Iterator<PropertySerializerTrieNode> i = childNodes.iterator(); i.hasNext();) {
113                 buf.append(i.next().getPropertyNameComponent());
114                 if (i.hasNext()) {
115                     buf.append(", ");
116                 }
117             }
118         }
119         return super.toString();
120     }
121 }