Debian Codegraph @

src/smetana/core/StructureDefinition.java

raw symbols SCIP
1 /* ========================================================================
2 * PlantUML : a free UML diagram generator
3 * ========================================================================
4 *
5 * Project Info: http://plantuml.com
6 *
7 * If you like this project or if you find it useful, you can support us at:
8 *
9 * http://plantuml.com/patreon (only 1$ per month!)
10 * http://plantuml.com/paypal
11 *
12 * This file is part of Smetana.
13 * Smetana is a partial translation of Graphviz/Dot sources from C to Java.
14 *
15 * (C) Copyright 2009-2020, Arnaud Roques
16 *
17 * This translation is distributed under the same Licence as the original C program.
18 *
19 * THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC
20 * LICENSE ("AGREEMENT"). [Eclipse Public License - v 1.0]
21 *
22 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES
23 * RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
24 *
25 * You may obtain a copy of the License at
26 *
27 * http://www.eclipse.org/legal/epl-v10.html
28 *
29 * Unless required by applicable law or agreed to in writing, software
30 * distributed under the License is distributed on an "AS IS" BASIS,
31 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32 * See the License for the specific language governing permissions and
33 * limitations under the License.
34 *
35 */
36
37 package smetana.core;
38
39 import java.util.Collection;
40 import java.util.HashMap;
41 import java.util.Iterator;
42 import java.util.LinkedHashMap;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Set;
46
47 public class StructureDefinition {
48
49 private static final Map<Class, StructureDefinition> all = new HashMap<Class, StructureDefinition>();
50
51 public static StructureDefinition from(Class cl) {
52 if (cl == null) {
53 throw new IllegalArgumentException();
54 }
55 StructureDefinition result = all.get(cl);
56 if (result == null) {
57 result = new StructureDefinition(cl);
58 all.put(cl, result);
59 }
60 return result;
61 }
62
63 private final Class cl;
64 private Map<String, Bucket> buckets; // = new LinkedHashMap<String, StructureDefinition.Bucket>();
65
66 private StructureDefinition(Class cl) {
67 // JUtils.LOG("BUIDLING StructureDefinition " + cl);
68 this.cl = cl;
69 }
70
71 public String toString() {
72 return (cl == null ? "NO_CLASS" : cl.getName()) + " " + buckets;
73 }
74
75 private Map<String, Bucket> buckets() {
76 if (buckets == null) {
77 final List<String> definition = CType.getDefinition(cl);
78 JUtils.LOG("StructureDefinition::run for " + cl);
79 JUtils.LOG("def=" + definition);
80 JUtils.LOG("first=" + definition.get(0));
81
82 buckets = new LinkedHashMap<String, Bucket>();
83
84 if (definition.get(0).equals("typedef enum")) {
85 final String last = definition.get(definition.size() - 1);
86 if (last.matches("\\w+") == false) {
87 throw new UnsupportedOperationException();
88 }
89 buckets.put(last, Bucket.buildEnum(last, definition));
90 return buckets;
91 }
92
93 if (definition.get(0).equals("typedef struct gvplugin_active_textlayout_s") == false
94 && definition.get(0).equals("typedef struct color_s") == false
95 && definition.get(0).equals("typedef struct") == false
96 && definition.get(0).equals("typedef struct pointf_s") == false
97 && definition.get(0).equals("typedef struct gvplugin_active_layout_s") == false
98 && definition.get(0).equals("typedef struct GVCOMMON_s") == false
99 && definition.get(0).equals("struct " + cl.getSimpleName()) == false
100 && definition.get(0).equals("typedef struct " + cl.getSimpleName()) == false
101 && definition.get(0).equals("typedef struct " + cl.getSimpleName().replaceFirst("_t", "_s")) == false
102 && definition.get(0).equals("typedef union " + cl.getSimpleName()) == false) {
103 throw new IllegalStateException("<struct " + cl.getSimpleName() + "> VERSUS <" + definition.get(0)
104 + ">");
105 }
106 if (definition.get(1).equals("{") == false) {
107 throw new IllegalStateException();
108 }
109
110 int last = definition.size() - 1;
111 if (definition.get(definition.size() - 2).equals("}")
112 && definition.get(definition.size() - 1).equals(cl.getSimpleName())) {
113 last--;
114 }
115
116 if (definition.get(last).equals("}") == false) {
117 throw new IllegalStateException();
118 }
119
120 for (Iterator<String> it = definition.subList(2, last).iterator(); it.hasNext();) {
121 buckets.putAll(Bucket.buildSome(it));
122 }
123 }
124 return buckets;
125 }
126
127 public Set<String> getFields() {
128 return buckets().keySet();
129 }
130
131 public Collection<Bucket> getBuckets() {
132 return buckets().values();
133 }
134
135 public Map<String, Bucket> getBucketsMap() {
136 return buckets();
137 }
138
139 public Bucket getBucket(String field) {
140 final Bucket result = buckets().get(field);
141 if (result == null) {
142 throw new IllegalArgumentException(field);
143 }
144 return result;
145 }
146
147 public Class getTheClass() {
148 return cl;
149 }
150
151 public boolean containsFieldName(String fieldName) {
152 return buckets().keySet().contains(fieldName);
153 }
154
155 }