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