Debian Codegraph @

build-system/manifest-merger/src/main/java/com/android/manifmerger/PlaceholderEncoder.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.annotations.NonNull;
20
21 import java.util.regex.Matcher;
22
23 /**
24 * encode all non resolved placeholders key names.
25 */
26 public class PlaceholderEncoder {
27
28 /**
29 * Visits a document's entire tree and check each attribute for a placeholder existence.
30 * If one is found, encode its name so tools like aapt will not object invalid characters and
31 * such.
32 * <p>
33 *
34 * @param xmlDocument the xml document to visit
35 */
36 public static void visit(@NonNull XmlDocument xmlDocument) {
37
38 visit(xmlDocument.getRootNode());
39 }
40
41 private static void visit(@NonNull XmlElement xmlElement) {
42
43 for (XmlAttribute xmlAttribute : xmlElement.getAttributes()) {
44 Matcher matcher = PlaceholderHandler.PATTERN.matcher(xmlAttribute.getValue());
45 if (matcher.matches()) {
46 String encodedValue = "dollar_openBracket_" + matcher.group(2) + "_closeBracket";
47 xmlAttribute.getXml().setValue(encodedValue);
48 }
49 }
50 for (XmlElement childElement : xmlElement.getMergeableElements()) {
51 visit(childElement);
52 }
53 }
54 }