Exemplo n.º 1
0
 private String _parseEmpty(String content) {
   LagartoParser lagartoParser = new LagartoParser(content, false);
   lagartoParser.getConfig().setCalculatePosition(true);
   final StringBuilder errors = new StringBuilder();
   lagartoParser.parse(
       new EmptyTagVisitor() {
         @Override
         public void error(String message) {
           errors.append(message);
           errors.append('\n');
         }
       });
   return errors.toString();
 }
Exemplo n.º 2
0
  private String[] _parse(String content, boolean isXml) {
    final StringBuilder result = new StringBuilder();
    final StringBuilder out = new StringBuilder();

    TagVisitor visitor =
        new TagVisitor() {

          public void start() {}

          public void end() {}

          public void tag(Tag tag) {
            result.append("tag:").append(tag.getName());
            result.append(':').append(tag.getDeepLevel());
            switch (tag.getType()) {
              case START:
                result.append('<');
                break;
              case END:
                result.append('>');
                break;
              case SELF_CLOSING:
                result.append("<>");
                break;
            }
            if (tag.getAttributeCount() > 0) {
              try {
                tag.writeTo(result);
              } catch (IOException ignored) {
              }
            }
            result.append(NEWLINE);
          }

          public void xml(CharSequence version, CharSequence encoding, CharSequence standalone) {
            result
                .append("xml:")
                .append(version)
                .append(":")
                .append(encoding)
                .append(":")
                .append(standalone);
            result.append(NEWLINE);
          }

          public void script(Tag tag, CharSequence bodyM) {
            result.append("scr:").append(tag.getDeepLevel());
            if (tag.getAttributeCount() > 0) {
              try {
                tag.writeTo(result);
              } catch (IOException ignored) {
              }
            }
            String body = bodyM.toString();
            body = StringUtil.removeChars(body, "\r\n\t\b");
            result.append('[').append(body).append(']');
            result.append(NEWLINE);
          }

          public void comment(CharSequence commentM) {
            String comment = commentM.toString();
            comment = StringUtil.removeChars(comment, "\r\n\t\b");
            result.append("com:[").append(comment).append(']').append(NEWLINE);
          }

          public void cdata(CharSequence cdataM) {
            String cdata = cdataM.toString();
            cdata = StringUtil.removeChars(cdata, "\r\n\t\b");
            result.append("cdt:[").append(cdata).append(']').append(NEWLINE);
          }

          public void doctype(Doctype doctype) {
            result.append("doc:[").append(doctype.getName()).append(' ');
            result
                .append(doctype.getPublicIdentifier())
                .append(' ')
                .append(doctype.getSystemIdentifier())
                .append(']')
                .append(NEWLINE);
          }

          public void condComment(
              CharSequence expression,
              boolean isStartingTag,
              boolean isHidden,
              boolean isHiddenEndTag) {
            result.append(isStartingTag ? "CC" : "cc").append(isHidden ? 'H' : 'S');
            result.append(isHiddenEndTag ? "h" : "");
            result.append(":[").append(expression).append(']');
            result.append(NEWLINE);
          }

          public void text(CharSequence text) {
            String t = text.toString();
            t = StringUtil.removeChars(t, "\r\n\t\b");
            if (t.length() != 0) {
              result.append("txt:[").append(t).append(']').append(NEWLINE);
            }
          }

          public void error(String message) {
            result.append("wrn:[").append(message).append(NEWLINE);
          }
        };

    LagartoParser lagartoParser = new LagartoParser(content, false);
    lagartoParser.getConfig().setCalculatePosition(true);

    if (isXml) {
      lagartoParser.getConfig().setParseXmlTags(true);
    }

    TagWriter tagWriter = new TagWriter(out);

    lagartoParser.parse(new TagVisitorChain(visitor, tagWriter));

    return new String[] {result.toString(), out.toString()};
  }