private static String getAndroidPrefix(@NonNull Element xml) { String toolsPrefix = XmlUtils.lookupNamespacePrefix( xml, SdkConstants.ANDROID_URI, SdkConstants.ANDROID_NS_NAME, false); if (!toolsPrefix.equals(SdkConstants.ANDROID_NS_NAME) && xml.getOwnerDocument() .getDocumentElement().getAttribute("xmlns:" + toolsPrefix) == null) { // this is weird, the document is using "android" prefix but it's not bound // to our namespace. Add the proper xmlns declaration. xml.setAttribute("xmlns:" + toolsPrefix, SdkConstants.ANDROID_URI); } return toolsPrefix; }
public OrphanXmlElement(@NonNull Element xml) { mXml = Preconditions.checkNotNull(xml); NodeTypes nodeType; String elementName = mXml.getNodeName(); // this is bit more complicated than it should be. Look first if there is a namespace // prefix in the name, most elements don't. If they do, however, strip it off if it is the // android prefix, but if it's custom namespace prefix, classify the node as CUSTOM. int indexOfColon = elementName.indexOf(':'); if (indexOfColon != -1) { String androidPrefix = XmlUtils.lookupNamespacePrefix(xml, SdkConstants.ANDROID_URI); if (androidPrefix.equals(elementName.substring(0, indexOfColon))) { nodeType = NodeTypes.fromXmlSimpleName(elementName.substring(indexOfColon + 1)); } else { nodeType = NodeTypes.CUSTOM; } } else { nodeType = NodeTypes.fromXmlSimpleName(elementName); } mType = nodeType; }
/** * Removes the android namespace from an element recursively. * * @param element the element */ private void clearNodeNamespaces(Element element) { String androidPrefix = XmlUtils.lookupNamespacePrefix(element, SdkConstants.ANDROID_URI); String name = element.getNodeName(); int colonIdx = name.indexOf(':'); if (colonIdx != -1) { String prefix = name.substring(0, colonIdx); if (prefix.equals(androidPrefix)) { String newName = name.substring(colonIdx + 1); getXml().renameNode(element, null, newName); } } NodeList childrenNodeList = element.getChildNodes(); for (int i = 0; i < childrenNodeList.getLength(); i++) { Node n = childrenNodeList.item(i); if (n instanceof Element) { clearNodeNamespaces((Element) n); } } } }
/** * 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. 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, * e.g. {@link SdkConstants#ANDROID_URI} * @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) { String defaultPrefix = ANDROID_URI.equals(nsUri) ? ANDROID_NS_NAME : APP_PREFIX; 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 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 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); }
if (attributeName.indexOf(XmlUtils.NS_SEPARATOR) == -1) { String toolsPrefix = XmlUtils .lookupNamespacePrefix(getXml(), SdkConstants.TOOLS_URI, SdkConstants.ANDROID_NS_NAME, false);