コード例 #1
0
  @Override
  public Void visitXmlTagNode(XmlTagNode node) {
    if (isScriptNode(node)) {
      Source htmlSource = htmlElement.getSource();
      String scriptSourcePath = getScriptSourcePath(node);
      if (node.getAttributeEnd().getType() == TokenType.GT && scriptSourcePath == null) {
        EmbeddedHtmlScriptElementImpl script = new EmbeddedHtmlScriptElementImpl(node);
        String contents = node.getContent();

        // TODO (danrubel): Move scanning embedded scripts into AnalysisContextImpl
        // so that clients such as builder can scan, parse, and get errors without resolving
        AnalysisErrorListener errorListener =
            new AnalysisErrorListener() {
              @Override
              public void onError(AnalysisError error) {
                // TODO (danrubel): make errors accessible once this is moved into
                // AnalysisContextImpl
              }
            };
        StringScanner scanner = new StringScanner(null, contents, errorListener);
        com.google.dart.engine.scanner.Token firstToken = scanner.tokenize();
        int[] lineStarts = scanner.getLineStarts();

        // TODO (danrubel): Move parsing embedded scripts into AnalysisContextImpl
        // so that clients such as builder can scan, parse, and get errors without resolving
        Parser parser = new Parser(null, errorListener);
        CompilationUnit unit = parser.parseCompilationUnit(firstToken);

        try {
          CompilationUnitBuilder builder = new CompilationUnitBuilder();
          CompilationUnitElementImpl elem = builder.buildCompilationUnit(htmlSource, unit);
          LibraryElementImpl library = new LibraryElementImpl(context, null);
          library.setDefiningCompilationUnit(elem);
          script.setScriptLibrary(library);
        } catch (AnalysisException e) {
          // TODO (danrubel): Handle or forward the exception
          e.printStackTrace();
        }

        scripts.add(script);
      } else {
        ExternalHtmlScriptElementImpl script = new ExternalHtmlScriptElementImpl(node);
        if (scriptSourcePath != null) {
          script.setScriptSource(
              context.getSourceFactory().resolveUri(htmlSource, scriptSourcePath));
        }
        scripts.add(script);
      }
    } else {
      node.visitChildren(this);
    }
    return null;
  }
コード例 #2
0
 private void validate(int scriptIndex, EmbeddedHtmlScriptElementImpl script) {
   LibraryElement library = script.getScriptLibrary();
   assertNotNull("script " + scriptIndex, library);
   assertSame("script " + scriptIndex, context, script.getContext());
   CompilationUnitElement unit = library.getDefiningCompilationUnit();
   assertNotNull("script " + scriptIndex, unit);
   TopLevelVariableElement[] variables = unit.getTopLevelVariables();
   assertLength(expectedVariables.length, variables);
   for (int index = 0; index < variables.length; index++) {
     expectedVariables[index].validate(scriptIndex, variables[index]);
   }
   assertSame("script " + scriptIndex, script, library.getEnclosingElement());
 }