/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settingslib.license;
import android.text.TextUtils;
import android.util.Log;
import android.util.Xml;
import androidx.annotation.VisibleForTesting;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.zip.GZIPInputStream;
/**
* The utility class that generate a license html file from xml files.
* All the HTML snippets and logic are copied from build/make/tools/generate-notice-files.py.
*
* TODO: Remove duplicate codes once backward support ends.
*/
class LicenseHtmlGeneratorFromXml {
private static final String TAG = "LicenseGeneratorFromXml";
private static final String TAG_ROOT = "licenses";
private static final String TAG_FILE_NAME = "file-name";
private static final String TAG_FILE_CONTENT = "file-content";
private static final String ATTR_CONTENT_ID = "contentId";
private static final String ATTR_LIBRARY_NAME = "lib";
private static final String HTML_HEAD_STRING =
"
\n"
+ "\n"
+ ""
+ "\n"
+ "";
private static final String LIBRARY_HEAD_STRING =
"
Libraries\n
";
private static final String LIBRARY_TAIL_STRING = "
\n
Files";
private static final String FILES_HEAD_STRING = "
";
private static final String FILES_TAIL_STRING = "
\n
";
private static final String CONTENT_HEAD_STRING =
"";
private static final String CONTENT_TAIL_STRING = "
";
private static final String IMAGES_HEAD_STRING =
"Images\n
";
private static final String IMAGES_TAIL_STRING = "
\n";
private static final String PATH_COUNTS_HEAD_STRING =
"\n Path prefix | Count |
\n";
private static final String PATH_COUNTS_TAIL_STRING = "
\n";
private static final String HTML_TAIL_STRING =
"";
private final List mXmlFiles;
/*
* A map from a file name to a library name (may be empty) to a content id (MD5 sum of file
* content) for its license.
* For example, "/system/priv-app/TeleService/TeleService.apk" maps to "service/Telephony" to
* "9645f39e9db895a4aa6e02cb57294595". Here "9645f39e9db895a4aa6e02cb57294595" is a MD5 sum
* of the content of packages/services/Telephony/MODULE_LICENSE_APACHE2.
*/
private final Map>> mFileNameToLibraryToContentIdMap =
new HashMap();
/*
* A map from a content id (MD5 sum of file content) to a license file content.
* For example, "9645f39e9db895a4aa6e02cb57294595" maps to the content string of
* packages/services/Telephony/MODULE_LICENSE_APACHE2. Here "9645f39e9db895a4aa6e02cb57294595"
* is a MD5 sum of the file content.
*/
private final Map mContentIdToFileContentMap = new HashMap();
static class ContentIdAndFileNames {
final String mContentId;
final Map> mLibraryToFileNameMap = new TreeMap();
ContentIdAndFileNames(String contentId) {
mContentId = contentId;
}
}
private LicenseHtmlGeneratorFromXml(List xmlFiles) {
mXmlFiles = xmlFiles;
}
public static boolean generateHtml(List xmlFiles, File outputFile,
String noticeHeader) {
LicenseHtmlGeneratorFromXml genertor = new LicenseHtmlGeneratorFromXml(xmlFiles);
return genertor.generateHtml(outputFile, noticeHeader);
}
private boolean generateHtml(File outputFile, String noticeHeader) {
for (File xmlFile : mXmlFiles) {
parse(xmlFile);
}
if (mFileNameToLibraryToContentIdMap.isEmpty() || mContentIdToFileContentMap.isEmpty()) {
return false;
}
PrintWriter writer = null;
try {
writer = new PrintWriter(outputFile);
generateHtml(mXmlFiles, mFileNameToLibraryToContentIdMap, mContentIdToFileContentMap,
writer, noticeHeader);
writer.flush();
writer.close();
return true;
} catch (IOException | SecurityException e) {
Log.e(TAG, "Failed to generate " + outputFile, e);
if (writer != null) {
writer.close();
}
return false;
}
}
private void parse(File xmlFile) {
if (xmlFile == null || !xmlFile.exists() || xmlFile.length() == 0) {
return;
}
InputStreamReader in = null;
try {
if (xmlFile.getName().endsWith(".gz")) {
in = new InputStreamReader(new GZIPInputStream(new FileInputStream(xmlFile)));
} else {
in = new FileReader(xmlFile);
}
parse(in, mFileNameToLibraryToContentIdMap, mContentIdToFileContentMap);
in.close();
} catch (XmlPullParserException | IOException e) {
Log.e(TAG, "Failed to parse " + xmlFile, e);
if (in != null) {
try {
in.close();
} catch (IOException ie) {
Log.w(TAG, "Failed to close " + xmlFile);
}
}
}
}
/*
* Parses an input stream and fills a map from a file name to a content id for its license
* and a map from a content id to a license file content.
*
* Following xml format is expected from the input stream.
*
*
* file1
* file2
* file2
* ...
* license1 file contents
* license2 file contents
* ...
*
*/
@VisibleForTesting
static void parse(InputStreamReader in,
Map>> outFileNameToLibraryToContentIdMap,
Map outContentIdToFileContentMap)
throws XmlPullParserException, IOException {
Map>> fileNameToLibraryToContentIdMap =
new HashMap>>();
Map contentIdToFileContentMap = new HashMap();
XmlPullParser parser = Xml.newPullParser();
parser.setInput(in);
parser.nextTag();
parser.require(XmlPullParser.START_TAG, "", TAG_ROOT);
int state = parser.getEventType();
while (state != XmlPullParser.END_DOCUMENT) {
if (state == XmlPullParser.START_TAG) {
if (TAG_FILE_NAME.equals(parser.getName())) {
String contentId = parser.getAttributeValue("", ATTR_CONTENT_ID);
String libraryName = parser.getAttributeValue("", ATTR_LIBRARY_NAME);
if (!TextUtils.isEmpty(contentId)) {
String fileName = readText(parser).trim();
if (!TextUtils.isEmpty(fileName)) {
Map> libs =
fileNameToLibraryToContentIdMap.computeIfAbsent(
fileName, k -> new HashMap<>());
Set contentIds = libs.computeIfAbsent(
libraryName, k -> new HashSet<>());
contentIds.add(contentId);
}
}
} else if (TAG_FILE_CONTENT.equals(parser.getName())) {
String contentId = parser.getAttributeValue("", ATTR_CONTENT_ID);
if (!TextUtils.isEmpty(contentId)
&& !outContentIdToFileContentMap.containsKey(contentId)
&& !contentIdToFileContentMap.containsKey(contentId)) {
String fileContent = readText(parser);
if (!TextUtils.isEmpty(fileContent)) {
contentIdToFileContentMap.put(contentId, fileContent);
}
}
}
}
state = parser.next();
}
for (Map.Entry>> mapEntry :
fileNameToLibraryToContentIdMap.entrySet()) {
outFileNameToLibraryToContentIdMap.merge(
mapEntry.getKey(), mapEntry.getValue(), (m1, m2) -> {
for (Map.Entry> entry : m2.entrySet()) {
m1.merge(entry.getKey(), entry.getValue(), (s1, s2) -> {
s1.addAll(s2);
return s1;
});
}
return m1;
});
}
outContentIdToFileContentMap.putAll(contentIdToFileContentMap);
}
private static String readText(XmlPullParser parser)
throws IOException, XmlPullParserException {
StringBuffer result = new StringBuffer();
int state = parser.next();
while (state == XmlPullParser.TEXT) {
result.append(parser.getText());
state = parser.next();
}
return result.toString();
}
private static String pathPrefix(String path) {
String prefix = path;
while (prefix.length() > 0 && prefix.substring(0, 1).equals("/")) {
prefix = prefix.substring(1);
}
int idx = prefix.indexOf("/");
if (idx > 0) {
prefix = prefix.substring(0, idx);
}
return prefix;
}
@VisibleForTesting
static void generateHtml(List xmlFiles,
Map>> fileNameToLibraryToContentIdMap,
Map contentIdToFileContentMap, PrintWriter writer,
String noticeHeader) throws IOException {
List fileNameList = new ArrayList();
fileNameList.addAll(fileNameToLibraryToContentIdMap.keySet());
Collections.sort(fileNameList);
SortedMap prefixToCount = new TreeMap();
for (String f : fileNameList) {
String prefix = pathPrefix(f);
prefixToCount.merge(prefix, 1, Integer::sum);
}
SortedMap> libraryToContentIdMap = new TreeMap();
for (Map> libraryToContentValue :
fileNameToLibraryToContentIdMap.values()) {
for (Map.Entry> entry : libraryToContentValue.entrySet()) {
if (TextUtils.isEmpty(entry.getKey())) {
continue;
}
libraryToContentIdMap.merge(
entry.getKey(), entry.getValue(), (s1, s2) -> {
s1.addAll(s2);
return s1;
});
}
}
writer.println(HTML_HEAD_STRING);
if (!TextUtils.isEmpty(noticeHeader)) {
writer.println(noticeHeader);
}
int count = 0;
Map contentIdToOrderMap = new HashMap();
List contentIdAndFileNamesList = new ArrayList();
if (!libraryToContentIdMap.isEmpty()) {
writer.println(LIBRARY_HEAD_STRING);
for (Map.Entry> entry: libraryToContentIdMap.entrySet()) {
String libraryName = entry.getKey();
for (String contentId : entry.getValue()) {
// Assigns an id to a newly referred license file content.
if (!contentIdToOrderMap.containsKey(contentId)) {
contentIdToOrderMap.put(contentId, count);
// An index in contentIdAndFileNamesList is the order of each element.
contentIdAndFileNamesList.add(new ContentIdAndFileNames(contentId));
count++;
}
int id = contentIdToOrderMap.get(contentId);
writer.format("%s\n", id, libraryName);
}
}
writer.println(LIBRARY_TAIL_STRING);
}
if (!fileNameList.isEmpty()) {
writer.println(FILES_HEAD_STRING);
// Prints all the file list with a link to its license file content.
for (String fileName : fileNameList) {
for (Map.Entry> libToContentId :
fileNameToLibraryToContentIdMap.get(fileName).entrySet()) {
String libraryName = libToContentId.getKey();
if (libraryName == null) {
libraryName = "";
}
for (String contentId : libToContentId.getValue()) {
// Assigns an id to a newly referred license file content.
if (!contentIdToOrderMap.containsKey(contentId)) {
contentIdToOrderMap.put(contentId, count);
// An index in contentIdAndFileNamesList is the order of each element.
contentIdAndFileNamesList.add(new ContentIdAndFileNames(contentId));
count++;
}
int id = contentIdToOrderMap.get(contentId);
ContentIdAndFileNames elem = contentIdAndFileNamesList.get(id);
List files = elem.mLibraryToFileNameMap.computeIfAbsent(
libraryName, k -> new ArrayList());
files.add(fileName);
if (TextUtils.isEmpty(libraryName)) {
writer.format("%s\n", id, fileName);
} else {
writer.format("%s - %s\n",
id, fileName, libraryName);
}
}
}
}
writer.println(FILES_TAIL_STRING);
}
if (!contentIdAndFileNamesList.isEmpty()) {
writer.println(CONTENT_HEAD_STRING);
// Prints all contents of the license files in order of id.
for (ContentIdAndFileNames contentIdAndFileNames : contentIdAndFileNamesList) {
// Assigns an id to a newly referred license file content (should never happen here)
if (!contentIdToOrderMap.containsKey(contentIdAndFileNames.mContentId)) {
contentIdToOrderMap.put(contentIdAndFileNames.mContentId, count);
count++;
}
int id = contentIdToOrderMap.get(contentIdAndFileNames.mContentId);
writer.format("\n", id);
for (Map.Entry> libraryFiles :
contentIdAndFileNames.mLibraryToFileNameMap.entrySet()) {
String libraryName = libraryFiles.getKey();
if (TextUtils.isEmpty(libraryName)) {
writer.println(" Notices for file(s): ");
} else {
writer.format("%s used by: \n",
libraryName);
}
writer.println("");
for (String fileName : libraryFiles.getValue()) {
writer.format("%s \n", fileName);
}
writer.println(" ");
}
writer.println("");
writer.println(contentIdToFileContentMap.get(
contentIdAndFileNames.mContentId));
writer.println(" ");
writer.println(" |
");
}
writer.println(CONTENT_TAIL_STRING);
}
if (!xmlFiles.isEmpty()) {
writer.println(IMAGES_HEAD_STRING);
for (File file : xmlFiles) {
writer.format(" %s\n", pathPrefix(file.getCanonicalPath()));
}
writer.println(IMAGES_TAIL_STRING);
}
if (!prefixToCount.isEmpty()) {
writer.println(PATH_COUNTS_HEAD_STRING);
for (Map.Entry entry : prefixToCount.entrySet()) {
writer.format(" %s | %d |
\n",
entry.getKey(), entry.getValue());
}
writer.println(PATH_COUNTS_TAIL_STRING);
}
writer.println(HTML_TAIL_STRING);
}
}