1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.util.documentserializer;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.util.KRADConstants;
20
21 import java.util.StringTokenizer;
22
23
24
25
26
27
28 public class PropertySerializerTrie {
29 private static final String PROPERTY_NAME_COMPONENT_SEPARATOR = ".";
30 private PropertySerializerTrieNode rootNode;
31
32 public PropertySerializerTrie() {
33 rootNode = new PropertySerializerTrieNode(KRADConstants.EMPTY_STRING, KRADConstants.EMPTY_STRING);
34 }
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public void addSerializablePropertyName(String propertyName, boolean setPropertySerializabilityToObjectAndAllPrimitivesForAll) {
57 if (propertyName == null) {
58 throw new IllegalArgumentException("Null attribute name specified");
59 }
60 if (StringUtils.isBlank(propertyName)) {
61 rootNode.setPropertySerializabilityToObjectAndAllPrimitives();
62 }
63 else {
64 StringTokenizer tok = new StringTokenizer(propertyName, PROPERTY_NAME_COMPONENT_SEPARATOR, false);
65 StringBuilder buf = new StringBuilder();
66
67 if(setPropertySerializabilityToObjectAndAllPrimitivesForAll)
68 rootNode.setPropertySerializabilityToObjectAndAllPrimitives();
69
70 PropertySerializerTrieNode currentNode = rootNode;
71 while (tok.hasMoreTokens()) {
72 String attributeNameComponent = tok.nextToken();
73 validateAttributeNameComponent(attributeNameComponent);
74
75 buf.append(attributeNameComponent);
76
77
78 PropertySerializerTrieNode childNode = currentNode.getChildNode(attributeNameComponent);
79 if (childNode == null) {
80 childNode = new PropertySerializerTrieNode(buf.toString(), attributeNameComponent);
81 currentNode.addChildNode(childNode);
82 }
83
84 if (tok.hasMoreTokens()) {
85 buf.append(PROPERTY_NAME_COMPONENT_SEPARATOR);
86 }
87 currentNode = childNode;
88 if(setPropertySerializabilityToObjectAndAllPrimitivesForAll)
89 currentNode.setPropertySerializabilityToObjectAndAllPrimitives();
90 }
91
92 currentNode.setPropertySerializabilityToObjectAndAllPrimitives();
93 }
94 }
95
96
97
98
99
100
101
102 public PropertySerializabilityMetadata getPropertySerializabilityMetadata(String propertyName) {
103 if (propertyName == null) {
104 throw new IllegalArgumentException("Null attribute name specified");
105 }
106 if (StringUtils.isBlank(propertyName)) {
107 return rootNode;
108 }
109 else {
110 StringTokenizer tok = new StringTokenizer(propertyName, PROPERTY_NAME_COMPONENT_SEPARATOR, false);
111
112 PropertySerializerTrieNode currentNode = rootNode;
113 while (tok.hasMoreTokens()) {
114 String attributeNameComponent = tok.nextToken();
115 validateAttributeNameComponent(attributeNameComponent);
116
117
118 PropertySerializerTrieNode childNode = currentNode.getChildNode(attributeNameComponent);
119 if (childNode == null) {
120
121 return null;
122 }
123 else {
124
125 currentNode = childNode;
126 }
127 }
128 return currentNode;
129 }
130 }
131
132
133
134
135
136
137 public PropertySerializabilityMetadata getRootPropertySerializibilityMetadata() {
138 return rootNode;
139 }
140
141 protected void validateAttributeNameComponent(String attributeNameComponent) {
142 if (StringUtils.isBlank(attributeNameComponent)) {
143 throw new IllegalArgumentException("Blank attribute name component specified");
144 }
145 }
146 }