Debian Codegraph @

build-system/manifest-merger/src/main/java/com/android/manifmerger/XmlLoader.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.PlaceholderHandler.KeyBasedValueResolver;
20
21 import com.android.annotations.NonNull;
22 import com.android.ide.common.blame.SourceFile;
23 import com.android.utils.PositionXmlParser;
24 import com.google.common.base.Optional;
25
26 import org.w3c.dom.Document;
27 import org.xml.sax.SAXException;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.InputStream;
32
33 import javax.xml.parsers.ParserConfigurationException;
34
35 /**
36 * Responsible for loading XML files.
37 */
38 public final class XmlLoader {
39
40 private XmlLoader() {}
41
42 /**
43 * Loads an xml file without doing xml validation and return a {@link XmlDocument}
44 *
45 * @param displayName the xml file display name.
46 * @param xmlFile the xml file.
47 * @return the initialized {@link com.android.manifmerger.XmlDocument}
48 */
49 @NonNull
50 public static XmlDocument load(
51 @NonNull KeyResolver<String> selectors,
52 @NonNull KeyBasedValueResolver<ManifestSystemProperty> systemPropertyResolver,
53 @NonNull String displayName,
54 @NonNull File xmlFile,
55 @NonNull InputStream inputStream,
56 @NonNull XmlDocument.Type type,
57 @NonNull Optional<String> mainManifestPackageName)
58 throws IOException, SAXException, ParserConfigurationException {
59 Document domDocument = PositionXmlParser.parse(inputStream);
60 return new XmlDocument(
61 new SourceFile(xmlFile, displayName),
62 selectors,
63 systemPropertyResolver,
64 domDocument.getDocumentElement(),
65 type,
66 mainManifestPackageName);
67 }
68
69 /**
70 * Loads a xml document from its {@link String} representation without doing xml validation and
71 * return a {@link com.android.manifmerger.XmlDocument}
72 * @param sourceFile the source location to use for logging and record collection.
73 * @param xml the persisted xml.
74 * @return the initialized {@link com.android.manifmerger.XmlDocument}
75 * @throws IOException this should never be thrown.
76 * @throws SAXException if the xml is incorrect
77 * @throws ParserConfigurationException if the xml engine cannot be configured.
78 */
79 @NonNull
80 public static XmlDocument load(
81 @NonNull KeyResolver<String> selectors,
82 @NonNull KeyBasedValueResolver<ManifestSystemProperty> systemPropertyResolver,
83 @NonNull SourceFile sourceFile,
84 @NonNull String xml,
85 @NonNull XmlDocument.Type type,
86 @NonNull Optional<String> mainManifestPackageName)
87 throws IOException, SAXException, ParserConfigurationException {
88 Document domDocument = PositionXmlParser.parse(xml);
89 return new XmlDocument(
90 sourceFile,
91 selectors,
92 systemPropertyResolver,
93 domDocument.getDocumentElement(),
94 type,
95 mainManifestPackageName);
96 }
97 }