Skip to content

Commit 2a6b9e0

Browse files
committed
improved single json schema
1 parent 56a0b17 commit 2a6b9e0

6 files changed

Lines changed: 143 additions & 18 deletions

File tree

src/main/java/org/cip4/lib/jdf/jsonutil/JSONArrayHelper.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ public JSONArray getArray()
126126
return array;
127127
}
128128

129+
public JSONArray getCopy()
130+
{
131+
return (JSONArray) JSONObjHelper.getCopy(getArray());
132+
}
133+
129134
/**
130135
* @param i
131136
* @param def
@@ -227,7 +232,9 @@ public List<Object> getObjects()
227232
l.addAll(JSONArrayHelper.getHelper(o).getObjects());
228233
}
229234
else
235+
{
230236
l.add(o);
237+
}
231238
}
232239
return l;
233240
}
@@ -259,7 +266,7 @@ public String toString()
259266
*/
260267
public static JSONArrayHelper getHelper(final Object jsonArray)
261268
{
262-
return (jsonArray instanceof JSONArray) ? new JSONArrayHelper((JSONArray) jsonArray) : null;
269+
return (jsonArray instanceof final JSONArray j) ? new JSONArrayHelper(j) : null;
263270
}
264271

265272
/**
@@ -335,7 +342,9 @@ public void remove(final int i)
335342
public void remove(final Object o)
336343
{
337344
if (array != null)
345+
{
338346
array.remove(o);
347+
}
339348
}
340349

341350
public void addString(final String s)
@@ -359,7 +368,9 @@ public int indexOf(final Object o)
359368
public void clear()
360369
{
361370
if (array != null)
371+
{
362372
array.clear();
373+
}
363374
}
364375

365376
public boolean addAll(final Collection c)
@@ -370,7 +381,9 @@ public boolean addAll(final Collection c)
370381
public void sort(final Comparator c)
371382
{
372383
if (array != null)
384+
{
373385
array.sort(c);
386+
}
374387
}
375388

376389
@Override
@@ -383,11 +396,17 @@ public int hashCode()
383396
public boolean equals(final Object obj)
384397
{
385398
if (this == obj)
399+
{
386400
return true;
401+
}
387402
if (obj == null)
403+
{
388404
return false;
405+
}
389406
if (getClass() != obj.getClass())
407+
{
390408
return false;
409+
}
391410
final JSONArrayHelper other = (JSONArrayHelper) obj;
392411
return Objects.equals(array, other.array);
393412
}

src/main/java/org/cip4/lib/jdf/jsonutil/JSONObjHelper.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import java.util.ArrayList;
5454
import java.util.Collection;
5555
import java.util.List;
56+
import java.util.Map.Entry;
5657
import java.util.Set;
5758

5859
import org.apache.commons.lang3.StringUtils;
@@ -287,9 +288,8 @@ public static String undertocamel(final String toConvert)
287288
public static JSONObjHelper getHelper(final Object o)
288289
{
289290
final JSONObjHelper h;
290-
if (o instanceof InputStream)
291+
if (o instanceof final InputStream is)
291292
{
292-
final InputStream is = (InputStream) o;
293293
return getHelperFromStream(is, false);
294294
}
295295
else if (o instanceof JSONObject)
@@ -306,7 +306,7 @@ else if (o instanceof JSONObject)
306306

307307
/**
308308
* get the underlying object
309-
*
309+
*
310310
* @param o
311311
* @return
312312
*/
@@ -377,7 +377,7 @@ public int getInt(final String path, final int def)
377377
}
378378
if (base instanceof Integer)
379379
{
380-
return ((Integer) base).intValue();
380+
return ((Integer) base);
381381
}
382382
if (base instanceof Double)
383383
{
@@ -408,7 +408,7 @@ public double getDouble(final String path, final double def)
408408
}
409409
if (base instanceof Double)
410410
{
411-
return ((Double) base).doubleValue();
411+
return ((Double) base);
412412
}
413413
if (base instanceof String)
414414
{
@@ -427,7 +427,7 @@ public boolean getBool(final String path, final boolean def)
427427
final Object base = getPathObject(path);
428428
if (base instanceof Boolean)
429429
{
430-
return ((Boolean) base).booleanValue();
430+
return ((Boolean) base);
431431
}
432432
else if (base instanceof String)
433433
{
@@ -617,7 +617,7 @@ else if (StringUtil.isEmpty(path))
617617
nextString += "[0]";
618618
newNext = getFirstObject(nextString);
619619
}
620-
return (newNext instanceof JSONObject) ? new JSONObjHelper((JSONObject) newNext).getPathObject(next) : null;
620+
return (newNext instanceof final JSONObject j) ? new JSONObjHelper(j).getPathObject(next) : null;
621621
}
622622
}
623623

