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\" 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+": <"+prefixes.get(p)+"> .\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 // // System.out.println("import ttl: "+ttl); 1071 // Lexer lexer = new Lexer(ttl); 1072 // String subject = null; 1073 // String predicate = null; 1074 // while (!lexer.done()) { 1075 // if (subject == null) 1076 // subject = lexer.next(); 1077 // if (predicate == null) 1078 // predicate = lexer.next(); 1079 // if (lexer.peekType() == null) { 1080 // throw new Error("Unexpected end of input parsing turtle"); 1081 // } if (lexer.peekType() == LexerTokenType.TOKEN) { 1082 // sct.triple(subject, predicate, lexer.next()); 1083 // } else if (lexer.peek() == null) { 1084 // throw new Error("Unexected - turtle lexer found no token"); 1085 // } else if (lexer.peek().equals("[")) { 1086 // sct.triple(subject, predicate, importComplex(lexer)); 1087 // } else 1088 // throw new Exception("Not done yet"); 1089 // String n = lexer.next(); 1090 // if (Utilities.noString(n)) 1091 // break; 1092 // if (n.equals(".")) { 1093 // subject = null; 1094 // predicate = null; 1095 // } else if (n.equals(";")) { 1096 // predicate = null; 1097 // } else if (!n.equals(",")) 1098 // throw new Exception("Unexpected token "+n); 1099 // } 1100 // } 1101 // } 1102 1103 public void parse(String source) throws FHIRFormatError { 1104 prefixes.clear(); 1105 prefixes.put("_", "urn:uuid:4425b440-2c33-4488-b9fc-cf9456139995#"); 1106 parse(new Lexer(source)); 1107 } 1108 1109 private void parse(Lexer lexer) throws FHIRFormatError { 1110 boolean doPrefixes = true; 1111 while (!lexer.done()) { 1112 if (doPrefixes && (lexer.peek(LexerTokenType.TOKEN, "@") || lexer.peek(LexerTokenType.WORD, "PREFIX") || lexer.peek(LexerTokenType.WORD, "BASE"))) { 1113 boolean sparqlStyle = false; 1114 boolean base = false; 1115 if (lexer.peek(LexerTokenType.TOKEN, "@")) { 1116 lexer.token("@"); 1117 String p = lexer.word(); 1118 if (p.equals("base")) 1119 base = true; 1120 else if (!p.equals("prefix")) 1121 throw new FHIRFormatError("Unexpected token "+p); 1122 } else { 1123 sparqlStyle = true; 1124 String p = lexer.word(); 1125 if (p.equals("BASE")) 1126 base = true; 1127 else if (!p.equals("PREFIX")) 1128 throw new FHIRFormatError("Unexpected token "+p); 1129 } 1130 String prefix = null; 1131 if (!base) { 1132 prefix = lexer.peekType() == LexerTokenType.WORD ? lexer.next(LexerTokenType.WORD, false) : null; 1133 lexer.token(":"); 1134 } 1135 String url = lexer.next(LexerTokenType.URI, false); 1136 if (!sparqlStyle) 1137 lexer.token("."); 1138 if (!base) 1139 prefix(prefix, url); 1140 else if (this.base == null) 1141 this.base = url; 1142 else 1143 throw new FHIRFormatError("Duplicate @base"); 1144 } else if (lexer.peekType() == LexerTokenType.URI) { 1145 doPrefixes = false; 1146 TTLURL uri = new TTLURL(lexer.startLine, lexer.startCol); 1147 uri.setUri(lexer.uri()); 1148 TTLComplex complex = parseComplex(lexer); 1149 objects.put(uri, complex); 1150 lexer.token("."); 1151 } else if (lexer.peekType() == LexerTokenType.WORD) { 1152 doPrefixes = false; 1153 TTLURL uri = new TTLURL(lexer.startLine, lexer.startCol); 1154 String pfx = lexer.word(); 1155 if (!prefixes.containsKey(pfx)) 1156 throw new FHIRFormatError("Unknown prefix "+pfx); 1157 lexer.token(":"); 1158 uri.setUri(prefixes.get(pfx)+lexer.word()); 1159 TTLComplex complex = parseComplex(lexer); 1160 objects.put(uri, complex); 1161 lexer.token("."); 1162 } else if (lexer.peek(LexerTokenType.TOKEN, ":")) { 1163 doPrefixes = false; 1164 TTLURL uri = new TTLURL(lexer.startLine, lexer.startCol); 1165 lexer.token(":"); 1166 if (!prefixes.containsKey(null)) 1167 throw new FHIRFormatError("Unknown prefix ''"); 1168 uri.setUri(prefixes.get(null)+lexer.word()); 1169 TTLComplex complex = parseComplex(lexer); 1170 objects.put(uri, complex); 1171 lexer.token("."); 1172 } else if (lexer.peek(LexerTokenType.TOKEN, "[")) { 1173 doPrefixes = false; 1174 lexer.token("["); 1175 TTLComplex bnode = parseComplex(lexer); 1176 lexer.token("]"); 1177 TTLComplex complex = null; 1178 if (!lexer.peek(LexerTokenType.TOKEN, ".")) { 1179 complex = parseComplex(lexer); 1180 // at this point, we collapse bnode and complex, and give bnode a fictional identity 1181 bnode.addPredicates(complex.predicates); 1182 } 1183 1184 objects.put(anonymousId(), bnode); 1185 lexer.token("."); 1186 } else 1187 throw lexer.error("Unknown token "+lexer.token); 1188 } 1189 } 1190 1191 private TTLURL anonymousId() throws FHIRFormatError { 1192 TTLURL url = new TTLURL(-1, -1); 1193 url.setUri("urn:uuid:"+UUID.randomUUID().toString().toLowerCase()); 1194 return url; 1195 } 1196 1197 private TTLComplex parseComplex(Lexer lexer) throws FHIRFormatError { 1198 TTLComplex result = new TTLComplex(lexer.startLine, lexer.startCol); 1199 1200 boolean done = lexer.peek(LexerTokenType.TOKEN, "]"); 1201 while (!done) { 1202 String uri = null; 1203 if (lexer.peekType() == LexerTokenType.URI) 1204 uri = lexer.uri(); 1205 else { 1206 String t = lexer.peekType() == LexerTokenType.WORD ? lexer.word() : null; 1207 if (lexer.type == LexerTokenType.TOKEN && lexer.token.equals(":")) { 1208 lexer.token(":"); 1209 if (!prefixes.containsKey(t)) 1210 throw new FHIRFormatError("unknown prefix "+t); 1211 uri = prefixes.get(t)+lexer.word(); 1212 } else if (t.equals("a")) 1213 uri = prefixes.get("rdfs")+"type"; 1214 else 1215 throw lexer.error("unexpected token"); 1216 } 1217 1218 boolean inlist = false; 1219 if (lexer.peek(LexerTokenType.TOKEN, "(")) { 1220 inlist = true; 1221 lexer.token("("); 1222 } 1223 1224 boolean rpt = false; 1225 do { 1226 if (lexer.peek(LexerTokenType.TOKEN, "[")) { 1227 lexer.token("["); 1228 result.addPredicate(uri, parseComplex(lexer)); 1229 lexer.token("]"); 1230 } else if (lexer.peekType() == LexerTokenType.URI) { 1231 TTLURL u = new TTLURL(lexer.startLine, lexer.startCol); 1232 u.setUri(lexer.uri()); 1233 result.addPredicate(uri, u); 1234 } else if (lexer.peekType() == LexerTokenType.LITERAL) { 1235 TTLLiteral u = new TTLLiteral(lexer.startLine, lexer.startCol); 1236 u.value = lexer.literal(); 1237 if (lexer.peek(LexerTokenType.TOKEN, "^")) { 1238 lexer.token("^"); 1239 lexer.token("^"); 1240 if (lexer.peekType() == LexerTokenType.URI) { 1241 u.type = lexer.uri(); 1242 } else { 1243 String l = lexer.word(); 1244 lexer.token(":"); 1245 u.type = prefixes.get(l)+ lexer.word(); 1246 } 1247 } 1248 if (lexer.peek(LexerTokenType.TOKEN, "@")) { 1249 //lang tag - skip it 1250 lexer.token("@"); 1251 String lang = lexer.word(); 1252 if (!lang.matches(LANG_REGEX)) { 1253 throw new FHIRFormatError("Invalid Language tag "+lang); 1254 } 1255 } 1256 result.addPredicate(uri, u); 1257 } else if (lexer.peekType() == LexerTokenType.WORD || lexer.peek(LexerTokenType.TOKEN, ":")) { 1258 int sl = lexer.startLine; 1259 int sc = lexer.startCol; 1260 String pfx = lexer.peekType() == LexerTokenType.WORD ? lexer.word() : null; 1261 if (Utilities.isDecimal(pfx, true, true) && !lexer.peek(LexerTokenType.TOKEN, ":")) { 1262 TTLLiteral u = new TTLLiteral(sl, sc); 1263 u.value = pfx; 1264 result.addPredicate(uri, u); 1265 } else if (("false".equals(pfx) || "true".equals(pfx)) && !lexer.peek(LexerTokenType.TOKEN, ":")) { 1266 TTLLiteral u = new TTLLiteral(sl, sc); 1267 u.value = pfx; 1268 result.addPredicate(uri, u); 1269 } else { 1270 if (!prefixes.containsKey(pfx)) 1271 throw new FHIRFormatError("Unknown prefix "+(pfx == null ? "''" : pfx)); 1272 TTLURL u = new TTLURL(sl, sc); 1273 lexer.token(":"); 1274 u.setUri(prefixes.get(pfx)+lexer.word()); 1275 result.addPredicate(uri, u); 1276 } 1277 } else if (!lexer.peek(LexerTokenType.TOKEN, ";") && (!inlist || !lexer.peek(LexerTokenType.TOKEN, ")"))) { 1278 throw new FHIRFormatError("unexpected token "+lexer.token); 1279 } 1280 1281 if (inlist) 1282 rpt = !lexer.peek(LexerTokenType.TOKEN, ")"); 1283 else { 1284 rpt = lexer.peek(LexerTokenType.TOKEN, ","); 1285 if (rpt) 1286 lexer.readNext(false); 1287 } 1288 } while (rpt); 1289 if (inlist) 1290 lexer.token(")"); 1291 1292 if (lexer.peek(LexerTokenType.TOKEN, ";")) { 1293 while ((lexer.peek(LexerTokenType.TOKEN, ";"))) 1294 lexer.token(";"); 1295 done = lexer.peek(LexerTokenType.TOKEN, ".") || lexer.peek(LexerTokenType.TOKEN, "]"); 1296 } else { 1297 done = true; 1298 } 1299 } 1300 return result; 1301 } 1302 1303 public Map<TTLURL, TTLComplex> getObjects() { 1304 return objects; 1305 } 1306 1307 public TTLComplex getObject(String url) { 1308 for (TTLURL t : objects.keySet()) { 1309 if (t.getUri().equals(url)) 1310 return objects.get(t); 1311 } 1312 return null; 1313 } 1314 1315 // public void parseFragment(Lexer lexer) throws Exception { 1316 // lexer.next(); // read [ 1317 // Complex obj = new Complex(); 1318 // while (!lexer.peek().equals("]")) { 1319 // String predicate = lexer.next(); 1320 // if (lexer.peekType() == LexerTokenType.TOKEN || lexer.peekType() == LexerTokenType.LITERAL) { 1321 // obj.predicate(predicate, lexer.next()); 1322 // } else if (lexer.peek().equals("[")) { 1323 // obj.predicate(predicate, importComplex(lexer)); 1324 // } else 1325 // throw new Exception("Not done yet"); 1326 // if (lexer.peek().equals(";")) 1327 // lexer.next(); 1328 // } 1329 // lexer.next(); // read ] 1330 // //return obj; 1331 // } 1332 // 1333 // public void importTtl(Section sct, String ttl) throws Exception { 1334 // if (!Utilities.noString(ttl)) { 1335 // // System.out.println("import ttl: "+ttl); 1336 // Lexer lexer = new Lexer(ttl); 1337 // String subject = null; 1338 // String predicate = null; 1339 // while (!lexer.done()) { 1340 // if (subject == null) 1341 // subject = lexer.next(); 1342 // if (predicate == null) 1343 // predicate = lexer.next(); 1344 // if (lexer.peekType() == null) { 1345 // throw new Error("Unexpected end of input parsing turtle"); 1346 // } if (lexer.peekType() == LexerTokenType.TOKEN) { 1347 // sct.triple(subject, predicate, lexer.next()); 1348 // } else if (lexer.peek() == null) { 1349 // throw new Error("Unexected - turtle lexer found no token"); 1350 // } else if (lexer.peek().equals("[")) { 1351 // sct.triple(subject, predicate, importComplex(lexer)); 1352 // } else 1353 // throw new Exception("Not done yet"); 1354 // String n = lexer.next(); 1355 // if (Utilities.noString(n)) 1356 // break; 1357 // if (n.equals(".")) { 1358 // subject = null; 1359 // predicate = null; 1360 // } else if (n.equals(";")) { 1361 // predicate = null; 1362 // } else if (!n.equals(",")) 1363 // throw new Exception("Unexpected token "+n); 1364 // } 1365 // } 1366 //} 1367 1368 // private Complex importComplex(Lexer lexer) throws Exception { 1369 // lexer.next(); // read [ 1370 // Complex obj = new Complex(); 1371 // while (!lexer.peek().equals("]")) { 1372 // String predicate = lexer.next(); 1373 // if (lexer.peekType() == LexerTokenType.TOKEN || lexer.peekType() == LexerTokenType.LITERAL) { 1374 // obj.predicate(predicate, lexer.next()); 1375 // } else if (lexer.peek().equals("[")) { 1376 // obj.predicate(predicate, importComplex(lexer)); 1377 // } else 1378 // throw new Exception("Not done yet"); 1379 // if (lexer.peek().equals(";")) 1380 // lexer.next(); 1381 // } 1382 // lexer.next(); // read ] 1383 // return obj; 1384 // } 1385 1386}