/**
   * Reads and parses markup from a file.
   *
   * @return The markup
   * @throws IOException
   * @throws ResourceStreamNotFoundException
   */
  public final Markup parse() throws IOException, ResourceStreamNotFoundException {
    // The root of all markup filters is the xml parser
    markupFilterChain = new RootMarkupFilter(xmlParser);

    // Convert the list of markup filters into a chain
    for (IMarkupFilter filter : getMarkupFilters()) {
      filter.setNextFilter(markupFilterChain);
      markupFilterChain = filter;
    }

    // Initialize the xml parser
    MarkupResourceStream markupResourceStream = markup.getMarkupResourceStream();
    xmlParser.parse(
        markupResourceStream.getResource().getInputStream(),
        markupSettings.getDefaultMarkupEncoding());

    // parse the xml markup and tokenize it into wicket relevant markup
    // elements
    parseMarkup();

    // markupResourceStream.setEncoding(xmlParser.getEncoding());
    markupResourceStream.setDoctype(xmlParser.getDoctype());

    if (xmlParser.getEncoding() == null) {
      String a = "The markup file does not have a XML declaration prolog with 'encoding' attribute";
      String b = ". E.g. <?xml version=\"1.0\" encoding=\"UTF-8\" ?>";

      if (markupSettings.getThrowExceptionOnMissingXmlDeclaration()) {
        throw new MarkupException(markupResourceStream.getResource(), a + b);
      } else {
        log.debug(a + ":" + markupResourceStream.getResource() + ". It is more save to use it" + b);
      }
    }

    return markup;
  }
 /**
  * Construct.
  *
  * @param markup The Markup object being filled while reading the markup resource
  */
 public HtmlHeaderSectionHandler(final Markup markup) {
   super(markup.getMarkupResourceStream());
   this.markup = markup;
 }
  /** Scans the given markup and extracts balancing tags. */
  private void parseMarkup() {
    try {
      // always remember the latest index (size)
      int size = markup.size();

      // Loop through tags
      MarkupElement elem;
      while (null != (elem = getNextTag())) {
        if (elem instanceof HtmlSpecialTag) {
          elem = new ComponentTag(((HtmlSpecialTag) elem).getXmlTag());
        }

        if (elem instanceof ComponentTag) {
          ComponentTag tag = (ComponentTag) elem;

          boolean add = (tag.getId() != null);
          if (!add && tag.isClose()) {
            add = ((tag.getOpenTag() != null) && (tag.getOpenTag().getId() != null));
          }

          // Add tag to list?
          if (add || /*tag.isModified() ||*/ (markup.size() != size)) {
            // Add text from last position to the current tag position
            CharSequence text = xmlParser.getInputFromPositionMarker(tag.getPos());
            if (text.length() > 0) {
              text = handleRawText(text.toString());

              // Make sure you add it at the correct location.
              // IMarkupFilters might have added elements as well.
              markup.addMarkupElement(size, new RawMarkup(text));
            }

            xmlParser.setPositionMarker();

            if (add) {
              // Add to the markup unless the tag has been flagged as
              // to be removed from the markup. (e.g. <wicket:remove>
              if (tag.isIgnore() == false) {
                markup.addMarkupElement(tag);
              }
            }
            /*else if (tag.isModified())
            {
            	markup.addMarkupElement(new RawMarkup(tag.toCharSequence()));
            }*/
            else {
              xmlParser.setPositionMarker(tag.getPos());
            }
          }

          // always remember the latest index (size)
          size = markup.size();
        }
      }
    } catch (final ParseException ex) {
      // Add remaining input string
      final CharSequence text = xmlParser.getInputFromPositionMarker(-1);
      if (text.length() > 0) {
        markup.addMarkupElement(new RawMarkup(text));
      }

      // markup.getMarkupResourceStream().setEncoding(xmlParser.getEncoding());
      markup.getMarkupResourceStream().setDoctype(xmlParser.getDoctype());

      final MarkupStream markupStream = new MarkupStream(markup);
      markupStream.setCurrentIndex(markup.size() - 1);
      throw new MarkupException(markupStream, ex.getMessage(), ex);
    }

    // Add tail?
    CharSequence text = xmlParser.getInputFromPositionMarker(-1);
    if (text.length() > 0) {
      text = handleRawText(text.toString());

      // Make sure you add it at the correct location.
      // IMarkupFilters might have added elements as well.
      markup.addMarkupElement(new RawMarkup(text));
    }

    postProcess(markup);

    // Make all tags immutable and the list of elements unmodifiable
    markup.makeImmutable();
  }
 /**
  * Applications which subclass initFilterChain() might also wish to access the markup resource
  * stream.
  *
  * @return The markup resource stream
  */
 protected MarkupResourceStream getMarkupResourceStream() {
   return markup.getMarkupResourceStream();
 }
 /**
  * In case you want to analyze markup which BY DEFAULT does not use "wicket" to find relevant
  * tags.
  *
  * @param namespace
  */
 public final void setWicketNamespace(final String namespace) {
   markup.getMarkupResourceStream().setWicketNamespace(namespace);
 }