Search in sources :

Example 1 with BLangXMLElementLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral in project ballerina by ballerina-lang.

the class TaintAnalyzer method visit.

public void visit(BLangXMLElementLiteral xmlElementLiteral) {
    SymbolEnv xmlElementEnv = SymbolEnv.getXMLElementEnv(xmlElementLiteral, env);
    // Visit in-line namespace declarations
    boolean inLineNamespaceTainted = false;
    for (BLangXMLAttribute attribute : xmlElementLiteral.attributes) {
        if (attribute.name.getKind() == NodeKind.XML_QNAME && ((BLangXMLQName) attribute.name).prefix.value.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
            attribute.accept(this);
            attribute.symbol.tainted = getObservedTaintedStatus();
            if (attribute.symbol.tainted) {
                inLineNamespaceTainted = true;
            }
        }
    }
    // Visit attributes.
    boolean attributesTainted = false;
    for (BLangXMLAttribute attribute : xmlElementLiteral.attributes) {
        if (attribute.name.getKind() == NodeKind.XML_QNAME && !((BLangXMLQName) attribute.name).prefix.value.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
            attribute.accept(this);
            attribute.symbol.tainted = getObservedTaintedStatus();
            if (attribute.symbol.tainted) {
                attributesTainted = true;
            }
        }
    }
    // Visit the tag names
    xmlElementLiteral.startTagName.accept(this);
    boolean startTagTaintedStatus = getObservedTaintedStatus();
    boolean endTagTaintedStatus = false;
    if (xmlElementLiteral.endTagName != null) {
        xmlElementLiteral.endTagName.accept(this);
        endTagTaintedStatus = getObservedTaintedStatus();
    }
    boolean tagNamesTainted = startTagTaintedStatus || endTagTaintedStatus;
    // Visit the children
    boolean childrenTainted = false;
    for (BLangExpression expr : xmlElementLiteral.children) {
        expr.accept(this);
        if (getObservedTaintedStatus()) {
            childrenTainted = true;
        }
    }
    setTaintedStatusList(inLineNamespaceTainted || attributesTainted || tagNamesTainted || childrenTainted);
}
Also used : BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) BLangXMLAttribute(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttribute) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 2 with BLangXMLElementLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral in project ballerina by ballerina-lang.

the class TypeChecker method visit.

public void visit(BLangXMLElementLiteral bLangXMLElementLiteral) {
    SymbolEnv xmlElementEnv = SymbolEnv.getXMLElementEnv(bLangXMLElementLiteral, env);
    // Visit in-line namespace declarations
    bLangXMLElementLiteral.attributes.forEach(attribute -> {
        if (attribute.name.getKind() == NodeKind.XML_QNAME && ((BLangXMLQName) attribute.name).prefix.value.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
            checkExpr((BLangExpression) attribute, xmlElementEnv, Lists.of(symTable.noType));
        }
    });
    // Visit attributes.
    bLangXMLElementLiteral.attributes.forEach(attribute -> {
        if (attribute.name.getKind() != NodeKind.XML_QNAME || !((BLangXMLQName) attribute.name).prefix.value.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
            checkExpr((BLangExpression) attribute, xmlElementEnv, Lists.of(symTable.noType));
        }
    });
    Map<Name, BXMLNSSymbol> namespaces = symResolver.resolveAllNamespaces(xmlElementEnv);
    Name defaultNs = names.fromString(XMLConstants.DEFAULT_NS_PREFIX);
    if (namespaces.containsKey(defaultNs)) {
        bLangXMLElementLiteral.defaultNsSymbol = namespaces.remove(defaultNs);
    }
    bLangXMLElementLiteral.namespacesInScope.putAll(namespaces);
    // Visit the tag names
    validateTags(bLangXMLElementLiteral, xmlElementEnv);
    // Visit the children
    bLangXMLElementLiteral.modifiedChildren = concatSimilarKindXMLNodes(bLangXMLElementLiteral.children, xmlElementEnv);
    resultTypes = Lists.of(types.checkType(bLangXMLElementLiteral, symTable.xmlType, expTypes.get(0)));
}
Also used : BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) BXMLNSSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 3 with BLangXMLElementLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral in project ballerina by ballerina-lang.

the class CodeGenerator method visitXMLTagName.

/**
 * Visit XML tag name and return the index of the tag name in the reference registry.
 *
 * @param tagName           Tag name expression
 * @param xmlElementEnv     Environment of the XML element of the tag
 * @param xmlElementLiteral XML element literal to which the tag name belongs to
 * @return Index of the tag name, in the reference registry
 */
