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.util.Map;
040import java.util.Stack;
041
042import org.hl7.fhir.utilities.TextFile;
043import org.hl7.fhir.utilities.Utilities;
044
045import com.google.gson.Gson;
046import com.google.gson.GsonBuilder;
047import com.google.gson.JsonArray;
048import com.google.gson.JsonElement;
049import com.google.gson.JsonNull;
050import com.google.gson.JsonObject;
051import com.google.gson.JsonPrimitive;
052
053
054/**
055 * This is created to get a json parser that can track line numbers... grr...
056 * 
057 * @author Grahame Grieve
058 *
059 */
060public class JsonTrackingParser {
061
062        public class PresentedBigDecimal extends BigDecimal {
063
064          public String presentation;
065          
066    public PresentedBigDecimal(String value) {
067      super(value);
068      presentation = value;
069    }
070
071    public String getPresentation() {
072      return presentation;
073    }
074
075  }
076
077  public enum TokenType {
078                Open, Close, String, Number, Colon, Comma, OpenArray, CloseArray, Eof, Null, Boolean;
079        }
080        
081        public class LocationData {
082                private int line;
083                private int col;
084                
085                protected LocationData(int line, int col) {
086                        super();
087                        this.line = line;
088                        this.col = col;
089                }
090                
091                public int getLine() {
092                        return line;
093                }
094                
095                public int getCol() {
096                        return col;
097                }
098                
099                public void newLine() {
100                        line++;
101                        col = 1;                
102                }
103
104                public LocationData copy() {
105                        return new LocationData(line, col);
106                }
107        }
108        
109        private class State {
110                private String name;
111                private boolean isProp;
112                protected State(String name, boolean isProp) {
113                        super();
114                        this.name = name;
115                        this.isProp = isProp;
116                }
117                public String getName() {
118                        return name;
119                }
120                public boolean isProp() {
121                        return isProp;
122                }
123        }
124        
125        private class Lexer {
126                private String source;
127                private int cursor;
128                private String peek;
129                private String value;
130                private TokenType type;
131                private Stack<State> states = new Stack<State>();
132                private LocationData lastLocationBWS;
133                private LocationData lastLocationAWS;
134                private LocationData location;
135                private StringBuilder b = new StringBuilder();
136                
137    public Lexer(String source) throws IOException {
138        this.source = source;
139        cursor = -1;
140        location = new LocationData(1, 1);  
141        start();
142    }
143    
144    private boolean more() {
145        return peek != null || cursor < source.length(); 
146    }
147    
148    private String getNext(int length) throws IOException {
149        String result = "";
150      if (peek != null) {
151        if (peek.length() > length) {
152                result = peek.substring(0, length);
153                peek = peek.substring(length);
154        } else {
155                result = peek;
156                peek = null;
157        }
158      }
159      if (result.length() < length) {
160        int len = length - result.length(); 
161        if (cursor > source.length() - len) 
162                throw error("Attempt to read past end of source");
163        result = result + source.substring(cursor+1, cursor+len+1);
164        cursor = cursor + len;
165      }
166       for (char ch : result.toCharArray())
167        if (ch == '\n')
168          location.newLine();
169        else
170          location.col++;
171      return result;
172    }
173    
174    private char getNextChar() throws IOException {
175      if (peek != null) {
176        char ch = peek.charAt(0);
177        peek = peek.length() == 1 ? null : peek.substring(1);
178        return ch;
179      } else {
180        cursor++;
181        if (cursor >= source.length())
182          return (char) 0;
183        char ch = source.charAt(cursor);
184        if (ch == '\n') {
185          location.newLine();
186        } else {
187          location.col++;
188        }
189        return ch;
190      }
191    }
192    
193    private void push(char ch){
194        peek = peek == null ? String.valueOf(ch) : String.valueOf(ch)+peek;
195    }
196    
197    private void parseWord(String word, char ch, TokenType type) throws IOException {
198      this.type = type;
199      value = ""+ch+getNext(word.length()-1);
200      if (!value.equals(word))
201        throw error("Syntax error in json reading special word "+word);
202    }
203    
204    private IOException error(String msg) {
205      return new IOException("Error parsing JSON source: "+msg+" at Line "+Integer.toString(location.line)+" (path=["+path()+"])");
206    }
207    
208    private String path() {
209      if (states.empty())
210        return value;
211      else {
212        String result = "";
213        for (State s : states) 
214          result = result + '/'+ s.getName();
215        result = result + value;
216        return result;
217      }
218    }
219
220    public void start() throws IOException {
221//      char ch = getNextChar();
222//      if (ch = '\.uEF')
223//      begin
224//        // skip BOM
225//        getNextChar();
226//        getNextChar();
227//      end
228//      else
229//        push(ch);
230      next();
231    }
232    
233    public TokenType getType() {
234        return type;
235    }
236    
237    public String getValue() {
238        return value;
239    }
240
241
242    public LocationData getLastLocationBWS() {
243        return lastLocationBWS;
244    }
245
246    public LocationData getLastLocationAWS() {
247        return lastLocationAWS;
248    }
249
250    public void next() throws IOException {
251        lastLocationBWS = location.copy();
252        char ch;
253        do {
254                ch = getNextChar();
255                if (allowComments && ch == '/') {
256                  char ch1 = getNextChar();
257                  if (ch1 == '/') {
258                    while (more() && !Utilities.charInSet(ch, '\r', '\n')) {
259                      ch = getNextChar();
260                    }
261                  } else {
262                    push(ch1);
263                  }               
264                }
265        } while (more() && Utilities.charInSet(ch, ' ', '\r', '\n', '\t'));
266        lastLocationAWS = location.copy();
267
268        if (!more()) {
269                type = TokenType.Eof;
270        } else {
271                switch (ch) {
272                case '{' : 
273                        type = TokenType.Open;
274                        break;
275                case '}' : 
276                        type = TokenType.Close;
277                        break;
278                case '"' :
279                        type = TokenType.String;
280                        b.setLength(0);
281                        do {
282                                ch = getNextChar();
283                                if (ch == '\\') {
284                                        ch = getNextChar();
285                                        switch (ch) {
286              case '"': b.append('"'); break;
287              case '\'': b.append('\''); break;
288                                        case '\\': b.append('\\'); break;
289                                        case '/': b.append('/'); break;
290                                        case 'n': b.append('\n'); break;
291                                        case 'r': b.append('\r'); break;
292                                        case 't': b.append('\t'); break;
293                                        case 'u': b.append((char) Integer.parseInt(getNext(4), 16)); break;
294                                        default :
295                                                throw error("unknown escape sequence: \\"+ch);
296                                        }
297                                        ch = ' ';
298                                } else if (ch != '"')
299                                        b.append(ch);
300                        } while (more() && (ch != '"'));
301                        if (!more())
302                                throw error("premature termination of json stream during a string");
303                        value = b.toString();
304                        break;
305                case ':' : 
306                        type = TokenType.Colon;
307                        break;
308                case ',' : 
309                        type = TokenType.Comma;
310                        break;
311                case '[' : 
312                        type = TokenType.OpenArray;
313                        break;
314                case ']' : 
315                        type = TokenType.CloseArray;
316                        break;
317                case 't' : 
318                        parseWord("true", ch, TokenType.Boolean);
319                        break;
320                case 'f' : 
321                        parseWord("false", ch, TokenType.Boolean);
322                        break;
323                case 'n' : 
324                        parseWord("null", ch, TokenType.Null);
325                        break;
326                default:
327                        if ((ch >= '0' && ch <= '9') || ch == '-') {
328                                type = TokenType.Number;
329                                b.setLength(0);
330                                while (more() && ((ch >= '0' && ch <= '9') || ch == '-' || ch == '.') || ch == '+' || ch == 'e' || ch == 'E') {
331                                        b.append(ch);
332                                        ch = getNextChar();
333                                }
334                                value = b.toString();
335                                push(ch);
336                        } else
337                                throw error("Unexpected char '"+ch+"' in json stream");
338                }
339        }
340    }
341
342    public String consume(TokenType type) throws IOException {
343      if (this.type != type)
344        throw error("JSON syntax error - found "+this.type.toString()+" expecting "+type.toString());
345      String result = value;
346      next();
347      return result;
348    }
349
350        }
351
352        enum ItemType {
353          Object, String, Number, Boolean, Array, End, Eof, Null;
354        }
355        private Map<JsonElement, LocationData> map;
356  private Lexer lexer;
357  private ItemType itemType = ItemType.Object;
358  private String itemName;
359  private String itemValue;
360  private boolean errorOnDuplicates = true;
361  private boolean allowComments = false;
362
363  public static JsonObject parseJson(String source) throws IOException {
364    return parse(source, null);
365  }
366  
367  public static JsonObject parseJson(InputStream stream) throws IOException {
368    return parse(TextFile.streamToString(stream), null);
369  }
370  
371  public static JsonObject parseJson(byte[] stream) throws IOException {
372    return parse(TextFile.bytesToString(stream), null);
373  }
374  
375  public static JsonObject parseJson(byte[] stream, boolean allowDuplicates) throws IOException {
376    return parse(TextFile.bytesToString(stream), null, allowDuplicates);
377  }
378  
379  public static JsonObject parseJson(File source) throws IOException {
380    return parse(TextFile.fileToString(source), null);
381  }
382  
383  public static JsonObject parseJsonFile(String source) throws IOException {
384    return parse(TextFile.fileToString(source), null);
385  }
386  
387  public static JsonObject parse(String source, Map<JsonElement, LocationData> map) throws IOException {
388    return parse(source, map, false);
389  }
390    
391  public static JsonObject parse(String source, Map<JsonElement, LocationData> map, boolean allowDuplicates) throws IOException {
392    return parse(source, map, allowDuplicates, false);
393  }
394  
395  public static JsonObject parse(String source, Map<JsonElement, LocationData> map, boolean allowDuplicates, boolean allowComments) throws IOException {
396                JsonTrackingParser self = new JsonTrackingParser();
397                self.map = map;
398                self.setErrorOnDuplicates(!allowDuplicates);
399                self.setAllowComments(allowComments);
400    return self.parse(Utilities.stripBOM(source));
401        }
402
403        private JsonObject parse(String source) throws IOException {
404                lexer = new Lexer(source);
405                JsonObject result = new JsonObject();
406                LocationData loc = lexer.location.copy();
407    if (lexer.getType() == TokenType.Open) {
408      lexer.next();
409      lexer.states.push(new State("", false));
410    } 
411    else
412      throw lexer.error("Unexpected content at start of JSON: "+lexer.getType().toString());
413
414    parseProperty();
415    readObject(result, true);
416    if (map != null)
417                  map.put(result, loc);
418    return result;
419        }
420
421        private void readObject(JsonObject obj, boolean root) throws IOException {
422          if (map != null)
423      map.put(obj, lexer.location.copy());
424
425                while (!(itemType == ItemType.End) || (root && (itemType == ItemType.Eof))) {
426                        switch (itemType) {
427                        case Object:
428                                JsonObject child = new JsonObject(); //(obj.path+'.'+ItemName);
429                                LocationData loc = lexer.location.copy();
430              if (!obj.has(itemName))
431                obj.add(itemName, child);
432              else if (errorOnDuplicates)
433                throw lexer.error("Duplicated property name: "+itemName);
434                                next();
435                                readObject(child, false);
436                                if (map != null)
437                      map.put(obj, loc);
438                                break;
439                        case Boolean :
440                                JsonPrimitive v = new JsonPrimitive(Boolean.valueOf(itemValue));
441        if (!obj.has(itemName))
442                                obj.add(itemName, v);
443        else if (errorOnDuplicates)
444          throw lexer.error("Duplicated property name: "+itemName);
445                                if (map != null)
446                      map.put(v, lexer.location.copy());
447                                break;
448                        case String:
449                                v = new JsonPrimitive(itemValue);
450        if (!obj.has(itemName))
451                                obj.add(itemName, v);
452        else if (errorOnDuplicates)
453          throw lexer.error("Duplicated property name: "+itemName);
454                                if (map != null)
455                      map.put(v, lexer.location.copy());
456                                break;
457                        case Number:
458                                v = new JsonPrimitive(new PresentedBigDecimal(itemValue));
459        if (!obj.has(itemName))
460                                obj.add(itemName, v);
461        else if (errorOnDuplicates)
462          throw lexer.error("Duplicated property name: "+itemName);
463                                if (map != null)
464                      map.put(v, lexer.location.copy());
465                                break;
466                        case Null:
467                                JsonNull n = new JsonNull();
468        if (!obj.has(itemName))
469                                obj.add(itemName, n);
470        else if (errorOnDuplicates)
471          throw lexer.error("Duplicated property name: "+itemName);
472                                if (map != null)
473                      map.put(n, lexer.location.copy());
474                                break;
475                        case Array:
476                                JsonArray arr = new JsonArray(); // (obj.path+'.'+ItemName);
477                                loc = lexer.location.copy();
478        if (!obj.has(itemName))
479                                obj.add(itemName, arr);
480        else if (errorOnDuplicates)
481          throw lexer.error("Duplicated property name: "+itemName);
482                                next();
483                                if (!readArray(arr, false))
484                                  next(true);
485                                if (map != null)
486                      map.put(arr, loc);
487                                break;
488                        case Eof : 
489                                throw lexer.error("Unexpected End of File");
490                        case End:
491                                // TODO GG: This isn't handled. Should it be?
492                                break;
493                        }
494                        next();
495                }
496        }
497
498        private boolean readArray(JsonArray arr, boolean root) throws IOException {
499          boolean res = false;
500          while (!((itemType == ItemType.End) || (root && (itemType == ItemType.Eof)))) {
501            res  = true;
502            switch (itemType) {
503            case Object:
504                JsonObject obj  = new JsonObject(); // (arr.path+'['+inttostr(i)+']');
505                                LocationData loc = lexer.location.copy();
506                arr.add(obj);
507              next();
508              readObject(obj, false);
509              if (map != null)
510                map.put(obj, loc);
511              break;
512            case String:
513                JsonPrimitive v = new JsonPrimitive(itemValue);
514                                arr.add(v);
515                                if (map != null)
516                      map.put(v, lexer.location.copy());
517                                break;
518            case Number:
519                v = new JsonPrimitive(new BigDecimal(itemValue));
520                                arr.add(v);
521                                if (map != null)
522                      map.put(v, lexer.location.copy());
523                                break;
524            case Null :
525                JsonNull n = new JsonNull();
526                                arr.add(n);
527                                if (map != null)
528                      map.put(n, lexer.location.copy());
529                                break;
530            case Array:
531        JsonArray child = new JsonArray(); // (arr.path+'['+inttostr(i)+']');
532                                loc = lexer.location.copy();
533                                arr.add(child);
534        next();
535              readArray(child, false);
536              if (map != null)
537                map.put(arr, loc);
538        break;
539            case Eof : 
540                throw lexer.error("Unexpected End of File");
541                 case End:
542                 case Boolean:
543                        // TODO GG: These aren't handled. SHould they be?
544                        break;
545            }
546            next();
547          }
548          return res;
549        }
550
551  private void next() throws IOException {
552    next(false);
553  }
554  
555        private void next(boolean noPop) throws IOException {
556                switch (itemType) {
557                case Object :
558                        lexer.consume(TokenType.Open);
559                        lexer.states.push(new State(itemName, false));
560                        if (lexer.getType() == TokenType.Close) {
561                                itemType = ItemType.End;
562                                lexer.next();
563                        } else
564                                parseProperty();
565                        break;
566                case Null:
567                case String:
568                case Number: 
569                case End: 
570                case Boolean :
571                        if (itemType == ItemType.End && !noPop)
572                                lexer.states.pop();
573                        if (lexer.getType() == TokenType.Comma) {
574                                lexer.next();
575                                parseProperty();
576                        } else if (lexer.getType() == TokenType.Close) {
577                                itemType = ItemType.End;
578                                lexer.next();
579                        } else if (lexer.getType() == TokenType.CloseArray) {
580                                itemType = ItemType.End;
581                                lexer.next();
582                        } else if (lexer.getType() == TokenType.Eof) {
583                                itemType = ItemType.Eof;
584                        } else
585                                throw lexer.error("Unexpected JSON syntax");
586                        break;
587                case Array :
588                        lexer.next();
589                        lexer.states.push(new State(itemName+"[]", true));
590                        parseProperty();
591                        break;
592                case Eof :
593                        throw lexer.error("JSON Syntax Error - attempt to read past end of json stream");
594                default:
595                        throw lexer.error("not done yet (a): "+itemType.toString());
596                }
597        }
598
599        private void parseProperty() throws IOException {
600                if (!lexer.states.peek().isProp) {
601                        itemName = lexer.consume(TokenType.String);
602                        itemValue = null;
603                        lexer.consume(TokenType.Colon);
604                }
605                switch (lexer.getType()) {
606                case Null :
607                        itemType = ItemType.Null;
608                        itemValue = lexer.value;
609                        lexer.next();
610                        break;
611                case String :
612                        itemType = ItemType.String;
613                        itemValue = lexer.value;
614                        lexer.next();
615                        break;
616                case Boolean :
617                        itemType = ItemType.Boolean;
618                        itemValue = lexer.value;
619                        lexer.next();
620                        break;
621                case Number :
622                        itemType = ItemType.Number;
623                        itemValue = lexer.value;
624                        lexer.next();
625                        break;
626                case Open :
627                        itemType = ItemType.Object;
628                        break;
629                case OpenArray :
630                        itemType = ItemType.Array;
631                        break;
632                case CloseArray :
633                        itemType = ItemType.End;
634                        break;
635                        // case Close, , case Colon, case Comma, case OpenArray,       !
636                default:
637                        throw lexer.error("not done yet (b): "+lexer.getType().toString());
638                }
639        }
640
641  public boolean isErrorOnDuplicates() {
642    return errorOnDuplicates;
643  }
644
645  public void setErrorOnDuplicates(boolean errorOnDuplicates) {
646    this.errorOnDuplicates = errorOnDuplicates;
647  }
648
649  
650  public boolean isAllowComments() {
651    return allowComments;
652  }
653
654  public void setAllowComments(boolean allowComments) {
655    this.allowComments = allowComments;
656  }
657
658  public static void write(JsonObject json, File file) throws IOException {
659    Gson gson = new GsonBuilder().setPrettyPrinting().create();
660    String jcnt = gson.toJson(json);
661    TextFile.stringToFile(jcnt, file);    
662  }
663  
664  public static void write(JsonObject json, String fileName) throws IOException {
665    Gson gson = new GsonBuilder().setPrettyPrinting().create();
666    String jcnt = gson.toJson(json);
667    TextFile.stringToFile(jcnt, fileName);    
668  }
669  
670  public static String write(JsonObject json) {
671    Gson gson = new GsonBuilder().setPrettyPrinting().create();
672    return gson.toJson(json);    
673  }
674  
675        
676}