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