Search in sources :

Example 1 with Severity

use of com.android.tools.klint.detector.api.Severity in project kotlin by JetBrains.

the class DefaultConfiguration method getSeverity.

@Override
@NonNull
public Severity getSeverity(@NonNull Issue issue) {
    ensureInitialized();
    Severity severity = mSeverity.get(issue.getId());
    if (severity == null) {
        severity = mSeverity.get(VALUE_ALL);
    }
    if (severity != null) {
        return severity;
    }
    if (mParent != null) {
        return mParent.getSeverity(issue);
    }
    return getDefaultSeverity(issue);
}
Also used : Severity(com.android.tools.klint.detector.api.Severity) NonNull(com.android.annotations.NonNull)

Example 2 with Severity

use of com.android.tools.klint.detector.api.Severity in project kotlin by JetBrains.

the class DefaultConfiguration method writeConfig.

private void writeConfig() {
    try {
        // Write the contents to a new file first such that we don't clobber the
        // existing file if some I/O error occurs.
        File file = new File(mConfigFile.getParentFile(), //$NON-NLS-1$
        mConfigFile.getName() + ".new");
        Writer writer = new BufferedWriter(new FileWriter(file));
        writer.write(//$NON-NLS-1$
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + //$NON-NLS-1$
        "<lint>\n");
        if (!mSuppressed.isEmpty() || !mSeverity.isEmpty()) {
            // Process the maps in a stable sorted order such that if the
            // files are checked into version control with the project,
            // there are no random diffs just because hashing algorithms
            // differ:
            Set<String> idSet = new HashSet<String>();
            for (String id : mSuppressed.keySet()) {
                idSet.add(id);
            }
            for (String id : mSeverity.keySet()) {
                idSet.add(id);
            }
            List<String> ids = new ArrayList<String>(idSet);
            Collections.sort(ids);
            for (String id : ids) {
                //$NON-NLS-1$
                writer.write("    <");
                writer.write(TAG_ISSUE);
                writeAttribute(writer, ATTR_ID, id);
                Severity severity = mSeverity.get(id);
                if (severity != null) {
                    writeAttribute(writer, ATTR_SEVERITY, severity.name().toLowerCase(Locale.US));
                }
                List<Pattern> regexps = mRegexps != null ? mRegexps.get(id) : null;
                List<String> paths = mSuppressed.get(id);
                if (paths != null && !paths.isEmpty() || regexps != null && !regexps.isEmpty()) {
                    writer.write('>');
                    writer.write('\n');
                    // by ignore(...)
                    if (paths != null) {
                        for (String path : paths) {
                            //$NON-NLS-1$
                            writer.write("        <");
                            writer.write(TAG_IGNORE);
                            writeAttribute(writer, ATTR_PATH, path.replace('\\', '/'));
                            //$NON-NLS-1$
                            writer.write(" />\n");
                        }
                    }
                    if (regexps != null) {
                        for (Pattern regexp : regexps) {
                            //$NON-NLS-1$
                            writer.write("        <");
                            writer.write(TAG_IGNORE);
                            writeAttribute(writer, ATTR_REGEXP, regexp.pattern());
                            //$NON-NLS-1$
                            writer.write(" />\n");
                        }
                    }
                    //$NON-NLS-1$
                    writer.write("    </");
                    writer.write(TAG_ISSUE);
                    writer.write('>');
                    writer.write('\n');
                } else {
                    //$NON-NLS-1$
                    writer.write(" />\n");
                }
            }
        }
        //$NON-NLS-1$
        writer.write("</lint>");
        writer.close();
        // Move file into place: move current version to lint.xml~ (removing the old ~ file
        // if it exists), then move the new version to lint.xml.
        File oldFile = new File(mConfigFile.getParentFile(), //$NON-NLS-1$
        mConfigFile.getName() + '~');
        if (oldFile.exists()) {
            oldFile.delete();
        }
        if (mConfigFile.exists()) {
            mConfigFile.renameTo(oldFile);
        }
        boolean ok = file.renameTo(mConfigFile);
        if (ok && oldFile.exists()) {
            oldFile.delete();
        }
    } catch (Exception e) {
        mClient.log(e, null);
    }
}
Also used : Pattern(java.util.regex.Pattern) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) Severity(com.android.tools.klint.detector.api.Severity) PatternSyntaxException(java.util.regex.PatternSyntaxException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) BufferedWriter(java.io.BufferedWriter) File(java.io.File) BufferedWriter(java.io.BufferedWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) HashSet(java.util.HashSet)

Example 3 with Severity

use of com.android.tools.klint.detector.api.Severity in project kotlin by JetBrains.

the class DefaultConfiguration method readConfig.

private void readConfig() {
    mSuppressed = new HashMap<String, List<String>>();
    mSeverity = new HashMap<String, Severity>();
    if (!mConfigFile.exists()) {
        return;
    }
    try {
        Document document = XmlUtils.parseUtfXmlFile(mConfigFile, false);
        NodeList issues = document.getElementsByTagName(TAG_ISSUE);
        Splitter splitter = Splitter.on(',').trimResults().omitEmptyStrings();
        for (int i = 0, count = issues.getLength(); i < count; i++) {
            Node node = issues.item(i);
            Element element = (Element) node;
            String idList = element.getAttribute(ATTR_ID);
            if (idList.isEmpty()) {
                formatError("Invalid lint config file: Missing required issue id attribute");
                continue;
            }
            Iterable<String> ids = splitter.split(idList);
            NamedNodeMap attributes = node.getAttributes();
            for (int j = 0, n = attributes.getLength(); j < n; j++) {
                Node attribute = attributes.item(j);
                String name = attribute.getNodeName();
                String value = attribute.getNodeValue();
                if (ATTR_ID.equals(name)) {
                // already handled
                } else if (ATTR_SEVERITY.equals(name)) {
                    for (Severity severity : Severity.values()) {
                        if (value.equalsIgnoreCase(severity.name())) {
                            for (String id : ids) {
                                mSeverity.put(id, severity);
                            }
                            break;
                        }
                    }
                } else {
                    formatError("Unexpected attribute \"%1$s\"", name);
                }
            }
            // Look up ignored errors
            NodeList childNodes = element.getChildNodes();
            if (childNodes.getLength() > 0) {
                for (int j = 0, n = childNodes.getLength(); j < n; j++) {
                    Node child = childNodes.item(j);
                    if (child.getNodeType() == Node.ELEMENT_NODE) {
                        Element ignore = (Element) child;
                        String path = ignore.getAttribute(ATTR_PATH);
                        if (path.isEmpty()) {
                            String regexp = ignore.getAttribute(ATTR_REGEXP);
                            if (regexp.isEmpty()) {
                                formatError("Missing required attribute %1$s or %2$s under %3$s", ATTR_PATH, ATTR_REGEXP, idList);
                            } else {
                                addRegexp(idList, ids, n, regexp, false);
                            }
                        } else {
                            // handle the file format containing / or \.
                            if (File.separatorChar == '/') {
                                path = path.replace('\\', '/');
                            } else {
                                path = path.replace('/', File.separatorChar);
                            }
                            if (path.indexOf('*') != -1) {
                                String regexp = globToRegexp(path);
                                addRegexp(idList, ids, n, regexp, false);
                            } else {
                                for (String id : ids) {
                                    List<String> paths = mSuppressed.get(id);
                                    if (paths == null) {
                                        paths = new ArrayList<String>(n / 2 + 1);
                                        mSuppressed.put(id, paths);
                                    }
                                    paths.add(path);
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (SAXParseException e) {
        formatError(e.getMessage());
    } catch (Exception e) {
        mClient.log(e, null);
    }
}
Also used : Splitter(com.google.common.base.Splitter) NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Severity(com.android.tools.klint.detector.api.Severity) Document(org.w3c.dom.Document) PatternSyntaxException(java.util.regex.PatternSyntaxException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXParseException(org.xml.sax.SAXParseException) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List)

Aggregations

Severity (com.android.tools.klint.detector.api.Severity)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 PatternSyntaxException (java.util.regex.PatternSyntaxException)2 SAXParseException (org.xml.sax.SAXParseException)2 NonNull (com.android.annotations.NonNull)1 Splitter (com.google.common.base.Splitter)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 Writer (java.io.Writer)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Pattern (java.util.regex.Pattern)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1