Java Code Examples for com.android.resources.ResourceFolderType#VALUES

The following examples show how to use com.android.resources.ResourceFolderType#VALUES . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResourcePrefixDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void beforeCheckFile(@NonNull Context context) {
    if (mPrefix != null && context instanceof XmlContext) {
        XmlContext xmlContext = (XmlContext) context;
        ResourceFolderType folderType = xmlContext.getResourceFolderType();
        if (folderType != null && folderType != ResourceFolderType.VALUES) {
            String name = LintUtils.getBaseName(context.file.getName());
            if (!name.startsWith(mPrefix)) {
                // Attempt to report the error on the root tag of the associated
                // document to make suppressing the error with a tools:suppress
                // attribute etc possible
                if (xmlContext.document != null) {
                    Element root = xmlContext.document.getDocumentElement();
                    if (root != null) {
                        xmlContext.report(ISSUE, root, xmlContext.getLocation(root),
                                getErrorMessage(name));
                        return;
                    }
                }
                context.report(ISSUE, Location.create(context.file),
                        getErrorMessage(name));
            }
        }
    }
}
 
Example 2
Source File: ResourcePrefixDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
    if (mPrefix == null || context.getResourceFolderType() != ResourceFolderType.VALUES) {
        return;
    }

    for (Element item : LintUtils.getChildren(element)) {
        Attr nameAttribute = item.getAttributeNode(ATTR_NAME);
        if (nameAttribute != null) {
            String name = nameAttribute.getValue();
            if (!name.startsWith(mPrefix)) {
                String message = getErrorMessage(name);
                context.report(ISSUE, nameAttribute, context.getLocation(nameAttribute),
                        message);
            }
        }
    }
}
 
Example 3
Source File: GeneratedResourceClassifier.java    From intellij with Apache License 2.0 6 votes vote down vote up
private static boolean mayHaveNonStringTranslations(String dirName) {
  // String translations only sit in the values-xx-rYY directories, so we can rule out other
  // directories quickly.
  if (!dirName.contains(SdkConstants.RES_QUALIFIER_SEP)) {
    return true;
  }
  if (ResourceFolderType.getFolderType(dirName) != ResourceFolderType.VALUES) {
    return true;
  }
  FolderConfiguration config = FolderConfiguration.getConfigForFolder(dirName);
  // Conservatively say it's interesting if there is an unrecognized configuration.
  if (config == null) {
    return true;
  }
  // If this is a translation mixed with something else, consider it a translation directory.
  boolean hasTranslation = false;
  for (ResourceQualifier qualifier : config.getQualifiers()) {
    if (qualifier instanceof LocaleQualifier) {
      hasTranslation = true;
    }
  }
  return !hasTranslation;
}
 
Example 4
Source File: ResourceSet.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a FolderData for the given folder.
 *
 * @param folder the folder.
 * @return the FolderData object, or null if we can't determine the {#link ResourceFolderType}
 * of the folder.
 */
@Nullable
private FolderData getFolderData(File folder) throws MergingException {
    FolderData fd = new FolderData();

    String folderName = folder.getName();
    int pos = folderName.indexOf(ResourceConstants.RES_QUALIFIER_SEP);
    if (pos != -1) {
        fd.folderType = ResourceFolderType.getTypeByName(folderName.substring(0, pos));
        if (fd.folderType == null) {
            return null;
        }

        FolderConfiguration folderConfiguration = FolderConfiguration.getConfigForFolder(folderName);
        if (folderConfiguration == null) {
            throw MergingException.withMessage("Invalid resource directory name")
                    .withFile(folder).build();
        }

        if (mNormalizeResources) {
            // normalize it
            folderConfiguration.normalize();
        }

        // get the qualifier portion from the folder config.
        // the returned string starts with "-" so we remove that.
        fd.qualifiers = folderConfiguration.getUniqueKey().substring(1);

    } else {
        fd.folderType = ResourceFolderType.getTypeByName(folderName);
    }

    if (fd.folderType != null && fd.folderType != ResourceFolderType.VALUES) {
        fd.type = FolderTypeRelationship.getRelatedResourceTypes(fd.folderType).get(0);
    }

    return fd;
}
 
