001package org.hl7.fhir.utilities.xhtml;
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.InputStream;
036import java.io.InputStreamReader;
037import java.io.Reader;
038import java.io.StringReader;
039import java.nio.charset.StandardCharsets;
040import java.util.ArrayList;
041import java.util.HashMap;
042import java.util.HashSet;
043import java.util.List;
044import java.util.Map;
045import java.util.Set;
046
047import org.hl7.fhir.exceptions.FHIRException;
048import org.hl7.fhir.exceptions.FHIRFormatError;
049import org.hl7.fhir.utilities.xhtml.XhtmlNode.Location;
050import org.w3c.dom.Attr;
051import org.w3c.dom.Element;
052import org.w3c.dom.Node;
053import org.xmlpull.v1.XmlPullParser;
054import org.xmlpull.v1.XmlPullParserException;
055
056public class XhtmlParser {
057  public static final String XHTML_NS = "http://www.w3.org/1999/xhtml";
058
059  public class NSMap {
060    private Map<String, String> nslist = new HashMap<String, String>();
061
062    public NSMap(NSMap nsm) {
063      if (nsm != null)
064        nslist.putAll(nsm.nslist);
065    }
066
067    public void def(String ns) {
068      nslist.put("", ns);
069    }
070
071    public void ns(String abbrev, String ns) {
072      nslist.put(abbrev, ns);  
073    }
074
075    public String def() {
076      return nslist.get("");
077    }
078
079    public boolean hasDef() {
080      return nslist.containsKey("");
081    }
082
083    public String get(String abbrev) {
084      return nslist.containsKey(abbrev) ? nslist.get(abbrev) : "http://error/undefined-namespace";
085    }
086  }
087
088  public class QName {
089    private String ns;
090    private String name;
091
092    public QName(String src) {
093      if (src.contains(":")) {
094        ns = src.substring(0, src.indexOf(":"));
095        name = src.substring(src.indexOf(":")+1);
096      } else {
097        ns = null;
098        name = src;
099      }
100    }
101
102    public String getName() {
103      return name;
104    }
105
106    public boolean hasNs() {
107      return ns != null;
108    }
109
110    public String getNs() {
111      return ns;
112    }
113
114    @Override
115    public String toString() {
116      return ns+"::"+name;
117    }
118    
119  }
120
121  private Set<String> elements = new HashSet<String>();
122  private Set<String> attributes = new HashSet<String>();  
123  private Map<String, String> entities = new HashMap<>();
124  
125  
126  public XhtmlParser() {
127  super();
128  policy = ParserSecurityPolicy.Accept; // for general parsing
129  
130  // set up sets
131  elements.add("p");
132  elements.add("br");
133  elements.add("div");
134  elements.add("h1");
135  elements.add("h2");
136  elements.add("h3");
137  elements.add("h4");
138  elements.add("h5");
139  elements.add("h6");
140  elements.add("a");
141  elements.add("span");
142  elements.add("b");
143  elements.add("em");
144  elements.add("i");
145  elements.add("strong");
146  elements.add("small");
147  elements.add("big");
148  elements.add("tt");
149  elements.add("small");
150  elements.add("dfn");
151  elements.add("q");
152  elements.add("var");
153  elements.add("abbr");
154  elements.add("acronym");
155  elements.add("cite");
156  elements.add("blockquote");
157  elements.add("hr");
158  elements.add("address");
159  elements.add("bdo");
160  elements.add("kbd");
161  elements.add("q");
162  elements.add("sub");
163  elements.add("sup");
164  elements.add("ul");
165  elements.add("ol");
166  elements.add("li");
167  elements.add("dl");
168  elements.add("dt");
169  elements.add("dd");
170  elements.add("pre");
171  elements.add("table");
172  elements.add("caption");
173  elements.add("colgroup");
174  elements.add("col");
175  elements.add("thead");
176  elements.add("tr");
177  elements.add("tfoot");
178  elements.add("tbody");
179  elements.add("th");
180  elements.add("td");
181  elements.add("code");
182  elements.add("samp");
183  elements.add("img");
184  elements.add("map");
185  elements.add("area");
186  
187  attributes.add("title");
188  attributes.add("style");
189  attributes.add("class");
190  attributes.add("id");
191  attributes.add("lang");
192  attributes.add("xml:lang");
193  attributes.add("dir");
194  attributes.add("accesskey");
195  attributes.add("tabindex");
196    // tables:
197  attributes.add("span");
198  attributes.add("width");
199  attributes.add("align");
200  attributes.add("valign");
201  attributes.add("char");
202  attributes.add("charoff");
203  attributes.add("abbr");
204  attributes.add("axis");
205  attributes.add("headers");
206  attributes.add("scope");
207  attributes.add("rowspan");
208  attributes.add("colspan");
209
210  attributes.add("a.href");
211  attributes.add("a.name");
212  attributes.add("img.src");
213  attributes.add("img.border");
214  attributes.add("div.xmlns");
215  attributes.add("blockquote.cite");
216  attributes.add("q.cite");
217  attributes.add("a.charset");
218  attributes.add("a.type");
219  attributes.add("a.name");
220  attributes.add("a.href");
221  attributes.add("a.hreflang");
222  attributes.add("a.rel");
223  attributes.add("a.rev");
224  attributes.add("a.shape");
225  attributes.add("a.coords");
226  attributes.add("img.src");
227  attributes.add("img.alt");
228  attributes.add("img.longdesc");
229  attributes.add("img.height");
230  attributes.add("img.width");
231  attributes.add("img.usemap");
232  attributes.add("img.ismap");
233  attributes.add("map.name");
234  attributes.add("area.shape");
235  attributes.add("area.coords");
236  attributes.add("area.href");
237  attributes.add("area.nohref");
238  attributes.add("area.alt");
239  attributes.add("table.summary");
240  attributes.add("table.width");
241  attributes.add("table.border");
242  attributes.add("table.frame");
243  attributes.add("table.rules");
244  attributes.add("table.cellspacing");
245  attributes.add("table.cellpadding");
246}
247
248public enum ParserSecurityPolicy {
249    Accept,
250    Drop,
251    Reject
252  }
253
254  private ParserSecurityPolicy policy;
255  
256  private boolean trimWhitespace;
257  private boolean mustBeWellFormed = true;
258  private boolean validatorMode;
259  
260  public boolean isTrimWhitespace() {
261    return trimWhitespace;
262  }
263
264  public void setTrimWhitespace(boolean trimWhitespace) {
265    this.trimWhitespace = trimWhitespace;
266  }
267
268  public boolean isMustBeWellFormed() {
269    return mustBeWellFormed;
270  }
271
272  public XhtmlParser setMustBeWellFormed(boolean mustBeWellFormed) {
273    this.mustBeWellFormed = mustBeWellFormed;
274    return this;
275  }
276  
277
278  public boolean isValidatorMode() {
279    return validatorMode;
280  }
281
282  public XhtmlParser setValidatorMode(boolean validatorMode) {
283    this.validatorMode = validatorMode;
284    return this;
285  }
286
287  public ParserSecurityPolicy getPolicy() {
288   return policy;
289  }
290
291  public void setPolicy(ParserSecurityPolicy policy) {
292  this.policy = policy; 
293  }
294
295  public XhtmlNode parseHtmlNode(Element node) throws FHIRFormatError  {
296    return parseHtmlNode(node, null);
297  }
298  
299  public XhtmlNode parseHtmlNode(Element node, String defaultNS) throws FHIRFormatError  {
300    XhtmlNode res = parseNode(node, defaultNS);
301    if (res.getNsDecl() == null)
302      res.getAttributes().put("xmlns", XHTML_NS);
303    return res;
304  }
305
306  private XhtmlNode parseNode(Element node, String defaultNS) throws FHIRFormatError  {
307    XhtmlNode res = new XhtmlNode(NodeType.Element);
308    res.setName(node.getLocalName());
309    defaultNS = checkNS(res, node, defaultNS);
310    for (int i = 0; i < node.getAttributes().getLength(); i++) {
311      Attr attr = (Attr) node.getAttributes().item(i);
312      if (attributeIsOk(res.getName(), attr.getName(), attr.getValue()) && !attr.getLocalName().startsWith("xmlns"))
313        res.getAttributes().put(attr.getName(), attr.getValue());
314    }
315    Node child = node.getFirstChild();
316    while (child != null) {
317      if (child.getNodeType() == Node.TEXT_NODE) {
318        res.addText(child.getTextContent());
319      } else if (child.getNodeType() == Node.COMMENT_NODE) {
320        res.addComment(child.getTextContent());
321      } else if (child.getNodeType() == Node.ELEMENT_NODE) {
322        if (elementIsOk(child.getLocalName()))
323          res.getChildNodes().add(parseNode((Element) child, defaultNS));
324      } else
325        throw new FHIRFormatError("Unhandled XHTML feature: "+Integer.toString(child.getNodeType())+descLoc());
326      child = child.getNextSibling();
327    }
328    return res;
329  }  
330
331  private String checkNS(XhtmlNode res, Element node, String defaultNS) {
332    if (!validatorMode)
333      return null;
334    String ns = node.getNamespaceURI();
335    if (ns == null)
336      return null;
337    if (!ns.equals(defaultNS)) {
338      res.getAttributes().put("xmlns", ns);
339      return ns;
340    }
341    return defaultNS;
342  }
343
344  public XhtmlNode parseHtmlNode(XmlPullParser xpp) throws XmlPullParserException, IOException, FHIRFormatError  {
345    XhtmlNode res = parseNode(xpp);
346    if (res.getNsDecl() == null)
347      res.getAttributes().put("xmlns", XHTML_NS);
348    return res;
349
350  }
351  private XhtmlNode parseNode(XmlPullParser xpp) throws XmlPullParserException, IOException, FHIRFormatError  {
352    XhtmlNode res = new XhtmlNode(NodeType.Element);
353    res.setName(xpp.getName());
354    
355    for (int i = 0; i < xpp.getAttributeCount(); i++) {
356      String an = "xml".equals(xpp.getAttributePrefix(i)) ? "xml:"+xpp.getAttributeName(i) : xpp.getAttributeName(i);
357      String av = xpp.getAttributeValue(i);
358      if (attributeIsOk(xpp.getName(), an, av)) {
359        res.getAttributes().put(an, av);
360      }
361    }
362    int eventType = xpp.next();
363    while (eventType != XmlPullParser.END_TAG) {
364      if (eventType == XmlPullParser.TEXT) {
365        res.addText(xpp.getText());
366        xpp.next();
367      } else if (eventType == XmlPullParser.COMMENT) {
368        res.addComment(xpp.getText());
369        xpp.next();
370      } else if (eventType == XmlPullParser.START_TAG) {
371        if (elementIsOk(xpp.getName()))
372          res.getChildNodes().add(parseNode(xpp));
373      } else
374        throw new FHIRFormatError("Unhandled XHTML feature: "+Integer.toString(eventType)+descLoc());
375      eventType = xpp.getEventType();
376    }
377    xpp.next();
378    return res;
379  }  
380
381  private boolean attributeIsOk(String elem, String attr, String value) throws FHIRFormatError  {
382    if (validatorMode)
383      return true;
384  boolean ok = attributes.contains(attr) || attributes.contains(elem+"."+attr);
385  if (ok)
386    return true;
387  else switch (policy) {
388    case Accept: return true;
389    case Drop: return false;
390    case Reject: throw new FHIRFormatError("Illegal HTML attribute "+elem+"."+attr);
391  }
392
393  if ((elem+"."+attr).equals("img.src") && !(value.startsWith("#") || value.startsWith("http:") || value.startsWith("https:"))) {
394    switch (policy) {
395      case Accept: return true;
396      case Drop: return false;
397      case Reject: throw new FHIRFormatError("Illegal Image Reference "+value);
398    }
399  }
400  return false;
401  }
402
403private boolean elementIsOk(String name) throws FHIRFormatError  {
404    if (validatorMode)
405      return true;
406    boolean ok = elements.contains(name);
407  if (ok)
408      return true;
409  else switch (policy) {
410    case Accept: return true;
411    case Drop: return false;
412    case Reject: throw new FHIRFormatError("Illegal HTML element "+name);
413  }
414  return false;
415}
416
417  private String descLoc() {
418    return " at line "+Integer.toString(line)+" column "+Integer.toString(col);
419  }
420
421  private Reader rdr;
422  private String cache = "";
423  private XhtmlNode unwindPoint;
424  private String lastText = "";
425  private int line = 1;
426  private int col = 0;
427  private char lastChar;
428  private Location lastLoc;
429  
430  public XhtmlDocument parse(String source, String entryName) throws FHIRFormatError, IOException  {
431    rdr = new StringReader(source);
432    return parse(entryName);
433  }
434  
435  public XhtmlDocument parse(InputStream input, String entryName) throws FHIRFormatError, IOException  {
436    rdr = new InputStreamReader(input, StandardCharsets.UTF_8);
437    return parse(entryName);
438  }
439   
440  private XhtmlDocument parse(String entryName) throws FHIRFormatError, IOException 
441  {
442    XhtmlDocument result = new XhtmlDocument();
443    skipWhiteSpaceAndComments(result);
444    if (peekChar() != '<')
445      throw new FHIRFormatError("Unable to Parse HTML - does not start with tag. Found "+peekChar()+descLoc());
446    readChar();
447    markLocation();
448    QName n = new QName(readName().toLowerCase());
449    if ((entryName != null) && !n.getName().equals(entryName))
450      throw new FHIRFormatError("Unable to Parse HTML - starts with '"+n+"' not '"+entryName+"'"+descLoc());
451    XhtmlNode root = result.addTag(n.getName());
452    root.setLocation(markLocation());
453    parseAttributes(root);
454    markLocation();
455    NSMap nsm = checkNamespaces(n, root, null, true);
456    if (readChar() == '/') {
457      if (peekChar() != '>')
458        throw new FHIRFormatError("unexpected non-end of element "+n+" "+descLoc());
459      readChar();
460    } else {
461      unwindPoint = null;
462      List<XhtmlNode> p = new ArrayList<>();
463      parseElementInner(root, p, nsm, true);
464    }
465    return result;
466  }
467  
468  private Location markLocation() {
469    Location res = lastLoc;
470    lastLoc = new Location(line, col);
471    return res;
472  }
473
474  private NSMap checkNamespaces(QName n, XhtmlNode node, NSMap nsm, boolean root) {
475    // what we do here is strip out any stated namespace attributes, putting them in the namesapce map
476    // then we figure out what the namespace of this element is, and state it explicitly if it's not the default
477    
478    NSMap result = new NSMap(nsm);
479    List<String> nsattrs = new ArrayList<String>();
480    for (String an : node.getAttributes().keySet()) {
481      if (an.equals("xmlns")) {
482        result.def(node.getAttribute(an));
483        nsattrs.add(an);
484      }
485      if (an.startsWith("xmlns:")) {
486        result.ns(an.substring(6), node.getAttribute(an));
487        nsattrs.add(an);
488      }
489    }
490    for (String s : nsattrs)
491      node.getAttributes().remove(s);
492    if (n.hasNs()) {
493      String nns = result.get(n.getNs());
494      if (!nns.equals(result.def())) {
495        node.getAttributes().put("xmlns", nns);
496        result.def(nns);
497      }
498    } else if (root && result.hasDef()) {
499      node.getAttributes().put("xmlns", result.def());
500    }
501    return result;
502  }
503
504  private void addTextNode(XhtmlNode node, StringBuilder s)
505  {
506    String t = isTrimWhitespace() ? s.toString().trim() : s.toString();
507    if (t.length() > 0)
508    {
509      lastText = t;
510      // System.out.println(t);
511      node.addText(t).setLocation(markLocation());
512      s.setLength(0);
513    }
514  }
515  private void parseElementInner(XhtmlNode node, List<XhtmlNode> parents, NSMap nsm, boolean escaping) throws FHIRFormatError, IOException 
516  {
517    StringBuilder s = new StringBuilder();
518    while (peekChar() != '\0' && !parents.contains(unwindPoint) && !(node == unwindPoint))
519    {
520      if (peekChar() == '<')
521      {
522        addTextNode(node, s);
523        readChar();
524        if (peekChar() == '!') {
525          String sc = readToCommentEnd();
526          // moved the validator
527//          if (sc.startsWith("DOCTYPE"))
528//            throw new FHIRFormatError("Malformed XHTML: Found a DocType declaration, and these are not allowed (XXE security vulnerability protection)");
529          node.addComment(sc).setLocation(markLocation());
530        } else if (peekChar() == '?')
531          node.addComment(readToTagEnd()).setLocation(markLocation());
532        else if (peekChar() == '/') {
533          readChar();
534          QName n = new QName(readToTagEnd());
535          if (node.getName().equals(n.getName()))
536            return;
537          else
538          {
539            if (mustBeWellFormed)
540              throw new FHIRFormatError("Malformed XHTML: Found \"</"+n.getName()+">\" expecting \"</"+node.getName()+">\""+descLoc());
541            for (int i = parents.size() - 1; i >= 0; i--)
542            {
543              if (parents.get(i).getName().equals(n))
544                unwindPoint = parents.get(i);
545            }
546            if (unwindPoint != null)
547            {
548              for (int i = parents.size(); i > 0; i--)
549              {
550                if (i < parents.size() && parents.get(i) == unwindPoint)
551                  return;
552                if (i == parents.size())
553                {
554                  parents.get(i - 1).getChildNodes().addAll(node.getChildNodes());
555                  node.getChildNodes().clear();
556                }
557                else
558                {
559                  parents.get(i - 1).getChildNodes().addAll(parents.get(i).getChildNodes());
560                  parents.get(i).getChildNodes().clear();
561                }
562              }
563            }
564          }
565        }
566        else if (Character.isLetterOrDigit(peekChar()))
567        {
568          parseElement(node, parents, nsm);
569        }
570        else
571          throw new FHIRFormatError("Unable to Parse HTML - node '" + node.getName() + "' has unexpected content '"+peekChar()+"' (last text = '"+lastText+"'"+descLoc());
572      }
573      else if (peekChar() == '&') // escaping && 
574      {
575        parseLiteral(s);
576      }
577      else
578        s.append(readChar());
579    }
580    addTextNode(node, s);
581  }
582
583  private void parseElement(XhtmlNode parent, List<XhtmlNode> parents, NSMap nsm) throws IOException, FHIRFormatError 
584  {
585    markLocation();
586    QName name = new QName(readName());
587    XhtmlNode node = parent.addTag(name.getName());
588    node.setLocation(markLocation());
589    List<XhtmlNode> newParents = new ArrayList<XhtmlNode>();
590    newParents.addAll(parents);
591    newParents.add(parent);
592    parseAttributes(node);
593    markLocation();
594    nsm = checkNamespaces(name, node, nsm, false);
595    if (readChar() == '/') {
596      if (peekChar() != '>')
597        throw new FHIRFormatError("unexpected non-end of element "+name+" "+descLoc());
598      readChar();
599    } else {
600       parseElementInner(node, newParents, nsm, "script".equals(name.getName()));
601    }
602  }
603  
604  private void parseAttributes(XhtmlNode node) throws FHIRFormatError, IOException 
605  {
606    while (Character.isWhitespace(peekChar()))
607      readChar();
608    while (peekChar() != '>' && peekChar() != '/' && peekChar() != '\0')
609    {
610      String name = readName();
611      if (name.length() == 0)
612      {
613        throw new FHIRFormatError("Unable to read attribute on <"+node.getName()+">"+descLoc());
614      }
615      while (Character.isWhitespace(peekChar()))
616        readChar();
617
618      if (isNameChar(peekChar()) || peekChar() == '>' || peekChar() == '/')
619        node.getAttributes().put(name, null);
620      else if (peekChar() != '=')
621      {
622        throw new FHIRFormatError("Unable to read attribute '"+name+"' value on <"+node.getName()+">"+descLoc());
623      }
624      else
625      {
626        readChar();
627        while (Character.isWhitespace(peekChar()))
628          readChar();
629        if (peekChar() == '"' || peekChar() == '\'')
630          node.getAttributes().put(name, parseAttributeValue(readChar()));
631        else
632          node.getAttributes().put(name, parseAttributeValue('\0'));
633      }
634      while (Character.isWhitespace(peekChar()))
635        readChar();
636    }
637  }
638
639  private String parseAttributeValue(char term) throws IOException, FHIRFormatError 
640  {
641    StringBuilder b = new StringBuilder();
642    while (peekChar() != '\0' && peekChar() != '>' && (term != '\0' || peekChar() != '/') && peekChar() != term)
643    {
644      if (peekChar() == '&')
645      {
646        parseLiteral(b);
647      }
648      else
649        b.append(readChar());
650    }
651    if (peekChar() == term)
652      readChar();
653    return b.toString();
654  }
655
656  
657  private void skipWhiteSpaceAndComments(XhtmlNode focus) throws IOException, FHIRFormatError  {
658    while (Character.isWhitespace(peekChar()) || (peekChar() == 0xfeff))
659      readChar();
660    if (peekChar() == '<')
661    {
662      char ch = readChar();
663      if (peekChar() == '!') {
664        readChar();
665        if (peekChar() == '-') {
666          readChar();
667          if (peekChar() == '-') {
668            readChar();
669            if (peekChar() == ' ')
670              readChar();
671            focus.addComment(readToCommentEnd());
672          } else 
673            throw new FHIRFormatError("unrecognised element type <!"+peekChar()+descLoc());
674        } else
675          focus.addDocType(readToDocTypeEnd());
676        skipWhiteSpaceAndComments(focus);
677      } else if (peekChar() == '?') {
678        String r = readToTagEnd();
679        focus.addInstruction(r.substring(1, r.length()-1));
680        skipWhiteSpaceAndComments(focus);
681      } else 
682        pushChar(ch);
683    }
684  }  
685  
686  private void skipWhiteSpace() throws IOException {
687    if (trimWhitespace)
688      while (Character.isWhitespace(peekChar()) || (peekChar() == 0xfeff))
689        readChar();
690  }
691  
692  private void skipWhiteSpaceInternal() throws IOException {
693    while (Character.isWhitespace(peekChar()) || (peekChar() == 0xfeff))
694      readChar();
695  }
696  
697  private void pushChar(char ch) {
698    cache = Character.toString(ch)+cache;    
699  }
700
701  private char peekChar() throws IOException
702  {
703    if (cache.length() > 0)
704      return cache.charAt(0);
705    else if (!rdr.ready())
706      return '\0';
707    else
708    {
709      char c = (char)rdr.read();
710      if (c == (char)-1)
711      {
712        cache = "";
713        return '\0';
714      }
715      cache =  Character.toString(c);
716      return c;
717    }
718  }
719
720  private char readChar() throws IOException
721  {
722    char c;
723    if (cache.length() > 0)
724    {
725      c = cache.charAt(0);
726      cache = cache.length() == 1 ? "" : cache.substring(1);
727    }
728    else if (!rdr.ready())
729      c = '\0';
730    else
731      c = (char)rdr.read();
732    if (c == '\r' || c == '\n') {
733      if (c == '\r' || lastChar != '\r') {
734        line++;
735        col = 0;
736      }  
737      lastChar = c;      
738    }      
739    col++;
740    return c;
741  }
742
743  private String readToTagEnd() throws IOException, FHIRFormatError 
744  {
745    StringBuilder s = new StringBuilder();
746    while (peekChar() != '>' && peekChar() != '\0')
747      s.append(readChar());
748    if (peekChar() != '\0')
749    {
750      readChar();
751      skipWhiteSpace();
752    } else if (mustBeWellFormed)
753      throw new FHIRFormatError("Unexpected termination of html source"+descLoc());
754    return s.toString();
755  }
756
757  private String readToDocTypeEnd() throws IOException, FHIRFormatError 
758  {
759    StringBuilder s = new StringBuilder();
760      
761    boolean done = false;
762    while (!done) {
763      char c = peekChar();
764      if (c == '>') {
765        done = true;
766        readChar();
767      } else if (c != '\0')
768        s.append(readChar());
769      else if (mustBeWellFormed)
770        throw new FHIRFormatError("Unexpected termination of html source"+descLoc());
771    }
772    return s.toString();
773  }
774
775  private String readToCommentEnd() throws IOException, FHIRFormatError 
776  {
777    if (peekChar() == '!')
778      readChar();
779    StringBuilder s = new StringBuilder();
780      
781    boolean simple = true;
782    if (peekChar() == '-') {
783      readChar();
784      simple = peekChar() != '-';
785      if (simple)
786        s.append('-');
787      else
788        readChar();
789    }
790    
791    boolean doctypeEntities = false;
792    
793    boolean done = false;
794    while (!done) {
795      char c = peekChar();
796      if (c == '-') {
797        readChar();
798        if (peekChar() == '-') {
799          readChar();
800          if (peekChar() == '>') {
801            done = true;
802          } else
803            s.append("--");
804        } else 
805          s.append('-');
806      } else if (doctypeEntities && c == ']') {
807        s.append(readChar());
808        if (peekChar() == '>') {
809          done = true;
810        }
811      } else if (simple && peekChar() == '>' && !doctypeEntities) {
812        done = true;
813      } else if (c == '[' && s.toString().startsWith("DOCTYPE ")) {
814        doctypeEntities = true;
815        s.append(readChar());
816      } else if (c != '\0')
817        s.append(readChar());
818      else if (mustBeWellFormed)
819        throw new FHIRFormatError("Unexpected termination of html source"+descLoc());
820    }
821    if (peekChar() != '\0')
822    {
823      readChar();
824      skipWhiteSpace();
825    }
826    if (doctypeEntities) {
827      parseDoctypeEntities(s.toString());
828    }
829    return s.toString();
830  }
831
832  private void parseDoctypeEntities(String s) {
833    while (s.contains("<!ENTITY")) {
834      s = s.substring(s.indexOf("<!ENTITY"));
835      int e = s.indexOf(">");
836      String ed = s.substring(0, e+1);
837      s = s.substring(e+1);
838      ed = ed.substring(8).trim();
839      e = ed.indexOf(" ");
840      String n = ed.substring(0, e).trim();
841      ed = ed.substring(e).trim();
842      e = ed.indexOf(" "); // SYSTEM
843      ed = ed.substring(e).trim();
844      String v = ed.substring(0, ed.length()-1);
845      entities.put(n, v);
846    }
847    
848  }
849
850  private boolean isNameChar(char ch)
851  {
852    return Character.isLetterOrDigit(ch) || ch == '_' || ch == '-' || ch == ':';
853  }
854
855  private String readName() throws IOException
856  {
857    StringBuilder s = new StringBuilder();
858    while (isNameChar(peekChar()))
859      s.append(readChar());
860    return s.toString();
861  }
862
863  private String readUntil(char ch) throws IOException
864  {
865    StringBuilder s = new StringBuilder();
866    while (peekChar() != 0 && peekChar() != ch)
867      s.append(readChar());
868    readChar();
869    return s.toString();
870  }
871  
872  private void parseLiteral(StringBuilder s) throws IOException, FHIRFormatError {
873    // UInt16 w;
874    readChar();
875    String c = readUntil(';');
876    if (c.equals("apos"))
877      s.append('\'');
878    else if (c.equals("quot"))
879      s.append('"');
880    else if (c.equals("nbsp"))
881      s.append(XhtmlNode.NBSP);
882    else if (c.equals("amp"))
883      s.append('&');
884    else if (c.equals("lsquo"))
885      s.append((char) 8216); // right single quotation, U+2019 ISOnum 
886    else if (c.equals("rsquo"))
887      s.append((char) 8217); // right single quotation, U+2019 ISOnum 
888      //s.append((char)0x60); // right single quote
889          //s.append('’');
890    else if (c.equals("gt"))
891      s.append('>');
892    else if (c.equals("lt"))
893      s.append('<');
894    else if (c.equals("copy"))
895      s.append((char) 169);
896    else if (c.equals("reg"))
897      s.append((char) 174);
898    else if (c.equals("sect"))
899      s.append((char) 0xA7);
900    else if (c.charAt(0) == '#') {
901      if (isInteger(c.substring(1), 10))
902        s.append((char) Integer.parseInt(c.substring(1)));
903      else if (c.charAt(1) == 'x' && isInteger(c.substring(2), 16))
904        s.append((char) Integer.parseInt(c.substring(2), 16));
905    } else if (c.equals("fnof"))
906      s.append((char) 402); // latin small f with hook = function = florin, U+0192 ISOtech -->
907    else if (c.equals("Alpha"))
908      s.append((char) 913); // greek capital letter alpha, U+0391
909    else if (c.equals("Beta"))
910      s.append((char) 914); // greek capital letter beta, U+0392
911    else if (c.equals("Gamma"))
912      s.append((char) 915); // greek capital letter gamma, U+0393 ISOgrk3
913    else if (c.equals("Delta"))
914      s.append((char) 916); // greek capital letter delta, U+0394 ISOgrk3
915    else if (c.equals("Epsilon"))
916      s.append((char) 917); // greek capital letter epsilon, U+0395
917    else if (c.equals("Zeta"))
918      s.append((char) 918); // greek capital letter zeta, U+0396
919    else if (c.equals("Eta"))
920      s.append((char) 919); // greek capital letter eta, U+0397
921    else if (c.equals("Theta"))
922      s.append((char) 920); // greek capital letter theta, U+0398 ISOgrk3
923    else if (c.equals("Iota"))
924      s.append((char) 921); // greek capital letter iota, U+0399
925    else if (c.equals("Kappa"))
926      s.append((char) 922); // greek capital letter kappa, U+039A
927    else if (c.equals("Lambda"))
928      s.append((char) 923); // greek capital letter lambda, U+039B ISOgrk3
929    else if (c.equals("Mu"))
930      s.append((char) 924); // greek capital letter mu, U+039C
931    else if (c.equals("Nu"))
932      s.append((char) 925); // greek capital letter nu, U+039D
933    else if (c.equals("Xi"))
934      s.append((char) 926); // greek capital letter xi, U+039E ISOgrk3
935    else if (c.equals("Omicron"))
936      s.append((char) 927); // greek capital letter omicron, U+039F
937    else if (c.equals("Pi"))
938      s.append((char) 928); // greek capital letter pi, U+03A0 ISOgrk3
939    else if (c.equals("Rho"))
940      s.append((char) 929); // greek capital letter rho, U+03A1
941    else if (c.equals("Sigma"))
942      s.append((char) 931); // greek capital letter sigma, U+03A3 ISOgrk3
943    else if (c.equals("Tau"))
944      s.append((char) 932); // greek capital letter tau, U+03A4
945    else if (c.equals("Upsilon"))
946      s.append((char) 933); // greek capital letter upsilon, U+03A5 ISOgrk3
947    else if (c.equals("Phi"))
948      s.append((char) 934); // greek capital letter phi, U+03A6 ISOgrk3
949    else if (c.equals("Chi"))
950      s.append((char) 935); // greek capital letter chi, U+03A7
951    else if (c.equals("Psi"))
952      s.append((char) 936); // greek capital letter psi, U+03A8 ISOgrk3
953    else if (c.equals("Omega"))
954      s.append((char) 937); // greek capital letter omega, U+03A9 ISOgrk3
955    else if (c.equals("alpha"))
956      s.append((char) 945); // greek small letter alpha, U+03B1 ISOgrk3
957    else if (c.equals("beta"))
958      s.append((char) 946); // greek small letter beta, U+03B2 ISOgrk3
959    else if (c.equals("gamma"))
960      s.append((char) 947); // greek small letter gamma, U+03B3 ISOgrk3
961    else if (c.equals("delta"))
962      s.append((char) 948); // greek small letter delta, U+03B4 ISOgrk3
963    else if (c.equals("epsilon"))
964      s.append((char) 949); // greek small letter epsilon, U+03B5 ISOgrk3
965    else if (c.equals("zeta"))
966      s.append((char) 950); // greek small letter zeta, U+03B6 ISOgrk3
967    else if (c.equals("eta"))
968      s.append((char) 951); // greek small letter eta, U+03B7 ISOgrk3
969    else if (c.equals("theta"))
970      s.append((char) 952); // greek small letter theta, U+03B8 ISOgrk3
971    else if (c.equals("iota"))
972      s.append((char) 953); // greek small letter iota, U+03B9 ISOgrk3
973    else if (c.equals("kappa"))
974      s.append((char) 954); // greek small letter kappa, U+03BA ISOgrk3
975    else if (c.equals("lambda"))
976      s.append((char) 955); // greek small letter lambda, U+03BB ISOgrk3
977    else if (c.equals("mu"))
978      s.append((char) 956); // greek small letter mu, U+03BC ISOgrk3
979    else if (c.equals("nu"))
980      s.append((char) 957); // greek small letter nu, U+03BD ISOgrk3
981    else if (c.equals("xi"))
982      s.append((char) 958); // greek small letter xi, U+03BE ISOgrk3
983    else if (c.equals("omicron"))
984      s.append((char) 959); // greek small letter omicron, U+03BF NEW
985    else if (c.equals("pi"))
986      s.append((char) 960); // greek small letter pi, U+03C0 ISOgrk3
987    else if (c.equals("rho"))
988      s.append((char) 961); // greek small letter rho, U+03C1 ISOgrk3
989    else if (c.equals("sigmaf"))
990      s.append((char) 962); // greek small letter final sigma, U+03C2 ISOgrk3
991    else if (c.equals("sigma"))
992      s.append((char) 963); // greek small letter sigma, U+03C3 ISOgrk3
993    else if (c.equals("tau"))
994      s.append((char) 964); // greek small letter tau, U+03C4 ISOgrk3
995    else if (c.equals("upsilon"))
996      s.append((char) 965); // greek small letter upsilon, U+03C5 ISOgrk3
997    else if (c.equals("phi"))
998      s.append((char) 966); // greek small letter phi, U+03C6 ISOgrk3
999    else if (c.equals("chi"))
1000      s.append((char) 967); // greek small letter chi, U+03C7 ISOgrk3
1001    else if (c.equals("psi"))
1002      s.append((char) 968); // greek small letter psi, U+03C8 ISOgrk3
1003    else if (c.equals("omega"))
1004      s.append((char) 969); // greek small letter omega, U+03C9 ISOgrk3
1005    else if (c.equals("thetasym"))
1006      s.append((char) 977); // greek small letter theta symbol, U+03D1 NEW
1007    else if (c.equals("upsih"))
1008      s.append((char) 978); // greek upsilon with hook symbol, U+03D2 NEW
1009    else if (c.equals("piv"))
1010      s.append((char) 982); // greek pi symbol, U+03D6 ISOgrk3
1011    else if (c.equals("bull"))
1012      s.append((char) 8226); // bullet = black small circle, U+2022 ISOpub
1013    else if (c.equals("hellip"))
1014      s.append((char) 8230); // horizontal ellipsis = three dot leader, U+2026 ISOpub
1015    else if (c.equals("prime"))
1016      s.append((char) 8242); // prime = minutes = feet, U+2032 ISOtech
1017    else if (c.equals("Prime"))
1018      s.append((char) 8243); // double prime = seconds = inches, U+2033 ISOtech
1019    else if (c.equals("oline"))
1020      s.append((char) 8254); // overline = spacing overscore, U+203E NEW
1021    else if (c.equals("frasl"))
1022      s.append((char) 8260); // fraction slash, U+2044 NEW
1023    else if (c.equals("weierp"))
1024      s.append((char) 8472); // script capital P = power set = Weierstrass p, U+2118 ISOamso
1025    else if (c.equals("image"))
1026      s.append((char) 8465); // blackletter capital I = imaginary part, U+2111 ISOamso
1027    else if (c.equals("real"))
1028      s.append((char) 8476); // blackletter capital R = real part symbol, U+211C ISOamso
1029    else if (c.equals("trade"))
1030      s.append((char) 8482); // trade mark sign, U+2122 ISOnum
1031    else if (c.equals("alefsym"))
1032      s.append((char) 8501); // alef symbol = first transfinite cardinal, U+2135 NEW
1033    else if (c.equals("larr"))
1034      s.append((char) 8592); // leftwards arrow, U+2190 ISOnum
1035    else if (c.equals("uarr"))
1036      s.append((char) 8593); // upwards arrow, U+2191 ISOnum
1037    else if (c.equals("rarr"))
1038      s.append((char) 8594); // rightwards arrow, U+2192 ISOnum
1039    else if (c.equals("darr"))
1040      s.append((char) 8595); // downwards arrow, U+2193 ISOnum
1041    else if (c.equals("harr"))
1042      s.append((char) 8596); // left right arrow, U+2194 ISOamsa
1043    else if (c.equals("crarr"))
1044      s.append((char) 8629); // downwards arrow with corner leftwards = carriage return, U+21B5 NEW
1045    else if (c.equals("lArr"))
1046      s.append((char) 8656); // leftwards double arrow, U+21D0 ISOtech
1047    else if (c.equals("uArr"))
1048      s.append((char) 8657); // upwards double arrow, U+21D1 ISOamsa
1049    else if (c.equals("rArr"))
1050      s.append((char) 8658); // rightwards double arrow, U+21D2 ISOtech
1051    else if (c.equals("dArr"))
1052      s.append((char) 8659); // downwards double arrow, U+21D3 ISOamsa
1053    else if (c.equals("hArr"))
1054      s.append((char) 8660); // left right double arrow, U+21D4 ISOamsa
1055    else if (c.equals("forall"))
1056      s.append((char) 8704); // for all, U+2200 ISOtech
1057    else if (c.equals("part"))
1058      s.append((char) 8706); // partial differential, U+2202 ISOtech
1059    else if (c.equals("exist"))
1060      s.append((char) 8707); // there exists, U+2203 ISOtech
1061    else if (c.equals("empty"))
1062      s.append((char) 8709); // empty set = null set = diameter, U+2205 ISOamso
1063    else if (c.equals("nabla"))
1064      s.append((char) 8711); // nabla = backward difference, U+2207 ISOtech
1065    else if (c.equals("isin"))
1066      s.append((char) 8712); // element of, U+2208 ISOtech
1067    else if (c.equals("notin"))
1068      s.append((char) 8713); // not an element of, U+2209 ISOtech
1069    else if (c.equals("ni"))
1070      s.append((char) 8715); // contains as member, U+220B ISOtech
1071    else if (c.equals("prod"))
1072      s.append((char) 8719); // n-ary product = product sign, U+220F ISOamsb
1073    else if (c.equals("sum"))
1074      s.append((char) 8721); // n-ary sumation, U+2211 ISOamsb
1075    else if (c.equals("minus"))
1076      s.append((char) 8722); // minus sign, U+2212 ISOtech
1077    else if (c.equals("lowast"))
1078      s.append((char) 8727); // asterisk operator, U+2217 ISOtech
1079    else if (c.equals("radic"))
1080      s.append((char) 8730); // square root = radical sign, U+221A ISOtech
1081    else if (c.equals("prop"))
1082      s.append((char) 8733); // proportional to, U+221D ISOtech
1083    else if (c.equals("infin"))
1084      s.append((char) 8734); // infinity, U+221E ISOtech -->
1085    else if (c.equals("ang"))
1086      s.append((char) 8736); // angle, U+2220 ISOamso
1087    else if (c.equals("and"))
1088      s.append((char) 8743); // logical and = wedge, U+2227 ISOtech
1089    else if (c.equals("or"))
1090      s.append((char) 8744); // logical or = vee, U+2228 ISOtech
1091    else if (c.equals("cap"))
1092      s.append((char) 8745); // intersection = cap, U+2229 ISOtech
1093    else if (c.equals("cup"))
1094      s.append((char) 8746); // union = cup, U+222A ISOtech
1095    else if (c.equals("int"))
1096      s.append((char) 8747); // integral, U+222B ISOtech
1097    else if (c.equals("there4"))
1098      s.append((char) 8756); // therefore, U+2234 ISOtech
1099    else if (c.equals("sim"))
1100      s.append((char) 8764); // tilde operator = varies with = similar t U+223C ISOtech
1101    else if (c.equals("cong"))
1102      s.append((char) 8773); // approximately equal to, U+2245 ISOtec
1103    else if (c.equals("asymp"))
1104      s.append((char) 8776); // almost equal to = asymptotic to, U+2248 ISOamsr
1105    else if (c.equals("ne"))
1106      s.append((char) 8800); // not equal to, U+2260 ISOtech
1107    else if (c.equals("equiv"))
1108      s.append((char) 8801); // identical to, U+2261 ISOtech
1109    else if (c.equals("le"))
1110      s.append((char) 8804); // less-than or equal to, U+2264 ISOtech
1111    else if (c.equals("ge"))
1112      s.append((char) 8805); // greater-than or equal to, U+2265 ISOtech
1113    else if (c.equals("sub"))
1114      s.append((char) 8834); // subset of, U+2282 ISOtech
1115    else if (c.equals("sup"))
1116      s.append((char) 8835); // superset of, U+2283 ISOtech
1117    else if (c.equals("nsub"))
1118      s.append((char) 8836); // not a subset of, U+2284 ISOamsn
1119    else if (c.equals("sube"))
1120      s.append((char) 8838); // subset of or equal to, U+2286 ISOtech
1121    else if (c.equals("supe"))
1122      s.append((char) 8839); // superset of or equal to, U+2287 ISOtech
1123    else if (c.equals("oplus"))
1124      s.append((char) 8853); // circled plus = direct sum, U+2295 ISOamsb
1125    else if (c.equals("otimes"))
1126      s.append((char) 8855); // circled times = vector product, U+2297 ISOamsb -->
1127    else if (c.equals("perp"))
1128      s.append((char) 8869); // up tack = orthogonal to = perpendicular, U+22A5 ISOtech
1129    else if (c.equals("sdot"))
1130      s.append((char) 8901); // dot operator, U+22C5 ISOamsb
1131    else if (c.equals("lceil"))
1132      s.append((char) 8968); // left ceiling = apl upstile, U+2308 ISOamsc
1133    else if (c.equals("rceil"))
1134      s.append((char) 8969); // right ceiling, U+2309 ISOamsc
1135    else if (c.equals("lfloor"))
1136      s.append((char) 8970); // left floor = apl downstile, U+230A ISOamsc
1137    else if (c.equals("rfloor"))
1138      s.append((char) 8971); // right floor, U+230B ISOamsc
1139    else if (c.equals("lang"))
1140      s.append((char) 9001); // left-pointing angle bracket = bra, U+2329 ISOtech
1141    else if (c.equals("rang"))
1142      s.append((char) 9002); // right-pointing angle bracket = ket, U+232A ISOtech
1143    else if (c.equals("loz"))
1144      s.append((char) 9674); // lozenge, U+25CA ISOpub
1145    else if (c.equals("spades"))
1146      s.append((char) 9824); // black spade suit, U+2660 ISOpub
1147    else if (c.equals("clubs"))
1148      s.append((char) 9827); // black club suit = shamrock, U+2663 ISOpub
1149    else if (c.equals("hearts"))
1150      s.append((char) 9829); // black heart suit = valentine, U+2665 ISOpub
1151    else if (c.equals("diams"))
1152      s.append((char) 9830); // black diamond suit, U+2666 ISOpub --
1153    else if (c.equals("ndash"))
1154      s.append((char) 8211); 
1155    else if (c.equals("mdash"))
1156      s.append((char) 8212); 
1157    else if (c.equals("ldquo"))
1158      s.append((char) 8221); 
1159    else if (c.equals("rdquo"))
1160      s.append((char) 201D); 
1161    else if (entities.containsKey(c))
1162      s.append(entities.get(c));
1163    else
1164      throw new FHIRFormatError("unable to parse character reference '" + c + "'' (last text = '" + lastText + "'" + descLoc());
1165  }
1166  
1167  private boolean isInteger(String s, int base) {
1168    try {
1169      Integer.parseInt(s, base);
1170      return true;
1171    } catch (Exception e) {
1172      return false;
1173    }
1174  }
1175
1176  public XhtmlNode parseFragment(String source) throws IOException, FHIRException  {
1177    rdr = new StringReader(source);
1178    return parseFragment();
1179  }
1180  
1181  public XhtmlNode parseFragment(InputStream input) throws IOException, FHIRException  {
1182    rdr = new InputStreamReader(input);
1183    return parseFragment();
1184  }
1185  
1186  private XhtmlNode parseFragment() throws IOException, FHIRException 
1187  {
1188    skipWhiteSpace();
1189    if (peekChar() != '<')
1190      throw new FHIRException("Unable to Parse HTML - does not start with tag. Found "+peekChar()+descLoc());
1191    readChar();
1192    if (peekChar() == '?') {
1193      readToTagEnd();
1194      skipWhiteSpaceInternal();
1195      if (peekChar() != '<')
1196        throw new FHIRException("Unable to Parse HTML - does not start with tag after processing instruction. Found "+peekChar()+descLoc());
1197      readChar();
1198    }
1199    String n = readName().toLowerCase();
1200    readToTagEnd();
1201    XhtmlNode result = new XhtmlNode(NodeType.Element);
1202
1203    int colonIndex = n.indexOf(':');
1204    if (colonIndex != -1) {
1205      n = n.substring(colonIndex + 1);
1206    }
1207    
1208    result.setName(n);
1209    unwindPoint = null;
1210    List<XhtmlNode> p = new ArrayList<>();
1211    parseElementInner(result, p, null, true);
1212
1213    return result;
1214  }
1215
1216  
1217}