aboutsummaryrefslogtreecommitdiffstats
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/resources/platform/AttrsXmlParser.java
diff options
context:
space:
mode:
authorTor Norbye <tnorbye@google.com>2012-03-21 18:04:18 -0700
committerTor Norbye <tnorbye@google.com>2012-03-22 14:47:16 -0700
commit61684adfd7345f1a0df24bcf9176e6f528295bbb (patch)
tree2b08ae518b0a0150ec610a85679501626b611c62 /eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/resources/platform/AttrsXmlParser.java
parent6b09013b6cb0fdcb48350a60bb3ed924ac9f562d (diff)
downloadsdk-61684adfd7345f1a0df24bcf9176e6f528295bbb.zip
sdk-61684adfd7345f1a0df24bcf9176e6f528295bbb.tar.gz
sdk-61684adfd7345f1a0df24bcf9176e6f528295bbb.tar.bz2
Change AttributeInfo.getFormats() to returning an EnumSet<Format>
This changeset changes the signature of AttributeInfo.getFormats() from returning a Format[] to returning an EnumSet<Format>. Furthermore, it defines a number of constants for the various common format sets such that they can be reused. Nearly all uses of the getFormats() method was really just trying to see "is format X among the formats", so using enumsets is more natural since it has a contains method. This also lets us replace some attribute init code which was building up a set from the array for this exact same purpose just reuse the format set directly. In the attribute parser, rather than computing the uppercase version of each format string ("dimension"=>"DIMENSION") and then doing a Format.valueOf("DIMENSION"), we now compute a map for all the format constants as lowercase to the corresponding format instance, and use that map when parsing the attrs.xml file. Note that there is a small semantic change from having an array of formats to using an enumset: The format specified an ordering. However, it does not look like any code depended on this, and it's also not clear that the ordering in attrs.xml is intentional. For example, it contains both "color|reference" and "reference|color", and in both cases the intent is that the attribute can specify an actual color or a reference to a color. Now with an enum set, the order used when traversing the formats will always be in their natural order, and for this purpose the REFERENCE type is moved to the end since it's the least specific. Change-Id: I1170cff48086f5cc13e4b70a35deea1f9979c883
Diffstat (limited to 'eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/resources/platform/AttrsXmlParser.java')
-rw-r--r--eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/resources/platform/AttrsXmlParser.java63
1 files changed, 46 insertions, 17 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/resources/platform/AttrsXmlParser.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/resources/platform/AttrsXmlParser.java
index abe56d8..52b0e15 100644
--- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/resources/platform/AttrsXmlParser.java
+++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/resources/platform/AttrsXmlParser.java
@@ -30,11 +30,11 @@ import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.EnumSet;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
-import java.util.TreeSet;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@@ -58,6 +58,9 @@ public final class AttrsXmlParser {
private final Map<String, DeclareStyleableInfo> mStyleMap =
new HashMap<String, DeclareStyleableInfo>();
+ /** Map from format name (lower case) to the uppercase version */
+ private Map<String, Format> mFormatNames = new HashMap<String, Format>(10);
+
/**
* Map of all (constant, value) pairs for attributes of format enum or flag.
* E.g. for attribute name=gravity, this tells us there's an enum/flag called "center"
@@ -117,6 +120,12 @@ public final class AttrsXmlParser {
mEnumFlagValues = new HashMap<String, Map<String,Integer>>(
inheritableAttributes.mEnumFlagValues);
}
+
+ // Pre-compute the set of format names such that we don't have to compute the uppercase
+ // version of the same format string names again and again
+ for (Format f : Format.values()) {
+ mFormatNames.put(f.name().toLowerCase(Locale.US), f);
+ }
}
/**
@@ -396,7 +405,7 @@ public final class AttrsXmlParser {
info = mAttributeMap.get(name);
// If the attribute is unknown yet, parse it.
// If the attribute is know but its format is unknown, parse it too.
- if (info == null || info.getFormats().length == 0) {
+ if (info == null || info.getFormats().size() == 0) {
info = parseAttributeTypes(attrNode, name);
if (info != null) {
mAttributeMap.put(name, info);
@@ -468,25 +477,28 @@ public final class AttrsXmlParser {
* When reusing a node, it is duplicated and its javadoc reassigned.
*/
private AttributeInfo parseAttributeTypes(Node attrNode, String name) {
- TreeSet<AttributeInfo.Format> formats = new TreeSet<AttributeInfo.Format>();
+ EnumSet<Format> formats = null;
String[] enumValues = null;
String[] flagValues = null;
Node attrFormat = attrNode.getAttributes().getNamedItem("format"); //$NON-NLS-1$
if (attrFormat != null) {
for (String f : attrFormat.getNodeValue().split("\\|")) { //$NON-NLS-1$
- try {
- Format format = AttributeInfo.Format.valueOf(f.toUpperCase(Locale.US));
- // enum and flags are handled differently right below
- if (format != null &&
- format != AttributeInfo.Format.ENUM &&
- format != AttributeInfo.Format.FLAG) {
- formats.add(format);
- }
- } catch (IllegalArgumentException e) {
- mLog.error(e,
+ Format format = mFormatNames.get(f);
+ if (format == null) {
+ mLog.printf(
"Unknown format name '%s' in <attr name=\"%s\">, file '%s'.", //$NON-NLS-1$
f, name, getOsAttrsXmlPath());
+ } else if (format != AttributeInfo.Format.ENUM &&
+ format != AttributeInfo.Format.FLAG) {
+ if (formats == null) {
+ formats = format.asSet();
+ } else {
+ if (formats.size() == 1) {
+ formats = EnumSet.copyOf(formats);
+ }
+ formats.add(format);
+ }
}
}
}
@@ -494,17 +506,34 @@ public final class AttrsXmlParser {
// does this <attr> have <enum> children?
enumValues = parseEnumFlagValues(attrNode, "enum", name); //$NON-NLS-1$
if (enumValues != null) {
- formats.add(AttributeInfo.Format.ENUM);
+ if (formats == null) {
+ formats = Format.ENUM_SET;
+ } else {
+ if (formats.size() == 1) {
+ formats = EnumSet.copyOf(formats);
+ }
+ formats.add(Format.ENUM);
+ }
}
// does this <attr> have <flag> children?
flagValues = parseEnumFlagValues(attrNode, "flag", name); //$NON-NLS-1$
if (flagValues != null) {
- formats.add(AttributeInfo.Format.FLAG);
+ if (formats == null) {
+ formats = Format.FLAG_SET;
+ } else {
+ if (formats.size() == 1) {
+ formats = EnumSet.copyOf(formats);
+ }
+ formats.add(Format.FLAG);
+ }
+ }
+
+ if (formats == null) {
+ formats = Format.NONE;
}
- AttributeInfo info = new AttributeInfo(name,
- formats.toArray(new AttributeInfo.Format[formats.size()]));
+ AttributeInfo info = new AttributeInfo(name, formats);
info.setEnumValues(enumValues);
info.setFlagValues(flagValues);
return info;