|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +// run simply as: java path/to/HostInliningVisualizer.java |
| 42 | +import java.io.IOException; |
| 43 | +import java.nio.file.Files; |
| 44 | +import java.nio.file.Paths; |
| 45 | + |
| 46 | +import javax.swing.JButton; |
| 47 | +import javax.swing.JFrame; |
| 48 | +import javax.swing.JPanel; |
| 49 | +import javax.swing.JScrollPane; |
| 50 | +import javax.swing.JTextField; |
| 51 | +import javax.swing.JTree; |
| 52 | +import javax.swing.SwingUtilities; |
| 53 | +import javax.swing.tree.DefaultMutableTreeNode; |
| 54 | +import javax.swing.tree.DefaultTreeModel; |
| 55 | +import javax.swing.tree.TreePath; |
| 56 | + |
| 57 | +public class HostInliningVisualizer extends JFrame { |
| 58 | + private JTree tree; |
| 59 | + private DefaultTreeModel treeModel; |
| 60 | + |
| 61 | + private static DefaultMutableTreeNode parse(String filePath) throws IOException { |
| 62 | + DefaultMutableTreeNode logData = new DefaultMutableTreeNode("Root"); |
| 63 | + int[] indentation = new int[100]; |
| 64 | + DefaultMutableTreeNode[] levels = new DefaultMutableTreeNode[100]; |
| 65 | + indentation[0] = 0; |
| 66 | + levels[0] = logData; |
| 67 | + |
| 68 | + for (String rawLine : Files.readAllLines(Paths.get(filePath))) { |
| 69 | + String line = rawLine.trim(); |
| 70 | + if (line.isEmpty()) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(line); |
| 75 | + int currentIndentation = rawLine.length() - rawLine.trim().length(); |
| 76 | + for (int i = 0; i < indentation.length; i++) { |
| 77 | + if (levels[i] == null || indentation[i] == currentIndentation) { |
| 78 | + levels[i] = newNode; |
| 79 | + levels[i - 1].add(newNode); |
| 80 | + indentation[i] = currentIndentation; |
| 81 | + break; |
| 82 | + } |
| 83 | + if (indentation[i] > currentIndentation) { |
| 84 | + System.err.println("Looks like wronlgy indented line: " + line); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + return logData; |
| 89 | + } |
| 90 | + |
| 91 | + private void initGui(String title, DefaultMutableTreeNode root) { |
| 92 | + treeModel = new DefaultTreeModel(root); |
| 93 | + tree = new JTree(treeModel); |
| 94 | + tree.setShowsRootHandles(true); |
| 95 | + |
| 96 | + JScrollPane scrollPane = new JScrollPane(tree); |
| 97 | + JPanel searchPanel = createPanel(); |
| 98 | + |
| 99 | + getContentPane().add(searchPanel, "North"); |
| 100 | + getContentPane().add(scrollPane); |
| 101 | + setTitle(title); |
| 102 | + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 103 | + setSize(400, 300); |
| 104 | + setLocationRelativeTo(null); |
| 105 | + setVisible(true); |
| 106 | + } |
| 107 | + |
| 108 | + private JPanel createPanel() { |
| 109 | + JTextField searchField = new JTextField(20); |
| 110 | + searchField.addActionListener(e -> searchNodes(searchField.getText())); |
| 111 | + |
| 112 | + JButton searchButton = new JButton("Search"); |
| 113 | + searchButton.addActionListener(e -> searchNodes(searchField.getText())); |
| 114 | + |
| 115 | + JButton collapseButton = new JButton("Collapse All"); |
| 116 | + collapseButton.addActionListener(e -> collapseAllNodes()); |
| 117 | + |
| 118 | + JPanel searchPanel = new JPanel(); |
| 119 | + searchPanel.add(searchField); |
| 120 | + searchPanel.add(searchButton); |
| 121 | + searchPanel.add(collapseButton); |
| 122 | + return searchPanel; |
| 123 | + } |
| 124 | + |
| 125 | + private void collapseAllNodes() { |
| 126 | + int row = tree.getRowCount() - 1; |
| 127 | + while (row >= 0) { |
| 128 | + tree.collapseRow(row); |
| 129 | + row--; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private void searchNodes(String searchText) { |
| 134 | + if (searchText.isEmpty()) { |
| 135 | + tree.clearSelection(); |
| 136 | + return; |
| 137 | + } |
| 138 | + searchInNode((DefaultMutableTreeNode) treeModel.getRoot(), searchText); |
| 139 | + } |
| 140 | + |
| 141 | + private void searchInNode(DefaultMutableTreeNode node, String searchText) { |
| 142 | + if (node.toString().toLowerCase().contains(searchText.toLowerCase())) { |
| 143 | + TreePath path = new TreePath(node.getPath()); |
| 144 | + tree.addSelectionPath(path); |
| 145 | + } |
| 146 | + |
| 147 | + for (int i = 0; i < node.getChildCount(); i++) { |
| 148 | + DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) node.getChildAt(i); |
| 149 | + searchInNode(childNode, searchText); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + public static void main(String[] args) { |
| 154 | + if (args.length != 1) { |
| 155 | + System.err.println("Error: expects one argument - filename with host inlining log"); |
| 156 | + System.exit(1); |
| 157 | + } |
| 158 | + var app = new HostInliningVisualizer(); |
| 159 | + try { |
| 160 | + DefaultMutableTreeNode logData = parse(args[0]); |
| 161 | + SwingUtilities.invokeLater(() -> app.initGui(args[0], logData)); |
| 162 | + } catch (IOException e) { |
| 163 | + e.printStackTrace(); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments