Search in sources :

Example 1 with ResourceContext

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

the class LintDriver method checkIndividualResources.

/** Checks individual resources */
private void checkIndividualResources(@NonNull Project project, @Nullable Project main, @NonNull List<ResourceXmlDetector> xmlDetectors, @Nullable List<Detector> dirChecks, @Nullable List<Detector> binaryChecks, @NonNull List<File> files) {
    for (File file : files) {
        if (file.isDirectory()) {
            // Is it a resource folder?
            ResourceFolderType type = ResourceFolderType.getFolderType(file.getName());
            if (type != null && new File(file.getParentFile(), RES_FOLDER).exists()) {
                // Yes.
                checkResourceFolder(project, main, file, type, xmlDetectors, dirChecks, binaryChecks);
            } else if (file.getName().equals(RES_FOLDER)) {
                // Is it the res folder?
                // Yes
                checkResFolder(project, main, file, xmlDetectors, dirChecks, binaryChecks);
            } else {
                mClient.log(null, "Unexpected folder %1$s; should be project, " + "\"res\" folder or resource folder", file.getPath());
            }
        } else if (file.isFile() && LintUtils.isXmlFile(file)) {
            // Yes, find out its resource type
            String folderName = file.getParentFile().getName();
            ResourceFolderType type = ResourceFolderType.getFolderType(folderName);
            if (type != null) {
                ResourceVisitor visitor = getVisitor(type, xmlDetectors, binaryChecks);
                if (visitor != null) {
                    XmlContext context = new XmlContext(this, project, main, file, type, visitor.getParser());
                    fireEvent(EventType.SCANNING_FILE, context);
                    visitor.visitFile(context, file);
                }
            }
        } else if (binaryChecks != null && file.isFile() && LintUtils.isBitmapFile(file)) {
            // Yes, find out its resource type
            String folderName = file.getParentFile().getName();
            ResourceFolderType type = ResourceFolderType.getFolderType(folderName);
            if (type != null) {
                ResourceVisitor visitor = getVisitor(type, xmlDetectors, binaryChecks);
                if (visitor != null) {
                    ResourceContext context = new ResourceContext(this, project, main, file, type);
                    fireEvent(EventType.SCANNING_FILE, context);
                    visitor.visitBinaryResource(context);
                    if (mCanceled) {
                        return;
                    }
                }
            }
        }
    }
}
Also used : ResourceContext(com.android.tools.klint.detector.api.ResourceContext) ResourceFolderType(com.android.resources.ResourceFolderType) XmlContext(com.android.tools.klint.detector.api.XmlContext) File(java.io.File) PsiFile(com.intellij.psi.PsiFile)

Example 2 with ResourceContext

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

the class LintDriver method checkResourceFolder.

private void checkResourceFolder(@NonNull Project project, @Nullable Project main, @NonNull File dir, @NonNull ResourceFolderType type, @NonNull List<ResourceXmlDetector> xmlChecks, @Nullable List<Detector> dirChecks, @Nullable List<Detector> binaryChecks) {
    if (dirChecks != null && !dirChecks.isEmpty()) {
        ResourceContext context = new ResourceContext(this, project, main, dir, type);
        String folderName = dir.getName();
        fireEvent(EventType.SCANNING_FILE, context);
        for (Detector check : dirChecks) {
            if (check.appliesTo(type)) {
                check.beforeCheckFile(context);
                check.checkFolder(context, folderName);
                check.afterCheckFile(context);
            }
        }
        if (binaryChecks == null && xmlChecks.isEmpty()) {
            return;
        }
    }
    File[] files = dir.listFiles();
    if (files == null || files.length <= 0) {
        return;
    }
    ResourceVisitor visitor = getVisitor(type, xmlChecks, binaryChecks);
    if (visitor != null) {
        // if not, there are no applicable rules in this folder
        // Process files in alphabetical order, to ensure stable output
        // (for example for the duplicate resource detector)
        Arrays.sort(files);
        for (File file : files) {
            if (LintUtils.isXmlFile(file)) {
                XmlContext context = new XmlContext(this, project, main, file, type, visitor.getParser());
                fireEvent(EventType.SCANNING_FILE, context);
                visitor.visitFile(context, file);
            } else if (binaryChecks != null && (LintUtils.isBitmapFile(file) || type == ResourceFolderType.RAW)) {
                ResourceContext context = new ResourceContext(this, project, main, file, type);
                fireEvent(EventType.SCANNING_FILE, context);
                visitor.visitBinaryResource(context);
            }
            if (mCanceled) {
                return;
            }
        }
    }
}
Also used : ResourceContext(com.android.tools.klint.detector.api.ResourceContext) ResourceXmlDetector(com.android.tools.klint.detector.api.ResourceXmlDetector) Detector(com.android.tools.klint.detector.api.Detector) XmlContext(com.android.tools.klint.detector.api.XmlContext) File(java.io.File) PsiFile(com.intellij.psi.PsiFile)

Aggregations

ResourceContext (com.android.tools.klint.detector.api.ResourceContext)2 XmlContext (com.android.tools.klint.detector.api.XmlContext)2 PsiFile (com.intellij.psi.PsiFile)2 File (java.io.File)2 ResourceFolderType (com.android.resources.ResourceFolderType)1 Detector (com.android.tools.klint.detector.api.Detector)1 ResourceXmlDetector (com.android.tools.klint.detector.api.ResourceXmlDetector)1