001package org.hl7.fhir.utilities.turtle;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032
033
034import java.io.IOException;
035import java.io.OutputStream;
036import java.io.OutputStreamWriter;
037import java.io.UnsupportedEncodingException;
038import java.util.ArrayList;
039import java.util.Collections;
040import java.util.HashMap;
041import java.util.HashSet;
042import java.util.List;
043import java.util.Map;
044import java.util.Set;
045import java.util.UUID;
046
047import org.hl7.fhir.exceptions.FHIRFormatError;
048import org.hl7.fhir.utilities.Utilities;
049
050public class Turtle {
051
052        public static final String GOOD_IRI_CHAR = "a-zA-Z0-9\\x{00A0}-\\x{10FFFF}";
053
054  public static final String IRI_URL = "(([a-z])+:)*((%[0-9a-fA-F]{2})|[&'\\(\\)*+,;:@_~?!$\\/\\-\\#.\\="+GOOD_IRI_CHAR+"])+"; 
055  public static final String LANG_REGEX = "[a-z]{2}(\\-[a-zA-Z]{2})?";
056
057        // Object model
058        public abstract class Triple {
059                private String uri;
060        }
061
062        public class StringType extends Triple {
063                private String value;
064
065                public StringType(String value) {
066                        super();
067                        this.value = value;
068                }
069        }
070
071        public class Complex extends Triple {
072                protected List<Predicate> predicates = new ArrayList<Predicate>();
073
074                public Complex predicate(String predicate, String object) {
075                        return predicate(predicate, object, false);
076                }
077                
078                public Complex predicate(String predicate, String object, boolean asList) {
079                        predicateSet.add(predicate);
080                        objectSet.add(object);
081                        return predicate(predicate, new StringType(object), asList);
082                }
083
084        public Complex linkedPredicate(String predicate, String object, String link, String comment) {
085                return linkedPredicate(predicate, object, link, comment, false);
086        }
087                
088    public Complex linkedPredicate(String predicate, String object, String link, String comment, boolean asList) {
089      predicateSet.add(predicate);
090      objectSet.add(object);
091      return linkedPredicate(predicate, new StringType(object), link, comment, asList);
092    }
093    
094    public Complex predicate(String predicate, Triple object) {
095        return predicate(predicate, object, false);
096    }
097
098        public Complex predicate(String predicate, Triple object, boolean asList) {
099      Predicate p = getPredicate(predicate);
100      if (p == null) {
101        p = new Predicate();
102                        p.predicate = predicate;
103                        p.asList = asList;
104                        predicateSet.add(predicate);
105        predicates.add(p);
106      }
107                        if (object instanceof StringType)
108                                objectSet.add(((StringType) object).value);
109      p.objects.add(object);
110                        return this;
111        }
112
113    protected Predicate getPredicate(String predicate) {
114      for (Predicate p : predicates)
115        if (p.predicate.equals(predicate))
116          return p;
117      return null;
118    }
119    
120    public Complex linkedPredicate(String predicate, Triple object, String link, String comment) {
121        return linkedPredicate(predicate, object, link, comment, false);
122    }
123
124    public Complex linkedPredicate(String predicate, Triple object, String link, String comment, boolean asList) {
125      Predicate p = getPredicate(predicate);
126      if (p == null) {
127        p = new Predicate();
128      p.predicate = predicate;
129      p.link = link;
130      p.comment = comment;
131      p.asList = asList;
132      predicateSet.add(predicate);
133        predicates.add(p);
134      }
135      if (object instanceof StringType)
136        objectSet.add(((StringType) object).value);
137      p.objects.add(object);
138      return this;
139    }
140    
141    public Complex predicate(String predicate) {
142                return predicate(predicate, false);
143        }
144
145        public Complex predicate(String predicate, boolean asList) {
146                predicateSet.add(predicate);
147                Complex c = complex();
148                predicate(predicate, c, asList);
149                return c;
150        }
151
152        public Complex linkedPredicate(String predicate, String link, String comment) {
153                return linkedPredicate(predicate, link, comment, false);
154        }
155        
156    public Complex linkedPredicate(String predicate, String link, String comment, boolean asList) {
157      predicateSet.add(predicate);
158      Complex c = complex();
159      linkedPredicate(predicate, c, link, comment, asList);
160      return c;
161    }
162
163                public void prefix(String code, String url) {
164                        Turtle.this.prefix(code, url);
165                }
166        }
167
168        private class Predicate {
169                protected String predicate;
170                protected String link;
171    protected List<Triple> objects = new ArrayList<Turtle.Triple>();
172                protected String comment;
173                protected boolean asList = false;
174
175                public String getPredicate() {
176                        return predicate;
177                }
178                public String makelink() {
179      if (link == null)
180        return predicate;
181      else
182        return "<a href=\""+link+"\">"+Utilities.escapeXml(predicate)+"</a>";
183    }
184                
185    public List<Triple> getObjects() {
186      return objects;
187                }
188                public String getComment() {
189                        return comment;
190                }
191        }
192
193        public class Subject extends Complex {
194                private String id;
195
196                public Predicate predicate(String predicate, Triple object, String comment) {
197      Predicate p = getPredicate(predicate);
198      if (p == null) {
199        p = new Predicate();
200                        p.predicate = predicate;
201                        predicateSet.add(predicate);
202                        predicates.add(p);
203                        p.comment = comment; 
204      }
205      if (object instanceof StringType)
206        objectSet.add(((StringType) object).value);
207      p.objects.add(object);
208                        return p;
209                }
210
211                public void comment(String comment) {
212                        if (!Utilities.noString(comment)) {
213                                predicate("rdfs:comment", literal(comment), false);
214                                predicate("dcterms:description", literal(comment), false);
215                        }
216                }
217
218                public void label(String label) {
219                        if (!Utilities.noString(label)) {
220                                predicate("rdfs:label", literal(label), false);
221                                predicate("dc:title", literal(label), false);
222                        }
223                }
224
225        }
226
227        public class Section {
228          private List<String> comments = new ArrayList<>();
229                private String name;
230                private List<Subject> subjects = new ArrayList<Subject>();
231
232                public Subject triple(String subject, String predicate, String object, String comment) {
233                        return triple(subject, predicate, new StringType(object), comment);
234                }
235
236                public Subject triple(String subject, String predicate, String object) {
237                        return triple(subject, predicate, new StringType(object));
238                }
239
240                public Subject triple(String subject, String predicate, Triple object) {
241                        return triple(subject, predicate, object, null);     
242                }
243
244                public Subject triple(String subject, String predicate, Triple object, String comment) {
245                        Subject s = subject(subject);
246                        s.predicate(predicate, object, comment);
247                        return s;
248                }
249
250                public void comment(String subject, String comment) {
251                        triple(subject, "rdfs:comment", literal(comment));
252                        triple(subject, "dcterms:description", literal(comment));
253                }
254
255                public void label(String subject, String comment) {
256                        triple(subject, "rdfs:label", literal(comment));
257                        triple(subject, "dc:title", literal(comment));
258                }
259
260                public Subject subject(String subject) {
261                        for (Subject ss : subjects) 
262                                if (ss.id.equals(subject))
263                                        return ss;
264                        Subject s = new Subject();
265                        s.id = subject;
266                        subjects.add(s);
267                        return s;
268                }
269
270    public boolean hasSubject(String subject) {
271      for (Subject ss : subjects) 
272        if (ss.id.equals(subject))
273          return true;
274      return false;
275    }
276
277    public void stringComment(String cnt) {
278      comments.add(cnt);
279    }
280        }
281
282        private List<Section> sections = new ArrayList<Section>();
283        protected Set<String> subjectSet = new HashSet<String>();
284        protected Set<String> predicateSet = new HashSet<String>();
285        protected Set<String> objectSet = new HashSet<String>();
286        protected Map<String, String> prefixes = new HashMap<String, String>();
287
288        public void prefix(String code, String url) {
289                prefixes.put(code, url);
290        }
291
292        protected boolean hasSection(String sn) {
293                for (Section s : sections)
294                        if (s.name.equals(sn))
295                                return true;
296                return false;
297
298        }
299
300        public Section section(String sn) {
301                if (hasSection(sn))
302                        throw new Error("Duplicate section name "+sn);
303                Section s = new Section();
304                s.name = sn;
305                sections.add(s);
306                return s;
307        }
308
309        protected String matches(String url, String prefixUri, String prefix) {
310                if (url.startsWith(prefixUri)) {
311                        prefixes.put(prefix, prefixUri);
312                        return prefix+":"+escape(url.substring(prefixUri.length()), false);
313                }
314                return null;
315        }
316
317        protected Complex complex() {
318                return new Complex();
319        }
320
321        private void checkPrefix(Triple object) {
322                if (object instanceof StringType)
323                        checkPrefix(((StringType) object).value);
324                else {
325                        Complex obj = (Complex) object;
326                        for (Predicate po : obj.predicates) {
327                                checkPrefix(po.getPredicate());
328        for (Triple o : po.getObjects())
329          checkPrefix(o);
330                        }
331                }
332        }
333
334        protected void checkPrefix(String pname) {
335                if (pname.startsWith("("))
336                        return;
337                if (pname.startsWith("\""))
338                        return;
339                if (pname.startsWith("<"))
340                        return;
341
342                if (pname.contains(":")) {
343                        String prefix = pname.substring(0, pname.indexOf(":"));
344                        if (!prefixes.containsKey(prefix) && !prefix.equals("http")&& !prefix.equals("urn"))
345                                throw new Error("undefined prefix "+prefix); 
346                }
347        }
348
349        protected StringType literal(String s) {
350                return new StringType("\""+escape(s, true)+"\"");
351        }
352
353  protected StringType literalTyped(String s, String t) {
354    return new StringType("\""+escape(s, true)+"\"^^xs:"+t);
355  }
356
357        public static String escape(String s, boolean string) {
358                if (s == null)
359                        return "";
360
361                StringBuilder b = new StringBuilder();
362                for (char c : s.toCharArray()) {
363                        if (c == '\r')
364                                b.append("\\r");
365                        else if (c == '\n')
366                                b.append("\\n");
367                        else if (c == '"')
368                                b.append("\\\"");
369                        else if (c == '\\')
370                                b.append("\\\\");
371                        else if (c == '/' && !string)
372                                b.append("\\/");
373                        else 
374                                b.append(c);
375                }   
376                return b.toString();
377        }
378
379        protected String pctEncode(String s) {
380                if (s == null)
381                        return "";
382
383                StringBuilder b = new StringBuilder();
384                for (char c : s.toCharArray()) {
385                        if (c >= 'A' && c <= 'Z')
386                                b.append(c);
387                        else if (c >= 'a' && c <= 'z')
388                                b.append(c);
389                        else if (c >= '0' && c <= '9')
390                                b.append(c);
391                        else if (c == '.')
392                                b.append(c);
393                        else 
394                                b.append("%"+Integer.toHexString(c));
395                }   
396                return b.toString();
397        }
398
399        protected List<String> sorted(Set<String> keys) {
400                List<String> names = new ArrayList<String>();
401                names.addAll(keys);
402                Collections.sort(names);
403                return names;
404        }
405
406        public void commit(OutputStream destination, boolean header) throws IOException {
407                LineOutputStreamWriter writer = new LineOutputStreamWriter(destination);
408                commitPrefixes(writer, header);
409                for (Section s : sections) {
410                        commitSection(writer, s);
411                }
412                writer.ln("# -------------------------------------------------------------------------------------");
413                writer.ln();
414                writer.flush();
415                writer.close();
416        }
417
418  public String asHtml(boolean prism) throws Exception {
419    StringBuilder b = new StringBuilder();
420    b.append("<pre class=\"rdf\" data-fhir=\"generated\" style=\"white-space: pre; overflow: hidden\">"+(prism ? "<code class=\"language-turtle\">" : "")+"\r\n");
421    commitPrefixes(b);
422    for (Section s : sections) {
423      commitSection(b, s);
424    }
425    b.append((prism ? "</code>" : "")+"</pre>\r\n");
426    b.append("\r\n");
427    return b.toString();
428  }
429
430        private void commitPrefixes(LineOutputStreamWriter writer, boolean header) throws IOException {
431                if (header) {
432                        writer.ln("# FHIR Sub-definitions");
433                        writer.write("# This is work in progress, and may change rapidly \r\n");
434                        writer.ln();
435                        writer.write("# A note about policy: the focus here is providing the knowledge from \r\n"); 
436                        writer.write("# the FHIR specification as a set of triples for knowledge processing. \r\n");
437                        writer.write("# Where appopriate, predicates defined external to FHIR are used. \"Where \r\n");
438                        writer.write("# appropriate\" means that the predicates are a faithful representation \r\n");
439                        writer.write("# of the FHIR semantics, and do not involve insane (or owful) syntax. \r\n");
440                        writer.ln();
441                        writer.write("# Where the community agrees on additional predicate statements (such \r\n");
442                        writer.write("# as OWL constraints) these are added in addition to the direct FHIR \r\n");
443                        writer.write("# predicates \r\n");
444                        writer.ln();
445                        writer.write("# This it not a formal ontology, though it is possible it may start to become one eventually\r\n");
446                        writer.ln();
447                        writer.write("# this file refers to concepts defined in rim.ttl and to others defined elsewhere outside HL7 \r\n");
448                        writer.ln();
449                }
450                for (String p : sorted(prefixes.keySet()))
451                        writer.ln("@prefix "+p+": <"+prefixes.get(p)+"> .");
452                writer.ln();
453                if (header) {
454                        writer.ln("# Predicates used in this file:");
455                        for (String s : sorted(predicateSet)) 
456                                writer.ln(" # "+s);
457                        writer.ln();
458                }
459        }
460
461  private void commitPrefixes(StringBuilder b) throws Exception {
462    for (String p : sorted(prefixes.keySet()))
463      b.append("@prefix "+p+": &lt;"+prefixes.get(p)+"&gt; .\r\n");
464    b.append("\r\n");
465  }
466
467        //  private String lastSubject = null;
468        //  private String lastComment = "";
469
470        private void commitSection(LineOutputStreamWriter writer, Section section) throws IOException {
471          writer.ln("# - "+section.name+" "+Utilities.padLeft("", '-', 75-section.name.length()));
472          writer.ln();
473          if (!section.comments.isEmpty()) {
474            for (String s : section.comments) {
475              writer.ln("# "+formatMultilineComment(s));
476            }
477            writer.ln();
478          }
479                for (Subject sbj : section.subjects) {
480      if (Utilities.noString(sbj.id)) {
481        writer.write("[");
482      } else {
483                        writer.write(sbj.id);
484                        writer.write(" ");
485      }
486                        int i = 0;
487
488                        for (Predicate p : sbj.predicates) {
489                          //
490                                writer.write(p.getPredicate());
491                                writer.write(" ");
492        boolean first = true;
493        for (Triple o : p.getObjects()) {
494          if (first) {
495            first = false;
496            if (p.asList) writer.write("( ");
497          } else {
498                  if (!p.asList) writer.write(", ");
499                  else writer.write(" ");
500          }
501          if (o instanceof StringType)
502            writer.write(((StringType) o).value);
503                                else {
504                                        writer.write("[");
505            if (write((Complex) o, writer, 4))
506                                                writer.write("\r\n  ]");
507                                        else
508                                                writer.write("]");
509                                }
510        }
511                                String comment = p.comment == null? "" : " # "+formatMultilineComment(p.comment);
512                                if (p.asList) writer.write(" )");       
513                                i++;
514                                if (i < sbj.predicates.size())
515                                        writer.write(" ;"+comment+"\r\n  ");
516        else {
517          if (Utilities.noString(sbj.id)) 
518            writer.write("]");
519          writer.write(" ."+comment+"\r\n\r\n");
520                }
521          }
522        }
523  }
524        
525  private String formatMultilineComment(String s) {
526          return s.replace("\n", "\n#");
527  }
528
529
530  private void commitSection(StringBuilder b, Section section) throws Exception {
531    b.append("# - "+section.name+" "+Utilities.padLeft("", '-', 75-section.name.length())+"\r\n");
532    b.append("\r\n");
533    if (!section.comments.isEmpty()) {
534      for (String s : section.comments) {
535        b.append("# "+s+"\r\n");      
536      }
537      b.append("\r\n");
538    }
539    for (Subject sbj : section.subjects) {
540      b.append(Utilities.escapeXml(sbj.id));
541      b.append(" ");
542      int i = 0;
543
544      for (Predicate p : sbj.predicates) {
545        //        b.append("# test\r\n ");      
546        b.append(p.makelink());
547        b.append(" ");
548        boolean first = true;
549        for (Triple o : p.getObjects()) {
550        if (first) {
551          first = false;
552          if (p.asList) b.append("( ");
553        } else
554                if (!p.asList) b.append(", ");
555                else b.append(" ");
556          if (o instanceof StringType)
557            b.append(Utilities.escapeXml(((StringType) o).value));
558          else {
559            b.append("[");
560            if (write((Complex) o, b, 4))
561              b.append("\r\n  ]");
562            else
563              b.append("]");
564          }
565        }
566        String comment = p.comment == null? "" : " # "+formatMultilineComment(p.comment);
567        if (p.asList) b.append(" )");
568        i++;
569        if (i < sbj.predicates.size())
570          b.append(" ;"+Utilities.escapeXml(comment)+"\r\n  ");
571        else
572          b.append(" ."+Utilities.escapeXml(comment)+"\r\n\r\n");
573      }
574    }
575  }
576
577        protected class LineOutputStreamWriter extends OutputStreamWriter {
578                private LineOutputStreamWriter(OutputStream out) throws UnsupportedEncodingException {
579                        super(out, "UTF-8");
580                }
581
582                private void ln() throws IOException {
583                        write("\r\n");
584                }
585
586                private void ln(String s) throws IOException {
587                        write(s);
588                        write("\r\n");
589                }
590        }
591
592        public boolean write(Complex complex, LineOutputStreamWriter writer, int indent) throws IOException {
593                if (complex.predicates.isEmpty()) 
594                        return false;
595    if (complex.predicates.size() == 1 && complex.predicates.get(0).getObjects().size()== 1 && complex.predicates.get(0).getObjects().get(0) instanceof StringType && Utilities.noString(complex.predicates.get(0).comment)) {
596      writer.write(" "+complex.predicates.get(0).predicate+" "+((StringType) complex.predicates.get(0).getObjects().get(0)).value);
597                        return false;
598                }
599                String left = Utilities.padLeft("", ' ', indent);
600                int i = 0;
601                for (Predicate po : complex.predicates) {
602                        writer.write("\r\n");
603      boolean first = true;
604      for (Triple o : po.getObjects()) {
605        if (first) {
606          first = false;
607          writer.write(left+" "+po.getPredicate()+" ");
608          if (po.asList) writer.write("( ");
609        } else {
610                  if (!po.asList) writer.write(", ");
611                  else writer.write(" ");
612        }
613        if (o instanceof StringType)
614          writer.write(((StringType) o).value);
615                        else {
616          writer.write("[");
617          if (write((Complex) o, writer, indent+2))
618            writer.write("\r\n"+left+" ]");
619                                else
620                                        writer.write(" ]");
621                        }
622      }
623      if (po.asList) writer.write(" )");
624                        i++;
625                        if (i < complex.predicates.size())
626                                writer.write(" ;");
627                        if (!Utilities.noString(po.comment)) 
628                                writer.write(" # "+escape(po.comment, false));
629                }
630                return true;      
631        }
632
633  public boolean write(Complex complex, StringBuilder b, int indent) throws Exception {
634    if (complex.predicates.isEmpty()) 
635      return false;
636    if (complex.predicates.size() == 1 && complex.predicates.get(0).getObjects().size()== 1 && complex.predicates.get(0).getObjects().get(0) instanceof StringType && Utilities.noString(complex.predicates.get(0).comment)) {
637      b.append(" "+complex.predicates.get(0).makelink()+" "+Utilities.escapeXml(((StringType) complex.predicates.get(0).getObjects().get(0)).value));
638      return false;
639    }
640    String left = Utilities.padLeft("", ' ', indent);
641    int i = 0;
642    for (Predicate po : complex.predicates) {
643      b.append("\r\n");
644      boolean first = true;
645      for (Triple o : po.getObjects()) {
646        if (first) {
647          first = false;
648          if (po.asList) b.append(left+"( ");
649          b.append(po.makelink()+" ");
650        } else
651                if (!po.asList) b.append(", ");
652                else b.append(" ");
653        if (o instanceof StringType)
654          b.append(Utilities.escapeXml(((StringType) o).value));
655      else {
656          b.append("[");
657          if (write((Complex) o, b, indent+2))
658          b.append(left+" ]");
659        else
660          b.append(" ]");
661      }
662      }
663      if (po.asList) b.append(" )");
664      i++;
665      if (i < complex.predicates.size())
666        b.append(" ;");
667      if (!Utilities.noString(po.comment)) 
668        b.append(" # "+Utilities.escapeXml(escape(po.comment, false)));
669    }
670    return true;      
671  }
672
673
674  public abstract class TTLObject {
675                protected int line;
676                protected int col;
677
678    abstract public boolean hasValue(String value);
679
680    public int getLine() {
681      return line;
682    }
683
684    public int getCol() {
685      return col;
686    }
687    
688    
689        }
690
691
692        public class TTLLiteral extends TTLObject {
693
694                private String value;
695                private String type;
696                protected TTLLiteral(int line, int col) {
697                        this.line = line;
698                        this.col = col;
699                }
700    @Override
701    public boolean hasValue(String value) {
702      return value.equals(this.value);
703    }
704    public String getValue() {
705      return value;
706    }
707    public String getType() {
708      return type;
709    }
710
711        }
712
713        public class TTLURL extends TTLObject {
714                private String uri;
715
716                protected TTLURL(int line, int col) {
717                        this.line = line;
718                        this.col = col;
719                }
720
721                public String getUri() {
722                        return uri;
723                }
724
725    public void setUri(String uri) throws FHIRFormatError {
726                        if (!uri.matches(IRI_URL))
727        throw new FHIRFormatError("Illegal URI "+uri);
728                        this.uri = uri;
729                }
730
731    @Override
732    public boolean hasValue(String value) {
733      return value.equals(this.uri);
734        }
735  }
736
737  public class TTLList extends TTLObject {
738    private List<TTLObject> list = new ArrayList<Turtle.TTLObject>();
739
740    public TTLList(TTLObject obj) {
741      super();
742      list.add(obj);
743    }
744    
745    @Override
746    public boolean hasValue(String value) {
747      for (TTLObject obj : list)
748        if (obj.hasValue(value))
749          return true;
750      return false;
751    }
752
753    public List<TTLObject> getList() {
754      return list;
755    }
756    
757  }
758        public class TTLComplex extends TTLObject {
759                private Map<String, TTLObject> predicates = new HashMap<String, Turtle.TTLObject>();
760                protected TTLComplex(int line, int col) {
761                        this.line = line;
762                        this.col = col;
763                }
764    public Map<String, TTLObject> getPredicates() {
765      return predicates;
766        }
767    @Override
768    public boolean hasValue(String value) {
769      return false;
770    }
771    public void addPredicate(String uri, TTLObject obj) {
772      if (!predicates.containsKey(uri))
773        predicates.put(uri, obj);
774      else {
775        TTLObject eo = predicates.get(uri);
776        TTLList list = null; 
777        if (eo instanceof TTLList) 
778          list = (TTLList) eo; 
779        else {
780          list = new TTLList(eo);
781          predicates.put(uri, list);
782        }
783        list.list.add(obj);
784      }
785    }
786    public void addPredicates(Map<String, TTLObject> values) {
787      for (String s : values.keySet()) {
788        addPredicate(s, values.get(s));
789      }
790    }
791  }
792
793  private Map<TTLURL, TTLComplex> objects = new HashMap<TTLURL, Turtle.TTLComplex>();
794
795  private Object base;
796
797        public enum LexerTokenType {
798                TOKEN, // [, ], :, @
799                WORD, // a word 
800                URI, // a URI <>
801                LITERAL // "..."
802        }
803
804        public class Lexer {
805
806
807                private String source;
808                private LexerTokenType type;
809                private int cursor, line, col, startLine, startCol;
810                private String token;
811
812    public Lexer(String source) throws FHIRFormatError {
813                        this.source = source;
814                        cursor = 0;
815                        line = 1;
816                        col = 1;
817      readNext(false);
818                }
819
820                private void skipWhitespace() {
821                        while (cursor < source.length()) {
822                                char ch = source.charAt(cursor);
823                                if (Character.isWhitespace(ch))
824                                        grab();
825                                else if (ch == '#') {
826                                        ch = grab();
827                                        while (cursor < source.length()) {
828                                                ch = grab();
829                                                if (ch == '\r' || ch == '\n') {
830                                                        break;
831                                                }
832                                        }          
833                                } else
834                                        break;
835                        }
836                }
837
838                private char grab() {
839                        char c = source.charAt(cursor);
840                        if (c == '\n') {
841                                line++;
842                                col = 1;
843                        } else
844                                col++;
845
846                        cursor++;
847                        return c;
848                }
849
850    private void readNext(boolean postColon) throws FHIRFormatError {    
851                        token = null;
852                        type = null;
853                        skipWhitespace();
854                        if (cursor >= source.length())
855                                return;
856                        startLine = line;
857                        startCol = col;
858                        char ch = grab();
859                        StringBuilder b = new StringBuilder();
860                        switch (ch) {
861                        case '@':
862                        case '.': 
863                        case ':': 
864                        case ';': 
865                        case '^': 
866                        case ',': 
867                        case ']': 
868                        case '[': 
869                        case '(': 
870                        case ')': 
871                                type = LexerTokenType.TOKEN;
872                                b.append(ch);
873                                token = b.toString();
874                                return;
875                        case '<': 
876                                while (cursor < source.length()) {
877                                        ch = grab();
878                                        if (ch == '>')
879                                                break;
880                                        b.append(ch);
881                                }
882                                type = LexerTokenType.URI;
883                                token = unescape(b.toString(), true);
884                                return;        
885                        case '"': 
886                                b.append(ch);
887                                String end = "\"";
888                                while (cursor < source.length()) {
889                                        ch = grab();
890          if (b.length() == 2 && ch != '"' && b.equals("\"\"")) {
891                                                cursor--;
892                                                break;
893                                        }
894                                        b.append(ch);
895          if (ch == '"')
896                                        if (b.toString().equals("\"\"\""))
897                                                end = "\"\"\"";
898            else if (!b.toString().equals("\"\"") && b.toString().endsWith(end) && !b.toString().endsWith("\\"+end))
899                                                break;
900                                }
901                                type = LexerTokenType.LITERAL;
902                                token = unescape(b.toString().substring(end.length(), b.length()-end.length()), false);
903                                return;        
904                        case '\'': 
905                                b.append(ch);
906                                end = "'";
907                                while (cursor < source.length()) {
908                                        ch = grab();
909                                        if (b.equals("''") && ch != '\'') {
910                                                cursor--;
911                                                break;
912                                        }
913                                        b.append(ch);
914                                        if (b.toString().equals("'''"))
915                                                end = "'''";
916                                        else if (!b.toString().equals("''") && b.toString().endsWith(end))
917                                                break;
918                                }
919                                type = LexerTokenType.LITERAL;
920                                token = unescape(b.toString().substring(end.length(), b.length()-end.length()), false);
921                                return;        
922                        default:
923        if (Utilities.charInRange(ch, '0', '9') || Utilities.charInRange(ch, 'a', 'z') || Utilities.charInRange(ch, 'A', 'Z') || Utilities.charInSet(ch, '_', '-', '+', '%')) {
924                                        b.append(ch);
925                                        while (cursor < source.length()) {
926                                                ch = grab();
927            //                                          if (!Utilities.charInRange(ch, '0', '9') && !Utilities.charInRange(ch, 'a', 'z') && !Utilities.charInRange(ch, 'A', 'Z') && !Utilities.charInSet(ch, '_', '-', '+', '.', '\\', '#'))
928            if (Character.isWhitespace(ch) || Utilities.charInSet(ch, ';', ']', ')', '~') || (( ch == ':') && !postColon))
929                                                        break;
930                                                b.append(ch);
931                                        }
932                                        type = LexerTokenType.WORD;
933                                        token = b.toString();
934                                        cursor--;
935                                        return;        
936                                } else
937                                        throw error("unexpected lexer char "+ch);
938                        }
939                }
940
941    private String unescape(String s, boolean isUri) throws FHIRFormatError {
942                        StringBuilder b = new StringBuilder();
943                        int i = 0;
944                        while (i < s.length()) {
945                                char ch = s.charAt(i);
946                                if (ch == '\\' && i < s.length()-1) {
947                                        i++;
948                                        switch (s.charAt(i)) {
949                                        case 't': 
950                                                b.append('\t');
951                                                break;
952                                        case 'r':
953                                                b.append('\r');
954                                                break;
955                                        case 'n': 
956                                                b.append('\n');
957                                                break;
958                                        case 'f': 
959                                                b.append('\f');
960                                                break;
961                                        case '\'':
962                                                b.append('\'');
963                                                break;
964          case '\"':
965            b.append('\"');
966            break;
967                                        case '\\': 
968                                                b.append('\\');
969                                                break;
970                                        case '/': 
971                                                b.append('\\');
972                                                break;
973                                        case 'U':
974                                        case 'u':
975                                                i++;
976                                                int l = 4;
977                                                String ss = s.substring(i, i+l);
978            int uc = Integer.parseInt(ss, 16);
979                                                if (uc < (isUri ? 33 : 32)) {
980                                                        l = 8;
981                                                        ss = s.substring(i, i+l);
982                                                        uc = Integer.parseInt(ss, 16);
983                                                }
984                                                if (uc < (isUri ? 33 : 32) || (isUri && (uc == 0x3C || uc == 0x3E)))
985              throw new FHIRFormatError("Illegal unicode character");
986                                                try {
987                                                  b.append(Character.toString(uc));
988                                                } catch (Exception e) {
989                                                  throw new FHIRFormatError("Illegal Unicode Sequence: "+ss);
990                                                }
991                                                i = i + l - 1; // -1 cause we're about to i++
992                                                break;
993                                        default:
994            throw new FHIRFormatError("Unknown character escape \\"+s.charAt(i));
995                                        }
996                                } else {
997                                        b.append(ch);
998                                }
999        i++;
1000                        }
1001                        return b.toString();
1002                }
1003
1004                public boolean done() {
1005                        return type == null;
1006                }
1007
1008    public String next(LexerTokenType type, boolean postColon) throws FHIRFormatError {
1009                        if (type != null && this.type != type)
1010                                throw error("Unexpected type. Found "+this.type.toString()+" looking for a "+type.toString());
1011                        String res = token;
1012      readNext(postColon);
1013                        return res;
1014                }
1015
1016                public String peek() throws Exception {
1017                        return token;
1018                }
1019
1020                public LexerTokenType peekType() {
1021                        return type;
1022                }
1023
1024    public void token(String token) throws FHIRFormatError {
1025                        if (!token.equals(this.token))
1026                                throw error("Unexpected word "+this.token+" looking for "+token);
1027      next(LexerTokenType.TOKEN, token.equals(":"));
1028                }
1029
1030                public void word(String word) throws Exception {
1031                        if (!word.equals(this.token))
1032                                throw error("Unexpected word "+this.token+" looking for "+word);
1033      next(LexerTokenType.WORD, false);
1034                }
1035
1036    public String word() throws FHIRFormatError {
1037                        String t = token;
1038      next(LexerTokenType.WORD, false);
1039                        return t;
1040                }
1041
1042    public String uri() throws FHIRFormatError {
1043                        if (this.type != LexerTokenType.URI)
1044                                throw error("Unexpected type. Found "+this.type.toString()+" looking for a URI");
1045                        String t = token;
1046      next(LexerTokenType.URI, false);
1047                        return t;
1048                }
1049
1050    public String literal() throws FHIRFormatError {
1051                        if (this.type != LexerTokenType.LITERAL)
1052                                throw error("Unexpected type. Found "+this.type.toString()+" looking for a Literal");
1053                        String t = token;
1054      next(LexerTokenType.LITERAL, false);
1055                        return t;
1056                }
1057
1058                public boolean peek(LexerTokenType type, String token) {
1059                        return this.type == type && this.token.equals(token);
1060                }
1061
1062    public FHIRFormatError error(String message) {
1063      return new FHIRFormatError("Syntax Error parsing Turtle on line "+Integer.toString(line)+" col "+Integer.toString(col)+": "+message);
1064                }
1065
1066        }
1067        //      
1068        //      public void importTtl(Section sct, String ttl) throws Exception {
1069        //              if (!Utilities.noString(ttl)) {
1070        //                      Lexer lexer = new Lexer(ttl);
1071        //                      String subject = null;
1072        //                      String predicate = null;
1073        //                      while (!lexer.done()) {
1074        //                              if (subject == null)
1075        //                                      subject = lexer.next();
1076        //                              if (predicate == null)
1077        //                                      predicate = lexer.next();
1078        //                              if (lexer.peekType() == null) {
1079        //                                      throw new Error("Unexpected end of input parsing turtle");
1080        //                              } if (lexer.peekType() == LexerTokenType.TOKEN) {
1081        //                                      sct.triple(subject, predicate, lexer.next());
1082        //                              } else if (lexer.peek() == null) {
1083        //                                      throw new Error("Unexected - turtle lexer found no token");
1084        //                              } else if (lexer.peek().equals("[")) {
1085        //                                      sct.triple(subject, predicate, importComplex(lexer));
1086        //                              } else
1087        //                                      throw new Exception("Not done yet");
1088        //                              String n = lexer.next();
1089        //                              if (Utilities.noString(n))
1090        //                                      break;
1091        //                              if (n.equals(".")) {
1092        //                                      subject = null;
1093        //                                      predicate = null;
1094        //                              } else if (n.equals(";")) {
1095        //                                      predicate = null;
1096        //                              } else if (!n.equals(","))
1097        //                                      throw new Exception("Unexpected token "+n);          
1098        //                      }
1099        //              }
1100        //      }
1101
1102  public void parse(String source) throws FHIRFormatError {
1103                prefixes.clear();
1104                prefixes.put("_", "urn:uuid:4425b440-2c33-4488-b9fc-cf9456139995#");
1105                parse(new Lexer(source));
1106        }
1107
1108  private void parse(Lexer lexer) throws FHIRFormatError {
1109                boolean doPrefixes = true;
1110                while (!lexer.done()) {
1111      if (doPrefixes && (lexer.peek(LexerTokenType.TOKEN, "@") || lexer.peek(LexerTokenType.WORD, "PREFIX") || lexer.peek(LexerTokenType.WORD, "BASE"))) {
1112                                boolean sparqlStyle = false;
1113        boolean base = false;
1114                                if (lexer.peek(LexerTokenType.TOKEN, "@")) {
1115                                        lexer.token("@");
1116          String p = lexer.word();
1117          if (p.equals("base"))
1118            base = true;
1119          else if (!p.equals("prefix"))
1120            throw new FHIRFormatError("Unexpected token "+p);  
1121                                } else {
1122                                        sparqlStyle = true;
1123          String p = lexer.word();
1124          if (p.equals("BASE"))
1125            base = true;
1126          else if (!p.equals("PREFIX"))
1127            throw new FHIRFormatError("Unexpected token "+p);  
1128        }
1129        String prefix = null; 
1130        if (!base) {
1131          prefix = lexer.peekType() == LexerTokenType.WORD ? lexer.next(LexerTokenType.WORD, false) : null;
1132                                lexer.token(":");
1133        }
1134        String url = lexer.next(LexerTokenType.URI, false);
1135                                if (!sparqlStyle)
1136                                        lexer.token(".");
1137        if (!base)
1138                                prefix(prefix, url);
1139        else if (this.base == null)
1140          this.base = url;
1141        else
1142          throw new FHIRFormatError("Duplicate @base");  
1143                        } else if (lexer.peekType() == LexerTokenType.URI) {
1144                                doPrefixes = false;
1145                                TTLURL uri = new TTLURL(lexer.startLine, lexer.startCol);
1146                                uri.setUri(lexer.uri());
1147                                TTLComplex complex = parseComplex(lexer);
1148                                objects.put(uri, complex);
1149                                lexer.token(".");
1150                        } else if (lexer.peekType() == LexerTokenType.WORD) {
1151                                doPrefixes = false;
1152                                TTLURL uri = new TTLURL(lexer.startLine, lexer.startCol);
1153                                String pfx = lexer.word();
1154                                if (!prefixes.containsKey(pfx))
1155          throw new FHIRFormatError("Unknown prefix "+pfx);
1156                                lexer.token(":");
1157                                uri.setUri(prefixes.get(pfx)+lexer.word());
1158                                TTLComplex complex = parseComplex(lexer);
1159                                objects.put(uri, complex);
1160                                lexer.token(".");
1161                        } else if (lexer.peek(LexerTokenType.TOKEN, ":")) {
1162                                doPrefixes = false;
1163                                TTLURL uri = new TTLURL(lexer.startLine, lexer.startCol);
1164                                lexer.token(":");
1165                                if (!prefixes.containsKey(null))
1166          throw new FHIRFormatError("Unknown prefix ''");
1167                                uri.setUri(prefixes.get(null)+lexer.word());
1168                                TTLComplex complex = parseComplex(lexer);
1169                                objects.put(uri, complex);
1170                                lexer.token(".");
1171                        } else if (lexer.peek(LexerTokenType.TOKEN, "[")) {
1172                                doPrefixes = false;
1173                                lexer.token("[");
1174                                TTLComplex bnode = parseComplex(lexer);
1175                                lexer.token("]");
1176                                TTLComplex complex = null;
1177        if (!lexer.peek(LexerTokenType.TOKEN, ".")) {
1178                                        complex = parseComplex(lexer);
1179          // at this point, we collapse bnode and complex, and give bnode a fictional identity
1180          bnode.addPredicates(complex.predicates);
1181        }
1182        
1183        objects.put(anonymousId(), bnode);
1184                                lexer.token(".");
1185                        } else 
1186                                throw lexer.error("Unknown token "+lexer.token);
1187                }
1188        }
1189
1190  private TTLURL anonymousId() throws FHIRFormatError {
1191    TTLURL url = new TTLURL(-1, -1);
1192    url.setUri("urn:uuid:"+UUID.randomUUID().toString().toLowerCase());
1193    return url;
1194  }
1195
1196  private TTLComplex parseComplex(Lexer lexer) throws FHIRFormatError {
1197                TTLComplex result = new TTLComplex(lexer.startLine, lexer.startCol);
1198
1199                boolean done = lexer.peek(LexerTokenType.TOKEN, "]");
1200                while (!done) {
1201                        String uri = null;
1202                        if (lexer.peekType() == LexerTokenType.URI)
1203                                uri = lexer.uri();
1204                        else {
1205                                String t = lexer.peekType() == LexerTokenType.WORD ? lexer.word() : null;
1206                                if (lexer.type == LexerTokenType.TOKEN && lexer.token.equals(":")) {
1207                                        lexer.token(":");
1208                                        if (!prefixes.containsKey(t))
1209            throw new FHIRFormatError("unknown prefix "+t);
1210                                        uri = prefixes.get(t)+lexer.word();
1211                                } else if (t.equals("a"))
1212                                        uri = prefixes.get("rdfs")+"type";
1213                                else
1214                                        throw lexer.error("unexpected token");
1215                        }
1216
1217                        boolean inlist = false;
1218                        if (lexer.peek(LexerTokenType.TOKEN, "(")) {
1219                                inlist = true;
1220                                lexer.token("(");
1221                        }
1222
1223                        boolean rpt = false;
1224                        do {
1225                                if (lexer.peek(LexerTokenType.TOKEN, "[")) {
1226                                        lexer.token("[");
1227          result.addPredicate(uri, parseComplex(lexer));
1228                                        lexer.token("]");
1229                                } else if (lexer.peekType() == LexerTokenType.URI) {
1230                                        TTLURL u = new TTLURL(lexer.startLine, lexer.startCol);
1231                                        u.setUri(lexer.uri());
1232          result.addPredicate(uri, u);
1233                                } else if (lexer.peekType() == LexerTokenType.LITERAL) {
1234                                        TTLLiteral u = new TTLLiteral(lexer.startLine, lexer.startCol);
1235                                        u.value = lexer.literal();
1236                                        if (lexer.peek(LexerTokenType.TOKEN, "^")) {
1237                                                lexer.token("^");
1238                                                lexer.token("^");
1239                                                if (lexer.peekType() == LexerTokenType.URI) {
1240                                                        u.type = lexer.uri();
1241                                                } else {
1242                                                        String l = lexer.word();
1243                                                        lexer.token(":");
1244                                                        u.type = prefixes.get(l)+ lexer.word();
1245                                                }
1246                                        }
1247                                        if (lexer.peek(LexerTokenType.TOKEN, "@")) {
1248                                                //lang tag - skip it 
1249                                                lexer.token("@");
1250            String lang = lexer.word();
1251            if (!lang.matches(LANG_REGEX)) {
1252              throw new FHIRFormatError("Invalid Language tag "+lang);
1253            }
1254                                        }
1255          result.addPredicate(uri, u);
1256                                } else if (lexer.peekType() == LexerTokenType.WORD || lexer.peek(LexerTokenType.TOKEN, ":")) {
1257                                        int sl = lexer.startLine;
1258                                        int sc = lexer.startCol;
1259                                        String pfx = lexer.peekType() == LexerTokenType.WORD ? lexer.word() : null;
1260                                        if (Utilities.isDecimal(pfx, true, true) && !lexer.peek(LexerTokenType.TOKEN, ":")) {
1261                                                TTLLiteral u = new TTLLiteral(sl, sc);
1262                                                u.value = pfx;
1263            result.addPredicate(uri, u);                                        
1264                                        } else if (("false".equals(pfx) || "true".equals(pfx)) && !lexer.peek(LexerTokenType.TOKEN, ":")) {
1265                                                TTLLiteral u = new TTLLiteral(sl, sc);
1266                                                u.value = pfx;
1267            result.addPredicate(uri, u);                                        
1268                                        } else {
1269                                                if (!prefixes.containsKey(pfx))
1270              throw new FHIRFormatError("Unknown prefix "+(pfx == null ? "''" : pfx));                                          
1271                                                TTLURL u = new TTLURL(sl, sc);
1272                                                lexer.token(":");
1273                                                u.setUri(prefixes.get(pfx)+lexer.word());
1274            result.addPredicate(uri, u);
1275                                        } 
1276                                } else if (!lexer.peek(LexerTokenType.TOKEN, ";") && (!inlist || !lexer.peek(LexerTokenType.TOKEN, ")"))) {
1277          throw new FHIRFormatError("unexpected token "+lexer.token);
1278                                }
1279
1280                                if (inlist)
1281                                        rpt = !lexer.peek(LexerTokenType.TOKEN, ")");
1282                                else {
1283                                        rpt = lexer.peek(LexerTokenType.TOKEN, ",");
1284                                        if (rpt)
1285            lexer.readNext(false);
1286                                }
1287                        } while (rpt);
1288                        if (inlist)
1289                                lexer.token(")");
1290
1291                        if (lexer.peek(LexerTokenType.TOKEN, ";")) {
1292        while ((lexer.peek(LexerTokenType.TOKEN, ";")))
1293                                lexer.token(";");
1294        done = lexer.peek(LexerTokenType.TOKEN, ".") || lexer.peek(LexerTokenType.TOKEN, "]");
1295                        } else {
1296                                done = true;
1297                        }
1298                }
1299                return result;
1300        }
1301
1302  public Map<TTLURL, TTLComplex> getObjects() {
1303    return objects;
1304  }
1305
1306  public TTLComplex getObject(String url) {
1307    for (TTLURL t : objects.keySet()) {
1308      if (t.getUri().equals(url))
1309        return objects.get(t);
1310    }
1311    return null;
1312  }
1313
1314        //      public void parseFragment(Lexer lexer) throws Exception {
1315        //              lexer.next(); // read [
1316        //              Complex obj = new Complex();
1317        //              while (!lexer.peek().equals("]")) {
1318        //                      String predicate = lexer.next();
1319        //                      if (lexer.peekType() == LexerTokenType.TOKEN || lexer.peekType() == LexerTokenType.LITERAL) {
1320        //                              obj.predicate(predicate, lexer.next());
1321        //                      } else if (lexer.peek().equals("[")) {
1322        //                              obj.predicate(predicate, importComplex(lexer));
1323        //                      } else
1324        //                              throw new Exception("Not done yet");
1325        //                      if (lexer.peek().equals(";")) 
1326        //                              lexer.next();
1327        //              }
1328        //              lexer.next(); // read ]
1329        //              //return obj;
1330        //      }
1331        //
1332        //      public void importTtl(Section sct, String ttl) throws Exception {
1333        //              if (!Utilities.noString(ttl)) {
1334  //                    Lexer lexer = new Lexer(ttl);
1335        //                      String subject = null;
1336        //                      String predicate = null;
1337        //                      while (!lexer.done()) {
1338        //                              if (subject == null)
1339        //                                      subject = lexer.next();
1340        //                              if (predicate == null)
1341        //                                      predicate = lexer.next();
1342        //                              if (lexer.peekType() == null) {
1343        //                                      throw new Error("Unexpected end of input parsing turtle");
1344        //                              } if (lexer.peekType() == LexerTokenType.TOKEN) {
1345        //                                      sct.triple(subject, predicate, lexer.next());
1346        //                              } else if (lexer.peek() == null) {
1347        //                                      throw new Error("Unexected - turtle lexer found no token");
1348        //                              } else if (lexer.peek().equals("[")) {
1349        //                                      sct.triple(subject, predicate, importComplex(lexer));
1350        //                              } else
1351        //                                      throw new Exception("Not done yet");
1352        //                              String n = lexer.next();
1353        //                              if (Utilities.noString(n))
1354        //                                      break;
1355        //                              if (n.equals(".")) {
1356        //                                      subject = null;
1357        //                                      predicate = null;
1358        //                              } else if (n.equals(";")) {
1359        //                                      predicate = null;
1360        //                              } else if (!n.equals(","))
1361        //                                      throw new Exception("Unexpected token "+n);          
1362        //                      }
1363        //              }
1364        //}
1365
1366        //      private Complex importComplex(Lexer lexer) throws Exception {
1367        //              lexer.next(); // read [
1368        //              Complex obj = new Complex();
1369        //              while (!lexer.peek().equals("]")) {
1370        //                      String predicate = lexer.next();
1371        //                      if (lexer.peekType() == LexerTokenType.TOKEN || lexer.peekType() == LexerTokenType.LITERAL) {
1372        //                              obj.predicate(predicate, lexer.next());
1373        //                      } else if (lexer.peek().equals("[")) {
1374        //                              obj.predicate(predicate, importComplex(lexer));
1375        //                      } else
1376        //                              throw new Exception("Not done yet");
1377        //                      if (lexer.peek().equals(";")) 
1378        //                              lexer.next();
1379        //              }
1380        //              lexer.next(); // read ]
1381        //              return obj;
1382        //      }
1383
1384}