Search in sources :

Example 1 with FilesetEntry

use of com.google.devtools.build.lib.packages.FilesetEntry in project bazel by bazelbuild.

the class XmlOutputFormatter method createValueElement.

/**
   * Creates and returns a new DOM tree for the specified attribute values.
   * For non-configurable attributes, this is a single value. For configurable
   * attributes, this contains one value for each configuration.
   * (Only toplevel values are named attributes; list elements are unnamed.)
   *
   * <p>In the case of configurable attributes, multi-value attributes (e.g. lists)
   * merge all configured lists into an aggregate flattened list. Single-value attributes
   * simply refrain to set a value and annotate the DOM element as configurable.
   *
   * <P>(The ungainly qualified class name is required to avoid ambiguity with
   * OutputFormatter.OutputType.)
   */
private static Element createValueElement(Document doc, Type<?> type, Iterable<Object> values) {
    // "Import static" with method scope:
    Type<?> FILESET_ENTRY = BuildType.FILESET_ENTRY;
    Type<?> LABEL_LIST = BuildType.LABEL_LIST;
    Type<?> LICENSE = BuildType.LICENSE;
    Type<?> STRING_LIST = Type.STRING_LIST;
    final Element elem;
    final boolean hasMultipleValues = Iterables.size(values) > 1;
    Type<?> elemType = type.getListElementType();
    if (elemType != null) {
        // it's a list (includes "distribs")
        elem = doc.createElement("list");
        for (Object value : values) {
            for (Object elemValue : (Collection<?>) value) {
                elem.appendChild(createValueElement(doc, elemType, elemValue));
            }
        }
    } else if (type instanceof Type.DictType) {
        Set<Object> visitedValues = new HashSet<>();
        elem = doc.createElement("dict");
        Type.DictType<?, ?> dictType = (Type.DictType<?, ?>) type;
        for (Object value : values) {
            for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
                if (visitedValues.add(entry.getKey())) {
                    Element pairElem = doc.createElement("pair");
                    elem.appendChild(pairElem);
                    pairElem.appendChild(createValueElement(doc, dictType.getKeyType(), entry.getKey()));
                    pairElem.appendChild(createValueElement(doc, dictType.getValueType(), entry.getValue()));
                }
            }
        }
    } else if (type == LICENSE) {
        elem = createSingleValueElement(doc, "license", hasMultipleValues);
        if (!hasMultipleValues) {
            License license = (License) Iterables.getOnlyElement(values);
            Element exceptions = createValueElement(doc, LABEL_LIST, license.getExceptions());
            exceptions.setAttribute("name", "exceptions");
            elem.appendChild(exceptions);
            Element licenseTypes = createValueElement(doc, STRING_LIST, license.getLicenseTypes());
            licenseTypes.setAttribute("name", "license-types");
            elem.appendChild(licenseTypes);
        }
    } else if (type == FILESET_ENTRY) {
        // Fileset entries: not configurable.
        FilesetEntry filesetEntry = (FilesetEntry) Iterables.getOnlyElement(values);
        elem = doc.createElement("fileset-entry");
        elem.setAttribute("srcdir", filesetEntry.getSrcLabel().toString());
        elem.setAttribute("destdir", filesetEntry.getDestDir().toString());
        elem.setAttribute("symlinks", filesetEntry.getSymlinkBehavior().toString());
        elem.setAttribute("strip_prefix", filesetEntry.getStripPrefix());
        if (filesetEntry.getExcludes() != null) {
            Element excludes = createValueElement(doc, LABEL_LIST, filesetEntry.getExcludes());
            excludes.setAttribute("name", "excludes");
            elem.appendChild(excludes);
        }
        if (filesetEntry.getFiles() != null) {
            Element files = createValueElement(doc, LABEL_LIST, filesetEntry.getFiles());
            files.setAttribute("name", "files");
            elem.appendChild(files);
        }
    } else {
        // INTEGER STRING LABEL DISTRIBUTION OUTPUT
        elem = createSingleValueElement(doc, type.toString(), hasMultipleValues);
        if (!hasMultipleValues && !Iterables.isEmpty(values)) {
            Object value = Iterables.getOnlyElement(values);
            // Values such as those of attribute "linkstamp" may be null.
            if (value != null) {
                try {
                    elem.setAttribute("value", value.toString());
                } catch (DOMException e) {
                    elem.setAttribute("value", "[[[ERROR: could not be encoded as XML]]]");
                }
            }
        }
    }
    return elem;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) FilesetEntry(com.google.devtools.build.lib.packages.FilesetEntry) Element(org.w3c.dom.Element) License(com.google.devtools.build.lib.packages.License) DOMException(org.w3c.dom.DOMException) BuildType(com.google.devtools.build.lib.packages.BuildType) Type(com.google.devtools.build.lib.syntax.Type) FilesetEntry(com.google.devtools.build.lib.packages.FilesetEntry) Collection(java.util.Collection)

Aggregations

BuildType (com.google.devtools.build.lib.packages.BuildType)1 FilesetEntry (com.google.devtools.build.lib.packages.FilesetEntry)1 License (com.google.devtools.build.lib.packages.License)1 Type (com.google.devtools.build.lib.syntax.Type)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 DOMException (org.w3c.dom.DOMException)1 Element (org.w3c.dom.Element)1