001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.marshall.html;
018
019import static javax.xml.stream.XMLStreamConstants.*;
020import static org.apache.juneau.commons.utils.CollectionUtils.*;
021import static org.apache.juneau.commons.utils.Shorts.*;
022import static org.apache.juneau.commons.utils.StringUtils.*;
023import static org.apache.juneau.marshall.html.HtmlTag.*;
024
025import java.io.*;
026import java.lang.reflect.*;
027import java.util.*;
028
029import javax.xml.stream.*;
030
031import org.apache.juneau.commons.bean.*;
032import org.apache.juneau.commons.lang.*;
033import org.apache.juneau.commons.reflect.*;
034import org.apache.juneau.marshall.*;
035import org.apache.juneau.marshall.collections.*;
036import org.apache.juneau.marshall.parser.*;
037import org.apache.juneau.marshall.swap.spi.*;
038import org.apache.juneau.marshall.xml.*;
039
040/**
041 * ContextSession object that lives for the duration of a single use of {@link HtmlParser}.
042 *
043 * <h2 class='section'>Notes:</h2><ul>
044 *    <li class='warn'>This class is not thread safe and is typically discarded after one use.
045 * </ul>
046 *
047 * <h2 class='section'>See Also:</h2><ul>
048 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/HtmlSupport">HTML</a>
049
050 * </ul>
051 */
052@SuppressWarnings({
053   "java:S110", // Inheritance depth acceptable for this class hierarchy
054   "java:S115", // Constants use UPPER_snakeCase naming convention
055   "java:S1192", // Duplicated literals (argument/property names) read more clearly inline than as constants
056   "java:S2177", // Private readIntoBean/readIntoCollection/readIntoMap intentionally reuse the names of XmlParserSession methods with HTML-specific logic
057   "java:S3776", // readAnything, readTableIntoCollection and skipTag branch over HTML tag and element types
058   "rawtypes", // Raw types necessary for generic type handling
059   "unchecked" // Type erasure requires unchecked casts
060})
061public class HtmlParserSession extends XmlParserSession {
062
063   /**
064    * Builder class.
065    */
066   public static class Builder extends XmlParserSession.Builder<Builder> {
067
068      private final HtmlParser ctx;
069
070      /**
071       * Constructor
072       *
073       * @param ctx The context creating this session.
074       *    <br>Cannot be <jk>null</jk>.
075       */
076      protected Builder(HtmlParser ctx) {
077         super(reqnn("ctx", ctx));
078         this.ctx = ctx;
079      }
080
081      @Override
082      public HtmlParserSession build() {
083         return new HtmlParserSession(this);
084      }
085
086   }
087
088   private static final Set<String> whitespaceElements = set("br", "bs", "sp", "ff");
089
090   /**
091    * Creates a new builder for this object.
092    *
093    * @param ctx The context creating this session.
094    *    <br>Cannot be <jk>null</jk>.
095    * @return A new builder.
096    */
097   public static Builder create(HtmlParser ctx) {
098      return new Builder(reqnn("ctx", ctx));
099   }
100
101   private static String getAttribute(XmlReader r, String name, String def) {
102      for (var i = 0; i < r.getAttributeCount(); i++)
103         if (r.getAttributeLocalName(i).equals(name))
104            return r.getAttributeValue(i);
105      return def;
106   }
107
108   private static Map<String,String> getAttributes(XmlReader r) {
109      var m = new TreeMap<String,String>();
110      for (var i = 0; i < r.getAttributeCount(); i++)
111         m.put(r.getAttributeLocalName(i), r.getAttributeValue(i));
112      return m;
113   }
114
115   private static int skipWs(XmlReader r) throws XMLStreamException {
116      int event = r.getEventType();
117      while (event != START_ELEMENT && event != END_ELEMENT && event != END_DOCUMENT && r.isWhiteSpace())
118         event = r.next();
119      return event;
120   }
121
122   private final HtmlParser ctx;
123
124   /**
125    * Constructor.
126    *
127    * @param builder The builder for this object.
128    */
129   protected HtmlParserSession(Builder builder) {
130      super(builder);
131      ctx = builder.ctx;
132   }
133
134   /*
135    * Reads the next tag.  Advances past anything that's not a start or end tag.  Throws an exception if
136    *    it's not one of the expected tags.
137    * Precondition:  Must be pointing before the event we want to parse.
138    * Postcondition:  Pointing at the tag just parsed.
139    */
140   private HtmlTag nextTag(XmlReader r, HtmlTag...expected) throws ParseException, XMLStreamException {
141      int et = r.next();
142
143      while (et != START_ELEMENT && et != END_ELEMENT && et != END_DOCUMENT)
144         et = r.next();
145
146      if (et == END_DOCUMENT)
147         throw new ParseException(this, "Unexpected end of document.");
148
149      var tag = HtmlTag.forEvent(this, r);
150      if (expected.length == 0)
151         return tag;
152      for (var t : expected)
153         if (t == tag)
154            return tag;
155
156      throw new ParseException(this, "Unexpected tag: '%s'.  Expected one of the following: %s", tag, expected);
157   }
158
159   /*
160    * Reads an anchor tag and converts it into a bean.
161    */
162   private <T> T readAnchor(XmlReader r, ClassMeta<T> beanType) throws IOException, ParseException, XMLStreamException {
163      String href = r.getAttributeValue(null, "href");
164      String name = getElementText(r);
165      if (nn(beanType) && getAnnotationProvider().has(HtmlLink.class, beanType)) {
166         var uriProperty = Holder.<String>empty();
167         var nameProperty = Holder.<String>empty();
168         beanType.forEachAnnotation(HtmlLink.class, x -> ine(x.uriProperty()), x -> uriProperty.set(x.uriProperty()));
169         beanType.forEachAnnotation(HtmlLink.class, x -> ine(x.nameProperty()), x -> nameProperty.set(x.nameProperty()));
170         BeanMap<T> m = newBeanMap(beanType.inner());
171         m.put(uriProperty.orElse(""), href);
172         m.put(nameProperty.orElse(""), name);
173         return checkRequired(m).getBean();
174      }
175      return convertToType(href, beanType);
176   }
177
178   /*
179    * Reads anything starting at the current event.
180    * <p>
181    * Precondition:  Must be pointing at outer START_ELEMENT.
182    * Postcondition:  Pointing at outer END_ELEMENT.
183    */
184   private <T> T readAnything(ClassMeta<T> eType, XmlReader r, Object outer, boolean isRoot, BeanPropertyMeta pMeta) throws IOException, ParseException, ExecutableException, XMLStreamException {
185      enterParseDepth();
186      try {
187         return readAnything0(eType, r, outer, isRoot, pMeta);
188      } finally {
189         exitParseDepth();
190      }
191   }
192
193   // Workhorse method, wrapped by readAnything() with the recursion-depth guard.
194   private <T> T readAnything0(ClassMeta<T> eType, XmlReader r, Object outer, boolean isRoot, BeanPropertyMeta pMeta) throws IOException, ParseException, ExecutableException, XMLStreamException {
195
196      if (eType == null)
197         eType = (ClassMeta<T>)object();
198      var swap = (ObjectSwap<T,Object>)eType.getSwap(this);
199      var builder = (BuilderSwap<T,Object>)eType.getBuilderSwap(this);
200      ClassMeta<?> sType = null;
201      if (nn(builder))
202         sType = builder.getBuilderClassMeta(this);
203      else if (nn(swap))
204         sType = swap.getSwapClassMeta(this);
205      else
206         sType = eType;
207
208      if (sType.isOptional())
209         return (T)o(readAnything(eType.getElementType(), r, outer, isRoot, pMeta));
210
211      setCurrentClass(sType);
212
213      int event = r.getEventType();
214      if (event != START_ELEMENT)
215         throw new ParseException(this, "readAnything must be called on outer start element.");
216
217      if (! isRoot)
218         event = r.next();
219      var isEmpty = (event == END_ELEMENT);
220
221      // Skip until we find a start element, end document, or non-empty text.
222      if (! isEmpty)
223         event = skipWs(r);
224
225      if (event == END_DOCUMENT)
226         throw new ParseException(this, "Unexpected end of stream in readAnything for type '%s'", eType);
227
228      // Handle @Html(asXml=true) beans.
229      var hcm = getHtmlClassMeta(sType);
230      if (hcm.getFormat() == HtmlFormat.XML)
231         return super.readAnything(eType, null, r, outer, false, pMeta);
232
233      Object o = null;
234
235      var isValid = true;
236      var tag = (event == CHARACTERS ? null : HtmlTag.forString(r.getName().getLocalPart(), false));
237
238      // If it's not a known tag, then parse it as XML.
239      // Allows us to parse stuff like "<div/>" into HTML5 beans.
240      if (tag == null && event != CHARACTERS)
241         return super.readAnything(eType, null, r, outer, false, pMeta);
242
243      if (tag == HTML)
244         tag = skipToData(r);
245
246      if (isEmpty) {
247         o = "";
248      } else if (tag == null || tag.isOneOf(BR, BS, FF, SP)) {
249         String text = readText(r);
250         if (sType.isObject() || sType.isCharSequence())
251            o = text;
252         else if (sType.isChar())
253            o = parseCharacter(text);
254         else if (sType.isNumber())
255            o = parseNumber(text, (Class<? extends Number>)eType.inner());
256         else if (sType.isBoolean())
257            o = Boolean.parseBoolean(text);
258         else if (sType.isDate())
259            o = readDate(text, sType);
260         else if (sType.isCalendar())
261            o = readCalendar(text, sType);
262         else if (sType.isTemporal())
263            o = readTemporal(text, sType);
264         else if (sType.isDuration())
265            o = readDuration(text);
266         else if (sType.isPeriod())
267            o = readPeriod(text);
268         else if (sType.canCreateNewInstanceFromString(outer))
269            o = sType.newInstanceFromString(outer, text);
270         else
271            isValid = false;
272
273      } else if (tag == STRING || (tag == A && nn(pMeta) && nn(getHtmlBeanPropertyMeta(pMeta).getLink()))) {
274         String text = getElementText(r);
275         if (sType.isObject() || sType.isCharSequence())
276            o = text;
277         else if (sType.isChar())
278            o = parseCharacter(text);
279         else if (sType.isDate())
280            o = readDate(text, sType);
281         else if (sType.isCalendar())
282            o = readCalendar(text, sType);
283         else if (sType.isTemporal())
284            o = readTemporal(text, sType);
285         else if (sType.isDuration())
286            o = readDuration(text);
287         else if (sType.isPeriod())
288            o = readPeriod(text);
289         else if (sType.canCreateNewInstanceFromString(outer))
290            o = sType.newInstanceFromString(outer, text);
291         else
292            isValid = false;
293         skipTag(r, tag == STRING ? X_STRING : X_A);
294
295      } else if (tag == NUMBER) {
296         String text = getElementText(r);
297         if (sType.isObject())
298            o = parseNumber(text, Number.class);
299         else if (sType.isNumber())
300            o = parseNumber(text, (Class<? extends Number>)sType.inner());
301         else
302            isValid = false;
303         skipTag(r, X_NUMBER);
304
305      } else if (tag == BOOLEAN) {
306         String text = getElementText(r);
307         if (sType.isObject() || sType.isBoolean())
308            o = Boolean.parseBoolean(text);
309         else
310            isValid = false;
311         skipTag(r, X_BOOLEAN);
312
313      } else if (tag == P) {
314         String text = getElementText(r);
315         if (neq(text, "No Results"))
316            isValid = false;
317         skipTag(r, X_P);
318
319      } else if (tag == NULL) {
320         skipTag(r, NULL);
321         skipTag(r, X_NULL);
322
323      } else if (tag == A) {
324         o = readAnchor(r, swap == null ? eType : null);
325         skipTag(r, X_A);
326
327      } else if (tag == TABLE) {
328
329         String typeName = getAttribute(r, getBeanTypePropertyName(eType), "object");
330         ClassMeta cm = getClassMeta(typeName, pMeta, eType);
331
332         if (nn(cm)) {
333            sType = eType = cm;
334            typeName = sType.isCollectionOrArray() ? "array" : "object";
335         } else if (neq(typeName, "array")) {
336            // Type name could be a subtype name.
337            typeName = sType.isCollectionOrArray() ? "array" : "object";
338         }
339
340         if (typeName.equals("object")) {
341            if (sType.isObject()) {
342               o = readIntoMap(r, newGenericMap(sType), sType.getKeyType(), sType.getValueType(), pMeta);
343            } else if (nn(builder)) {
344               BeanMap m = toBeanMap(builder.create(this, eType));
345               o = builder.build(this, checkRequired(readIntoBean(r, m)).getBean(), eType);
346            } else if (sType.canCreateNewBean(outer)) {
347               BeanMap m = newBeanMap(outer, sType.inner());
348               o = checkRequired(readIntoBean(r, m)).getBean();
349            } else if (sType.isMap()) {
350               o = readIntoMap(r, (Map)(sType.canCreateNewInstance(outer) ? sType.newInstance(outer) : newGenericMap(sType)), sType.getKeyType(), sType.getValueType(), pMeta);
351            } else if (nn(sType.getProxyInvocationHandler())) {
352               BeanMap m = newBeanMap(outer, sType.inner());
353               o = checkRequired(readIntoBean(r, m)).getBean();
354            } else {
355               isValid = false;
356            }
357            skipTag(r, X_TABLE);
358
359         } else if (typeName.equals("array")) {
360            if (sType.isObject())
361               o = readTableIntoCollection(r, (Collection)newGenericList(), sType, pMeta);
362            else if (sType.isCollection())
363               o = readTableIntoCollection(r, (Collection)(sType.canCreateNewInstance(outer) ? sType.newInstance(outer) : newGenericList()), sType, pMeta);
364            else if (sType.isArray() || sType.isArgs()) {
365               var l = (ArrayList)readTableIntoCollection(r, list(), sType, pMeta);
366               o = toArray(sType, l);
367            } else
368               isValid = false;
369            skipTag(r, X_TABLE);
370
371         } else {
372            isValid = false;
373         }
374
375      } else if (tag == UL) {
376         String typeName = getAttribute(r, getBeanTypePropertyName(eType), "array");
377         ClassMeta cm = getClassMeta(typeName, pMeta, eType);
378         if (nn(cm))
379            sType = eType = cm;
380
381         if (sType.isObject())
382            o = readIntoCollection(r, newGenericList(), sType, pMeta);
383         else if (sType.isCollection() || sType.isObject())
384            o = readIntoCollection(r, (Collection)(sType.canCreateNewInstance(outer) ? sType.newInstance(outer) : newGenericList()), sType, pMeta);
385         else if (sType.isArray() || sType.isArgs())
386            o = toArray(sType, readIntoCollection(r, list(), sType, pMeta));
387         else
388            isValid = false;
389         skipTag(r, X_UL);
390
391      }
392
393      if (! isValid)
394         throw new ParseException(this, "Unexpected tag '%s' for type '%s'", tag, eType);
395
396      if (nn(swap) && nn(o))
397         o = unswap(swap, o, eType);
398
399      if (nn(parentBean()))
400         setParent(eType, o, parentBean());
401
402      skipWs(r);
403      return (T)o;
404   }
405
406   /*
407    * Reads contents of <table> element.
408    * Precondition:  Must be pointing at event following <table> event.
409    * Postcondition:  Pointing at next START_ELEMENT or END_DOCUMENT event.
410    */
411   private <T> BeanMap<T> readIntoBean(XmlReader r, BeanMap<T> m) throws IOException, ParseException, ExecutableException, XMLStreamException {
412      var pb = swapParentBean(m.getBean(false));
413      try {
414         while (true) {
415            HtmlTag tag = nextTag(r, TR, X_TABLE);
416            if (tag == X_TABLE)
417               break;
418            tag = nextTag(r, TD, TH);
419            // Skip over the column headers.
420            if (tag == TH) {
421               skipTag(r);
422               r.nextTag();
423               skipTag(r);
424            } else {
425               String key = getElementText(r);
426               nextTag(r, TD);
427               var pMeta = m.getPropertyMeta(key);
428               if (pMeta == null) {
429                  onUnknownProperty(key, m, readAnything(object(), r, null, false, null));
430               } else {
431                  var cm = (ClassMeta<?>) pMeta.getBeanInfo();
432                  Object value = readAnything(cm, r, m.getBean(false), false, pMeta);
433                  setName(cm, value, key);
434                  try {
435                     pMeta.set(m, key, value);
436                  } catch (BeanRuntimeException e) {
437                     onBeanSetterException(pMeta, e);
438                     throw e;
439                  }
440               }
441            }
442            HtmlTag t = nextTag(r, X_TD, X_TR);
443            if (t == X_TD)
444               nextTag(r, X_TR);
445         }
446         return m;
447      } finally {
448         swapParentBean(pb);
449      }
450   }
451
452   /*
453    * Reads contents of <ul> element.
454    * Precondition:  Must be pointing at event following <ul> event.
455    * Postcondition:  Pointing at next START_ELEMENT or END_DOCUMENT event.
456    */
457   private <E> Collection<E> readIntoCollection(XmlReader r, Collection<E> l, ClassMeta<?> type, BeanPropertyMeta pMeta) throws IOException, ParseException, ExecutableException, XMLStreamException {
458      int argIndex = 0;
459      while (true) {
460         HtmlTag tag = nextTag(r, LI, X_UL, X_LI);
461         if (tag == X_LI)
462            tag = nextTag(r, LI, X_UL, X_LI);
463         if (tag == X_UL)
464            break;
465         var elementType = type.isArgs() ? type.getArg(argIndex++) : type.getElementType();
466         l.add((E)readAnything(elementType, r, l, false, pMeta));
467      }
468      return l;
469   }
470
471   /*
472    * Reads contents of <table> element.
473    * Precondition:  Must be pointing at <table> event.
474    * Postcondition:  Pointing at next START_ELEMENT or END_DOCUMENT event.
475    */
476   private <K,V> Map<K,V> readIntoMap(XmlReader r, Map<K,V> m, ClassMeta<K> keyType, ClassMeta<V> valueType, BeanPropertyMeta pMeta)
477      throws IOException, ParseException, ExecutableException, XMLStreamException {
478      while (true) {
479         HtmlTag tag = nextTag(r, TR, X_TABLE);
480         if (tag == X_TABLE)
481            break;
482         tag = nextTag(r, TD, TH);
483         // Skip over the column headers.
484         if (tag == TH) {
485            skipTag(r);
486            r.nextTag();
487            skipTag(r);
488         } else {
489            K key = readAnything(keyType, r, m, false, pMeta);
490            nextTag(r, TD);
491            V value = readAnything(valueType, r, m, false, pMeta);
492            setName(valueType, value, key);
493            m.put(key, value);
494         }
495         tag = nextTag(r, X_TD, X_TR);
496         if (tag == X_TD)
497            nextTag(r, X_TR);
498      }
499
500      return m;
501   }
502
503   /*
504    * Reads contents of <ul> element.
505    * Precondition:  Must be pointing at event following <ul> event.
506    * Postcondition:  Pointing at next START_ELEMENT or END_DOCUMENT event.
507    */
508   private <E> Collection<E> readTableIntoCollection(XmlReader r, Collection<E> l, ClassMeta<E> type, BeanPropertyMeta pMeta)
509      throws IOException, ParseException, ExecutableException, XMLStreamException {
510
511      nextTag(r, TR);
512      List<String> keys = list();
513      HtmlTag tag;
514      while (true) {
515         tag = nextTag(r, TH, X_TR);
516         if (tag == X_TR)
517            break;
518         keys.add(getElementText(r));
519      }
520
521      int argIndex = 0;
522
523      while (true) {
524         r.nextTag();
525         tag = HtmlTag.forEvent(this, r);
526         if (tag == X_TABLE)
527            break;
528
529         ClassMeta elementType = null;
530         String beanType = getAttribute(r, getBeanTypePropertyName(type), null);
531         if (nn(beanType))
532            elementType = getClassMeta(beanType, pMeta, null);
533         if (elementType == null)
534            elementType = type.isArgs() ? type.getArg(argIndex++) : type.getElementType();
535         if (elementType == null)
536            elementType = object();
537
538         BuilderSwap<E,Object> builder = elementType.getBuilderSwap(this);
539
540         if (nn(builder) || elementType.canCreateNewBean(l)) {
541            // @formatter:off
542            BeanMap m =
543               nn(builder)
544               ? toBeanMap(builder.create(this, elementType))
545               : newBeanMap(l, elementType.inner())
546            ;
547            // @formatter:on
548            var pb = swapParentBean(m.getBean(false));
549            try {
550               for (var key : keys) {
551                  tag = nextTag(r, X_TD, TD, NULL);
552                  if (tag == X_TD)
553                     tag = nextTag(r, TD, NULL);
554                  if (tag == NULL) {
555                     m = null;
556                     nextTag(r, X_NULL);
557                     break;
558                  }
559                  BeanMapEntry e = m.getProperty(key);
560                  if (e == null) {
561                     readAnything(object(), r, l, false, null);
562                  } else {
563                     BeanPropertyMeta bpm = e.getMeta();
564                     var cm = (ClassMeta<?>) bpm.getBeanInfo();
565                     Object value = readAnything(cm, r, m.getBean(false), false, bpm);
566                     setName(cm, value, key);
567                     bpm.set(m, key, value);
568                  }
569               }
570            } finally {
571               swapParentBean(pb);
572            }
573            E element;
574            if (m == null) {
575               element = null;
576            } else if (nn(builder)) {
577               element = builder.build(this, checkRequired(m).getBean(), elementType);
578            } else {
579               element = (E)checkRequired(m).getBean();
580            }
581            if (nn(parentBean()) && nn(element))
582               setParent(elementType, element, parentBean());
583            l.add(element);
584         } else {
585            String c = getAttributes(r).get(getBeanTypePropertyName(type.getElementType()));
586            var m = (Map)(elementType.isMap() && elementType.canCreateNewInstance(l) ? elementType.newInstance(l) : newGenericMap(elementType));
587            for (var key : keys) {
588               tag = nextTag(r, TD, NULL);
589               if (tag == NULL) {
590                  m = null;
591                  nextTag(r, X_NULL);
592                  break;
593               }
594               if (nn(m)) {
595                  var kt = elementType.getKeyType();
596                  var vt = elementType.getValueType();
597                  Object value = readAnything(vt, r, l, false, pMeta);
598                  setName(vt, value, key);
599                  m.put(convertToType(key, kt), value);
600               }
601            }
602            if (nn(m) && nn(c)) {
603               var m2 = (m instanceof MarshalledMap m3 ? m3 : new JsonMap(m).session(this));
604               m2.put(getBeanTypePropertyName(type.getElementType()), c);
605               l.add((E)cast(m2, pMeta, elementType));
606            } else {
607               if (m instanceof MarshalledMap m2)
608                  l.add((E)convertToType(m2, elementType));
609               else
610                  l.add((E)m);
611            }
612         }
613         nextTag(r, X_TR);
614      }
615      return l;
616   }
617
618   /**
619    * Skips over the current element and advances to the next element.
620    * <p>
621    * Precondition:  Pointing to opening tag.
622    * Postcondition:  Pointing to next opening tag.
623    *
624    * @param r The stream being read from.
625    * @throws XMLStreamException
626    */
627   private void skipTag(XmlReader r) throws ParseException, XMLStreamException {
628      int et = r.getEventType();
629
630      if (et != START_ELEMENT)
631         throw new ParseException(this, "skipToNextTag() call on invalid event '%s'.  Must only be called on START_ELEMENT events.", XmlUtils.toReadableEvent(r));
632
633      String n = r.getLocalName();
634
635      int depth = 0;
636      while (true) {
637         et = r.next();
638         if (et == START_ELEMENT) {
639            String n2 = r.getLocalName();
640            if (n.equals(n2))
641               depth++;
642         } else if (et == END_ELEMENT) {
643            String n2 = r.getLocalName();
644            if (n.equals(n2))
645               depth--;
646            if (depth < 0)
647               return;
648         }
649      }
650   }
651
652   private void skipTag(XmlReader r, HtmlTag...expected) throws ParseException, XMLStreamException {
653      var tag = HtmlTag.forEvent(this, r);
654      if (tag.isOneOf(expected))
655         r.next();
656      else
657         throw new ParseException(this, "Unexpected tag: '%s'.  Expected one of the following: %s", tag, expected);
658   }
659
660   /*
661    * For parsing output from HtmlDocSerializer, this skips over the head, title, and links.
662    */
663   private HtmlTag skipToData(XmlReader r) throws ParseException, XMLStreamException {
664      while (true) {
665         var event = r.next();
666         if (event == START_ELEMENT && eq(r.getLocalName(), "div") && eq(r.getAttributeValue(null, "id"), "data")) {
667            r.nextTag();
668            event = r.getEventType();
669            var isEmpty = (event == END_ELEMENT);
670            // Skip until we find a start element, end document, or non-empty text.
671            if (! isEmpty)
672               event = skipWs(r);
673            if (event == END_DOCUMENT)
674               throw new ParseException(this, "Unexpected end of stream looking for data.");
675            return (event == CHARACTERS ? null : HtmlTag.forString(r.getName().getLocalPart(), false));
676         }
677      }
678   }
679
680   @Override /* Overridden from ParserSession */
681   protected <T> T doRead(ParserPipe pipe, ClassMeta<T> type) throws IOException, ParseException, ExecutableException {
682      try {
683         return readAnything(type, getXmlReader(pipe), getOuter(), true, null);
684      } catch (XMLStreamException e) {
685         throw new ParseException(e);
686      }
687   }
688
689   @Override /* Overridden from ReaderParserSession */
690   protected <E> Collection<E> doReadIntoCollection(ParserPipe pipe, Collection<E> c, Type elementType) throws Exception {
691      return readIntoCollection(getXmlReader(pipe), c, getClassMeta(elementType), null);
692   }
693
694   @Override /* Overridden from ReaderParserSession */
695   protected <K,V> Map<K,V> doReadIntoMap(ParserPipe pipe, Map<K,V> m, Type keyType, Type valueType) throws Exception {
696      return readIntoMap(getXmlReader(pipe), m, (ClassMeta<K>)getClassMeta(keyType), (ClassMeta<V>)getClassMeta(valueType), null);
697   }
698
699   /**
700    * Identical to {@link #readText(XmlReader)} except assumes the current event is the opening tag.
701    *
702    * <p>
703    * Precondition:  Pointing to opening tag.
704    * Postcondition:  Pointing to closing tag.
705    *
706    * @param r The stream being read from.
707    * @return The parsed string.
708    * @throws XMLStreamException Thrown by underlying XML stream.
709    * @throws ParseException Malformed input encountered.
710    */
711   @Override /* Overridden from XmlParserSession */
712   protected String getElementText(XmlReader r) throws IOException, XMLStreamException, ParseException {
713      r.next();
714      return readText(r);
715   }
716
717   /**
718    * Returns the language-specific metadata on the specified bean property.
719    *
720    * @param bpm The bean property to return the metadata on.
721    * @return The metadata.
722    */
723   protected HtmlBeanPropertyMeta getHtmlBeanPropertyMeta(BeanPropertyMeta bpm) {
724      return ctx.getHtmlBeanPropertyMeta(bpm);
725   }
726
727   /**
728    * Returns the language-specific metadata on the specified class.
729    *
730    * @param cm The class to return the metadata on.
731    * @return The metadata.
732    */
733   protected HtmlClassMeta getHtmlClassMeta(ClassMeta<?> cm) {
734      return ctx.getHtmlClassMeta(cm);
735   }
736
737   @Override /* Overridden from XmlParserSession */
738   protected boolean isWhitespaceElement(XmlReader r) {
739      String s = r.getLocalName();
740      return whitespaceElements.contains(s);
741   }
742
743   /**
744    * Parses CHARACTERS data.
745    *
746    * <p>
747    * Precondition:  Pointing to event immediately following opening tag.
748    * Postcondition:  Pointing to closing tag.
749    *
750    * @param r The stream being read from.
751    * @return The parsed string.
752    * @throws XMLStreamException Thrown by underlying XML stream.
753    */
754   @Override /* Overridden from XmlParserSession */
755   protected String readText(XmlReader r) throws IOException, ParseException, XMLStreamException {
756
757      StringBuilder sb = getStringBuilder();
758
759      int et = r.getEventType();
760      if (et == END_ELEMENT)
761         return "";
762
763      int depth = 0;
764
765      String characters = null;
766
767      while (true) {
768         if (et == START_ELEMENT) {
769            if (nn(characters)) {
770               if (sb.isEmpty())
771                  characters = trimStart(characters);
772               sb.append(characters);
773               characters = null;
774            }
775            var tag = HtmlTag.forEvent(this, r);
776            if (tag == BR) {
777               sb.append('\n');
778               r.nextTag();
779            } else if (tag == BS) {
780               sb.append('\b');
781               r.nextTag();
782            } else if (tag == SP) {
783               et = r.next();
784               if (et == CHARACTERS) {
785                  String s = r.getText();
786                  if (ine(s)) {
787                     var c = r.getText().charAt(0);
788                     if (c == '\u2003')
789                        c = '\t';
790                     sb.append(c);
791                  }
792                  r.nextTag();
793               }
794            } else if (tag == FF) {
795               sb.append('\f');
796               r.nextTag();
797            } else if (tag.isOneOf(STRING, NUMBER, BOOLEAN)) {
798               et = r.next();
799               if (et == CHARACTERS) {
800                  sb.append(r.getText());
801                  r.nextTag();
802               }
803            } else {
804               sb.append('<').append(r.getLocalName());
805               for (var i = 0; i < r.getAttributeCount(); i++)
806                  sb.append(' ').append(r.getAttributeName(i)).append('=').append('\'').append(r.getAttributeValue(i)).append('\'');
807               sb.append('>');
808               depth++;
809            }
810         } else if (et == END_ELEMENT) {
811            if (nn(characters)) {
812               if (sb.isEmpty())
813                  characters = trimStart(characters);
814               if (depth == 0)
815                  characters = trimEnd(characters);
816               sb.append(characters);
817               characters = null;
818            }
819            if (depth == 0)
820               break;
821            sb.append('<').append(r.getLocalName()).append('>');
822            depth--;
823         } else if (et == CHARACTERS) {
824            characters = r.getText();
825         }
826         et = r.next();
827      }
828
829      String s = trim(sb.toString());
830      returnStringBuilder(sb);
831      return s;
832   }
833
834   @Override /* Overridden from XmlParserSession */
835   protected String readWhitespaceElement(XmlReader r) throws IOException, ParseException, XMLStreamException {
836
837      var tag = HtmlTag.forEvent(this, r);
838      int et = r.next();
839      if (tag == BR) {
840         return "\n";
841      } else if (tag == BS) {
842         return "\b";
843      } else if (tag == FF) {
844         return "\f";
845      } else if (tag == SP) {
846         if (et == CHARACTERS) {
847            String s = r.getText();
848            if (s.charAt(0) == '\u2003')
849               s = "\t";
850            r.next();
851            return decodeString(s);
852         }
853         return "";
854      } else {
855         throw new ParseException(this, "Invalid tag found in readWhitespaceElement(): '%s'", tag);
856      }
857   }
858}