Debian Codegraph @

brut.apktool/apktool-lib/src/main/java/brut/androlib/res/data/value/ResScalarValue.java

raw
1 /*
2 * Copyright (C) 2010 Ryszard Wiśniewski <[email protected]>
3 * Copyright (C) 2010 Connor Tumbleson <[email protected]>
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package brut.androlib.res.data.value;
18
19 import brut.androlib.AndrolibException;
20 import brut.androlib.res.data.ResResource;
21 import brut.androlib.res.xml.ResValuesXmlSerializable;
22 import brut.androlib.res.xml.ResXmlEncodable;
23 import brut.androlib.res.xml.ResXmlEncoders;
24 import org.xmlpull.v1.XmlSerializer;
25
26 import java.io.IOException;
27
28 public abstract class ResScalarValue extends ResIntBasedValue implements
29 ResXmlEncodable, ResValuesXmlSerializable {
30 protected final String mType;
31 protected final String mRawValue;
32
33 protected ResScalarValue(String type, int rawIntValue, String rawValue) {
34 super(rawIntValue);
35 mType = type;
36 mRawValue = rawValue;
37 }
38
39 @Override
40 public String encodeAsResXmlAttr() throws AndrolibException {
41 if (mRawValue != null) {
42 return mRawValue;
43 }
44 return encodeAsResXml();
45 }
46
47 public String encodeAsResXmlItemValue() throws AndrolibException {
48 return encodeAsResXmlValue();
49 }
50
51 @Override
52 public String encodeAsResXmlValue() throws AndrolibException {
53 if (mRawValue != null) {
54 return mRawValue;
55 }
56 return encodeAsResXml();
57 }
58
59 public String encodeAsResXmlNonEscapedItemValue() throws AndrolibException {
60 return encodeAsResXmlValue().replace("&amp;", "&").replace("&lt;","<");
61 }
62
63 public boolean hasMultipleNonPositionalSubstitutions() {
64 return ResXmlEncoders.hasMultipleNonPositionalSubstitutions(mRawValue);
65 }
66
67 @Override
68 public void serializeToResValuesXml(XmlSerializer serializer,
69 ResResource res) throws IOException, AndrolibException {
70 String type = res.getResSpec().getType().getName();
71 boolean item = !"reference".equals(mType) && !type.equals(mType);
72
73 String body = encodeAsResXmlValue();
74
75 // check for resource reference
76 if (!type.equalsIgnoreCase("color")) {
77 if (body.contains("@")) {
78 if (!res.getFilePath().contains("string")) {
79 item = true;
80 }
81 }
82 }
83
84 // Dummy attributes should be <item> with type attribute
85 if (res.getResSpec().isDummyResSpec()) {
86 item = true;
87 }
88
89 // Android does not allow values (false) for ids.xml anymore
90 // https://issuetracker.google.com/issues/80475496
91 // But it decodes as a ResBoolean, which makes no sense. So force it to empty
92 if (type.equalsIgnoreCase("id") && !body.isEmpty()) {
93 body = "";
94 }
95
96 // check for using attrib as node or item
97 String tagName = item ? "item" : type;
98
99 serializer.startTag(null, tagName);
100 if (item) {
101 serializer.attribute(null, "type", type);
102 }
103 serializer.attribute(null, "name", res.getResSpec().getName());
104
105 serializeExtraXmlAttrs(serializer, res);
106
107 if (!body.isEmpty()) {
108 serializer.ignorableWhitespace(body);
109 }
110
111 serializer.endTag(null, tagName);
112 }
113
114 public String getType() {
115 return mType;
116 }
117
118 protected void serializeExtraXmlAttrs(XmlSerializer serializer,
119 ResResource res) throws IOException {
120 }
121
122 protected abstract String encodeAsResXml() throws AndrolibException;
123 }