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.ArrayList; 22 import java.util.Iterator; 23 import java.util.List; 24 25 import javax.xml.XMLConstants; 26 import javax.xml.namespace.NamespaceContext; 27 import javax.xml.xpath.XPath; 28 import javax.xml.xpath.XPathFactory; 29 30 /** 31 * XPath factory with automatic support for the android name space. 32 */ 33 public class AndroidXPathFactory { 34 /** Default prefix for android name space: 'android' */ 35 public final static String DEFAULT_NS_PREFIX = "android"; //$NON-NLS-1$ 36 37 private final static XPathFactory sFactory = XPathFactory.newInstance(); 38 39 /** Name space context for Android resource XML files. */ 40 private static class AndroidNamespaceContext implements NamespaceContext { 41 private final static AndroidNamespaceContext sThis = new AndroidNamespaceContext( 42 DEFAULT_NS_PREFIX); 43 44 private final String mAndroidPrefix; 45 private final List<String> mAndroidPrefixes = new ArrayList<String>(); 46 47 /** 48 * Returns the default {@link AndroidNamespaceContext}. 49 */ 50 private static AndroidNamespaceContext getDefault() { 51 return sThis; 52 } 53 54 /** 55 * Construct the context with the prefix associated with the android namespace. 56 * @param androidPrefix the Prefix 57 */ 58 public AndroidNamespaceContext(String androidPrefix) { 59 mAndroidPrefix = androidPrefix; 60 mAndroidPrefixes.add(mAndroidPrefix); 61 } 62 63 public String getNamespaceURI(String prefix) { 64 if (prefix != null) { 65 if (prefix.equals(mAndroidPrefix)) { 66 return SdkConstants.NS_RESOURCES; 67 } 68 } 69 70 return XMLConstants.NULL_NS_URI; 71 } 72 73 public String getPrefix(String namespaceURI) { 74 if (SdkConstants.NS_RESOURCES.equals(namespaceURI)) { 75 return mAndroidPrefix; 76 } 77 78 return null; 79 } 80 81 public Iterator<?> getPrefixes(String namespaceURI) { 82 if (SdkConstants.NS_RESOURCES.equals(namespaceURI)) { 83 return mAndroidPrefixes.iterator(); 84 } 85 86 return null; 87 } 88 } 89 90 /** 91 * Creates a new XPath object, specifying which prefix in the query is used for the 92 * android namespace. 93 * @param androidPrefix The namespace prefix. 94 */ 95 public static XPath newXPath(String androidPrefix) { 96 XPath xpath = sFactory.newXPath(); 97 xpath.setNamespaceContext(new AndroidNamespaceContext(androidPrefix)); 98 return xpath; 99 } 100 101 /** 102 * Creates a new XPath object using the default prefix for the android namespace. 103 * @see #DEFAULT_NS_PREFIX 104 */ 105 public static XPath newXPath() { 106 XPath xpath = sFactory.newXPath(); 107 xpath.setNamespaceContext(AndroidNamespaceContext.getDefault()); 108 return xpath; 109 } 110 } 111