@@ -983,6 +983,43 @@ public Object put(final Object key, final Object value)
983983
return obj.put(key, value);
984984
}
985985

986+
public JSONObjHelper getCopy()
987+
{
988+
return (JSONObjHelper) getCopy(this);
989+
}
990+
991+
public static Object getCopy(final Object src)
992+
{
993+
if (src instanceof final JSONObject srcObject)
994+
{
995+
final JSONObject next = new JSONObject();
996+
for (final Object o : srcObject.entrySet())
997+
{
998+
final Entry e = (Entry) o;
999+
next.put(e.getKey(), getCopy(e.getValue()));
1000+
}
1001+
return next;
1002+
}
1003+
else if (src instanceof final JSONObjHelper srcObject)
1004+
{
1005+
return new JSONObjHelper((JSONObject) getCopy(srcObject.getRoot()));
1006+
}
1007+
else if (src instanceof final JSONArray srcArray)
1008+
{
1009+
final JSONArray next = new JSONArray();
1010+
for (final Object o : srcArray)
1011+
{
1012+
next.add(getCopy(o));
1013+
}
1014+
return next;
1015+
}
1016+
else if (src instanceof final JSONArrayHelper srcArray)
1017+
{
1018+
return new JSONArrayHelper((JSONArray) getCopy(srcArray.getArray()));
1019+
}
1020+
return src;
1021+
}
1022+
9861023
public void clear()
9871024
{
9881025
if (obj != null)

src/main/java/org/cip4/lib/jdf/jsonutil/JSONRootWalker.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ JDFAttributeMap getAttributes(final KElement e)
333333
{
334334
final JDFAttributeMap atts = e.getAttributeMap_KElement();
335335
atts.remove(AttributeName.XSITYPE);
336+
atts.remove(AttributeName.XMLNS);
336337
final List<String> keyList = ContainerUtil.getKeyList(atts);
337338
final boolean needXmlns = eJSONPrefix.context.equals(jsonWriter.prefix);
338339
if (keyList != null)

src/main/java/org/cip4/lib/jdf/jsonutil/schema/JSONSchemaMerger.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@
4747
import org.cip4.jdflib.core.StringArray;
4848
import org.cip4.jdflib.extensions.XJDFConstants;
4949
import org.cip4.jdflib.jmf.JDFMessage.EnumFamily;
50+
import org.cip4.jdflib.util.ContainerUtil;
5051
import org.cip4.jdflib.util.StringUtil;
5152
import org.cip4.lib.jdf.jsonutil.JSONArrayHelper;
5253
import org.cip4.lib.jdf.jsonutil.JSONObjHelper;
54+
import org.json.simple.JSONArray;
5355
import org.json.simple.JSONObject;
5456

5557
public class JSONSchemaMerger extends JSONSchemaUpdate
@@ -120,6 +122,10 @@ public void mergeSchema(final File f)
120122
void fixbroken()
121123
{
122124
setString(DEFS + "/SurfaceColor/type", OBJECT);
125+
setString(DEFS + "/Glue/properties/Name/const", "Glue");
126+
setString(DEFS + "/Glue/properties/Name/type", "string");
127+
setString(DEFS + "/Media/properties/Name/const", "Media");
128+
setString(DEFS + "/Media/properties/Name/type", "string");
123129

124130
}
125131

@@ -161,7 +167,9 @@ void updateSingleAudit(final String audit)
161167
{
162168
final JSONArrayHelper req = a.getCreateArray(REQUIRED);
163169
for (final String r : r0.getStrings())
170+
{
164171
req.appendUnique(r);
172+
}
165173
}
166174
a.setObj(PROPERTIES, p0);
167175
a.setString("properties/Header/$ref", "#/$defs/Header");
@@ -227,7 +235,7 @@ void updateAbstractIntent()
227235
h.remove(PRODUCT_INTENT);
228236
for (final String def : alldefs)
229237
{
230-
if (def.endsWith(XJDFConstants.Intent) && !def.equals(XJDFConstants.Intent) && !def.equals(PRODUCT_INTENT))
238+
if (def.endsWith(XJDFConstants.Intent) && !XJDFConstants.Intent.equals(def) && !PRODUCT_INTENT.equals(def))
231239
{
232240
h.setString(def + SLASH_REF, HASH_DEFS + def);
233241
explicitAbstract.add(def);
@@ -240,13 +248,29 @@ void updateAbstractMessage(final String key, final String fam)
240248
{
241249
final JSONObjHelper xjmfprop = getHelper("$defs/XJMF/properties");
242250
final JSONObjHelper msg = getHelper(DEFS_SLASH + key);
243-
final JSONObjHelper family = getHelper(DEFS_SLASH + fam);
244-
final JSONObjHelper message = getHelper("$defs/Message");
245-
final JSONObjHelper msgProp = getSchemaParent(msg).getHelper(PROPERTIES);
246-
final JSONObjHelper famProp = getSchemaParent(family).getHelper(PROPERTIES);
247-
final JSONObjHelper messageProp = getSchemaParent(message).getHelper(PROPERTIES);
248-
msgProp.putAll(famProp);
249-
msgProp.putAll(messageProp);
251+
// final JSONObjHelper family = getHelper(DEFS_SLASH + fam);
252+
// final JSONObjHelper message = getHelper("$defs/Message");
253+
final List<JSONObjHelper> allProps = getSchemaParents(msg);
254+
// allProps.addAll(getSchemaParents(family));
255+
// allProps.addAll(getSchemaParents(message));
256+
//
257+
// msgProp.putAll(famProp);
258+
// msgProp.putAll(messageProp);
259+
final JSONObjHelper updated = new JSONObjHelper(new JSONObject());
260+
final JSONObjHelper first = allProps.remove(0);
261+
updated.putAll(first.getCopy());
262+
final JSONObjHelper firstProp = updated.getHelper(PROPERTIES);
263+
JSONArray firstReq = updated.getCreateArray(REQUIRED).getArray();
264+
for (final JSONObjHelper prop : allProps)
265+
{
266+
final JSONObjHelper messageProp = prop.getHelper(PROPERTIES).getCopy();
267+
final JSONArray messageReq = prop.getArray(REQUIRED);
268+
269+
firstReq = (JSONArray) ContainerUtil.addAll(firstReq, messageReq);
270+
firstProp.putAll(messageProp.getCopy());
271+
}
272+
updated.put(REQUIRED, firstReq);
273+
setObj(DEFS_SLASH + key, updated);
250274
xjmfprop.setString(key + SLASH_REF, HASH_DEFS + key);
251275
explicitAbstract.add(key);
252276
}

src/main/java/org/cip4/lib/jdf/jsonutil/schema/JSONSchemaUpdate.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
import java.io.File;
4141
import java.io.InputStream;
42+
import java.util.ArrayList;
43+
import java.util.Collection;
4244
import java.util.HashSet;
4345
import java.util.List;
4446
import java.util.Set;
@@ -192,6 +194,46 @@ JSONObjHelper getSchemaParent(final JSONObjHelper h)
192194
return null;
193195
}
194196

197+
List<JSONObjHelper> getSchemaParents(final JSONObjHelper h)
198+
{
199+
200+
final List<JSONObjHelper> ret = new ArrayList<>();
201+
if (h != null)
202+
{
203+
final JSONObjHelper p = h.getHelper(PROPERTIES);
204+
if (p != null)
205+
{
206+
ret.add(h);
207+
}
208+
else
209+
{
210+
final String ref = h.getString(REF);
211+
if (ref != null)
212+
{
213+
final JSONObjHelper refHelper = getHelper(DEFS_SLASH + StringUtil.token(ref, -1, "/"));
214+
ContainerUtil.addAll(ret, getSchemaParents(refHelper));
215+
}
216+
final JSONArrayHelper ah = h.getArrayHelper(ALL_OF);
217+
for (int i = 0; ah != null && i < 42; i++)
218+
{
219+
final JSONObjHelper o = ah.getJSONHelper(i);
220+
if (o == null)
221+
{
222+
return ret;
223+
}
224+
final Collection<JSONObjHelper> schemaParents = getSchemaParents(o);
225+
for (final JSONObjHelper schemaParent : schemaParents)
226+
{
227+
228+
ret.add(o.getString(REF) == null ? o : schemaParent);
229+
}
230+
231+
}
232+
}
233+
}
234+
return ret;
235+
}
236+
195237
void updateOneOf(final StringArray xRoots, final StringArray roots)
196238
{
197239
final JSONArrayHelper oneOf = getCreateArray(ONE_OF);

src/test/java/org/cip4/lib/jdf/jsonutil/schema/JSONSchemaMergerTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ void testJSONSchemaUpdateBad() throws URISyntaxException
8080
@Test
8181
void testJSONSchemaMerge() throws URISyntaxException
8282
{
83-
new File(sm_dirTestDataTemp + "schema/test/xjdf.json").delete();
84-
getNewSchema();
83+
File f0 = getNewSchema();
84+
FileUtil.deleteAll(f0);
85+
f0 = getNewSchema();
86+
assertTrue(f0.canRead());
8587
}
8688

8789
@Test

0 commit comments

Comments
 (0)