From a881b0b34678ad76c9f5eba62fac7a00a22ac606 Mon Sep 17 00:00:00 2001 From: Tor Norbye <tnorbye@google.com> Date: Sun, 20 May 2012 12:32:34 -0700 Subject: Move XML code to the common library The ManifestMerger library needs to look up the prefix to use for the Android namespace, and the Document.lookupPrefix method is not implemented by the Eclipse DOM implementation (which throws an exception). However, we have an implementation of this in the ADT plugin. This changeset creates a new XmlUtils class in the common/ library (which is accessible by both ADT and the manifest merger, and the anttasks where the manifest merger is used), and moves the namespace prefix lookup code in there. It also moves the XML escape methods into that class. It also adds a new method to the ManifestMerger for merging directly from documents (rather than files), and makes sure that all the merging code goes via the prefix utility method rather than calling the document.lookupPrefix method. Finally, it moves the various string constants associated with XML namespaces into the single XmlUtils class, since these were spread across several different classes before (and many of them are needed in the XmlUtils class). The vast majority of the diffs in this changeset are related to simple import statement changes to reflect the new locations of these constants. Change-Id: Ib8f3d0e5c89e47e61ea509a23925af7b6580abee --- .../tests/src/com/android/util/XmlUtilsTest.java | 84 ++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 common/tests/src/com/android/util/XmlUtilsTest.java (limited to 'common/tests/src/com/android') diff --git a/common/tests/src/com/android/util/XmlUtilsTest.java b/common/tests/src/com/android/util/XmlUtilsTest.java new file mode 100644 index 0000000..655829b --- /dev/null +++ b/common/tests/src/com/android/util/XmlUtilsTest.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2012 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.util; + +import org.w3c.dom.Attr; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import junit.framework.TestCase; + +@SuppressWarnings("javadoc") +public class XmlUtilsTest extends TestCase { + public void testlookupNamespacePrefix() throws Exception { + // Setup + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + factory.setValidating(false); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.newDocument(); + Element rootElement = document.createElement("root"); + Attr attr = document.createAttributeNS(XmlUtils.XMLNS_URI, + "xmlns:customPrefix"); + attr.setValue(XmlUtils.ANDROID_URI); + rootElement.getAttributes().setNamedItemNS(attr); + document.appendChild(rootElement); + Element root = document.getDocumentElement(); + root.appendChild(document.createTextNode(" ")); + Element foo = document.createElement("foo"); + root.appendChild(foo); + root.appendChild(document.createTextNode(" ")); + Element bar = document.createElement("bar"); + root.appendChild(bar); + Element baz = document.createElement("baz"); + root.appendChild(baz); + + String prefix = XmlUtils.lookupNamespacePrefix(baz, XmlUtils.ANDROID_URI); + assertEquals("customPrefix", prefix); + + prefix = XmlUtils.lookupNamespacePrefix(baz, + "http://schemas.android.com/tools", "tools"); + assertEquals("tools", prefix); + } + + public void testToXmlAttributeValue() throws Exception { + assertEquals("", XmlUtils.toXmlAttributeValue("")); + assertEquals("foo", XmlUtils.toXmlAttributeValue("foo")); + assertEquals("foo<bar", XmlUtils.toXmlAttributeValue("foo<bar")); + assertEquals("foo>bar", XmlUtils.toXmlAttributeValue("foo>bar")); + + assertEquals(""", XmlUtils.toXmlAttributeValue("\"")); + assertEquals("'", XmlUtils.toXmlAttributeValue("'")); + assertEquals("foo"b''ar", + XmlUtils.toXmlAttributeValue("foo\"b''ar")); + assertEquals("<"'>&", XmlUtils.toXmlAttributeValue("<\"'>&")); + } + + public void testAppendXmlAttributeValue() throws Exception { + StringBuilder sb = new StringBuilder(); + XmlUtils.appendXmlAttributeValue(sb, "<\"'>&"); + assertEquals("<"'>&", sb.toString()); + } + + public void testAppendXmlTextValue() throws Exception { + StringBuilder sb = new StringBuilder(); + XmlUtils.appendXmlTextValue(sb, "<\"'>&"); + assertEquals("<\"'>&", sb.toString()); + } +} -- cgit v1.1