001package org.hl7.fhir.r5.elementmodel;
002
003/*-
004 * #%L
005 * org.hl7.fhir.r5
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023
024import java.io.IOException;
025import java.io.InputStream;
026import java.io.OutputStream;
027import java.io.OutputStreamWriter;
028import java.math.BigDecimal;
029import java.util.HashMap;
030import java.util.HashSet;
031import java.util.IdentityHashMap;
032import java.util.List;
033import java.util.Map;
034import java.util.Map.Entry;
035import java.util.Set;
036
037import org.hl7.fhir.exceptions.FHIRException;
038import org.hl7.fhir.exceptions.FHIRFormatError;
039import org.hl7.fhir.r5.conformance.ProfileUtilities;
040import org.hl7.fhir.r5.context.IWorkerContext;
041import org.hl7.fhir.r5.elementmodel.Element.SpecialElement;
042import org.hl7.fhir.r5.formats.IParser.OutputStyle;
043import org.hl7.fhir.r5.formats.JsonCreator;
044import org.hl7.fhir.r5.formats.JsonCreatorCanonical;
045import org.hl7.fhir.r5.formats.JsonCreatorGson;
046import org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent;
047import org.hl7.fhir.r5.model.StructureDefinition;
048import org.hl7.fhir.utilities.TextFile;
049import org.hl7.fhir.utilities.Utilities;
050import org.hl7.fhir.utilities.json.JsonTrackingParser;
051import org.hl7.fhir.utilities.json.JsonTrackingParser.LocationData;
052import org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity;
053import org.hl7.fhir.utilities.validation.ValidationMessage.IssueType;
054import org.hl7.fhir.utilities.xhtml.XhtmlParser;
055
056import com.google.gson.JsonArray;
057import com.google.gson.JsonElement;
058import com.google.gson.JsonNull;
059import com.google.gson.JsonObject;
060import com.google.gson.JsonPrimitive;
061
062public class JsonParser extends ParserBase {
063
064        private JsonCreator json;
065        private Map<JsonElement, LocationData> map;
066
067        public JsonParser(IWorkerContext context) {
068                super(context);
069        }
070
071        public Element parse(String source, String type) throws Exception {
072          JsonObject obj = (JsonObject) new com.google.gson.JsonParser().parse(source);
073    String path = "/"+type;
074    StructureDefinition sd = getDefinition(-1, -1, type);
075    if (sd == null)
076      return null;
077
078    Element result = new Element(type, new Property(context, sd.getSnapshot().getElement().get(0), sd));
079    checkObject(obj, path);
080    result.setType(type);
081    parseChildren(path, obj, result, true);
082    result.numberChildren();
083    return result;
084  }
085
086
087        @Override
088        public Element parse(InputStream stream) throws IOException, FHIRException {
089                // if we're parsing at this point, then we're going to use the custom parser
090                map = new IdentityHashMap<JsonElement, LocationData>();
091                String source = TextFile.streamToString(stream);
092                if (policy == ValidationPolicy.EVERYTHING) {
093                        JsonObject obj = null; 
094      try {
095                          obj = JsonTrackingParser.parse(source, map);
096      } catch (Exception e) {  
097                                logError(-1, -1, "(document)", IssueType.INVALID, "Error parsing JSON: "+e.getMessage(), IssueSeverity.FATAL);
098        return null;
099      }
100                  assert (map.containsKey(obj));
101                        return parse(obj);      
102                } else {
103                        JsonObject obj = JsonTrackingParser.parse(source, null); // (JsonObject) new com.google.gson.JsonParser().parse(source);
104//                      assert (map.containsKey(obj));
105                        return parse(obj);      
106                } 
107        }
108
109        public Element parse(JsonObject object, Map<JsonElement, LocationData> map) throws FHIRException {
110                this.map = map;
111                return parse(object);
112        }
113
114  public Element parse(JsonObject object) throws FHIRException {
115                JsonElement rt = object.get("resourceType");
116                if (rt == null) {
117                        logError(line(object), col(object), "$", IssueType.INVALID, "Unable to find resourceType property", IssueSeverity.FATAL);
118                        return null;
119                } else {
120                        String name = rt.getAsString();
121                        String path = name;
122
123                        StructureDefinition sd = getDefinition(line(object), col(object), name);
124                        if (sd == null)
125                                return null;
126
127                        Element result = new Element(name, new Property(context, sd.getSnapshot().getElement().get(0), sd));
128                        checkObject(object, path);
129                        result.markLocation(line(object), col(object));
130                        result.setType(name);
131                        parseChildren(path, object, result, true);
132                        result.numberChildren();
133                        return result;
134                }
135        }
136
137        private void checkObject(JsonObject object, String path) throws FHIRFormatError {
138                if (policy == ValidationPolicy.EVERYTHING) {
139                        boolean found = false;
140                        for (Entry<String, JsonElement> e : object.entrySet()) {
141                                //              if (!e.getKey().equals("fhir_comments")) {
142                                found = true;
143                                break;
144                                //              }
145                        }
146                        if (!found)
147                                logError(line(object), col(object), path, IssueType.INVALID, "Object must have some content", IssueSeverity.ERROR);
148                }
149        }
150
151        private void parseChildren(String path, JsonObject object, Element context, boolean hasResourceType) throws FHIRException {
152                reapComments(object, context);
153                List<Property> properties = context.getProperty().getChildProperties(context.getName(), null);
154                Set<String> processed = new HashSet<String>();
155                if (hasResourceType)
156                        processed.add("resourceType");
157                processed.add("fhir_comments");
158
159                // note that we do not trouble ourselves to maintain the wire format order here - we don't even know what it was anyway
160                // first pass: process the properties
161                for (Property property : properties) {
162                        parseChildItem(path, object, context, processed, property);
163                }
164
165                // second pass: check for things not processed
166                if (policy != ValidationPolicy.NONE) {
167                        for (Entry<String, JsonElement> e : object.entrySet()) {
168                                if (!processed.contains(e.getKey())) {
169                                        logError(line(e.getValue()), col(e.getValue()), path, IssueType.STRUCTURE, "Unrecognised property '@"+e.getKey()+"'", IssueSeverity.ERROR);                     
170                                }
171                        }
172                }
173        }
174
175  public void parseChildItem(String path, JsonObject object, Element context, Set<String> processed, Property property) {
176    if (property.isChoice()) {
177        for (TypeRefComponent type : property.getDefinition().getType()) {
178                String eName = property.getName().substring(0, property.getName().length()-3) + Utilities.capitalize(type.getWorkingCode());
179                if (!isPrimitive(type.getWorkingCode()) && object.has(eName)) {
180                        parseChildComplex(path, object, context, processed, property, eName);
181                        break;
182                } else if (isPrimitive(type.getWorkingCode()) && (object.has(eName) || object.has("_"+eName))) {
183                        parseChildPrimitive(object, context, processed, property, path, eName);
184                        break;
185                }
186        }
187    } else if (property.isPrimitive(property.getType(null))) {
188        parseChildPrimitive(object, context, processed, property, path, property.getName());
189    } else if (object.has(property.getName())) {
190        parseChildComplex(path, object, context, processed, property, property.getName());
191    }
192  }
193
194        private void parseChildComplex(String path, JsonObject object, Element context, Set<String> processed, Property property, String name) throws FHIRException {
195                processed.add(name);
196                String npath = path+"."+property.getName();
197                JsonElement e = object.get(name);
198                if (property.isList() && (e instanceof JsonArray)) {
199                        JsonArray arr = (JsonArray) e;
200                        int c = 0;
201                        for (JsonElement am : arr) {
202                                parseChildComplexInstance(npath+"["+c+"]", object, context, property, name, am);
203                                c++;
204                        }
205                } else {
206                  if (property.isList()) {
207              logError(line(e), col(e), npath, IssueType.INVALID, "This property must be an Array, not "+describeType(e), IssueSeverity.ERROR);
208                  }
209                        parseChildComplexInstance(npath, object, context, property, name, e);
210                }
211        }
212
213        private String describeType(JsonElement e) {
214          if (e.isJsonArray())
215            return "an Array";
216          if (e.isJsonObject())
217      return "an Object";
218    if (e.isJsonPrimitive())
219      return "a primitive property";
220    if (e.isJsonNull())
221      return "a Null";
222    return null;
223  }
224
225  private void parseChildComplexInstance(String npath, JsonObject object, Element context, Property property, String name, JsonElement e) throws FHIRException {
226                if (e instanceof JsonObject) {
227                        JsonObject child = (JsonObject) e;
228                        Element n = new Element(name, property).markLocation(line(child), col(child));
229                        checkObject(child, npath);
230                        context.getChildren().add(n);
231                        if (property.isResource())
232                                parseResource(npath, child, n, property);
233                        else
234                                parseChildren(npath, child, n, false);
235                } else 
236                        logError(line(e), col(e), npath, IssueType.INVALID, "This property must be "+(property.isList() ? "an Array" : "an Object")+", not "+describe(e), IssueSeverity.ERROR);
237        }
238        
239        private String describe(JsonElement e) {
240          if (e instanceof JsonArray) {
241            return "an array";
242          }
243    if (e instanceof JsonObject) {
244      return "an object";
245    }
246    return "a primitive property";
247  }
248
249  private void parseChildPrimitive(JsonObject object, Element context, Set<String> processed, Property property, String path, String name) throws FHIRException {
250                String npath = path+"."+property.getName();
251                processed.add(name);
252                processed.add("_"+name);
253                JsonElement main = object.has(name) ? object.get(name) : null; 
254                JsonElement fork = object.has("_"+name) ? object.get("_"+name) : null;
255                if (main != null || fork != null) {
256                        if (property.isList() && ((main == null) || (main instanceof JsonArray)) &&((fork == null) || (fork instanceof JsonArray)) ) {
257                                JsonArray arr1 = (JsonArray) main;
258                                JsonArray arr2 = (JsonArray) fork;
259                                for (int i = 0; i < Math.max(arrC(arr1), arrC(arr2)); i++) {
260                                        JsonElement m = arrI(arr1, i);
261                                        JsonElement f = arrI(arr2, i);
262                                        parseChildPrimitiveInstance(context, property, name, npath, m, f);
263                                }
264                        } else
265                                parseChildPrimitiveInstance(context, property, name, npath, main, fork);
266                }
267        }
268
269        private JsonElement arrI(JsonArray arr, int i) {
270        return arr == null || i >= arr.size() || arr.get(i) instanceof JsonNull ? null : arr.get(i);
271        }
272
273        private int arrC(JsonArray arr) {
274        return arr == null ? 0 : arr.size();
275        }
276
277        private void parseChildPrimitiveInstance(Element context, Property property, String name, String npath,
278            JsonElement main, JsonElement fork) throws FHIRException {
279                        if (main != null && !(main instanceof JsonPrimitive))
280                                logError(line(main), col(main), npath, IssueType.INVALID, "This property must be an simple value, not "+describe(main), IssueSeverity.ERROR);
281                        else if (fork != null && !(fork instanceof JsonObject))
282                                logError(line(fork), col(fork), npath, IssueType.INVALID, "This property must be an object, not "+describe(fork), IssueSeverity.ERROR);
283                        else {
284                                Element n = new Element(name, property).markLocation(line(main != null ? main : fork), col(main != null ? main : fork));
285                                context.getChildren().add(n);
286                                if (main != null) {
287                                        JsonPrimitive p = (JsonPrimitive) main;
288                                        if (p.isNumber() && p.getAsNumber() instanceof JsonTrackingParser.PresentedBigDecimal) {
289                                          String rawValue = ((JsonTrackingParser.PresentedBigDecimal) p.getAsNumber()).getPresentation();
290                                          n.setValue(rawValue);
291          } else {
292            n.setValue(p.getAsString());
293          }
294                                        if (!n.getProperty().isChoice() && n.getType().equals("xhtml")) {
295                                                try {
296                  n.setXhtml(new XhtmlParser().setValidatorMode(policy == ValidationPolicy.EVERYTHING).parse(n.getValue(), null).getDocumentElement());
297                                                } catch (Exception e) {
298                                                        logError(line(main), col(main), npath, IssueType.INVALID, "Error parsing XHTML: "+e.getMessage(), IssueSeverity.ERROR);
299                                                }
300                                        }
301                                        if (policy == ValidationPolicy.EVERYTHING) {
302                                                // now we cross-check the primitive format against the stated type
303                                                if (Utilities.existsInList(n.getType(), "boolean")) {
304                                                        if (!p.isBoolean())
305                                                                logError(line(main), col(main), npath, IssueType.INVALID, "Error parsing JSON: the primitive value must be a boolean", IssueSeverity.ERROR);
306                                                } else if (Utilities.existsInList(n.getType(), "integer", "unsignedInt", "positiveInt", "decimal")) {
307                                                        if (!p.isNumber())
308                                                                logError(line(main), col(main), npath, IssueType.INVALID, "Error parsing JSON: the primitive value must be a number", IssueSeverity.ERROR);
309                                                } else if (!p.isString())
310                                  logError(line(main), col(main), npath, IssueType.INVALID, "Error parsing JSON: the primitive value must be a string", IssueSeverity.ERROR);
311                                        }
312                                }
313                                if (fork != null) {
314                                        JsonObject child = (JsonObject) fork;
315                                        checkObject(child, npath);
316                                        parseChildren(npath, child, n, false);
317                                }
318                        }
319                }
320
321
322        private void parseResource(String npath, JsonObject res, Element parent, Property elementProperty) throws FHIRException {
323                JsonElement rt = res.get("resourceType");
324                if (rt == null) {
325                        logError(line(res), col(res), npath, IssueType.INVALID, "Unable to find resourceType property", IssueSeverity.FATAL);
326                } else {
327                        String name = rt.getAsString();
328                        StructureDefinition sd = context.fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(name, context.getOverrideVersionNs()));
329                        if (sd == null)
330                                throw new FHIRFormatError("Contained resource does not appear to be a FHIR resource (unknown name '"+name+"')");
331                        parent.updateProperty(new Property(context, sd.getSnapshot().getElement().get(0), sd), SpecialElement.fromProperty(parent.getProperty()), elementProperty);
332                        parent.setType(name);
333                        parseChildren(npath, res, parent, true);
334                }
335        }
336
337        private void reapComments(JsonObject object, Element context) {
338                if (object.has("fhir_comments")) {
339                        JsonArray arr = object.getAsJsonArray("fhir_comments");
340                        for (JsonElement e : arr) {
341                                context.getComments().add(e.getAsString());
342                        }
343                }
344        }
345
346        private int line(JsonElement e) {
347                if (map == null|| !map.containsKey(e))
348                        return -1;
349                else
350                        return map.get(e).getLine();
351        }
352
353        private int col(JsonElement e) {
354                if (map == null|| !map.containsKey(e))
355                        return -1;
356                else
357                        return map.get(e).getCol();
358        }
359
360
361        protected void prop(String name, String value, String link) throws IOException {
362    json.link(link);
363                if (name != null)
364                        json.name(name);
365                json.value(value);
366        }
367
368        protected void open(String name, String link) throws IOException {
369          json.link(link);
370                if (name != null) 
371                        json.name(name);
372                json.beginObject();
373        }
374
375        protected void close() throws IOException {
376                json.endObject();
377        }
378
379        protected void openArray(String name, String link) throws IOException {
380    json.link(link);
381                if (name != null) 
382                        json.name(name);
383                json.beginArray();
384        }
385
386        protected void closeArray() throws IOException {
387                json.endArray();
388        }
389
390
391        @Override
392        public void compose(Element e, OutputStream stream, OutputStyle style, String identity) throws FHIRException, IOException {
393                OutputStreamWriter osw = new OutputStreamWriter(stream, "UTF-8");
394                if (style == OutputStyle.CANONICAL)
395                        json = new JsonCreatorCanonical(osw);
396                else
397                        json = new JsonCreatorGson(osw);
398                json.setIndent(style == OutputStyle.PRETTY ? "  " : "");
399                json.beginObject();
400                prop("resourceType", e.getType(), null);
401                Set<String> done = new HashSet<String>();
402                for (Element child : e.getChildren()) {
403                        compose(e.getName(), e, done, child);
404                }
405                json.endObject();
406                json.finish();
407                osw.flush();
408        }
409
410  public void compose(Element e, JsonCreator json) throws Exception {
411    this.json = json;
412    json.beginObject();
413    
414    prop("resourceType", e.getType(), linkResolver == null ? null : linkResolver.resolveProperty(e.getProperty()));
415    Set<String> done = new HashSet<String>();
416    for (Element child : e.getChildren()) {
417      compose(e.getName(), e, done, child);
418    }
419    json.endObject();
420    json.finish();
421  }
422
423        private void compose(String path, Element e, Set<String> done, Element child) throws IOException {
424          boolean isList = child.hasElementProperty() ? child.getElementProperty().isList() : child.getProperty().isList();
425                if (!isList) {// for specials, ignore the cardinality of the stated type
426                        compose(path, child);
427                } else if (!done.contains(child.getName())) {
428                        done.add(child.getName());
429                        List<Element> list = e.getChildrenByName(child.getName());
430                        composeList(path, list);
431                }
432        }
433
434        private void composeList(String path, List<Element> list) throws IOException {
435                // there will be at least one element
436                String name = list.get(0).getName();
437                boolean complex = true;
438                if (list.get(0).isPrimitive()) {
439                        boolean prim = false;
440                        complex = false;
441                        for (Element item : list) { 
442                                if (item.hasValue())
443                                        prim = true;
444                                if (item.hasChildren())
445                                        complex = true;
446                        }
447                        if (prim) {
448                                openArray(name, linkResolver == null ? null : linkResolver.resolveProperty(list.get(0).getProperty()));
449                                for (Element item : list) { 
450                                        if (item.hasValue())
451                                                primitiveValue(null, item);
452                                        else
453                                                json.nullValue();
454                                }                               
455                                closeArray();
456                        }
457                        name = "_"+name;
458                }
459                if (complex) {
460                        openArray(name, linkResolver == null ? null : linkResolver.resolveProperty(list.get(0).getProperty()));
461                        for (Element item : list) { 
462                                if (item.hasChildren()) {
463                                        open(null,null);
464                                        if (item.getProperty().isResource()) {
465                                                prop("resourceType", item.getType(), linkResolver == null ? null : linkResolver.resolveType(item.getType()));
466                                        }
467                                        Set<String> done = new HashSet<String>();
468                                        for (Element child : item.getChildren()) {
469                                                compose(path+"."+name+"[]", item, done, child);
470                                        }
471                                        close();
472                                } else
473                                        json.nullValue();
474                        }                               
475                        closeArray();
476                }               
477        }
478
479        private void primitiveValue(String name, Element item) throws IOException {
480                if (name != null) {
481                  if (linkResolver != null)
482                    json.link(linkResolver.resolveProperty(item.getProperty()));
483                        json.name(name);
484                }
485                String type = item.getType();
486                if (Utilities.existsInList(type, "boolean"))
487                json.value(item.getValue().trim().equals("true") ? new Boolean(true) : new Boolean(false));
488                else if (Utilities.existsInList(type, "integer", "unsignedInt", "positiveInt"))
489                        json.value(new Integer(item.getValue()));
490                else if (Utilities.existsInList(type, "decimal"))
491                  try {
492                        json.value(new BigDecimal(item.getValue()));
493                  } catch (Exception e) {
494                    throw new NumberFormatException("error writing number '"+item.getValue()+"' to JSON");
495                  }
496                else
497                        json.value(item.getValue());    
498        }
499
500        private void compose(String path, Element element) throws IOException {
501                String name = element.getName();
502                if (element.isPrimitive() || isPrimitive(element.getType())) {
503                        if (element.hasValue())
504                                primitiveValue(name, element);
505                        name = "_"+name;
506                        if (element.getType().equals("xhtml"))
507                          json.anchor("end-xhtml");
508                }
509                if (element.hasChildren()) {
510                        open(name, linkResolver == null ? null : linkResolver.resolveProperty(element.getProperty()));
511                        if (element.getProperty().isResource()) {
512                                prop("resourceType", element.getType(), linkResolver == null ? null : linkResolver.resolveType(element.getType()));
513                        }
514                        Set<String> done = new HashSet<String>();
515                        for (Element child : element.getChildren()) {
516                                compose(path+"."+element.getName(), element, done, child);
517                        }
518                        close();
519                }
520        }
521
522}