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 | 0 | public PropertySerializerTrie() { |
33 | 0 | rootNode = new PropertySerializerTrieNode(KRADConstants.EMPTY_STRING, KRADConstants.EMPTY_STRING); |
34 | 0 | } |
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 | 0 | if (propertyName == null) { |
58 | 0 | throw new IllegalArgumentException("Null attribute name specified"); |
59 | |
} |
60 | 0 | if (StringUtils.isBlank(propertyName)) { |
61 | 0 | rootNode.setPropertySerializabilityToObjectAndAllPrimitives(); |
62 | |
} |
63 | |
else { |
64 | 0 | StringTokenizer tok = new StringTokenizer(propertyName, PROPERTY_NAME_COMPONENT_SEPARATOR, false); |
65 | 0 | StringBuilder buf = new StringBuilder(); |
66 | |
|
67 | 0 | if(setPropertySerializabilityToObjectAndAllPrimitivesForAll) |
68 | 0 | rootNode.setPropertySerializabilityToObjectAndAllPrimitives(); |
69 | |
|
70 | 0 | PropertySerializerTrieNode currentNode = rootNode; |
71 | 0 | while (tok.hasMoreTokens()) { |
72 | 0 | String attributeNameComponent = tok.nextToken(); |
73 | 0 | validateAttributeNameComponent(attributeNameComponent); |
74 | |
|
75 | 0 | buf.append(attributeNameComponent); |
76 | |
|
77 | |
|
78 | 0 | PropertySerializerTrieNode childNode = currentNode.getChildNode(attributeNameComponent); |
79 | 0 | if (childNode == null) { |
80 | 0 | childNode = new PropertySerializerTrieNode(buf.toString(), attributeNameComponent); |
81 | 0 | currentNode.addChildNode(childNode); |
82 | |
} |
83 | |
|
84 | 0 | if (tok.hasMoreTokens()) { |
85 | 0 | buf.append(PROPERTY_NAME_COMPONENT_SEPARATOR); |
86 | |
} |
87 | 0 | currentNode = childNode; |
88 | 0 | if(setPropertySerializabilityToObjectAndAllPrimitivesForAll) |
89 | 0 | currentNode.setPropertySerializabilityToObjectAndAllPrimitives(); |
90 | 0 | } |
91 | |
|
92 | 0 | currentNode.setPropertySerializabilityToObjectAndAllPrimitives(); |
93 | |
} |
94 | 0 | } |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public PropertySerializabilityMetadata getPropertySerializabilityMetadata(String propertyName) { |
103 | 0 | if (propertyName == null) { |
104 | 0 | throw new IllegalArgumentException("Null attribute name specified"); |
105 | |
} |
106 | 0 | if (StringUtils.isBlank(propertyName)) { |
107 | 0 | return rootNode; |
108 | |
} |
109 | |
else { |
110 | 0 | StringTokenizer tok = new StringTokenizer(propertyName, PROPERTY_NAME_COMPONENT_SEPARATOR, false); |
111 | |
|
112 | 0 | PropertySerializerTrieNode currentNode = rootNode; |
113 | 0 | while (tok.hasMoreTokens()) { |
114 | 0 | String attributeNameComponent = tok.nextToken(); |
115 | 0 | validateAttributeNameComponent(attributeNameComponent); |
116 | |
|
117 | |
|
118 | 0 | PropertySerializerTrieNode childNode = currentNode.getChildNode(attributeNameComponent); |
119 | 0 | if (childNode == null) { |
120 | |
|
121 | 0 | return null; |
122 | |
} |
123 | |
else { |
124 | |
|
125 | 0 | currentNode = childNode; |
126 | |
} |
127 | 0 | } |
128 | 0 | return currentNode; |
129 | |
} |
130 | |
} |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public PropertySerializabilityMetadata getRootPropertySerializibilityMetadata() { |
138 | 0 | return rootNode; |
139 | |
} |
140 | |
|
141 | |
protected void validateAttributeNameComponent(String attributeNameComponent) { |
142 | 0 | if (StringUtils.isBlank(attributeNameComponent)) { |
143 | 0 | throw new IllegalArgumentException("Blank attribute name component specified"); |
144 | |
} |
145 | 0 | } |
146 | |
} |