001package org.hl7.fhir.utilities.json;
002
003import java.io.File;
004
005/*
006  Copyright (c) 2011+, HL7, Inc.
007  All rights reserved.
008  
009  Redistribution and use in source and binary forms, with or without modification, 
010  are permitted provided that the following conditions are met:
011    
012   * Redistributions of source code must retain the above copyright notice, this 
013     list of conditions and the following disclaimer.
014   * Redistributions in binary form must reproduce the above copyright notice, 
015     this list of conditions and the following disclaimer in the documentation 
016     and/or other materials provided with the distribution.
017   * Neither the name of HL7 nor the names of its contributors may be used to 
018     endorse or promote products derived from this software without specific 
019     prior written permission.
020  
021  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
022  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
023  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
024  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
025  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
026  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
027  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
028  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
029  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
030  POSSIBILITY OF SUCH DAMAGE.
031  
032 */
033
034
035
036import java.io.IOException;
037import java.io.InputStream;
038import java.math.BigDecimal;
039import java.nio.charset.StandardCharsets;
040import java.util.Arrays;
041import java.util.Map;
042import java.util.Stack;
043
044import org.hl7.fhir.utilities.FileUtilities;
045import org.hl7.fhir.utilities.Utilities;
046import org.hl7.fhir.utilities.http.HTTPResult;
047import org.hl7.fhir.utilities.http.ManagedWebAccess;
048
049import com.google.gson.Gson;
050import com.google.gson.GsonBuilder;
051import com.google.gson.JsonArray;
052import com.google.gson.JsonElement;
053import com.google.gson.JsonNull;
054import com.google.gson.JsonObject;
055import com.google.gson.JsonPrimitive;
056
057
058/**
059 * This is created to get a json parser that can track line numbers... grr...
060 * 
061 * @author Grahame Grieve
062 *
063 */
064public class JsonTrackingParser {
065
066        public class PresentedBigDecimal extends BigDecimal {
067
068          public String presentation;
069          
070    public PresentedBigDecimal(String value) {
071      super(value);
072      presentation = value;
073    }
074
075    public String getPresentation() {
076      return presentation;
077    }
078
079  }
080
081  public enum TokenType {
082                Open, Close, String, Number, Colon, Comma, OpenArray, CloseArray, Eof, Null, Boolean;
083        }
084        
085        public class LocationData {
086                private int line;
087                private int col;
088                
089                protected LocationData(int line, int col) {
090                        super();
091                        this.line = line;
092                        this.col = col;
093                }
094                
095                public int getLine() {
096                        return line;
097                }
098                
099                public int getCol() {
100                        return col;
101                }
102                
103                public void newLine() {
104                        line++;
105                        col = 1;                
106                }
107
108                public LocationData copy() {
109                        return new LocationData(line, col);
110                }
111        }
112        
113        private class State {
114                private String name;
115                private boolean isProp;
116                protected State(String name, boolean isProp) {
117                        super();
118                        this.name = name;
119                        this.isProp = isProp;
120                }
121                public String getName() {
122                        return name;
123                }
124                public boolean isProp() {
125                        return isProp;
126                }
127        }
128        
129        private class Lexer {
130                private String source;
131                private int cursor;
132                private String peek;
133                private String value;
134                private TokenType type;
135                private Stack<State> states = new Stack<State>();
136                private LocationData lastLocationBWS;
137                private LocationData lastLocationAWS;
138                private LocationData location;
139                private StringBuilder b = new StringBuilder();
140                
141    public Lexer(String source) throws IOException {
142        this.source = source;
143        cursor = -1;
144        location = new LocationData(1, 1);  
145        start();
146    }
147    
148    private boolean more() {
149        return peek != null || cursor < source.length(); 
150    }
151    
152    private String getNext(int length) throws IOException {
153        String result = "";
154      if (peek != null) {
155        if (peek.length() > length) {
156                result = peek.substring(0, length);
157                peek = peek.substring(length);
158        } else {
159                result = peek;
160                peek = null;
161        }
162      }
163      if (result.length() < length) {
164        int len = length - result.length(); 
165        if (cursor > source.length() - len) 
166                throw error("Attempt to read past end of source");
167        result = result + source.substring(cursor+1, cursor+len+1);
168        cursor = cursor + len;
169      }
170       for (char ch : result.toCharArray())
171        if (ch == '\n')
172          location.newLine();
173        else
174          location.col++;
175      return result;
176    }
177    
178    private char getNextChar() throws IOException {
179      if (peek != null) {
180        char ch = peek.charAt(0);
181        peek = peek.length() == 1 ? null : peek.substring(1);
182        return ch;
183      } else {
184        cursor++;
185        if (cursor >= source.length())
186          return (char) 0;
187        char ch = source.charAt(cursor);
188        if (ch == '\n') {
189          location.newLine();
190        } else {
191          location.col++;
192        }
193        return ch;
194      }
195    }
196    
197    private void push(char ch){
198        peek = peek == null ? String.valueOf(ch) : String.valueOf(ch)+peek;
199    }
200    
201    private void parseWord(String word, char ch, TokenType type) throws IOException {
202      this.type = type;
203      value = ""+ch+getNext(word.length()-1);
204      if (!value.equals(word))
205        throw error("Syntax error in json reading special word "+word);
206    }
207    
208    private IOException error(String msg) {
209      return new IOException("Error parsing JSON source: "+msg+" at Line "+Integer.toString(location.line)+" (path=["+path()+"])");
210    }
211    
212    private String path() {
213      if (states.empty())
214        return value;
215      else {
216        String result = "";
217        for (State s : states) 
218          result = result + '/'+ s.getName();
219        result = result + value;
220        return result;
221      }
222    }
223
224    public void start() throws IOException {
225//      char ch = getNextChar();
226//      if (ch = '\.uEF')
227//      begin
228//        // skip BOM
229//        getNextChar();
230//        getNextChar();
231//      end
232//      else
233//        push(ch);
234      next();
235    }
236    
237    public TokenType getType() {
238        return type;
239    }
240    
241    public String getValue() {
242        return value;
243    }
244
245
246    public LocationData getLastLocationBWS() {
247        return lastLocationBWS;
248    }
249
250    public LocationData getLastLocationAWS() {
251        return lastLocationAWS;
252    }
253
254    public void next() throws IOException {
255        lastLocationBWS = location.copy();
256        char ch;
257        do {
258                ch = getNextChar();
259                if (allowComments && ch == '/') {
260                  char ch1 = getNextChar();
261                  if (ch1 == '/') {
262                    while (more() && !Utilities.charInSet(ch, '\r', '\n')) {
263                      ch = getNextChar();
264                    }
265                  } else {
266                    push(ch1);
267                  }               
268                }
269        } while (more() && Utilities.charInSet(ch, ' ', '\r', '\n', '\t'));
270        lastLocationAWS = location.copy();
271
272        if (!more()) {
273                type = TokenType.Eof;
274        } else {
275                switch (ch) {
276                case '{' : 
277                        type = TokenType.Open;
278                        break;
279                case '}' : 
280                        type = TokenType.Close;
281                        break;
282                case '"' :
283                        type = TokenType.String;
284                        b.setLength(0);
285                        do {
286                                ch = getNextChar();
287                                if (ch == '\\') {
288                                        ch = getNextChar();
289                                        switch (ch) {
290              case '"': b.append('"'); break;
291              case '\'': b.append('\''); break;
292                                        case '\\': b.append('\\'); break;
293                                        case '/': b.append('/'); break;
294                                        case 'n': b.append('\n'); break;
295                                        case 'r': b.append('\r'); break;
296                                        case 't': b.append('\t'); break;
297                                        case 'u': b.append((char) Integer.parseInt(getNext(4), 16)); break;
298                                        default :
299                                                throw error("unknown escape sequence: \\"+ch);
300                                        }
301                                        ch = ' ';
302                                } else if (ch != '"')
303                                        b.append(ch);
304                        } while (more() && (ch != '"'));
305                        if (!more())
306                                throw error("premature termination of json stream during a string");
307                        value = b.toString();
308                        break;
309                case ':' : 
310                        type = TokenType.Colon;
311                        break;
312                case ',' : 
313                        type = TokenType.Comma;
314                        break;
315                case '[' : 
316                        type = TokenType.OpenArray;
317                        break;
318                case ']' : 
319                        type = TokenType.CloseArray;
320                        break;
321                case 't' : 
322                        parseWord("true", ch, TokenType.Boolean);
323                        break;
324                case 'f' : 
325                        parseWord("false", ch, TokenType.Boolean);
326                        break;
327                case 'n' : 
328                        parseWord("null", ch, TokenType.Null);
329                        break;
330                default:
331                        if ((ch >= '0' && ch <= '9') || ch == '-') {
332                                type = TokenType.Number;
333                                b.setLength(0);
334                                while (more() && ((ch >= '0' && ch <= '9') || ch == '-' || ch == '.') || ch == '+' || ch == 'e' || ch == 'E') {
335                                        b.append(ch);
336                                        ch = getNextChar();
337                                }
338                                value = b.toString();
339                                push(ch);
340                        } else
341                                throw error("Unexpected char '"+ch+"' in json stream");
342                }
343        }
344    }
345
346    public String consume(TokenType type) throws IOException {
347      if (this.type != type)
348        throw error("JSON syntax error - found "+this.type.toString()+" expecting "+type.toString());
349      String result = value;
350      next();
351      return result;
352    }
353
354        }
355
356        enum ItemType {
357          Object, String, Number, Boolean, Array, End, Eof, Null;
358        }
359        private Map<JsonElement, LocationData> map;
360  private Lexer lexer;
361  private ItemType itemType = ItemType.Object;
362  private String itemName;
363  private String itemValue;
364  private boolean errorOnDuplicates = true;
365  private boolean allowComments = false;
366
367  public static JsonObject parseJson(String source) throws IOException {
368    return parse(source, null);
369  }
370  
371  public static JsonObject parseJson(InputStream stream) throws IOException {
372    return parse(FileUtilities.streamToString(stream), null);
373  }
374  
375  public static JsonObject parseJson(byte[] stream) throws IOException {
376    return parse(FileUtilities.bytesToString(stream), null);
377  }
378  
379  public static JsonArray parseJsonArray(byte[] stream) throws IOException {
380    return parseArray(FileUtilities.bytesToString(stream), null);
381  }
382  
383  public static JsonObject parseJson(byte[] stream, boolean allowDuplicates) throws IOException {
384    return parse(FileUtilities.bytesToString(stream), null, allowDuplicates);
385  }
386  
387  public static JsonObject parseJson(File source) throws IOException {
388    return parse(FileUtilities.fileToString(source), null);
389  }
390  
391  public static JsonObject parseJsonFile(String source) throws IOException {
392    return parse(FileUtilities.fileToString(source), null);
393  }
394  
395  public static JsonObject parse(String source, Map<JsonElement, LocationData> map) throws IOException {
396    return parse(source, map, false);
397  }
398    
399  public static JsonArray parseArray(String source, Map<JsonElement, LocationData> map) throws IOException {
400    return parseArray(source, map, false);
401  }
402    
403    
404  public static JsonObject parse(String source, Map<JsonElement, LocationData> map, boolean allowDuplicates) throws IOException {
405    return parse(source, map, allowDuplicates, false);
406  }
407  
408  public static JsonArray parseArray(String source, Map<JsonElement, LocationData> map, boolean allowDuplicates) throws IOException {
409    return parseArray(source, map, allowDuplicates, false);
410  }
411  
412  public static JsonObject parse(String source, Map<JsonElement, LocationData> map, boolean allowDuplicates, boolean allowComments) throws IOException {
413    JsonTrackingParser self = new JsonTrackingParser();
414    self.map = map;
415    self.setErrorOnDuplicates(!allowDuplicates);
416    self.setAllowComments(allowComments);
417    return self.parse(Utilities.stripBOM(source));
418  }
419
420  public static JsonArray parseArray(String source, Map<JsonElement, LocationData> map, boolean allowDuplicates, boolean allowComments) throws IOException {
421    JsonTrackingParser self = new JsonTrackingParser();
422    self.map = map;
423    self.setErrorOnDuplicates(!allowDuplicates);
424    self.setAllowComments(allowComments);
425    return self.parseArray(Utilities.stripBOM(source));
426  }
427
428        private JsonObject parse(String source) throws IOException {
429                lexer = new Lexer(source);
430                JsonObject result = new JsonObject();
431                LocationData loc = lexer.location.copy();
432    if (lexer.getType() == TokenType.Open) {
433      lexer.next();
434      lexer.states.push(new State("", false));
435    } 
436    else
437      throw lexer.error("Unexpected content at start of JSON: "+lexer.getType().toString());
438
439    if (lexer.getType() != TokenType.Close) {
440      parseProperty();
441      readObject(result, true);
442    }
443    if (map != null)
444                  map.put(result, loc);
445    return result;
446        }
447
448  private JsonArray parseArray(String source) throws IOException {
449    return new Gson().fromJson(source, JsonArray.class);
450  }
451
452        private void readObject(JsonObject obj, boolean root) throws IOException {
453          if (map != null)
454      map.put(obj, lexer.location.copy());
455
456                while (!(itemType == ItemType.End) || (root && (itemType == ItemType.Eof))) {
457                        switch (itemType) {
458                        case Object:
459                                JsonObject child = new JsonObject(); //(obj.path+'.'+ItemName);
460                                LocationData loc = lexer.location.copy();
461              if (!obj.has(itemName))
462                obj.add(itemName, child);
463              else if (errorOnDuplicates)
464                throw lexer.error("Duplicated property name: "+itemName);
465                                next();
466                                readObject(child, false);
467                                if (map != null)
468                      map.put(obj, loc);
469                                break;
470                        case Boolean :
471                                JsonPrimitive v = new JsonPrimitive(Boolean.valueOf(itemValue));
472        if (!obj.has(itemName))
473                                obj.add(itemName, v);
474        else if (errorOnDuplicates)
475          throw lexer.error("Duplicated property name: "+itemName);
476                                if (map != null)
477                      map.put(v, lexer.location.copy());
478                                break;
479                        case String:
480                                v = new JsonPrimitive(itemValue);
481        if (!obj.has(itemName))
482                                obj.add(itemName, v);
483        else if (errorOnDuplicates)
484          throw lexer.error("Duplicated property name: "+itemName);
485                                if (map != null)
486                      map.put(v, lexer.location.copy());
487                                break;
488                        case Number:
489                                v = new JsonPrimitive(new PresentedBigDecimal(itemValue));
490        if (!obj.has(itemName))
491                                obj.add(itemName, v);
492        else if (errorOnDuplicates)
493          throw lexer.error("Duplicated property name: "+itemName);
494                                if (map != null)
495                      map.put(v, lexer.location.copy());
496                                break;
497                        case Null:
498                                JsonNull n = new JsonNull();
499        if (!obj.has(itemName))
500                                obj.add(itemName, n);
501        else if (errorOnDuplicates)
502          throw lexer.error("Duplicated property name: "+itemName);
503                                if (map != null)
504                      map.put(n, lexer.location.copy());
505                                break;
506                        case Array:
507                                JsonArray arr = new JsonArray(); // (obj.path+'.'+ItemName);
508                                loc = lexer.location.copy();
509        if (!obj.has(itemName))
510                                obj.add(itemName, arr);
511        else if (errorOnDuplicates)
512          throw lexer.error("Duplicated property name: "+itemName);
513                                next();
514                                if (!readArray(arr, false))
515                                  next(true);
516                                if (map != null)
517                      map.put(arr, loc);
518                                break;
519                        case Eof : 
520                                throw lexer.error("Unexpected End of File");
521                        case End:
522                                // TODO GG: This isn't handled. Should it be?
523                                break;
524                        }
525                        next();
526                }
527        }
528
529        private boolean readArray(JsonArray arr, boolean root) throws IOException {
530          boolean res = false;
531          while (!((itemType == ItemType.End) || (root && (itemType == ItemType.Eof)))) {
532            res  = true;
533            switch (itemType) {
534            case Object:
535                JsonObject obj  = new JsonObject(); // (arr.path+'['+inttostr(i)+']');
536                                LocationData loc = lexer.location.copy();
537                arr.add(obj);
538              next();
539              readObject(obj, false);
540              if (map != null)
541                map.put(obj, loc);
542              break;
543            case String:
544                JsonPrimitive v = new JsonPrimitive(itemValue);
545                                arr.add(v);
546                                if (map != null)
547                      map.put(v, lexer.location.copy());
548                                break;
549            case Number:
550                v = new JsonPrimitive(new BigDecimal(itemValue));
551                                arr.add(v);
552                                if (map != null)
553                      map.put(v, lexer.location.copy());
554                                break;
555            case Null :
556                JsonNull n = new JsonNull();
557                                arr.add(n);
558                                if (map != null)
559                      map.put(n, lexer.location.copy());
560                                break;
561            case Array:
562        JsonArray child = new JsonArray(); // (arr.path+'['+inttostr(i)+']');
563                                loc = lexer.location.copy();
564                                arr.add(child);
565        next();
566              readArray(child, false);
567              if (map != null)
568                map.put(arr, loc);
569        break;
570            case Eof : 
571                throw lexer.error("Unexpected End of File");
572                 case End:
573                 case Boolean:
574                        // TODO GG: These aren't handled. SHould they be?
575                        break;
576            }
577            next();
578          }
579          return res;
580        }
581
582  private void next() throws IOException {
583    next(false);
584  }
585  
586        private void next(boolean noPop) throws IOException {
587                switch (itemType) {
588                case Object :
589                        lexer.consume(TokenType.Open);
590                        lexer.states.push(new State(itemName, false));
591                        if (lexer.getType() == TokenType.Close) {
592                                itemType = ItemType.End;
593                                lexer.next();
594                        } else
595                                parseProperty();
596                        break;
597                case Null:
598                case String:
599                case Number: 
600                case End: 
601                case Boolean :
602                        if (itemType == ItemType.End && !noPop)
603                                lexer.states.pop();
604                        if (lexer.getType() == TokenType.Comma) {
605                                lexer.next();
606                                parseProperty();
607                        } else if (lexer.getType() == TokenType.Close) {
608                                itemType = ItemType.End;
609                                lexer.next();
610                        } else if (lexer.getType() == TokenType.CloseArray) {
611                                itemType = ItemType.End;
612                                lexer.next();
613                        } else if (lexer.getType() == TokenType.Eof) {
614                                itemType = ItemType.Eof;
615                        } else
616                                throw lexer.error("Unexpected JSON syntax");
617                        break;
618                case Array :
619                        lexer.next();
620                        lexer.states.push(new State(itemName+"[]", true));
621                        parseProperty();
622                        break;
623                case Eof :
624                        throw lexer.error("JSON Syntax Error - attempt to read past end of json stream");
625                default:
626                        throw lexer.error("not done yet (a): "+itemType.toString());
627                }
628        }
629
630        private void parseProperty() throws IOException {
631                if (!lexer.states.peek().isProp) {
632                        itemName = lexer.consume(TokenType.String);
633                        itemValue = null;
634                        lexer.consume(TokenType.Colon);
635                }
636                switch (lexer.getType()) {
637                case Null :
638                        itemType = ItemType.Null;
639                        itemValue = lexer.value;
640                        lexer.next();
641                        break;
642                case String :
643                        itemType = ItemType.String;
644                        itemValue = lexer.value;
645                        lexer.next();
646                        break;
647                case Boolean :
648                        itemType = ItemType.Boolean;
649                        itemValue = lexer.value;
650                        lexer.next();
651                        break;
652                case Number :
653                        itemType = ItemType.Number;
654                        itemValue = lexer.value;
655                        lexer.next();
656                        break;
657                case Open :
658                        itemType = ItemType.Object;
659                        break;
660                case OpenArray :
661                        itemType = ItemType.Array;
662                        break;
663                case CloseArray :
664                        itemType = ItemType.End;
665                        break;
666                        // case Close, , case Colon, case Comma, case OpenArray,       !
667                default:
668                        throw lexer.error("not done yet (b): "+lexer.getType().toString());
669                }
670        }
671
672  public boolean isErrorOnDuplicates() {
673    return errorOnDuplicates;
674  }
675
676  public void setErrorOnDuplicates(boolean errorOnDuplicates) {
677    this.errorOnDuplicates = errorOnDuplicates;
678  }
679
680  
681  public boolean isAllowComments() {
682    return allowComments;
683  }
684
685  public void setAllowComments(boolean allowComments) {
686    this.allowComments = allowComments;
687  }
688
689  public static void write(JsonObject json, File file) throws IOException {
690    Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
691    String jcnt = gson.toJson(json);
692    FileUtilities.stringToFile(jcnt, file);    
693  }
694    
695  public static void write(JsonObject json, File file, boolean pretty) throws IOException {
696    Gson gson = pretty ? new GsonBuilder().setPrettyPrinting().create() : new GsonBuilder().create();
697    String jcnt = gson.toJson(json);
698    FileUtilities.stringToFile(jcnt, file);    
699  }
700    
701  public static void write(JsonObject json, String fileName) throws IOException {
702    Gson gson = new GsonBuilder().setPrettyPrinting().create();
703    String jcnt = gson.toJson(json);
704    FileUtilities.stringToFile(jcnt, fileName);    
705  }
706  
707  public static String write(JsonObject json) {
708    Gson gson = new GsonBuilder().setPrettyPrinting().create();
709    return gson.toJson(json);    
710  }
711
712  public static String writeDense(JsonObject json) {
713    Gson gson = new GsonBuilder().create();
714    return gson.toJson(json);    
715  }
716
717  public static byte[] writeBytes(JsonObject json, boolean pretty) {
718    if (pretty) {
719      Gson gson = new GsonBuilder().setPrettyPrinting().create();
720      return gson.toJson(json).getBytes(StandardCharsets.UTF_8);    
721    } else {
722      Gson gson = new GsonBuilder().create();
723      return gson.toJson(json).getBytes(StandardCharsets.UTF_8);    
724    }    
725  }
726
727  public static byte[] writeBytes(JsonArray json, boolean pretty) {
728    if (pretty) {
729      Gson gson = new GsonBuilder().setPrettyPrinting().create();
730      return gson.toJson(json).getBytes(StandardCharsets.UTF_8);    
731    } else {
732      Gson gson = new GsonBuilder().create();
733      return gson.toJson(json).getBytes(StandardCharsets.UTF_8);    
734    }    
735  }
736  
737  public static JsonObject fetchJson(String source) throws IOException {
738    HTTPResult res = ManagedWebAccess.get(Arrays.asList("web"), source+"?nocache=" + System.currentTimeMillis(), "application/json, application/fhir+json");
739    res.checkThrowException();
740    return parseJson(res.getContent());
741  }
742  
743  public static JsonArray fetchJsonArray(String source) throws IOException {
744    HTTPResult res = ManagedWebAccess.get(Arrays.asList("web"),source+"?nocache=" + System.currentTimeMillis(), "application/json, application/fhir+json");
745    res.checkThrowException();
746    return parseJsonArray(res.getContent());
747  }
748        
749}