Loading packages/SettingsLib/src/com/android/settingslib/license/LicenseHtmlGeneratorFromXml.java +114 −67 Original line number Diff line number Diff line Loading @@ -27,7 +27,6 @@ import org.xmlpull.v1.XmlPullParserException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; Loading Loading @@ -76,14 +75,22 @@ class LicenseHtmlGeneratorFromXml { private static final String LIBRARY_TAIL_STRING = "</ul>\n<strong>Files</strong>"; private static final String FILES_HEAD_STRING = "<ul class=\"files\">"; private static final String FILES_TAIL_STRING = "</ul>\n</div><!-- table of contents -->"; private static final String HTML_MIDDLE_STRING = "</ul>\n" + "</div><!-- table of contents -->\n" + "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">"; private static final String CONTENT_HEAD_STRING = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">"; private static final String CONTENT_TAIL_STRING = "</table>"; private static final String HTML_REAR_STRING = "</table></body></html>"; private static final String IMAGES_HEAD_STRING = "<div class=\"images-list\"><strong>Images</strong>\n<ul class=\"images\">"; private static final String IMAGES_TAIL_STRING = "</ul></div>\n"; private static final String PATH_COUNTS_HEAD_STRING = "<div class=\"path-counts\"><table>\n <tr><th>Path prefix</th><th>Count</th></tr>\n"; private static final String PATH_COUNTS_TAIL_STRING = "</table></div>\n"; private static final String HTML_TAIL_STRING = "</body></html>"; private final List<File> mXmlFiles; Loading Loading @@ -137,13 +144,13 @@ class LicenseHtmlGeneratorFromXml { try { writer = new PrintWriter(outputFile); generateHtml(mFileNameToLibraryToContentIdMap, mContentIdToFileContentMap, writer, noticeHeader); generateHtml(mXmlFiles, mFileNameToLibraryToContentIdMap, mContentIdToFileContentMap, writer, noticeHeader); writer.flush(); writer.close(); return true; } catch (FileNotFoundException | SecurityException e) { } catch (IOException | SecurityException e) { Log.e(TAG, "Failed to generate " + outputFile, e); if (writer != null) { Loading Loading @@ -271,14 +278,33 @@ class LicenseHtmlGeneratorFromXml { 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(Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap, static void generateHtml(List<File> xmlFiles, Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap, Map<String, String> contentIdToFileContentMap, PrintWriter writer, String noticeHeader) { String noticeHeader) throws IOException { List<String> fileNameList = new ArrayList(); fileNameList.addAll(fileNameToLibraryToContentIdMap.keySet()); Collections.sort(fileNameList); SortedMap<String, Integer> prefixToCount = new TreeMap(); for (String f : fileNameList) { String prefix = pathPrefix(f); prefixToCount.merge(prefix, 1, Integer::sum); } SortedMap<String, Set<String>> libraryToContentIdMap = new TreeMap(); for (Map<String, Set<String>> libraryToContentValue : fileNameToLibraryToContentIdMap.values()) { Loading Loading @@ -324,8 +350,8 @@ class LicenseHtmlGeneratorFromXml { 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<String, Set<String>> libToContentId : Loading Loading @@ -358,9 +384,11 @@ class LicenseHtmlGeneratorFromXml { } } } writer.println(FILES_TAIL_STRING); } writer.println(HTML_MIDDLE_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) Loading Loading @@ -391,7 +419,26 @@ class LicenseHtmlGeneratorFromXml { writer.println("</pre><!-- license-text -->"); writer.println("</td></tr><!-- same-license -->"); } writer.println(CONTENT_TAIL_STRING); } if (!xmlFiles.isEmpty()) { writer.println(IMAGES_HEAD_STRING); for (File file : xmlFiles) { writer.format(" <li>%s</li>\n", pathPrefix(file.getCanonicalPath())); } writer.println(IMAGES_TAIL_STRING); } if (!prefixToCount.isEmpty()) { writer.println(PATH_COUNTS_HEAD_STRING); for (Map.Entry<String, Integer> entry : prefixToCount.entrySet()) { writer.format(" <tr><td>%s</td><td>%d</td></tr>\n", entry.getKey(), entry.getValue()); } writer.println(PATH_COUNTS_TAIL_STRING); } writer.println(HTML_REAR_STRING); writer.println(HTML_TAIL_STRING); } } packages/SettingsLib/tests/robotests/src/com/android/settingslib/license/LicenseHtmlGeneratorFromXmlTest.java +15 −8 Original line number Diff line number Diff line Loading @@ -24,13 +24,16 @@ import org.robolectric.RobolectricTestRunner; import org.xmlpull.v1.XmlPullParserException; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; Loading Loading @@ -202,7 +205,8 @@ public class LicenseHtmlGeneratorFromXmlTest { } @Test public void testGenerateHtml() { public void testGenerateHtml() throws Exception { List<File> xmlFiles = new ArrayList<>(); Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>(); Map<String, Set<String>> toBoth = new HashMap<>(); Loading @@ -218,13 +222,14 @@ public class LicenseHtmlGeneratorFromXmlTest { StringWriter output = new StringWriter(); LicenseHtmlGeneratorFromXml.generateHtml( fileNameToLibraryToContentIdMap, contentIdToFileContentMap, xmlFiles, fileNameToLibraryToContentIdMap, contentIdToFileContentMap, new PrintWriter(output), ""); assertThat(output.toString()).isEqualTo(EXPECTED_OLD_HTML_STRING); } @Test public void testGenerateNewHtml() { public void testGenerateNewHtml() throws Exception { List<File> xmlFiles = new ArrayList<>(); Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>(); Map<String, Set<String>> toBoth = new HashMap<>(); Loading @@ -244,13 +249,14 @@ public class LicenseHtmlGeneratorFromXmlTest { StringWriter output = new StringWriter(); LicenseHtmlGeneratorFromXml.generateHtml( fileNameToLibraryToContentIdMap, contentIdToFileContentMap, xmlFiles, fileNameToLibraryToContentIdMap, contentIdToFileContentMap, new PrintWriter(output), ""); assertThat(output.toString()).isEqualTo(EXPECTED_NEW_HTML_STRING); } @Test public void testGenerateHtmlWithCustomHeading() { public void testGenerateHtmlWithCustomHeading() throws Exception { List<File> xmlFiles = new ArrayList<>(); Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>(); Map<String, Set<String>> toBoth = new HashMap<>(); Loading @@ -266,13 +272,14 @@ public class LicenseHtmlGeneratorFromXmlTest { StringWriter output = new StringWriter(); LicenseHtmlGeneratorFromXml.generateHtml( fileNameToLibraryToContentIdMap, contentIdToFileContentMap, xmlFiles, fileNameToLibraryToContentIdMap, contentIdToFileContentMap, new PrintWriter(output), HTML_CUSTOM_HEADING); assertThat(output.toString()).isEqualTo(EXPECTED_OLD_HTML_STRING_WITH_CUSTOM_HEADING); } @Test public void testGenerateNewHtmlWithCustomHeading() { public void testGenerateNewHtmlWithCustomHeading() throws Exception { List<File> xmlFiles = new ArrayList<>(); Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>(); Map<String, Set<String>> toBoth = new HashMap<>(); Loading @@ -292,7 +299,7 @@ public class LicenseHtmlGeneratorFromXmlTest { StringWriter output = new StringWriter(); LicenseHtmlGeneratorFromXml.generateHtml( fileNameToLibraryToContentIdMap, contentIdToFileContentMap, xmlFiles, fileNameToLibraryToContentIdMap, contentIdToFileContentMap, new PrintWriter(output), HTML_CUSTOM_HEADING); assertThat(output.toString()).isEqualTo(EXPECTED_NEW_HTML_STRING_WITH_CUSTOM_HEADING); } Loading Loading
packages/SettingsLib/src/com/android/settingslib/license/LicenseHtmlGeneratorFromXml.java +114 −67 Original line number Diff line number Diff line Loading @@ -27,7 +27,6 @@ import org.xmlpull.v1.XmlPullParserException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; Loading Loading @@ -76,14 +75,22 @@ class LicenseHtmlGeneratorFromXml { private static final String LIBRARY_TAIL_STRING = "</ul>\n<strong>Files</strong>"; private static final String FILES_HEAD_STRING = "<ul class=\"files\">"; private static final String FILES_TAIL_STRING = "</ul>\n</div><!-- table of contents -->"; private static final String HTML_MIDDLE_STRING = "</ul>\n" + "</div><!-- table of contents -->\n" + "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">"; private static final String CONTENT_HEAD_STRING = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">"; private static final String CONTENT_TAIL_STRING = "</table>"; private static final String HTML_REAR_STRING = "</table></body></html>"; private static final String IMAGES_HEAD_STRING = "<div class=\"images-list\"><strong>Images</strong>\n<ul class=\"images\">"; private static final String IMAGES_TAIL_STRING = "</ul></div>\n"; private static final String PATH_COUNTS_HEAD_STRING = "<div class=\"path-counts\"><table>\n <tr><th>Path prefix</th><th>Count</th></tr>\n"; private static final String PATH_COUNTS_TAIL_STRING = "</table></div>\n"; private static final String HTML_TAIL_STRING = "</body></html>"; private final List<File> mXmlFiles; Loading Loading @@ -137,13 +144,13 @@ class LicenseHtmlGeneratorFromXml { try { writer = new PrintWriter(outputFile); generateHtml(mFileNameToLibraryToContentIdMap, mContentIdToFileContentMap, writer, noticeHeader); generateHtml(mXmlFiles, mFileNameToLibraryToContentIdMap, mContentIdToFileContentMap, writer, noticeHeader); writer.flush(); writer.close(); return true; } catch (FileNotFoundException | SecurityException e) { } catch (IOException | SecurityException e) { Log.e(TAG, "Failed to generate " + outputFile, e); if (writer != null) { Loading Loading @@ -271,14 +278,33 @@ class LicenseHtmlGeneratorFromXml { 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(Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap, static void generateHtml(List<File> xmlFiles, Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap, Map<String, String> contentIdToFileContentMap, PrintWriter writer, String noticeHeader) { String noticeHeader) throws IOException { List<String> fileNameList = new ArrayList(); fileNameList.addAll(fileNameToLibraryToContentIdMap.keySet()); Collections.sort(fileNameList); SortedMap<String, Integer> prefixToCount = new TreeMap(); for (String f : fileNameList) { String prefix = pathPrefix(f); prefixToCount.merge(prefix, 1, Integer::sum); } SortedMap<String, Set<String>> libraryToContentIdMap = new TreeMap(); for (Map<String, Set<String>> libraryToContentValue : fileNameToLibraryToContentIdMap.values()) { Loading Loading @@ -324,8 +350,8 @@ class LicenseHtmlGeneratorFromXml { 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<String, Set<String>> libToContentId : Loading Loading @@ -358,9 +384,11 @@ class LicenseHtmlGeneratorFromXml { } } } writer.println(FILES_TAIL_STRING); } writer.println(HTML_MIDDLE_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) Loading Loading @@ -391,7 +419,26 @@ class LicenseHtmlGeneratorFromXml { writer.println("</pre><!-- license-text -->"); writer.println("</td></tr><!-- same-license -->"); } writer.println(CONTENT_TAIL_STRING); } if (!xmlFiles.isEmpty()) { writer.println(IMAGES_HEAD_STRING); for (File file : xmlFiles) { writer.format(" <li>%s</li>\n", pathPrefix(file.getCanonicalPath())); } writer.println(IMAGES_TAIL_STRING); } if (!prefixToCount.isEmpty()) { writer.println(PATH_COUNTS_HEAD_STRING); for (Map.Entry<String, Integer> entry : prefixToCount.entrySet()) { writer.format(" <tr><td>%s</td><td>%d</td></tr>\n", entry.getKey(), entry.getValue()); } writer.println(PATH_COUNTS_TAIL_STRING); } writer.println(HTML_REAR_STRING); writer.println(HTML_TAIL_STRING); } }
packages/SettingsLib/tests/robotests/src/com/android/settingslib/license/LicenseHtmlGeneratorFromXmlTest.java +15 −8 Original line number Diff line number Diff line Loading @@ -24,13 +24,16 @@ import org.robolectric.RobolectricTestRunner; import org.xmlpull.v1.XmlPullParserException; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; Loading Loading @@ -202,7 +205,8 @@ public class LicenseHtmlGeneratorFromXmlTest { } @Test public void testGenerateHtml() { public void testGenerateHtml() throws Exception { List<File> xmlFiles = new ArrayList<>(); Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>(); Map<String, Set<String>> toBoth = new HashMap<>(); Loading @@ -218,13 +222,14 @@ public class LicenseHtmlGeneratorFromXmlTest { StringWriter output = new StringWriter(); LicenseHtmlGeneratorFromXml.generateHtml( fileNameToLibraryToContentIdMap, contentIdToFileContentMap, xmlFiles, fileNameToLibraryToContentIdMap, contentIdToFileContentMap, new PrintWriter(output), ""); assertThat(output.toString()).isEqualTo(EXPECTED_OLD_HTML_STRING); } @Test public void testGenerateNewHtml() { public void testGenerateNewHtml() throws Exception { List<File> xmlFiles = new ArrayList<>(); Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>(); Map<String, Set<String>> toBoth = new HashMap<>(); Loading @@ -244,13 +249,14 @@ public class LicenseHtmlGeneratorFromXmlTest { StringWriter output = new StringWriter(); LicenseHtmlGeneratorFromXml.generateHtml( fileNameToLibraryToContentIdMap, contentIdToFileContentMap, xmlFiles, fileNameToLibraryToContentIdMap, contentIdToFileContentMap, new PrintWriter(output), ""); assertThat(output.toString()).isEqualTo(EXPECTED_NEW_HTML_STRING); } @Test public void testGenerateHtmlWithCustomHeading() { public void testGenerateHtmlWithCustomHeading() throws Exception { List<File> xmlFiles = new ArrayList<>(); Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>(); Map<String, Set<String>> toBoth = new HashMap<>(); Loading @@ -266,13 +272,14 @@ public class LicenseHtmlGeneratorFromXmlTest { StringWriter output = new StringWriter(); LicenseHtmlGeneratorFromXml.generateHtml( fileNameToLibraryToContentIdMap, contentIdToFileContentMap, xmlFiles, fileNameToLibraryToContentIdMap, contentIdToFileContentMap, new PrintWriter(output), HTML_CUSTOM_HEADING); assertThat(output.toString()).isEqualTo(EXPECTED_OLD_HTML_STRING_WITH_CUSTOM_HEADING); } @Test public void testGenerateNewHtmlWithCustomHeading() { public void testGenerateNewHtmlWithCustomHeading() throws Exception { List<File> xmlFiles = new ArrayList<>(); Map<String, Map<String, Set<String>>> fileNameToLibraryToContentIdMap = new HashMap<>(); Map<String, String> contentIdToFileContentMap = new HashMap<>(); Map<String, Set<String>> toBoth = new HashMap<>(); Loading @@ -292,7 +299,7 @@ public class LicenseHtmlGeneratorFromXmlTest { StringWriter output = new StringWriter(); LicenseHtmlGeneratorFromXml.generateHtml( fileNameToLibraryToContentIdMap, contentIdToFileContentMap, xmlFiles, fileNameToLibraryToContentIdMap, contentIdToFileContentMap, new PrintWriter(output), HTML_CUSTOM_HEADING); assertThat(output.toString()).isEqualTo(EXPECTED_NEW_HTML_STRING_WITH_CUSTOM_HEADING); } Loading