Search in sources :

Example 61 with NonNull

use of com.android.annotations.NonNull in project buck by facebook.

the class ManifestMerger2 method load.

/**
     * Load an xml file and perform placeholder substitution
     * @param manifestInfo the android manifest information like if it is a library, an
     *                     overlay or a main manifest file.
     * @param selectors all the libraries selectors
     * @param mergingReportBuilder the merging report to store events and errors.
     * @return a loaded manifest info.
     * @throws MergeFailureException
     */
@NonNull
private LoadedManifestInfo load(@NonNull ManifestInfo manifestInfo, @NonNull KeyResolver<String> selectors, @NonNull MergingReport.Builder mergingReportBuilder) throws MergeFailureException {
    File xmlFile = manifestInfo.mLocation;
    XmlDocument xmlDocument;
    try {
        InputStream inputStream = mFileStreamProvider.getInputStream(xmlFile);
        xmlDocument = XmlLoader.load(selectors, mSystemPropertyResolver, manifestInfo.mName, xmlFile, inputStream, manifestInfo.getType(), manifestInfo.getMainManifestPackageName());
    } catch (Exception e) {
        throw new MergeFailureException(e);
    }
    String originalPackageName = xmlDocument.getPackageName();
    MergingReport.Builder builder = manifestInfo.getType() == XmlDocument.Type.MAIN ? mergingReportBuilder : new MergingReport.Builder(mergingReportBuilder.getLogger());
    builder.getActionRecorder().recordDefaultNodeAction(xmlDocument.getRootNode());
    // perform place holder substitution, this is necessary to do so early in case placeholders
    // are used in key attributes.
    performPlaceHolderSubstitution(manifestInfo, xmlDocument, builder);
    return new LoadedManifestInfo(manifestInfo, Optional.fromNullable(originalPackageName), xmlDocument);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) NonNull(com.android.annotations.NonNull)

Example 62 with NonNull

use of com.android.annotations.NonNull in project buck by facebook.

the class XmlAttribute method handleBothToolsAttributePresent.

/**
     * Handles tools: namespace attributes presence in both documents.
     * @param higherPriority the higherPriority attribute
     */
private void handleBothToolsAttributePresent(@NonNull XmlAttribute higherPriority) {
    // do not merge tools:node attributes, the higher priority one wins.
    if (getName().getLocalName().equals(NodeOperationType.NODE_LOCAL_NAME)) {
        return;
    }
    // everything else should be merged, duplicates should be eliminated.
    @NonNull Splitter splitter = Splitter.on(',');
    @NonNull ImmutableSet.Builder<String> targetValues = ImmutableSet.builder();
    targetValues.addAll(splitter.split(higherPriority.getValue()));
    targetValues.addAll(splitter.split(getValue()));
    higherPriority.getXml().setValue(Joiner.on(',').join(targetValues.build()));
}
Also used : Splitter(com.google.common.base.Splitter) ImmutableSet(com.google.common.collect.ImmutableSet) NonNull(com.android.annotations.NonNull)

Example 63 with NonNull

use of com.android.annotations.NonNull in project buck by facebook.

the class XmlElement method mergeChild.

