com.google.caja.parser.js.Identifier

Here are the examples of the java api class com.google.caja.parser.js.Identifier taken from open source projects.

1. TrailingUnderscoresHole#createSubstitutes()

Project: caja
File: TrailingUnderscoresHole.java
@Override
protected boolean createSubstitutes(List<ParseTreeNode> substitutes, Map<String, ParseTreeNode> bindings) {
    ParseTreeNode n = bindings.get(getIdentifier());
    if (n == null || !(n instanceof Identifier)) {
        return false;
    }
    Identifier withoutSuffix = (Identifier) n;
    Identifier withSuffix = new Identifier(withoutSuffix.getFilePosition(), n.getValue() + trailing);
    withSuffix.getAttributes().putAll(withoutSuffix.getAttributes());
    substitutes.add(withSuffix);
    return true;
}

2. StringLiteralQuasiNode#createSubstitutes()

Project: caja
File: StringLiteralQuasiNode.java
@Override
protected boolean createSubstitutes(List<ParseTreeNode> substitutes, Map<String, ParseTreeNode> bindings) {
    ParseTreeNode binding = bindings.get(bindingName);
    Identifier ident;
    if (binding instanceof Identifier) {
        ident = (Identifier) binding;
    } else if (binding instanceof Reference) {
        ident = ((Reference) binding).getIdentifier();
    } else {
        return false;
    }
    if (ident.getName() == null) {
        return false;
    }
    StringLiteral sl = StringLiteral.valueOf(ident.getFilePosition(), ident.getName());
    substitutes.add(sl);
    return true;
}

3. Scope#declareStartOfScopeTempVariable()

Project: caja
File: Scope.java
/**
   * Add a temporary variable declaration to the start of the closest enclosing true
   * scope, and return the name of the declared variable.
   *
   * @return the identifier for the newly declared variable.
   * @see #addStartOfScopeStatement(com.google.caja.parser.js.Statement)
   */
public Identifier declareStartOfScopeTempVariable() {
    Scope s = getClosestDeclarationContainer();
    // TODO(ihab.awad): Uses private access to 's' which is of same class but
    // distinct instance. Violates capability discipline; kittens unduly
    // sacrificed. Refactor.
    Identifier id = new Identifier(FilePosition.UNKNOWN, "temp" + (s.tempVariableCounter++) + "_");
    s.addStartOfScopeStatement((Statement) substV("var @id;", "id", id));
    return id;
}

4. LocalVarRenamer#renameOne()

Project: caja
File: LocalVarRenamer.java
private static void renameOne(MutableParseTreeNode n) {
    Identifier id = (Identifier) n.children().get(0);
    String origName = id.getName();
    ScopeInfo u = n.getAttributes().get(SCOPE);
    Scope s = (n instanceof FunctionConstructor) ? u.s : u.s.thatDefines(origName);
    if (s != null) {
        // Will be null for undeclared globals.
        String newName = u.withScope(s).mapping.get(origName);
        if (!newName.equals(origName)) {
            n.replaceChild(new Identifier(id.getFilePosition(), newName), id);
        }
    }
}

5. ProxyServlet#checkIdentifier()

Project: caja
File: ProxyServlet.java
/**
   * Checks whether a string is a JavaScript Identifier.
   */
/* visible for testing */
static boolean checkIdentifier(String candidate) {
    // Using a simple regex is possible if we reject anything but 7-bit ASCII.
    // However, this implementation ensures Caja has a single point of truth
    // regarding what constitutes a JS identifier.
    // TODO(kpreid): Reevaluate whether this is worth the complexity and the
    // runtime dependency on the JS parser now that there is no cajoler. 
    MessageQueue mq = new SimpleMessageQueue();
    Parser parser = new Parser(new JsTokenQueue(new JsLexer(CharProducer.Factory.fromString("var " + candidate + ";", InputSource.UNKNOWN)), InputSource.UNKNOWN), mq);
    ParseTreeNode node;
    try {
        node = parser.parse();
    } catch (ParseException e) {
        return false;
    }
    if (node == null || !mq.getMessages().isEmpty()) {
        return false;
    }
    Map<String, ParseTreeNode> bindings = Maps.newHashMap();
    if (!QuasiBuilder.match("{ var @p; }", node, bindings)) {
        return false;
    }
    if (bindings.size() != 1) {
        return false;
    }
    if (bindings.get("p") == null) {
        return false;
    }
    if (!(bindings.get("p") instanceof Identifier)) {
        return false;
    }
    Identifier p = (Identifier) bindings.get("p");
    if (!candidate.equals(p.getName())) {
        return false;
    }
    return true;
}