OSDN Git Service

Import testrunner changes from puppetmaster to keep them in sync.
[android-x86/development.git] / tools / sdkmanager / libs / sdklib / src / com / android / sdklib / xml / AndroidXPathFactory.java
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.sdklib.xml;
18
19 import com.android.sdklib.SdkConstants;
20
21 import java.util.Iterator;
22
23 import javax.xml.XMLConstants;
24 import javax.xml.namespace.NamespaceContext;
25 import javax.xml.xpath.XPath;
26 import javax.xml.xpath.XPathFactory;
27
28 /**
29  * XPath factory with automatic support for the android name space.
30  */
31 public class AndroidXPathFactory {
32     /** Default prefix for android name space: 'android' */
33     public final static String DEFAULT_NS_PREFIX = "android"; //$NON-NLS-1$
34
35     private final static XPathFactory sFactory = XPathFactory.newInstance();
36
37     /** Name space context for Android resource XML files. */
38     private static class AndroidNamespaceContext implements NamespaceContext {
39         private final static AndroidNamespaceContext sThis = new AndroidNamespaceContext(
40                 DEFAULT_NS_PREFIX);
41
42         private String mAndroidPrefix;
43
44         /**
45          * Returns the default {@link AndroidNamespaceContext}.
46          */
47         private static AndroidNamespaceContext getDefault() {
48             return sThis;
49         }
50
51         /**
52          * Construct the context with the prefix associated with the android namespace.
53          * @param androidPrefix the Prefix
54          */
55         public AndroidNamespaceContext(String androidPrefix) {
56             mAndroidPrefix = androidPrefix;
57         }
58
59         public String getNamespaceURI(String prefix) {
60             if (prefix != null) {
61                 if (prefix.equals(mAndroidPrefix)) {
62                     return SdkConstants.NS_RESOURCES;
63                 }
64             }
65
66             return XMLConstants.NULL_NS_URI;
67         }
68
69         public String getPrefix(String namespaceURI) {
70             // This isn't necessary for our use.
71             assert false;
72             return null;
73         }
74
75         public Iterator<?> getPrefixes(String namespaceURI) {
76             // This isn't necessary for our use.
77             assert false;
78             return null;
79         }
80     }
81
82     /**
83      * Creates a new XPath object, specifying which prefix in the query is used for the
84      * android namespace.
85      * @param androidPrefix The namespace prefix.
86      */
87     public static XPath newXPath(String androidPrefix) {
88         XPath xpath = sFactory.newXPath();
89         xpath.setNamespaceContext(new AndroidNamespaceContext(androidPrefix));
90         return xpath;
91     }
92
93     /**
94      * Creates a new XPath object using the default prefix for the android namespace.
95      * @see #DEFAULT_NS_PREFIX
96      */
97     public static XPath newXPath() {
98         XPath xpath = sFactory.newXPath();
99         xpath.setNamespaceContext(AndroidNamespaceContext.getDefault());
100         return xpath;
101     }
102 }