Debian Codegraph @

build-system/manifest-merger/src/main/java/com/android/manifmerger/OrphanXmlElement.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 static com.android.manifmerger.ManifestModel.NodeTypes;
20
21 import com.android.SdkConstants;
22 import com.android.annotations.NonNull;
23 import com.android.annotations.Nullable;
24 import com.android.ide.common.blame.SourceFile;
25 import com.android.ide.common.blame.SourcePosition;
26 import com.android.utils.XmlUtils;
27 import com.google.common.base.Preconditions;
28 import com.google.common.base.Strings;
29
30 import org.w3c.dom.Element;
31
32 /**
33 * An xml element that does not belong to a {@link com.android.manifmerger.XmlDocument}
34 */
35 public class OrphanXmlElement extends XmlNode {
36
37 @NonNull
38 private final Element mXml;
39
40 @NonNull
41 private final NodeTypes mType;
42
43 public OrphanXmlElement(@NonNull Element xml) {
44
45 mXml = Preconditions.checkNotNull(xml);
46 NodeTypes nodeType;
47 String elementName = mXml.getNodeName();
48 // this is bit more complicated than it should be. Look first if there is a namespace
49 // prefix in the name, most elements don't. If they do, however, strip it off if it is the
50 // android prefix, but if it's custom namespace prefix, classify the node as CUSTOM.
51 int indexOfColon = elementName.indexOf(':');
52 if (indexOfColon != -1) {
53 String androidPrefix = XmlUtils.lookupNamespacePrefix(xml, SdkConstants.ANDROID_URI);
54 if (androidPrefix.equals(elementName.substring(0, indexOfColon))) {
55 nodeType = NodeTypes.fromXmlSimpleName(elementName.substring(indexOfColon + 1));
56 } else {
57 nodeType = NodeTypes.CUSTOM;
58 }
59 } else {
60 nodeType = NodeTypes.fromXmlSimpleName(elementName);
61 }
62 mType = nodeType;
63 }
64
65 /**
66 * Returns true if this xml element's {@link NodeTypes} is
67 * the passed one.
68 */
69 public boolean isA(NodeTypes type) {
70 return this.mType == type;
71 }
72
73 @NonNull
74 @Override
75 public Element getXml() {
76 return mXml;
77 }
78
79
80 @NonNull
81 @Override
82 public NodeKey getId() {
83 return new NodeKey(Strings.isNullOrEmpty(getKey())
84 ? getName().toString()
85 : getName().toString() + "#" + getKey());
86 }
87
88 @NonNull
89 @Override
90 public NodeName getName() {
91 return XmlNode.unwrapName(mXml);
92 }
93
94 /**
95 * Returns this xml element {@link NodeTypes}
96 */
97 @NonNull
98 public NodeTypes getType() {
99 return mType;
100 }
101
102 /**
103 * Returns the unique key for this xml element within the xml file or null if there can be only
104 * one element of this type.
105 */
106 @Nullable
107 public String getKey() {
108 return mType.getNodeKeyResolver().getKey(mXml);
109 }
110
111 @NonNull
112 @Override
113 public SourcePosition getPosition() {
114 return SourcePosition.UNKNOWN;
115 }
116
117 @Override
118 @NonNull
119 public SourceFile getSourceFile() {
120 return SourceFile.UNKNOWN;
121 }
122 }
123