// merge a child of a lower priority node into this higher priority node.
private void mergeChild(@NonNull XmlElement lowerPriorityChild, @NonNull MergingReport.Builder mergingReport) {
    ILogger logger = mergingReport.getLogger();
    // If this a custom element, we just blindly merge it in.
    if (lowerPriorityChild.getType() == ManifestModel.NodeTypes.CUSTOM) {
        handleCustomElement(lowerPriorityChild, mergingReport);
        return;
    }
    Optional<XmlElement> thisChildOptional = getNodeByTypeAndKey(lowerPriorityChild.getType(), lowerPriorityChild.getKey());
    // only in the lower priority document ?
    if (!thisChildOptional.isPresent()) {
        addElement(lowerPriorityChild, mergingReport);
        return;
    }
    // it's defined in both files.
    logger.verbose(lowerPriorityChild.getId() + " defined in both files...");
    XmlElement thisChild = thisChildOptional.get();
    switch(thisChild.getType().getMergeType()) {
        case CONFLICT:
            addMessage(mergingReport, MergingReport.Record.Severity.ERROR, String.format("Node %1$s cannot be present in more than one input file and it's " + "present at %2$s and %3$s", thisChild.getType(), thisChild.printPosition(), lowerPriorityChild.printPosition()));
            break;
        case ALWAYS:
            // no merging, we consume the lower priority node unmodified.
            // if the two elements are equal, just skip it.
            // but check first that we are not supposed to replace or remove it.
            @NonNull NodeOperationType operationType = calculateNodeOperationType(thisChild, lowerPriorityChild);
            if (operationType == NodeOperationType.REMOVE || operationType == NodeOperationType.REPLACE) {
                mergingReport.getActionRecorder().recordNodeAction(thisChild, Actions.ActionType.REJECTED, lowerPriorityChild);
                break;
            }
            if (thisChild.getType().areMultipleDeclarationAllowed()) {
                mergeChildrenWithMultipleDeclarations(lowerPriorityChild, mergingReport);
            } else {
                if (!thisChild.isEquals(lowerPriorityChild)) {
                    addElement(lowerPriorityChild, mergingReport);
                }
            }
            break;
        default:
            // 2 nodes exist, some merging need to happen
            handleTwoElementsExistence(thisChild, lowerPriorityChild, mergingReport);
            break;
    }
}
Also used : NonNull(com.android.annotations.NonNull) ILogger(com.android.common.utils.ILogger)

Example 64 with NonNull

use of com.android.annotations.NonNull in project buck by facebook.

the class AndroidManifest method getStringValue.

@NonNull
private static String getStringValue(@NonNull IAbstractFile file, @NonNull String xPath) throws StreamException {
    XPath xpath = AndroidXPathFactory.newXPath();
    InputStream is = null;
    try {
        is = file.getContents();
        return xpath.evaluate(xPath, new InputSource(is));
    } catch (XPathExpressionException e) {
        throw new RuntimeException("Malformed XPath expression when reading the attribute from the manifest," + "exp = " + xPath, e);
    } finally {
        Closeables.closeQuietly(is);
    }
}
Also used : XPath(javax.xml.xpath.XPath) InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NonNull(com.android.annotations.NonNull)

Example 65 with NonNull

use of com.android.annotations.NonNull in project buck by facebook.

the class PositionXmlParser method parse.

/**
     * Parses the XML content from the given input stream.
     *
     * @param input the input stream containing the XML to be parsed
     * @param checkDtd whether or not download the DTD and validate it
     * @return the corresponding document
     * @throws ParserConfigurationException if a SAX parser is not available
     * @throws SAXException if the document contains a parsing error
     * @throws IOException if something is seriously wrong. This should not
     *             happen since the input source is known to be constructed from
     *             a string.
     */
@NonNull
public static Document parse(@NonNull InputStream input, boolean checkDtd) throws ParserConfigurationException, SAXException, IOException {
    // Read in all the data
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    while (true) {
        int r = input.read(buf);
        if (r == -1) {
            break;
        }
        out.write(buf, 0, r);
    }
    input.close();
    return parse(out.toByteArray(), checkDtd);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) NonNull(com.android.annotations.NonNull)

Aggregations

NonNull (com.android.annotations.NonNull)89 File (java.io.File)23 TextRange (com.intellij.openapi.util.TextRange)13 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)8 OutputFile (com.android.build.OutputFile)5 DefaultPosition (com.android.tools.klint.detector.api.DefaultPosition)5 Position (com.android.tools.klint.detector.api.Position)5 Issue (com.android.tools.klint.detector.api.Issue)4 Position (com.android.tools.lint.detector.api.Position)4 Module (com.intellij.openapi.module.Module)4 StringReader (java.io.StringReader)4 AbstractInsnNode (org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode)4 Node (org.w3c.dom.Node)4 NodeList (org.w3c.dom.NodeList)4 InputSource (org.xml.sax.InputSource)4 Nullable (com.android.annotations.Nullable)3 AaptPackageProcessBuilder (com.android.builder.core.AaptPackageProcessBuilder)3 AtlasBuilder (com.android.builder.core.AtlasBuilder)3 AndroidProject (com.android.builder.model.AndroidProject)3