1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.fosstrak.llrp.commander.util;
23
24 import java.util.List;
25
26 import org.llrp.ltk.types.LLRPEnumeration;
27 import org.llrp.ltk.types.LLRPMessage;
28 import org.llrp.ltk.types.LLRPParameter;
29 import org.llrp.ltk.types.LLRPType;
30 import org.llrp.ltkGenerator.generated.ChoiceParameterReference;
31 import org.llrp.ltkGenerator.generated.FieldDefinition;
32 import org.llrp.ltkGenerator.generated.ChoiceDefinition;
33
34
35
36
37
38
39
40 public class LLRPFactory {
41
42 private static final String MESSAGES_PACKAGE = "org.llrp.ltk.generated.messages";
43 private static final String PARAMETERS_PACKAGE = "org.llrp.ltk.generated.parameters";
44 private static final String TYPES_PACKAGE = "org.llrp.ltk.types";
45 private static final String ENUMERATIONS_PACKAGE = "org.llrp.ltk.generated.enumerations";
46
47
48
49
50
51
52
53
54
55 public static LLRPMessage createLLRPMessage(String messageType){
56 LLRPMessage message = null;
57 LLRPTreeMaintainer treeMaintainer = null;
58 try {
59 Class messageClass = Class.forName(MESSAGES_PACKAGE + "." + messageType);
60 message = (LLRPMessage) messageClass.getConstructor(new Class[0]).newInstance(new Object[0]);
61 treeMaintainer = new LLRPTreeMaintainer(message);
62
63 initializeFields(message, treeMaintainer);
64 initializeMandatoryParameters(message, treeMaintainer);
65
66 } catch (Exception e) {
67 e.printStackTrace();
68 }
69 return message;
70 }
71
72 private static LLRPParameter createLLRPParameter(String parameterName, LLRPTreeMaintainer treeMaintainer){
73 LLRPParameter parameter = null;
74 try {
75 Class parameterTypeClass = Class.forName(PARAMETERS_PACKAGE + "." + parameterName);
76 parameter = (LLRPParameter) parameterTypeClass.getConstructor(new Class[0]).newInstance(new Object[0]);
77
78 initializeFields(parameter, treeMaintainer);
79 initializeMandatoryParameters(parameter, treeMaintainer);
80
81 } catch (Exception e) {
82 e.printStackTrace();
83 }
84 return parameter;
85 }
86
87 private static void initializeFields(Object messageOrParameter, LLRPTreeMaintainer treeMaintainer){
88 Object messageOrParameterDefinition = treeMaintainer.getDefinition(messageOrParameter);
89 for (FieldDefinition fieldDefinition : LLRP.getFieldDefinitions(messageOrParameterDefinition)){
90 if (LLRP.isEnumeration(fieldDefinition)){
91 LLRPEnumeration enumeration = createLLRPEnumeration(fieldDefinition.getEnumeration());
92 treeMaintainer.setField(messageOrParameter, fieldDefinition.getName(), (LLRPType) enumeration);
93 }
94 else {
95 String fieldType = LLRP.getFieldType(fieldDefinition);
96 LLRPType fieldValue = null;
97 LLRPRangeConstraint rangeConstraint = LLRPConstraints.getRangeConstraint(messageOrParameter, fieldDefinition.getName(), treeMaintainer);
98 LLRPArrayConstraint arrayConstraint = LLRPConstraints.getArrayConstraint(messageOrParameter, fieldDefinition.getName(), treeMaintainer);
99 if (rangeConstraint != null){
100 try {
101 fieldValue = createLLRPType(fieldType, Integer.toString(rangeConstraint.getDefaultValue()));
102 } catch (Exception e) {
103 e.printStackTrace();
104 }
105 }
106 else if (arrayConstraint != null){
107 try {
108 String s = "";
109 for (int i = 0; i < arrayConstraint.getDefaultValue().length; i++){
110 s = s + arrayConstraint.getDefaultValue()[i];
111 if (i < arrayConstraint.getDefaultValue().length - 1){
112 s = s + " ";
113 }
114 }
115 fieldValue = createLLRPType(fieldType, s);
116 } catch (Exception e) {
117 e.printStackTrace();
118 }
119 }
120 else{
121 fieldValue = createLLRPType(fieldType);
122 }
123 treeMaintainer.setField(messageOrParameter, fieldDefinition.getName(), fieldValue);
124 }
125 }
126 }
127
128 private static void initializeMandatoryParameters(Object messageOrParameter, LLRPTreeMaintainer treeMaintainer) {
129 Object messageOrParameterDefinition = treeMaintainer.getDefinition(messageOrParameter);
130 for (String childName : LLRP.getParameterAndChoiceNames(messageOrParameterDefinition)){
131
132 if (LLRP.canOccurMultipleTimes(messageOrParameterDefinition, childName)){
133 if (LLRP.mustOccurAtLeastOnce(messageOrParameterDefinition, childName)){
134 String parameterName;
135 if (LLRP.isChoice(messageOrParameterDefinition, childName)){
136 ChoiceDefinition choiceDefinition = LLRP.getChoiceDefinition(childName);
137 List<ChoiceParameterReference> choiceParameterReferences = choiceDefinition.getParameter();
138 parameterName = choiceParameterReferences.get(0).getType();
139 }
140 else{
141 parameterName = childName;
142 }
143 LLRPParameter parameter = createLLRPParameter(parameterName, treeMaintainer);
144 List<LLRPParameter> list = (List<LLRPParameter>) treeMaintainer.getChild(messageOrParameter, childName);
145 treeMaintainer.addChild(list, parameter);
146 }
147 }
148 else{
149 if (LLRP.mustOccurAtLeastOnce(messageOrParameterDefinition, childName)){
150 String parameterName;
151 if (LLRP.isChoice(messageOrParameterDefinition, childName)){
152 ChoiceDefinition choiceDefinition = LLRP.getChoiceDefinition(childName);
153 List<ChoiceParameterReference> choiceParameterReferences = choiceDefinition.getParameter();
154 parameterName = choiceParameterReferences.get(0).getType();
155 }
156 else{
157 parameterName = childName;
158 }
159 LLRPParameter parameter = createLLRPParameter(parameterName, treeMaintainer);
160 treeMaintainer.setChild(messageOrParameter, childName, parameter);
161 }
162 }
163 }
164 }
165
166 private static LLRPType createLLRPType(String typeName){
167 LLRPType lLRPType = null;
168 try {
169 Class fieldTypeClass = Class.forName(TYPES_PACKAGE + "." + typeName);
170 lLRPType = (LLRPType) fieldTypeClass.getConstructor(new Class[0]).newInstance(new Object[0]);
171 } catch (Exception e) {
172 e.printStackTrace();
173 }
174 return lLRPType;
175 }
176
177
178
179
180
181
182
183
184
185 public static LLRPType createLLRPType(String typeName, String value) throws Exception{
186 LLRPType result = null;
187 if (!value.equals("")){
188 Class fieldTypeClass;
189 fieldTypeClass = Class.forName(TYPES_PACKAGE + "." + typeName);
190 result = (LLRPType) fieldTypeClass.getConstructor(new Class[]{String.class}).newInstance(new Object[]{value});
191 }
192 return result;
193 }
194
195 private static LLRPEnumeration createLLRPEnumeration(String enumerationName){
196 LLRPEnumeration enumeration = null;
197 try {
198 Class enumerationClass = Class.forName(ENUMERATIONS_PACKAGE + "." + enumerationName);
199 enumeration = (LLRPEnumeration) enumerationClass.getConstructor(new Class[0]).newInstance(new Object[0]);
200 } catch (Exception e) {
201 e.printStackTrace();
202 }
203 return enumeration;
204 }
205 }