2 * Copyright (C) 2009 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.sdklib.xml;
19 import com.android.sdklib.SdkConstants;
21 import java.util.Iterator;
23 import javax.xml.XMLConstants;
24 import javax.xml.namespace.NamespaceContext;
25 import javax.xml.xpath.XPath;
26 import javax.xml.xpath.XPathFactory;
29 * XPath factory with automatic support for the android name space.
31 public class AndroidXPathFactory {
32 /** Default prefix for android name space: 'android' */
33 public final static String DEFAULT_NS_PREFIX = "android"; //$NON-NLS-1$
35 private final static XPathFactory sFactory = XPathFactory.newInstance();
37 /** Name space context for Android resource XML files. */
38 private static class AndroidNamespaceContext implements NamespaceContext {
39 private final static AndroidNamespaceContext sThis = new AndroidNamespaceContext(
42 private String mAndroidPrefix;
45 * Returns the default {@link AndroidNamespaceContext}.
47 private static AndroidNamespaceContext getDefault() {
52 * Construct the context with the prefix associated with the android namespace.
53 * @param androidPrefix the Prefix
55 public AndroidNamespaceContext(String androidPrefix) {
56 mAndroidPrefix = androidPrefix;
59 public String getNamespaceURI(String prefix) {
61 if (prefix.equals(mAndroidPrefix)) {
62 return SdkConstants.NS_RESOURCES;
66 return XMLConstants.NULL_NS_URI;
69 public String getPrefix(String namespaceURI) {
70 // This isn't necessary for our use.
75 public Iterator<?> getPrefixes(String namespaceURI) {
76 // This isn't necessary for our use.
83 * Creates a new XPath object, specifying which prefix in the query is used for the
85 * @param androidPrefix The namespace prefix.
87 public static XPath newXPath(String androidPrefix) {
88 XPath xpath = sFactory.newXPath();
89 xpath.setNamespaceContext(new AndroidNamespaceContext(androidPrefix));
94 * Creates a new XPath object using the default prefix for the android namespace.
95 * @see #DEFAULT_NS_PREFIX
97 public static XPath newXPath() {
98 XPath xpath = sFactory.newXPath();
99 xpath.setNamespaceContext(AndroidNamespaceContext.getDefault());