Example 5
Source File: ParsedAndroidData.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
  try {
    if (!Files.isDirectory(path) && !path.getFileName().toString().startsWith(".")) {
      if (folderType == ResourceFolderType.VALUES) {
        DataResourceXml.parse(
            XmlResourceValues.getXmlInputFactory(),
            path,
            fqnFactory,
            overwritingConsumer,
            combiningResources);
      } else if (folderType != null) {
        FullyQualifiedName key = fqnFactory.parse(path);
        if (ID_PROVIDING_RESOURCE_TYPES.contains(folderType)
            && path.getFileName().toString().endsWith(SdkConstants.DOT_XML)) {
          DataValueFileWithIds.parse(
              XmlResourceValues.getXmlInputFactory(),
              path,
              key,
              fqnFactory,
              overwritingConsumer,
              combiningResources);
        } else {
          overwritingConsumer.accept(key, DataValueFile.of(path));
        }
      }
    }
  } catch (IllegalArgumentException | XMLStreamException e) {
    errors.add(e);
  }
  return super.visitFile(path, attrs);
}
 
Example 6
Source File: OverdrawDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean appliesTo(@NonNull ResourceFolderType folderType) {
    // Look in layouts for drawable resources
    return super.appliesTo(folderType)
            // and in resource files for theme definitions
            || folderType == ResourceFolderType.VALUES
            // and in drawable files for bitmap tiling modes
            || folderType == ResourceFolderType.DRAWABLE;
}
 
Example 7
Source File: ResourceCycleDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean appliesTo(@NonNull ResourceFolderType folderType) {
    return folderType == ResourceFolderType.VALUES
            || folderType == ResourceFolderType.COLOR
            || folderType == ResourceFolderType.DRAWABLE
            || folderType == ResourceFolderType.LAYOUT;
}
 
Example 8
Source File: ResourcePrefixDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void checkBinaryResource(@NonNull ResourceContext context) {
    if (mPrefix != null) {
        ResourceFolderType folderType = context.getResourceFolderType();
        if (folderType != null && folderType != ResourceFolderType.VALUES) {
            String name = LintUtils.getBaseName(context.file.getName());
            if (!name.startsWith(mPrefix)) {
                Location location = Location.create(context.file);
                context.report(ISSUE, location, getErrorMessage(name));
            }
        }
    }
}
 
Example 9
Source File: ResourceSet.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a FolderData for the given folder
 * @param folder the folder.
 * @return the FolderData object.
 */
@Nullable
private static FolderData getFolderData(File folder) {
    FolderData fd = new FolderData();

    String folderName = folder.getName();
    int pos = folderName.indexOf(ResourceConstants.RES_QUALIFIER_SEP);
    if (pos != -1) {
        fd.folderType = ResourceFolderType.getTypeByName(folderName.substring(0, pos));
        if (fd.folderType == null) {
            return null;
        }

        FolderConfiguration folderConfiguration = FolderConfiguration.getConfigForFolder(folderName);
        if (folderConfiguration == null) {
            return null;
        }

        // normalize it
        folderConfiguration.normalize();

        // get the qualifier portion from the folder config.
        // the returned string starts with "-" so we remove that.
        fd.qualifiers = folderConfiguration.getUniqueKey().substring(1);

    } else {
        fd.folderType = ResourceFolderType.getTypeByName(folderName);
    }

    if (fd.folderType != null && fd.folderType != ResourceFolderType.VALUES) {
        fd.type = FolderTypeRelationship.getRelatedResourceTypes(fd.folderType).get(0);
    }

    return fd;
}
 
Example 10
Source File: PxUsageDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean appliesTo(@NonNull ResourceFolderType folderType) {
    // Look in both layouts (at attribute values) and in value files (at style definitions)
    return folderType == ResourceFolderType.LAYOUT || folderType == ResourceFolderType.VALUES;
}
 
Example 11
Source File: ArraySizeDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean appliesTo(@NonNull ResourceFolderType folderType) {
    return folderType == ResourceFolderType.VALUES;
}
 
Example 12
Source File: PluralsDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean appliesTo(@NonNull ResourceFolderType folderType) {
    return folderType == ResourceFolderType.VALUES;
}
 
Example 13
Source File: StringFormatDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean appliesTo(@NonNull ResourceFolderType folderType) {
    return folderType == ResourceFolderType.VALUES;
}
 
Example 14
Source File: NegativeMarginDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean appliesTo(@NonNull ResourceFolderType folderType) {
    // Look in both layouts (at attribute values) and in value files (style and dimension
    // definitions)
    return folderType == ResourceFolderType.LAYOUT || folderType == ResourceFolderType.VALUES;
}
 
Example 15
Source File: TranslationDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean appliesTo(@NonNull ResourceFolderType folderType) {
    return folderType == ResourceFolderType.VALUES;
}
 
Example 16
Source File: DuplicateResourceDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean appliesTo(@NonNull ResourceFolderType folderType) {
    return folderType == ResourceFolderType.VALUES;
}
 
