1 /* 2 * Copyright 2017 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 org.conscrypt.graphgen; 18 19 import static java.nio.file.FileVisitResult.CONTINUE; 20 21 import com.bazaarvoice.jolt.Chainr; 22 import com.bazaarvoice.jolt.JsonUtils; 23 import java.io.BufferedInputStream; 24 import java.io.BufferedOutputStream; 25 import java.io.FileInputStream; 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 import java.io.InputStream; 29 import java.io.OutputStream; 30 import java.io.PrintStream; 31 import java.net.URI; 32 import java.net.URISyntaxException; 33 import java.nio.charset.StandardCharsets; 34 import java.nio.file.FileSystem; 35 import java.nio.file.FileSystems; 36 import java.nio.file.FileVisitResult; 37 import java.nio.file.Files; 38 import java.nio.file.Path; 39 import java.nio.file.Paths; 40 import java.nio.file.SimpleFileVisitor; 41 import java.nio.file.attribute.BasicFileAttributes; 42 import java.util.Collections; 43 import java.util.List; 44 45 /** 46 * Utility to convert from the JMH JSON output to an HTML file. 47 */ 48 public class Main { 49 50 public static final String JSON_TEMPLATES = "/json/templates/"; 51 public static final String HTML_TEMPLATES = "/html/"; 52 main(String[] args)53 public static void main(String[] args) throws IOException, URISyntaxException { 54 if (args.length != 3) { 55 System.err.println("Usage: graphgen [template] [input.json] [output.html]"); 56 listAllResources(System.err); 57 System.exit(1); 58 } 59 60 try (InputStream spec = Main.class.getResourceAsStream(JSON_TEMPLATES + args[0]); 61 InputStream jmhIn = new BufferedInputStream(new FileInputStream(args[1])); 62 OutputStream output = new BufferedOutputStream(new FileOutputStream(args[2]))) { 63 writeHtml(output, "header.html"); 64 convertJmhJsonData(spec, jmhIn, output); 65 writeHtml(output, "footer.html"); 66 } 67 } 68 writeHtml(OutputStream out, String name)69 private static void writeHtml(OutputStream out, String name) throws IOException { 70 InputStream header = Main.class.getResourceAsStream(HTML_TEMPLATES + name); 71 byte[] buffer = new byte[4096]; 72 int numRead; 73 while ((numRead = header.read(buffer)) != -1) { 74 out.write(buffer, 0, numRead); 75 } 76 } 77 78 /** 79 * Load the JSON template data and convert it. 80 */ convertJmhJsonData(InputStream specIn, InputStream jmhIn, OutputStream out)81 private static void convertJmhJsonData(InputStream specIn, InputStream jmhIn, OutputStream out) throws IOException { 82 List<?> chainrConfig = JsonUtils.jsonToList(specIn); 83 Chainr chainr = Chainr.fromSpec(chainrConfig); 84 List<Object> input = JsonUtils.jsonToList(jmhIn); 85 Object jsonOutput = chainr.transform(input); 86 out.write(JsonUtils.toJsonString(jsonOutput).getBytes(StandardCharsets.UTF_8)); 87 } 88 89 /** 90 * Lists all the JSON templates in the Classpath. 91 */ listAllResources(PrintStream err)92 private static void listAllResources(PrintStream err) throws IOException, URISyntaxException { 93 URI uri = Main.class.getResource(JSON_TEMPLATES).toURI(); 94 95 final Path templatesPath; 96 if (uri.getScheme().equals("jar")) { 97 FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap()); 98 templatesPath = fs.getPath(JSON_TEMPLATES); 99 } else { 100 templatesPath = Paths.get(uri); 101 } 102 103 err.println("Possible templates:"); 104 PrintFileNames pfn = new PrintFileNames(" ", err); 105 Files.walkFileTree(templatesPath, pfn); 106 } 107 108 private static class PrintFileNames extends SimpleFileVisitor<Path> { 109 private final String prefix; 110 private final PrintStream out; 111 PrintFileNames(String prefix, PrintStream out)112 public PrintFileNames(String prefix, PrintStream out) { 113 this.prefix = prefix; 114 this.out = out; 115 } 116 117 @Override visitFile(Path path, BasicFileAttributes basicFileAttributes)118 public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes) 119 throws IOException { 120 out.println(prefix + path.getFileName()); 121 return CONTINUE; 122 } 123 } 124 } 125