private RegIndex visitXMLTagName(BLangExpression tagName, SymbolEnv xmlElementEnv, BLangXMLElementLiteral xmlElementLiteral) {
    genNode(tagName, xmlElementEnv);
    RegIndex startTagNameRegIndex = tagName.regIndex;
    // If this is a string representation of element name, generate the namespace lookup instructions
    if (tagName.getKind() != NodeKind.XML_QNAME) {
        RegIndex localNameRegIndex = getRegIndex(TypeTags.STRING);
        RegIndex uriRegIndex = getRegIndex(TypeTags.STRING);
        emit(InstructionCodes.S2QNAME, startTagNameRegIndex, localNameRegIndex, uriRegIndex);
        startTagNameRegIndex = getRegIndex(TypeTags.XML);
        generateURILookupInstructions(xmlElementLiteral.namespacesInScope, localNameRegIndex, uriRegIndex, startTagNameRegIndex, xmlElementLiteral.pos, xmlElementEnv);
        tagName.regIndex = startTagNameRegIndex;
    }
    return startTagNameRegIndex;
}
Also used : RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 4 with BLangXMLElementLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

@Override
public void visit(BLangXMLAttribute xmlAttribute) {
    SymbolEnv xmlAttributeEnv = SymbolEnv.getXMLAttributeEnv(xmlAttribute, env);
    BLangExpression attrNameExpr = xmlAttribute.name;
    attrNameExpr.regIndex = calcAndGetExprRegIndex(attrNameExpr);
    genNode(attrNameExpr, xmlAttributeEnv);
    RegIndex attrQNameRegIndex = attrNameExpr.regIndex;
    // If the attribute name is a string representation of qname
    if (attrNameExpr.getKind() != NodeKind.XML_QNAME) {
        RegIndex localNameRegIndex = getRegIndex(TypeTags.STRING);
        RegIndex uriRegIndex = getRegIndex(TypeTags.STRING);
        emit(InstructionCodes.S2QNAME, attrQNameRegIndex, localNameRegIndex, uriRegIndex);
        attrQNameRegIndex = getRegIndex(TypeTags.XML);
        generateURILookupInstructions(((BLangXMLElementLiteral) env.node).namespacesInScope, localNameRegIndex, uriRegIndex, attrQNameRegIndex, xmlAttribute.pos, xmlAttributeEnv);
        attrNameExpr.regIndex = attrQNameRegIndex;
    }
    BLangExpression attrValueExpr = xmlAttribute.value;
    genNode(attrValueExpr, env);
    if (xmlAttribute.isNamespaceDeclr) {
        ((BXMLNSSymbol) xmlAttribute.symbol).nsURIIndex = attrValueExpr.regIndex;
    }
}
Also used : SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 5 with BLangXMLElementLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral in project ballerina by ballerina-lang.

the class Desugar method visit.

@Override
public void visit(BLangXMLElementLiteral xmlElementLiteral) {
    xmlElementLiteral.startTagName = rewriteExpr(xmlElementLiteral.startTagName);
    xmlElementLiteral.endTagName = rewriteExpr(xmlElementLiteral.endTagName);
    xmlElementLiteral.modifiedChildren = rewriteExprs(xmlElementLiteral.modifiedChildren);
    xmlElementLiteral.attributes = rewriteExprs(xmlElementLiteral.attributes);
    // Separate the in-line namepsace declarations and attributes.
    Iterator<BLangXMLAttribute> attributesItr = xmlElementLiteral.attributes.iterator();
    while (attributesItr.hasNext()) {
        BLangXMLAttribute attribute = attributesItr.next();
        if (!attribute.isNamespaceDeclr) {
            continue;
        }
        // Create local namepace declaration for all in-line namespace declarations
        BLangLocalXMLNS xmlns = new BLangLocalXMLNS();
        xmlns.namespaceURI = attribute.value.concatExpr;
        xmlns.prefix = ((BLangXMLQName) attribute.name).localname;
        xmlns.symbol = (BXMLNSSymbol) attribute.symbol;
        xmlElementLiteral.inlineNamespaces.add(xmlns);
        attributesItr.remove();
    }
    result = xmlElementLiteral;
}
Also used : BLangXMLAttribute(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttribute) BLangLocalXMLNS(org.wso2.ballerinalang.compiler.tree.BLangXMLNS.BLangLocalXMLNS)

Aggregations

BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)7 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)4 BLangXMLElementLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral)4 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)3 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)3 BLangXMLAttribute (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLAttribute)2 XMLLiteralNode (org.ballerinalang.model.tree.expressions.XMLLiteralNode)1 BXMLNSSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol)1 BLangLocalXMLNS (org.wso2.ballerinalang.compiler.tree.BLangXMLNS.BLangLocalXMLNS)1 Name (org.wso2.ballerinalang.compiler.util.Name)1