Ejemplo n.º 1
0
  /** Classify an input by extension and use the appropriate parser. */
  static ParseTreeNode parseInput(InputSource is, CharProducer cp, MessageQueue mq)
      throws ParseException {

    String path = is.getUri().getPath();

    ParseTreeNode input;
    if (path.endsWith(".js")) {
      JsLexer lexer = new JsLexer(cp);
      JsTokenQueue tq = new JsTokenQueue(lexer, is);
      if (tq.isEmpty()) {
        return null;
      }
      Parser p = new Parser(tq, mq);
      input = p.parse();
      tq.expectEmpty();
    } else if (path.endsWith(".html") || path.endsWith(".xhtml")) {
      DomParser p = new DomParser(new HtmlLexer(cp), is, mq);
      if (p.getTokenQueue().isEmpty()) {
        return null;
      }
      input = p.parseFragment();
      p.getTokenQueue().expectEmpty();
    } else if (path.endsWith(".css")) {
      CssLexer lexer = new CssLexer(cp);
      TokenQueue<CssTokenType> tq =
          new TokenQueue<CssTokenType>(
              lexer,
              is,
              new Criterion<Token<CssTokenType>>() {
                public boolean accept(Token<CssTokenType> tok) {
                  return tok.type != CssTokenType.COMMENT && tok.type != CssTokenType.SPACE;
                }
              });
      if (tq.isEmpty()) {
        return null;
      }

      CssParser p = new CssParser(tq);
      input = p.parseStyleSheet();
      tq.expectEmpty();
    } else {
      throw new AssertionError("Can't classify input " + is);
    }
    return input;
  }
  private void runTest(String goldenIhtml, String inputIhtml, Message... expectedMessages)
      throws Exception {
    Element ihtmlRoot =
        new DomParser(
                DomParser.makeTokenQueue(
                    FilePosition.startOfFile(is), new StringReader(inputIhtml), true, false),
                true,
                mq)
            .parseDocument();
    new IhtmlSanityChecker(mq).check(ihtmlRoot);

    for (Message msg : expectedMessages) {
      assertMessage(
          true,
          msg.getMessageType(),
          msg.getMessageLevel(),
          msg.getMessageParts().toArray(new MessagePart[0]));
    }
    assertMessagesLessSevereThan(MessageLevel.WARNING);

    String checkedIhtml = Nodes.render(ihtmlRoot, MarkupRenderMode.XML);
    assertEquals(goldenIhtml, checkedIhtml);
  }