Debian Codegraph @

build-system/manifest-merger/src/main/java/com/android/manifmerger/XmlNode.java

raw
1 /*
2 * Copyright (C) 2014 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.manifmerger;
18
19 import com.android.SdkConstants;
20 import com.android.annotations.NonNull;
21 import com.android.annotations.Nullable;
22 import com.android.annotations.concurrency.Immutable;
23 import com.android.ide.common.blame.SourceFile;
24 import com.android.ide.common.blame.SourceFilePosition;
25 import com.android.ide.common.blame.SourcePosition;
26 import com.google.common.base.Function;
27 import com.google.common.base.Objects;
28 import com.google.common.base.Preconditions;
29
30 import org.w3c.dom.Element;
31 import org.w3c.dom.Node;
32
33 /**
34 * Common behavior of any xml declaration.
35 */
36 public abstract class XmlNode {
37
38 protected static final Function<Node, String> NODE_TO_NAME =
39 new Function<Node, String>() {
40 @Override
41 public String apply(Node input) {
42 return input.getNodeName();
43 }
44 };
45
46 @Nullable
47 private NodeKey mOriginalId = null;
48
49 /**
50 * Returns a constant Nodekey that can be used throughout the lifecycle of the xml element.
51 * The {@link #getId} can return different values over time as the key of the element can be
52 * for instance, changed through placeholder replacement.
53 */
54 @NonNull
55 public synchronized NodeKey getOriginalId() {
56 if (mOriginalId == null) {
57 mOriginalId = getId();
58 }
59 return mOriginalId;
60 }
61
62 /**
63 * Returns an unique id within the manifest file for the element.
64 */
65 @NonNull
66 public abstract NodeKey getId();
67
68 /**
69 * Returns the element's position
70 */
71 @NonNull
72 public abstract SourcePosition getPosition();
73
74 /**
75 * Returns the element's document xml source file location.
76 */
77 @NonNull
78 public abstract SourceFile getSourceFile();
79
80 /**
81 * Returns the element's document xml source file location.
82 */
83 @NonNull
84 public SourceFilePosition getSourceFilePosition() {
85 return new SourceFilePosition(getSourceFile(), getPosition());
86 }
87
88 /**
89 * Returns the element's xml
90 */
91 @NonNull
92 public abstract Node getXml();
93
94 /**
95 * Returns the name of this xml element or attribute.
96 */
97 @NonNull
98 public abstract NodeName getName();
99
100 /**
101 * Abstraction to an xml name to isolate whether the name has a namespace or not.
102 */
103 public interface NodeName {
104
105 /**
106 * Returns true if this attribute name has a namespace declaration and that namespapce is
107 * the same as provided, false otherwise.
108 */
109 boolean isInNamespace(@NonNull String namespaceURI);
110
111 /**
112 * Adds a new attribute of this name to a xml element with a value.
113 * @param to the xml element to add the attribute to.
114 * @param withValue the new attribute's value.
115 */
116 void addToNode(@NonNull Element to, String withValue);
117
118 /**
119 * The local name.
120 */
121 String getLocalName();
122 }
123
124 /**
125 * Factory method to create an instance of {@link com.android.manifmerger.XmlNode.NodeName}
126 * for an existing xml node.
127 * @param node the xml definition.
128 * @return an instance of {@link com.android.manifmerger.XmlNode.NodeName} providing
129 * namespace handling.
130 */
131 @NonNull
132 public static NodeName unwrapName(@NonNull Node node) {
133 return node.getNamespaceURI() == null
134 ? new Name(node.getNodeName())
135 : new NamespaceAwareName(node);
136 }
137
138 @NonNull
139 public static NodeName fromXmlName(@NonNull String name) {
140 if (name.contains(":")) {
141 String prefix = name.substring(0, name.indexOf(':'));
142 return new NamespaceAwareName(
143 SdkConstants.XMLNS.equals(prefix) ? SdkConstants.XMLNS_URI : SdkConstants.ANDROID_URI,
144 prefix, name.substring(name.indexOf(':') + 1));
145 }
146 return new Name(name);
147 }
148
149 @NonNull
150 public static NodeName fromNSName(
151 @NonNull String namespaceUri, @NonNull String prefix, @NonNull String localName) {
152 return new NamespaceAwareName(namespaceUri, prefix, localName);
153 }
154
155 /**
156 * Returns the position of this attribute in the original xml file. This may return an invalid
157 * location as this xml fragment does not exist in any xml file but is the temporary result
158 * of the merging process.
159 * @return a human readable position.
160 */
161 @NonNull
162 public String printPosition() {
163 return getSourceFilePosition().print(true /*shortFormat*/);
164 }
165
166 /**
167 * Implementation of {@link com.android.manifmerger.XmlNode.NodeName} for an
168 * node's declaration not using a namespace.
169 */
170 public static final class Name implements NodeName {
171 private final String mName;
172
173 private Name(@NonNull String name) {
174 this.mName = Preconditions.checkNotNull(name);
175 }
176
177 @Override
178 public boolean isInNamespace(@NonNull String namespaceURI) {
179 return false;
180 }
181
182 @Override
183 public void addToNode(@NonNull Element to, String withValue) {
184 to.setAttribute(mName, withValue);
185 }
186
187 @Override
188 public boolean equals(@Nullable Object o) {
189 return (o != null && o instanceof Name && ((Name) o).mName.equals(this.mName));
190 }
191
192 @Override
193 public int hashCode() {
194 return Objects.hashCode(mName);
195 }
196
197 @Override
198 public String toString() {
199 return mName;
200 }
201
202 @Override
203 public String getLocalName() {
204 return mName;
205 }
206 }
207
208 /**
209 * Implementation of the {@link com.android.manifmerger.XmlNode.NodeName} for a namespace aware attribute.
210 */
211 public static final class NamespaceAwareName implements NodeName {
212
213 @NonNull
214 private final String mNamespaceURI;
215
216 // ignore for comparison and hashcoding since different documents can use different
217 // prefixes for the same namespace URI.
218 @NonNull
219 private final String mPrefix;
220 @NonNull
221 private final String mLocalName;
222
223 private NamespaceAwareName(@NonNull Node node) {
224 this.mNamespaceURI = Preconditions.checkNotNull(node.getNamespaceURI());
225 this.mPrefix = Preconditions.checkNotNull(node.getPrefix());
226 this.mLocalName = Preconditions.checkNotNull(node.getLocalName());
227 }
228
229 private NamespaceAwareName(@NonNull String namespaceURI,
230 @NonNull String prefix,
231 @NonNull String localName) {
232 mNamespaceURI = Preconditions.checkNotNull(namespaceURI);
233 mPrefix = Preconditions.checkNotNull(prefix);
234 mLocalName = Preconditions.checkNotNull(localName);
235 }
236
237 @Override
238 public boolean isInNamespace(@NonNull String namespaceURI) {
239 return mNamespaceURI.equals(namespaceURI);
240 }
241
242 @Override
243 public void addToNode(@NonNull Element to, String withValue) {
244 // TODO: consider standardizing everything on "android:"
245 to.setAttributeNS(mNamespaceURI, mPrefix + ":" + mLocalName, withValue);
246 }
247
248 @Override
249 public int hashCode() {
250 return Objects.hashCode(mNamespaceURI, mLocalName);
251 }
252
253 @Override
254 public boolean equals(@Nullable Object o) {
255 return (o != null && o instanceof NamespaceAwareName
256 && ((NamespaceAwareName) o).mLocalName.equals(this.mLocalName)
257 && ((NamespaceAwareName) o).mNamespaceURI.equals(this.mNamespaceURI));
258 }
259
260 @NonNull
261 @Override
262 public String toString() {
263 return mPrefix + ":" + mLocalName;
264 }
265
266 @NonNull
267 @Override
268 public String getLocalName() {
269 return mLocalName;
270 }
271 }
272
273 /**
274 * A xml element or attribute key.
275 */
276 @Immutable
277 public static class NodeKey {
278
279 @NonNull
280 private final String mKey;
281
282 NodeKey(@NonNull String key) {
283 mKey = key;
284 }
285
286 public static NodeKey fromXml(@NonNull Element element) {
287 return new OrphanXmlElement(element).getId();
288 }
289
290 @NonNull
291 @Override
292 public String toString() {
293 return mKey;
294 }
295
296 @Override
297 public boolean equals(@Nullable Object o) {
298 return (o != null && o instanceof NodeKey && ((NodeKey) o).mKey.equals(this.mKey));
299 }
300
301 @Override
302 public int hashCode() {
303 return Objects.hashCode(mKey);
304 }
305 }
306 }