aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--anttasks/src/com/android/ant/AndroidXPathFactory.java89
-rw-r--r--anttasks/src/com/android/ant/XPathTask.java80
2 files changed, 169 insertions, 0 deletions
diff --git a/anttasks/src/com/android/ant/AndroidXPathFactory.java b/anttasks/src/com/android/ant/AndroidXPathFactory.java
new file mode 100644
index 0000000..45ccf42
--- /dev/null
+++ b/anttasks/src/com/android/ant/AndroidXPathFactory.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.ant;
+
+import com.android.sdklib.SdkConstants;
+
+import java.util.Iterator;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathFactory;
+
+/**
+ * XPath factory with automatic support for the android namespace.
+ */
+class AndroidXPathFactory {
+ public final static String DEFAULT_NS_PREFIX = "android"; //$NON-NLS-1$
+
+ private final static XPathFactory sFactory = XPathFactory.newInstance();
+
+ /** Namespace context for Android resource XML files. */
+ private static class AndroidNamespaceContext implements NamespaceContext {
+ private String mAndroidPrefix;
+
+ /**
+ * Construct the context with the prefix associated with the android namespace.
+ * @param androidPrefix the Prefix
+ */
+ public AndroidNamespaceContext(String androidPrefix) {
+ mAndroidPrefix = androidPrefix;
+ }
+
+ public String getNamespaceURI(String prefix) {
+ if (prefix != null) {
+ if (prefix.equals(mAndroidPrefix)) {
+ return SdkConstants.NS_RESOURCES;
+ }
+ }
+
+ return XMLConstants.NULL_NS_URI;
+ }
+
+ public String getPrefix(String namespaceURI) {
+ // This isn't necessary for our use.
+ assert false;
+ return null;
+ }
+
+ public Iterator<?> getPrefixes(String namespaceURI) {
+ // This isn't necessary for our use.
+ assert false;
+ return null;
+ }
+ }
+
+ /**
+ * Creates a new XPath object, specifying which prefix in the query is used for the
+ * android namespace.
+ * @param androidPrefix The namespace prefix.
+ */
+ public static XPath newXPath(String androidPrefix) {
+ XPath xpath = sFactory.newXPath();
+ xpath.setNamespaceContext(new AndroidNamespaceContext(androidPrefix));
+ return xpath;
+ }
+
+ /**
+ * Creates a new XPath object using the default prefix for the android namespace.
+ * @see #DEFAULT_NS_PREFIX
+ */
+ public static XPath newXPath() {
+ return newXPath(DEFAULT_NS_PREFIX);
+ }
+}
diff --git a/anttasks/src/com/android/ant/XPathTask.java b/anttasks/src/com/android/ant/XPathTask.java
new file mode 100644
index 0000000..73cf86a
--- /dev/null
+++ b/anttasks/src/com/android/ant/XPathTask.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.ant;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Task;
+import org.apache.tools.ant.types.Path;
+import org.xml.sax.InputSource;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathExpressionException;
+
+/**
+ * Android specific XPath task.
+ * The goal is to get the result of an XPath expression on Android XML files. The android namespace
+ * (http://schemas.android.com/apk/res/android) must be associated to the "android" prefix.
+ */
+public class XPathTask extends Task {
+
+ private Path mManifestFile;
+ private String mProperty;
+ private String mExpression;
+
+ public void setInput(Path manifestFile) {
+ mManifestFile = manifestFile;
+ }
+
+ public void setOutput(String property) {
+ mProperty = property;
+ }
+
+ public void setExpression(String expression) {
+ mExpression = expression;
+ }
+
+ @Override
+ public void execute() throws BuildException {
+ try {
+ if (mManifestFile == null || mManifestFile.list().length == 0) {
+ throw new BuildException("input attribute is missing!");
+ }
+
+ if (mProperty == null) {
+ throw new BuildException("output attribute is missing!");
+ }
+
+ if (mExpression == null) {
+ throw new BuildException("expression attribute is missing!");
+ }
+
+ XPath xpath = AndroidXPathFactory.newXPath();
+
+ String file = mManifestFile.list()[0];
+ String result = xpath.evaluate(mExpression, new InputSource(new FileInputStream(file)));
+
+ getProject().setProperty(mProperty, result);
+ } catch (XPathExpressionException e) {
+ throw new BuildException(e);
+ } catch (FileNotFoundException e) {
+ throw new BuildException(e);
+ }
+ }
+}