From a881b0b34678ad76c9f5eba62fac7a00a22ac606 Mon Sep 17 00:00:00 2001 From: Tor Norbye 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 --- .../com/android/manifmerger/ManifestMerger.java | 33 ++++++++++++++++++++-- .../src/com/android/manifmerger/XmlUtils.java | 4 +-- 2 files changed, 32 insertions(+), 5 deletions(-) (limited to 'manifmerger') diff --git a/manifmerger/src/com/android/manifmerger/ManifestMerger.java b/manifmerger/src/com/android/manifmerger/ManifestMerger.java index 6eac978..8dfd398 100755 --- a/manifmerger/src/com/android/manifmerger/ManifestMerger.java +++ b/manifmerger/src/com/android/manifmerger/ManifestMerger.java @@ -186,6 +186,35 @@ public class ManifestMerger { return success; } + /** + * Performs the merge operation in-place in the given DOM. + *

+ * This does NOT stop on errors, in an attempt to accumulate as much + * info as possible to return to the user. + * + * @param mainDoc The document to merge into. Will be modified in-place. + * @param libraryDocs The library manifest documents to merge in. Must not be null. + * @return True on success, false if any error occurred (printed to the {@link ISdkLog}). + */ + public boolean process(@NonNull Document mainDoc, @NonNull Document... libraryDocs) { + + boolean success = true; + mMainDoc = mainDoc; + + String prefix = XmlUtils.lookupNsPrefix(mainDoc, SdkConstants.NS_RESOURCES); + mXPath = AndroidXPathFactory.newXPath(prefix); + + for (Document libDoc : libraryDocs) { + if (!mergeLibDoc(libDoc)) { + success = false; + } + } + + mXPath = null; + mMainDoc = null; + return success; + } + // -------- /** @@ -884,8 +913,8 @@ public class ManifestMerger { */ private Node insertAtEndOf(Element dest, Node start, Node end) { // Check whether we'll need to adjust URI prefixes - String destPrefix = mMainDoc.lookupPrefix(NS_URI); - String srcPrefix = start.getOwnerDocument().lookupPrefix(NS_URI); + String destPrefix = XmlUtils.lookupNsPrefix(mMainDoc, NS_URI); + String srcPrefix = XmlUtils.lookupNsPrefix(start.getOwnerDocument(), NS_URI); boolean needPrefixChange = destPrefix != null && !destPrefix.equals(srcPrefix); // First let's figure out the insertion point. diff --git a/manifmerger/src/com/android/manifmerger/XmlUtils.java b/manifmerger/src/com/android/manifmerger/XmlUtils.java index d18eebb..ed5b8a8 100755 --- a/manifmerger/src/com/android/manifmerger/XmlUtils.java +++ b/manifmerger/src/com/android/manifmerger/XmlUtils.java @@ -215,9 +215,7 @@ class XmlUtils { * @return The namespace prefix if found or null. */ static String lookupNsPrefix(Document doc, String nsUri) { - // Note: if this is not available, there's an alternate implementation at - // com.android.ide.eclipse.adt.internal.editors.uimodel.UiElementNode.lookupNamespacePrefix(Node, String) - return doc.lookupPrefix(nsUri); + return com.android.util.XmlUtils.lookupNamespacePrefix(doc, nsUri); } /** -- cgit v1.1