Loading packages/SettingsLib/src/com/android/settingslib/license/LicenseHtmlGeneratorFromXml.java +30 −18 Original line number Diff line number Diff line Loading @@ -35,8 +35,10 @@ 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.zip.GZIPInputStream; /** Loading Loading @@ -84,7 +86,7 @@ class LicenseHtmlGeneratorFromXml { * "9645f39e9db895a4aa6e02cb57294595". Here "9645f39e9db895a4aa6e02cb57294595" is a MD5 sum * of the content of packages/services/Telephony/MODULE_LICENSE_APACHE2. */ private final Map<String, String> mFileNameToContentIdMap = new HashMap(); private final Map<String, Set<String>> mFileNameToContentIdMap = new HashMap(); /* * A map from a content id (MD5 sum of file content) to a license file content. Loading Loading @@ -186,10 +188,10 @@ class LicenseHtmlGeneratorFromXml { * </licenses> */ @VisibleForTesting static void parse(InputStreamReader in, Map<String, String> outFileNameToContentIdMap, static void parse(InputStreamReader in, Map<String, Set<String>> outFileNameToContentIdMap, Map<String, String> outContentIdToFileContentMap) throws XmlPullParserException, IOException { Map<String, String> fileNameToContentIdMap = new HashMap<String, String>(); Map<String, Set<String>> fileNameToContentIdMap = new HashMap<String, Set<String>>(); Map<String, String> contentIdToFileContentMap = new HashMap<String, String>(); XmlPullParser parser = Xml.newPullParser(); Loading @@ -206,7 +208,10 @@ class LicenseHtmlGeneratorFromXml { if (!TextUtils.isEmpty(contentId)) { String fileName = readText(parser).trim(); if (!TextUtils.isEmpty(fileName)) { fileNameToContentIdMap.put(fileName, contentId); Set<String> contentIds = fileNameToContentIdMap.computeIfAbsent( fileName, k -> new HashSet<>()); contentIds.add(contentId); } } } else if (TAG_FILE_CONTENT.equals(parser.getName())) { Loading @@ -224,7 +229,13 @@ class LicenseHtmlGeneratorFromXml { state = parser.next(); } outFileNameToContentIdMap.putAll(fileNameToContentIdMap); for (Map.Entry<String, Set<String>> entry : fileNameToContentIdMap.entrySet()) { outFileNameToContentIdMap.merge( entry.getKey(), entry.getValue(), (s1, s2) -> { s1.addAll(s2); return s1; }); } outContentIdToFileContentMap.putAll(contentIdToFileContentMap); } Loading @@ -240,7 +251,7 @@ class LicenseHtmlGeneratorFromXml { } @VisibleForTesting static void generateHtml(Map<String, String> fileNameToContentIdMap, static void generateHtml(Map<String, Set<String>> fileNameToContentIdMap, Map<String, String> contentIdToFileContentMap, PrintWriter writer, String noticeHeader) { List<String> fileNameList = new ArrayList(); Loading @@ -259,7 +270,7 @@ class LicenseHtmlGeneratorFromXml { // Prints all the file list with a link to its license file content. for (String fileName : fileNameList) { String contentId = fileNameToContentIdMap.get(fileName); for (String contentId : fileNameToContentIdMap.get(fileName)) { // Assigns an id to a newly referred license file content. if (!contentIdToOrderMap.containsKey(contentId)) { contentIdToOrderMap.put(contentId, count); Loading @@ -273,6 +284,7 @@ class LicenseHtmlGeneratorFromXml { contentIdAndFileNamesList.get(id).mFileNameList.add(fileName); writer.format("<li><a href=\"#id%d\">%s</a></li>\n", id, fileName); } } writer.println(HTML_MIDDLE_STRING); Loading packages/SettingsLib/tests/robotests/src/com/android/settingslib/license/LicenseHtmlGeneratorFromXmlTest.java +25 −10 Original line number Diff line number Diff line Loading @@ -28,8 +28,11 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringWriter; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; @RunWith(RobolectricTestRunner.class) public class LicenseHtmlGeneratorFromXmlTest { Loading Loading @@ -68,6 +71,7 @@ public class LicenseHtmlGeneratorFromXmlTest { private static final String HTML_BODY_STRING = "<li><a href=\"#id0\">/file0</a></li>\n" + "<li><a href=\"#id1\">/file0</a></li>\n" + "<li><a href=\"#id0\">/file1</a></li>\n" + "</ul>\n" + "</div><!-- table of contents -->\n" Loading @@ -82,6 +86,15 @@ public class LicenseHtmlGeneratorFromXmlTest { + "license content #0\n" + "</pre><!-- license-text -->\n" + "</td></tr><!-- same-license -->\n" + "<tr id=\"id1\"><td class=\"same-license\">\n" + "<div class=\"label\">Notices for file(s):</div>\n" + "<div class=\"file-list\">\n" + "/file0 <br/>\n" + "</div><!-- file-list -->\n" + "<pre class=\"license-text\">\n" + "license content #1\n" + "</pre><!-- license-text -->\n" + "</td></tr><!-- same-license -->\n" + "</table></body></html>\n"; private static final String EXPECTED_HTML_STRING = HTML_HEAD_STRING + HTML_BODY_STRING; Loading @@ -91,22 +104,22 @@ public class LicenseHtmlGeneratorFromXmlTest { @Test public void testParseValidXmlStream() throws XmlPullParserException, IOException { Map<String, String> fileNameToContentIdMap = new HashMap<>(); Map<String, Set<String>> fileNameToContentIdMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>(); LicenseHtmlGeneratorFromXml.parse( new InputStreamReader(new ByteArrayInputStream(VALILD_XML_STRING.getBytes())), fileNameToContentIdMap, contentIdToFileContentMap); assertThat(fileNameToContentIdMap.size()).isEqualTo(2); assertThat(fileNameToContentIdMap.get("/file0")).isEqualTo("0"); assertThat(fileNameToContentIdMap.get("/file1")).isEqualTo("0"); assertThat(fileNameToContentIdMap.get("/file0")).containsExactly("0"); assertThat(fileNameToContentIdMap.get("/file1")).containsExactly("0"); assertThat(contentIdToFileContentMap.size()).isEqualTo(1); assertThat(contentIdToFileContentMap.get("0")).isEqualTo("license content #0"); } @Test(expected = XmlPullParserException.class) public void testParseInvalidXmlStream() throws XmlPullParserException, IOException { Map<String, String> fileNameToContentIdMap = new HashMap<>(); Map<String, Set<String>> fileNameToContentIdMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>(); LicenseHtmlGeneratorFromXml.parse( Loading @@ -116,12 +129,13 @@ public class LicenseHtmlGeneratorFromXmlTest { @Test public void testGenerateHtml() { Map<String, String> fileNameToContentIdMap = new HashMap<>(); Map<String, Set<String>> fileNameToContentIdMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>(); fileNameToContentIdMap.put("/file0", "0"); fileNameToContentIdMap.put("/file1", "0"); fileNameToContentIdMap.put("/file0", new HashSet<String>(Arrays.asList("0", "1"))); fileNameToContentIdMap.put("/file1", new HashSet<String>(Arrays.asList("0"))); contentIdToFileContentMap.put("0", "license content #0"); contentIdToFileContentMap.put("1", "license content #1"); StringWriter output = new StringWriter(); LicenseHtmlGeneratorFromXml.generateHtml( Loading @@ -131,12 +145,13 @@ public class LicenseHtmlGeneratorFromXmlTest { @Test public void testGenerateHtmlWithCustomHeading() { Map<String, String> fileNameToContentIdMap = new HashMap<>(); Map<String, Set<String>> fileNameToContentIdMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>(); fileNameToContentIdMap.put("/file0", "0"); fileNameToContentIdMap.put("/file1", "0"); fileNameToContentIdMap.put("/file0", new HashSet<String>(Arrays.asList("0", "1"))); fileNameToContentIdMap.put("/file1", new HashSet<String>(Arrays.asList("0"))); contentIdToFileContentMap.put("0", "license content #0"); contentIdToFileContentMap.put("1", "license content #1"); StringWriter output = new StringWriter(); LicenseHtmlGeneratorFromXml.generateHtml( Loading Loading
packages/SettingsLib/src/com/android/settingslib/license/LicenseHtmlGeneratorFromXml.java +30 −18 Original line number Diff line number Diff line Loading @@ -35,8 +35,10 @@ 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.zip.GZIPInputStream; /** Loading Loading @@ -84,7 +86,7 @@ class LicenseHtmlGeneratorFromXml { * "9645f39e9db895a4aa6e02cb57294595". Here "9645f39e9db895a4aa6e02cb57294595" is a MD5 sum * of the content of packages/services/Telephony/MODULE_LICENSE_APACHE2. */ private final Map<String, String> mFileNameToContentIdMap = new HashMap(); private final Map<String, Set<String>> mFileNameToContentIdMap = new HashMap(); /* * A map from a content id (MD5 sum of file content) to a license file content. Loading Loading @@ -186,10 +188,10 @@ class LicenseHtmlGeneratorFromXml { * </licenses> */ @VisibleForTesting static void parse(InputStreamReader in, Map<String, String> outFileNameToContentIdMap, static void parse(InputStreamReader in, Map<String, Set<String>> outFileNameToContentIdMap, Map<String, String> outContentIdToFileContentMap) throws XmlPullParserException, IOException { Map<String, String> fileNameToContentIdMap = new HashMap<String, String>(); Map<String, Set<String>> fileNameToContentIdMap = new HashMap<String, Set<String>>(); Map<String, String> contentIdToFileContentMap = new HashMap<String, String>(); XmlPullParser parser = Xml.newPullParser(); Loading @@ -206,7 +208,10 @@ class LicenseHtmlGeneratorFromXml { if (!TextUtils.isEmpty(contentId)) { String fileName = readText(parser).trim(); if (!TextUtils.isEmpty(fileName)) { fileNameToContentIdMap.put(fileName, contentId); Set<String> contentIds = fileNameToContentIdMap.computeIfAbsent( fileName, k -> new HashSet<>()); contentIds.add(contentId); } } } else if (TAG_FILE_CONTENT.equals(parser.getName())) { Loading @@ -224,7 +229,13 @@ class LicenseHtmlGeneratorFromXml { state = parser.next(); } outFileNameToContentIdMap.putAll(fileNameToContentIdMap); for (Map.Entry<String, Set<String>> entry : fileNameToContentIdMap.entrySet()) { outFileNameToContentIdMap.merge( entry.getKey(), entry.getValue(), (s1, s2) -> { s1.addAll(s2); return s1; }); } outContentIdToFileContentMap.putAll(contentIdToFileContentMap); } Loading @@ -240,7 +251,7 @@ class LicenseHtmlGeneratorFromXml { } @VisibleForTesting static void generateHtml(Map<String, String> fileNameToContentIdMap, static void generateHtml(Map<String, Set<String>> fileNameToContentIdMap, Map<String, String> contentIdToFileContentMap, PrintWriter writer, String noticeHeader) { List<String> fileNameList = new ArrayList(); Loading @@ -259,7 +270,7 @@ class LicenseHtmlGeneratorFromXml { // Prints all the file list with a link to its license file content. for (String fileName : fileNameList) { String contentId = fileNameToContentIdMap.get(fileName); for (String contentId : fileNameToContentIdMap.get(fileName)) { // Assigns an id to a newly referred license file content. if (!contentIdToOrderMap.containsKey(contentId)) { contentIdToOrderMap.put(contentId, count); Loading @@ -273,6 +284,7 @@ class LicenseHtmlGeneratorFromXml { contentIdAndFileNamesList.get(id).mFileNameList.add(fileName); writer.format("<li><a href=\"#id%d\">%s</a></li>\n", id, fileName); } } writer.println(HTML_MIDDLE_STRING); Loading
packages/SettingsLib/tests/robotests/src/com/android/settingslib/license/LicenseHtmlGeneratorFromXmlTest.java +25 −10 Original line number Diff line number Diff line Loading @@ -28,8 +28,11 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringWriter; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; @RunWith(RobolectricTestRunner.class) public class LicenseHtmlGeneratorFromXmlTest { Loading Loading @@ -68,6 +71,7 @@ public class LicenseHtmlGeneratorFromXmlTest { private static final String HTML_BODY_STRING = "<li><a href=\"#id0\">/file0</a></li>\n" + "<li><a href=\"#id1\">/file0</a></li>\n" + "<li><a href=\"#id0\">/file1</a></li>\n" + "</ul>\n" + "</div><!-- table of contents -->\n" Loading @@ -82,6 +86,15 @@ public class LicenseHtmlGeneratorFromXmlTest { + "license content #0\n" + "</pre><!-- license-text -->\n" + "</td></tr><!-- same-license -->\n" + "<tr id=\"id1\"><td class=\"same-license\">\n" + "<div class=\"label\">Notices for file(s):</div>\n" + "<div class=\"file-list\">\n" + "/file0 <br/>\n" + "</div><!-- file-list -->\n" + "<pre class=\"license-text\">\n" + "license content #1\n" + "</pre><!-- license-text -->\n" + "</td></tr><!-- same-license -->\n" + "</table></body></html>\n"; private static final String EXPECTED_HTML_STRING = HTML_HEAD_STRING + HTML_BODY_STRING; Loading @@ -91,22 +104,22 @@ public class LicenseHtmlGeneratorFromXmlTest { @Test public void testParseValidXmlStream() throws XmlPullParserException, IOException { Map<String, String> fileNameToContentIdMap = new HashMap<>(); Map<String, Set<String>> fileNameToContentIdMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>(); LicenseHtmlGeneratorFromXml.parse( new InputStreamReader(new ByteArrayInputStream(VALILD_XML_STRING.getBytes())), fileNameToContentIdMap, contentIdToFileContentMap); assertThat(fileNameToContentIdMap.size()).isEqualTo(2); assertThat(fileNameToContentIdMap.get("/file0")).isEqualTo("0"); assertThat(fileNameToContentIdMap.get("/file1")).isEqualTo("0"); assertThat(fileNameToContentIdMap.get("/file0")).containsExactly("0"); assertThat(fileNameToContentIdMap.get("/file1")).containsExactly("0"); assertThat(contentIdToFileContentMap.size()).isEqualTo(1); assertThat(contentIdToFileContentMap.get("0")).isEqualTo("license content #0"); } @Test(expected = XmlPullParserException.class) public void testParseInvalidXmlStream() throws XmlPullParserException, IOException { Map<String, String> fileNameToContentIdMap = new HashMap<>(); Map<String, Set<String>> fileNameToContentIdMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>(); LicenseHtmlGeneratorFromXml.parse( Loading @@ -116,12 +129,13 @@ public class LicenseHtmlGeneratorFromXmlTest { @Test public void testGenerateHtml() { Map<String, String> fileNameToContentIdMap = new HashMap<>(); Map<String, Set<String>> fileNameToContentIdMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>(); fileNameToContentIdMap.put("/file0", "0"); fileNameToContentIdMap.put("/file1", "0"); fileNameToContentIdMap.put("/file0", new HashSet<String>(Arrays.asList("0", "1"))); fileNameToContentIdMap.put("/file1", new HashSet<String>(Arrays.asList("0"))); contentIdToFileContentMap.put("0", "license content #0"); contentIdToFileContentMap.put("1", "license content #1"); StringWriter output = new StringWriter(); LicenseHtmlGeneratorFromXml.generateHtml( Loading @@ -131,12 +145,13 @@ public class LicenseHtmlGeneratorFromXmlTest { @Test public void testGenerateHtmlWithCustomHeading() { Map<String, String> fileNameToContentIdMap = new HashMap<>(); Map<String, Set<String>> fileNameToContentIdMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>(); fileNameToContentIdMap.put("/file0", "0"); fileNameToContentIdMap.put("/file1", "0"); fileNameToContentIdMap.put("/file0", new HashSet<String>(Arrays.asList("0", "1"))); fileNameToContentIdMap.put("/file1", new HashSet<String>(Arrays.asList("0"))); contentIdToFileContentMap.put("0", "license content #0"); contentIdToFileContentMap.put("1", "license content #1"); StringWriter output = new StringWriter(); LicenseHtmlGeneratorFromXml.generateHtml( Loading