aboutsummaryrefslogtreecommitdiffstats
path: root/common/src/com/android
diff options
context:
space:
mode:
authorTor Norbye <tnorbye@google.com>2012-12-04 09:50:58 -0800
committerTor Norbye <tnorbye@google.com>2012-12-04 14:01:25 -0800
commitba7270b2b13b437bd9b384a47672d6b7820e6d81 (patch)
tree73245034395dd9f6ce61550ce644fcd9d9cbb33c /common/src/com/android
parent474bd94478a8ced503e292b17c7e5962de414d99 (diff)
downloadsdk-ba7270b2b13b437bd9b384a47672d6b7820e6d81.zip
sdk-ba7270b2b13b437bd9b384a47672d6b7820e6d81.tar.gz
sdk-ba7270b2b13b437bd9b384a47672d6b7820e6d81.tar.bz2
Add namespace elements from code completion lazily
The XmlUtils.lookupNamespace() method would unconditionally insert a namespace declaration if called with a namespace that is not already declared in the document. This had the negative effect that simply bringing up the code completion dialog on a custom view would immediately insert the app namespace declaration, even if the user didn't select one of the app namespace attributes. Furthermore, this edit would sometimes result in the caret being placed in the wrong place after insertion. This changeset adds a new parameter to the lookupNamespace() method, "create", which lets the caller deliberately decide whether the new namespace element should be created. Second, code completion now only inserts the namespace declaration as part of the completion apply handler, meaning you only get the namespace if you pick one of the custom attributes. It also uses a document position to ensure that the insert and caret positions are preserved properly and take the namespace insertion into account. Change-Id: I21cf678df454b09460139fe35d33ca88b8e91757
Diffstat (limited to 'common/src/com/android')
-rw-r--r--common/src/com/android/utils/XmlUtils.java51
1 files changed, 38 insertions, 13 deletions
diff --git a/common/src/com/android/utils/XmlUtils.java b/common/src/com/android/utils/XmlUtils.java
index 94b7405..0969eb1 100644
--- a/common/src/com/android/utils/XmlUtils.java
+++ b/common/src/com/android/utils/XmlUtils.java
@@ -42,7 +42,9 @@ public class XmlUtils {
/**
* Returns the namespace prefix matching the requested namespace URI.
* If no such declaration is found, returns the default "android" prefix for
- * the Android URI, and "app" for other URI's.
+ * the Android URI, and "app" for other URI's. By default the app namespace
+ * will be created. If this is not desirable, call
+ * {@link #lookupNamespacePrefix(Node, String, boolean)} instead.
*
* @param node The current node. Must not be null.
* @param nsUri The namespace URI of which the prefix is to be found,
@@ -53,24 +55,47 @@ public class XmlUtils {
@NonNull
public static String lookupNamespacePrefix(@NonNull Node node, @NonNull String nsUri) {
String defaultPrefix = ANDROID_URI.equals(nsUri) ? ANDROID_NS_NAME : APP_PREFIX;
- return lookupNamespacePrefix(node, nsUri, defaultPrefix);
+ return lookupNamespacePrefix(node, nsUri, defaultPrefix, true /*create*/);
}
/**
- * Returns the namespace prefix matching the requested namespace URI.
- * If no such declaration is found, returns the default "android" prefix.
+ * Returns the namespace prefix matching the requested namespace URI. If no
+ * such declaration is found, returns the default "android" prefix for the
+ * Android URI, and "app" for other URI's.
*
* @param node The current node. Must not be null.
- * @param nsUri The namespace URI of which the prefix is to be found,
- * e.g. {@link SdkConstants#ANDROID_URI}
- * @param defaultPrefix The default prefix (root) to use if the namespace
- * is not found. If null, do not create a new namespace
- * if this URI is not defined for the document.
- * @return The first prefix declared or the provided prefix (possibly with
- * a number appended to avoid conflicts with existing prefixes.
+ * @param nsUri The namespace URI of which the prefix is to be found, e.g.
+ * {@link SdkConstants#ANDROID_URI}
+ * @param create whether the namespace declaration should be created, if
+ * necessary
+ * @return The first prefix declared or the default "android" prefix (or
+ * "app" for non-Android URIs)
+ */
+ @NonNull
+ public static String lookupNamespacePrefix(@NonNull Node node, @NonNull String nsUri,
+ boolean create) {
+ String defaultPrefix = ANDROID_URI.equals(nsUri) ? ANDROID_NS_NAME : APP_PREFIX;
+ return lookupNamespacePrefix(node, nsUri, defaultPrefix, create);
+ }
+
+ /**
+ * Returns the namespace prefix matching the requested namespace URI. If no
+ * such declaration is found, returns the default "android" prefix.
+ *
+ * @param node The current node. Must not be null.
+ * @param nsUri The namespace URI of which the prefix is to be found, e.g.
+ * {@link SdkConstants#ANDROID_URI}
+ * @param defaultPrefix The default prefix (root) to use if the namespace is
+ * not found. If null, do not create a new namespace if this URI
+ * is not defined for the document.
+ * @param create whether the namespace declaration should be created, if
+ * necessary
+ * @return The first prefix declared or the provided prefix (possibly with a
+ * number appended to avoid conflicts with existing prefixes.
*/
public static String lookupNamespacePrefix(
- @Nullable Node node, @Nullable String nsUri, @Nullable String defaultPrefix) {
+ @Nullable Node node, @Nullable String nsUri, @Nullable String defaultPrefix,
+ boolean create) {
// Note: Node.lookupPrefix is not implemented in wst/xml/core NodeImpl.java
// The following code emulates this simple call:
// String prefix = node.lookupPrefix(NS_RESOURCES);
@@ -140,7 +165,7 @@ public class XmlUtils {
while (node != null && node.getNodeType() != Node.ELEMENT_NODE) {
node = node.getNextSibling();
}
- if (node != null) {
+ if (node != null && create) {
// This doesn't work:
//Attr attr = doc.createAttributeNS(XMLNS_URI, prefix);
//attr.setPrefix(XMLNS);