Mercurial > hg > Members > kono > jpf-core
comparison src/main/gov/nasa/jpf/util/json/CGCall.java @ 0:61d41facf527
initial v8 import (history reset)
author | Peter Mehlitz <Peter.C.Mehlitz@nasa.gov> |
---|---|
date | Fri, 23 Jan 2015 10:14:01 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:61d41facf527 |
---|---|
1 /* | |
2 * Copyright (C) 2014, United States Government, as represented by the | |
3 * Administrator of the National Aeronautics and Space Administration. | |
4 * All rights reserved. | |
5 * | |
6 * The Java Pathfinder core (jpf-core) platform is licensed under the | |
7 * Apache License, Version 2.0 (the "License"); you may not use this file except | |
8 * in compliance with the License. You may obtain a copy of the License at | |
9 * | |
10 * http://www.apache.org/licenses/LICENSE-2.0. | |
11 * | |
12 * Unless required by applicable law or agreed to in writing, software | |
13 * distributed under the License is distributed on an "AS IS" BASIS, | |
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
15 * See the License for the specific language governing permissions and | |
16 * limitations under the License. | |
17 */ | |
18 | |
19 package gov.nasa.jpf.util.json; | |
20 | |
21 import gov.nasa.jpf.vm.ChoiceGenerator; | |
22 | |
23 import java.util.ArrayList; | |
24 import java.util.List; | |
25 | |
26 /** | |
27 * | |
28 * @author Ivan Mushketik | |
29 */ | |
30 public class CGCall { | |
31 | |
32 private ArrayList<Value> params = new ArrayList<Value>(); | |
33 private String name; | |
34 | |
35 public CGCall(String name) { | |
36 this.name = name; | |
37 } | |
38 | |
39 void addParam(Value value) { | |
40 if (value == null) { | |
41 throw new NullPointerException("Null value added to CGCall"); | |
42 } | |
43 | |
44 params.add(value); | |
45 } | |
46 | |
47 public Value[] getValues() { | |
48 Value paramsArr[] = new Value[params.size()]; | |
49 params.toArray(paramsArr); | |
50 | |
51 return paramsArr; | |
52 } | |
53 | |
54 public String getName() { | |
55 return name; | |
56 } | |
57 | |
58 /** | |
59 * This method go through JSON object and finds all CGs to set in a current state. | |
60 * It also gives each CG it's a unique id. Each id is calculated in a following way: | |
61 * <li> If CG call is in the root of JSON object it's id will be equals to it's key in JSON | |
62 * <li> If CG call isn't in a root object it's id will be concatenated keys of | |
63 * all parent objects + key of CG. | |
64 * <li> If CG call is in an object that is an element of array CG's id will be | |
65 * concatenated keys of all parent array + "[i" (where i is a pos of parent object | |
66 * of CG in array + CG's key) | |
67 * | |
68 * This id also used in JSONObject.fillObject() to find CG to use when field that | |
69 * should be set with CG is found. | |
70 * | |
71 * @param jsonObject - parsed JSON object | |
72 * @param CGCreators - hash of factories to create Choice Generators | |
73 * @return list of choice generators that should be set in a current state. | |
74 */ | |
75 public static List<ChoiceGenerator<?>> createCGList(JSONObject jsonObject) { | |
76 List<ChoiceGenerator<?>> result = new ArrayList<ChoiceGenerator<?>>(); | |
77 createCGs(jsonObject, "", result); | |
78 | |
79 return result; | |
80 } | |
81 | |
82 private static void createCGs(JSONObject jsonObject, String prefix, List<ChoiceGenerator<?>> result) { | |
83 for (String cgKey : jsonObject.getCGCallsKeys()) { | |
84 CGCall cgCall = jsonObject.getCGCall(cgKey); | |
85 CGCreator creator = CGCreatorFactory.getFactory().getCGCreator(cgCall.getName()); | |
86 | |
87 ChoiceGenerator<?> newCG = creator.createCG(prefix + cgKey, cgCall.getValues()); | |
88 result.add(newCG); | |
89 } | |
90 | |
91 for (String valueKey : jsonObject.getValuesKeys()) { | |
92 Value v = jsonObject.getValue(valueKey); | |
93 | |
94 if (v instanceof JSONObjectValue) { | |
95 createCGs(v.getObject(), prefix + valueKey, result); | |
96 | |
97 } else if (v instanceof ArrayValue) { | |
98 Value[] values = v.getArray(); | |
99 | |
100 for (int i = 0; i < values.length; i++) { | |
101 if (values[i] instanceof JSONObjectValue) { | |
102 createCGs(values[i].getObject(), prefix + valueKey + "[" + i, result); | |
103 } | |
104 } | |
105 } | |
106 } | |
107 } | |
108 } |