Example 17
Source File: TypographyDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean appliesTo(@NonNull ResourceFolderType folderType) {
    return folderType == ResourceFolderType.VALUES;
}
 
Example 18
Source File: XmlPrettyPrinter.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private static void formatFile(@NonNull XmlFormatPreferences prefs, File file,
        boolean stdout) {
    if (file.isDirectory()) {
        File[] files = file.listFiles();
        if (files != null) {
            for (File child : files) {
                formatFile(prefs, child, stdout);
            }
        }
    } else if (file.isFile() && SdkUtils.endsWithIgnoreCase(file.getName(), DOT_XML)) {
        XmlFormatStyle style = null;
        if (file.getName().equals(SdkConstants.ANDROID_MANIFEST_XML)) {
            style = XmlFormatStyle.MANIFEST;
        } else {
            File parent = file.getParentFile();
            if (parent != null) {
                String parentName = parent.getName();
                ResourceFolderType folderType = ResourceFolderType.getFolderType(parentName);
                if (folderType == ResourceFolderType.LAYOUT) {
                    style = XmlFormatStyle.LAYOUT;
                } else if (folderType == ResourceFolderType.VALUES) {
                    style = XmlFormatStyle.RESOURCE;
                }
            }
        }

        try {
            String xml = Files.toString(file, Charsets.UTF_8);
            Document document = XmlUtils.parseDocumentSilently(xml, true);
            if (document == null) {
                System.err.println("Could not parse " + file);
                System.exit(1);
                return;
            }

            if (style == null) {
                style = XmlFormatStyle.get(document);
            }
            boolean endWithNewline = xml.endsWith("\n");
            int firstNewLine = xml.indexOf('\n');
            String lineSeparator = firstNewLine > 0 && xml.charAt(firstNewLine - 1) == '\r' ?
                    "\r\n" : "\n";
            String formatted = XmlPrettyPrinter.prettyPrint(document, prefs, style,
                    lineSeparator, endWithNewline);
            if (stdout) {
                System.out.println(formatted);
            } else {
                Files.write(formatted, file, Charsets.UTF_8);
            }
        } catch (IOException e) {
            System.err.println("Could not read " + file);
            System.exit(1);
        }
    }
}
 
Example 19
Source File: WrongIdDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean appliesTo(@NonNull ResourceFolderType folderType) {
    return folderType == ResourceFolderType.LAYOUT || folderType == ResourceFolderType.VALUES;
}
 
Example 20
Source File: XmlPrettyPrinter.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
private static void formatFile(@NonNull XmlFormatPreferences prefs, File file,
        boolean stdout) {
    if (file.isDirectory()) {
        File[] files = file.listFiles();
        if (files != null) {
            for (File child : files) {
                formatFile(prefs, child, stdout);
            }
        }
    } else if (file.isFile() && SdkUtils.endsWithIgnoreCase(file.getName(), DOT_XML)) {
        XmlFormatStyle style = null;
        if (file.getName().equals(SdkConstants.ANDROID_MANIFEST_XML)) {
            style = XmlFormatStyle.MANIFEST;
        } else {
            File parent = file.getParentFile();
            if (parent != null) {
                String parentName = parent.getName();
                ResourceFolderType folderType = ResourceFolderType.getFolderType(parentName);
                if (folderType == ResourceFolderType.LAYOUT) {
                    style = XmlFormatStyle.LAYOUT;
                } else if (folderType == ResourceFolderType.VALUES) {
                    style = XmlFormatStyle.RESOURCE;
                }
            }
        }

        try {
            String xml = Files.toString(file, Charsets.UTF_8);
            Document document = XmlUtils.parseDocumentSilently(xml, true);
            if (document == null) {
                System.err.println("Could not parse " + file);
                System.exit(1);
                return;
            }

            if (style == null) {
                style = XmlFormatStyle.get(document);
            }
            boolean endWithNewline = xml.endsWith("\n");
            int firstNewLine = xml.indexOf('\n');
            String lineSeparator = firstNewLine > 0 && xml.charAt(firstNewLine - 1) == '\r' ?
                    "\r\n" : "\n";
            String formatted = XmlPrettyPrinter.prettyPrint(document, prefs, style,
                    lineSeparator, endWithNewline);
            if (stdout) {
                System.out.println(formatted);
            } else {
                Files.write(formatted, file, Charsets.UTF_8);
            }
        } catch (IOException e) {
            System.err.println("Could not read " + file);
            System.exit(1);
        }
    }
}