List of usage examples for java.io LineNumberReader readLine
public String readLine() throws IOException
From source file:com.github.fritaly.graphml4j.samples.GradleDependenciesWithGroupsAndBuffering.java
public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println(String.format("%s <output-file>", GradleDependenciesWithGroupsAndBuffering.class.getSimpleName())); System.exit(1);/*from w ww . ja v a2s . co m*/ } final File file = new File(args[0]); System.out.println("Writing GraphML file to " + file.getAbsolutePath() + " ..."); FileWriter fileWriter = null; Reader reader = null; LineNumberReader lineReader = null; try { fileWriter = new FileWriter(file); final com.github.fritaly.graphml4j.datastructure.Graph graph = new Graph(); // The dependency graph has been generated by Gradle with the // command "gradle dependencies". The output of this command has // been saved to a text file which will be parsed to rebuild the // dependency graph reader = new InputStreamReader( GradleDependenciesWithGroupsAndBuffering.class.getResourceAsStream("gradle-dependencies.txt")); lineReader = new LineNumberReader(reader); String line = null; // Stack containing the nodes per depth inside the dependency graph // (the topmost dependency is the first one in the stack) final Stack<Node> parentNodes = new Stack<Node>(); while ((line = lineReader.readLine()) != null) { // Determine the depth of the current dependency inside the // graph. The depth can be inferred from the indentation used by // Gradle. Each level of depth adds 5 more characters of // indentation final int initialLength = line.length(); // Remove the strings used by Gradle to indent dependencies line = StringUtils.replace(line, "+--- ", ""); line = StringUtils.replace(line, "| ", ""); line = StringUtils.replace(line, "\\--- ", ""); line = StringUtils.replace(line, " ", ""); // The depth can easily be inferred now final int depth = (initialLength - line.length()) / 5; // Remove unnecessary node ids while (depth <= parentNodes.size()) { parentNodes.pop(); } final Artifact artifact = createArtifact(line); Node node = graph.getNodeByData(artifact); // Has this dependency already been added to the graph ? if (node == null) { // No, add the node node = graph.addNode(artifact); } parentNodes.push(node); if (parentNodes.size() > 1) { // Generate an edge between the current node and its parent graph.addEdge("Depends on", parentNodes.get(parentNodes.size() - 2), node); } } // Create the groups after creating the nodes & edges for (Node node : graph.getNodes()) { final Artifact artifact = (Artifact) node.getData(); final String groupId = artifact.group; Node groupNode = graph.getNodeByData(groupId); if (groupNode == null) { groupNode = graph.addNode(groupId); } // add the node to the group node.setParent(groupNode); } graph.toGraphML(fileWriter, new Renderer() { @Override public String getNodeLabel(Node node) { return node.isGroup() ? node.getData().toString() : ((Artifact) node.getData()).getLabel(); } @Override public boolean isGroupOpen(Node node) { return true; } @Override public NodeStyle getNodeStyle(Node node) { // Customize the rendering of nodes final NodeStyle nodeStyle = new NodeStyle(); nodeStyle.setWidth(250.0f); return nodeStyle; } @Override public GroupStyles getGroupStyles(Node node) { return new GroupStyles(); } @Override public EdgeStyle getEdgeStyle(Edge edge) { return new EdgeStyle(); } }); System.out.println("Done"); } finally { // Calling GraphMLWriter.close() is necessary to dispose the underlying resources fileWriter.close(); lineReader.close(); reader.close(); } }
From source file:com.github.fritaly.graphml4j.samples.GradleDependencies.java
public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println(String.format("%s <output-file>", GradleDependencies.class.getSimpleName())); System.exit(1);//from w w w. j a v a 2 s. co m } final File file = new File(args[0]); System.out.println("Writing GraphML file to " + file.getAbsolutePath() + " ..."); FileWriter fileWriter = null; GraphMLWriter graphWriter = null; Reader reader = null; LineNumberReader lineReader = null; try { fileWriter = new FileWriter(file); graphWriter = new GraphMLWriter(fileWriter); // Customize the rendering of nodes final NodeStyle nodeStyle = graphWriter.getNodeStyle(); nodeStyle.setWidth(250.0f); graphWriter.setNodeStyle(nodeStyle); // The dependency graph has been generated by Gradle with the // command "gradle dependencies". The output of this command has // been saved to a text file which will be parsed to rebuild the // dependency graph reader = new InputStreamReader(GradleDependencies.class.getResourceAsStream("gradle-dependencies.txt")); lineReader = new LineNumberReader(reader); String line = null; // Stack containing the node identifiers per depth inside the // dependency graph (the topmost dependency is the first one in the // stack) final Stack<String> parentIds = new Stack<String>(); // Open the graph graphWriter.graph(); // Map storing the node identifiers per label final Map<String, String> nodeIdsByLabel = new TreeMap<String, String>(); while ((line = lineReader.readLine()) != null) { // Determine the depth of the current dependency inside the // graph. The depth can be inferred from the indentation used by // Gradle. Each level of depth adds 5 more characters of // indentation final int initialLength = line.length(); // Remove the strings used by Gradle to indent dependencies line = StringUtils.replace(line, "+--- ", ""); line = StringUtils.replace(line, "| ", ""); line = StringUtils.replace(line, "\\--- ", ""); line = StringUtils.replace(line, " ", ""); // The depth can easily be inferred now final int depth = (initialLength - line.length()) / 5; // Remove unnecessary node ids while (depth <= parentIds.size()) { parentIds.pop(); } // Compute a nice label from the dependency (group, artifact, // version) tuple final String label = computeLabel(line); // Has this dependency already been added to the graph ? if (!nodeIdsByLabel.containsKey(label)) { // No, add the node nodeIdsByLabel.put(label, graphWriter.node(label)); } final String nodeId = nodeIdsByLabel.get(label); parentIds.push(nodeId); if (parentIds.size() > 1) { // Generate an edge between the current node and its parent graphWriter.edge(parentIds.get(parentIds.size() - 2), nodeId); } } // Close the graph graphWriter.closeGraph(); System.out.println("Done"); } finally { // Calling GraphMLWriter.close() is necessary to dispose the underlying resources graphWriter.close(); fileWriter.close(); lineReader.close(); reader.close(); } }
From source file:com.github.fritaly.graphml4j.samples.GradleDependenciesWithGroups.java
public static void main(String[] args) throws Exception { if (args.length != 1) { System.out// w w w . j ava 2 s .co m .println(String.format("%s <output-file>", GradleDependenciesWithGroups.class.getSimpleName())); System.exit(1); } final File file = new File(args[0]); System.out.println("Writing GraphML file to " + file.getAbsolutePath() + " ..."); FileWriter fileWriter = null; GraphMLWriter graphWriter = null; Reader reader = null; LineNumberReader lineReader = null; try { fileWriter = new FileWriter(file); graphWriter = new GraphMLWriter(fileWriter); // Customize the rendering of nodes final NodeStyle nodeStyle = graphWriter.getNodeStyle(); nodeStyle.setWidth(250.0f); nodeStyle.setHeight(50.0f); graphWriter.setNodeStyle(nodeStyle); // The dependency graph has been generated by Gradle with the // command "gradle dependencies". The output of this command has // been saved to a text file which will be parsed to rebuild the // dependency graph reader = new InputStreamReader( GradleDependenciesWithGroups.class.getResourceAsStream("gradle-dependencies.txt")); lineReader = new LineNumberReader(reader); String line = null; // Stack containing the artifacts per depth inside the dependency // graph (the topmost dependency is the first one in the stack) final Stack<Artifact> stack = new Stack<Artifact>(); final Map<String, Set<Artifact>> artifactsByGroup = new HashMap<String, Set<Artifact>>(); // List of parent/child relationships between artifacts final List<Relationship> relationships = new ArrayList<Relationship>(); while ((line = lineReader.readLine()) != null) { // Determine the depth of the current dependency inside the // graph. The depth can be inferred from the indentation used by // Gradle. Each level of depth adds 5 more characters of // indentation final int initialLength = line.length(); // Remove the strings used by Gradle to indent dependencies line = StringUtils.replace(line, "+--- ", ""); line = StringUtils.replace(line, "| ", ""); line = StringUtils.replace(line, "\\--- ", ""); line = StringUtils.replace(line, " ", ""); // The depth can easily be inferred now final int depth = (initialLength - line.length()) / 5; // Remove unnecessary artifacts while (depth <= stack.size()) { stack.pop(); } // Create an artifact from the dependency (group, artifact, // version) tuple final Artifact artifact = createArtifact(line); stack.push(artifact); if (stack.size() > 1) { // Store the artifact and its parent relationships.add(new Relationship(stack.get(stack.size() - 2), artifact)); } if (!artifactsByGroup.containsKey(artifact.group)) { artifactsByGroup.put(artifact.group, new HashSet<Artifact>()); } artifactsByGroup.get(artifact.group).add(artifact); } // Open the graph graphWriter.graph(); final Map<Artifact, String> nodeIdsByArtifact = new HashMap<Artifact, String>(); // Loop over the groups and generate the associated nodes for (String group : artifactsByGroup.keySet()) { graphWriter.group(group, true); for (Artifact artifact : artifactsByGroup.get(group)) { final String nodeId = graphWriter.node(artifact.getLabel()); nodeIdsByArtifact.put(artifact, nodeId); } graphWriter.closeGroup(); } // Generate the edges for (Relationship relationship : relationships) { final String parentId = nodeIdsByArtifact.get(relationship.parent); final String childId = nodeIdsByArtifact.get(relationship.child); graphWriter.edge(parentId, childId); } // Close the graph graphWriter.closeGraph(); System.out.println("Done"); } finally { // Calling GraphMLWriter.close() is necessary to dispose the underlying resources graphWriter.close(); fileWriter.close(); lineReader.close(); reader.close(); } }
From source file:org.apache.nutch.protocol.http.api.RobotRulesParser.java
/** command-line main for testing */ public static void main(String[] argv) { if (argv.length < 3) { System.out.println("Usage:"); System.out.println(" java <robots-file> <url-file> <agent-name>+"); System.out.println(""); System.out.println("The <robots-file> will be parsed as a robots.txt file,"); System.out.println("using the given <agent-name> to select rules. URLs "); System.out.println("will be read (one per line) from <url-file>, and tested"); System.out.println("against the rules."); System.exit(-1);/*w ww . jav a 2s. co m*/ } try { FileInputStream robotsIn = new FileInputStream(argv[0]); LineNumberReader testsIn = new LineNumberReader(new FileReader(argv[1])); String[] robotNames = new String[argv.length - 2]; for (int i = 0; i < argv.length - 2; i++) robotNames[i] = argv[i + 2]; ArrayList bufs = new ArrayList(); byte[] buf = new byte[BUFSIZE]; int totBytes = 0; int rsize = robotsIn.read(buf); while (rsize >= 0) { totBytes += rsize; if (rsize != BUFSIZE) { byte[] tmp = new byte[rsize]; System.arraycopy(buf, 0, tmp, 0, rsize); bufs.add(tmp); } else { bufs.add(buf); buf = new byte[BUFSIZE]; } rsize = robotsIn.read(buf); } byte[] robotsBytes = new byte[totBytes]; int pos = 0; for (int i = 0; i < bufs.size(); i++) { byte[] currBuf = (byte[]) bufs.get(i); int currBufLen = currBuf.length; System.arraycopy(currBuf, 0, robotsBytes, pos, currBufLen); pos += currBufLen; } RobotRulesParser parser = new RobotRulesParser(robotNames); RobotRuleSet rules = parser.parseRules(robotsBytes); System.out.println("Rules:"); System.out.println(rules); System.out.println(); String testPath = testsIn.readLine().trim(); while (testPath != null) { System.out.println((rules.isAllowed(testPath) ? "allowed" : "not allowed") + ":\t" + testPath); testPath = testsIn.readLine(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:org.commoncrawl.service.crawler.RobotRulesParser.java
/** command-line main for testing */ public static void main(String[] argv) { if (argv.length < 3) { System.out.println("Usage:"); System.out.println(" java <robots-file> <url-file> <agent-name>+"); System.out.println(""); System.out.println("The <robots-file> will be parsed as a robots.txt file,"); System.out.println("using the given <agent-name> to select rules. URLs "); System.out.println("will be read (one per line) from <url-file>, and tested"); System.out.println("against the rules."); System.exit(-1);/*from w w w . j av a2 s.com*/ } try { FileInputStream robotsIn = new FileInputStream(argv[0]); File testsInFile = new File(argv[1]); LineNumberReader testsIn = null; String singleTestLine = null; if (testsInFile.isFile()) testsIn = new LineNumberReader(new FileReader(argv[1])); else singleTestLine = argv[1]; String[] robotNames = new String[argv.length - 2]; for (int i = 0; i < argv.length - 2; i++) robotNames[i] = argv[i + 2]; ArrayList bufs = new ArrayList(); byte[] buf = new byte[BUFSIZE]; int totBytes = 0; int rsize = robotsIn.read(buf); while (rsize >= 0) { totBytes += rsize; if (rsize != BUFSIZE) { byte[] tmp = new byte[rsize]; System.arraycopy(buf, 0, tmp, 0, rsize); bufs.add(tmp); } else { bufs.add(buf); buf = new byte[BUFSIZE]; } rsize = robotsIn.read(buf); } byte[] robotsBytes = new byte[totBytes]; int pos = 0; for (int i = 0; i < bufs.size(); i++) { byte[] currBuf = (byte[]) bufs.get(i); int currBufLen = currBuf.length; System.arraycopy(currBuf, 0, robotsBytes, pos, currBufLen); pos += currBufLen; } RobotRulesParser parser = new RobotRulesParser(robotNames); RobotRuleSet rules = parser.parseRules(robotsBytes, 0, robotsBytes.length); System.out.println("Rules:"); System.out.println(rules); System.out.println(); if (testsIn != null) { String testPath = testsIn.readLine().trim(); while (testPath != null) { System.out.println((rules.isAllowed(testPath) ? "allowed" : "not allowed") + ":\t" + testPath); testPath = testsIn.readLine(); } } else { System.out.println( (rules.isAllowed(singleTestLine) ? "allowed" : "not allowed") + ":\t" + singleTestLine); } } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static String getMac() { String macAdress = null;//from ww w. j a v a 2s .com String str = ""; try { Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address "); InputStreamReader ir = new InputStreamReader(pp.getInputStream()); LineNumberReader input = new LineNumberReader(ir); for (; null != str;) { str = input.readLine(); if (str != null) { macAdress = str.trim(); break; } } ir.close(); input.close(); } catch (IOException ex) { ex.printStackTrace(); } return macAdress; }
From source file:Main.java
static long getLineCountForFile(File file) throws IOException { LineNumberReader in = new LineNumberReader(new FileReader(file)); long numLines = 0; while (in.readLine() != null) ;/* w w w.j a v a 2 s .com*/ numLines = in.getLineNumber(); in.close(); return numLines; }
From source file:Main.java
private static String getMacAddress() { String macSerial = null;/* w w w .j a va 2 s .c om*/ String str = ""; try { Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address"); InputStreamReader ir = new InputStreamReader(pp.getInputStream()); LineNumberReader input = new LineNumberReader(ir); for (; null != str;) { str = input.readLine(); if (str != null) { str = str.trim(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < str.length(); i++) { if (str.charAt(i) != ':') { sb.append(str.charAt(i)); } } macSerial = sb.toString(); break; } } } catch (IOException ex) { ex.printStackTrace(); } return macSerial; }
From source file:Main.java
public static String getMac() { String result = ""; try {//from w ww. jav a 2 s . com Process process = Runtime.getRuntime().exec("ipconfig /all"); InputStreamReader ir = new InputStreamReader(process.getInputStream()); LineNumberReader input = new LineNumberReader(ir); String line; while ((line = input.readLine()) != null) if (line.indexOf("Physical Address") > 0) { String MACAddr = line.substring(line.indexOf("-") - 2); result = MACAddr; } } catch (java.io.IOException e) { System.err.println("IOException " + e.getMessage()); } return result; }
From source file:Main.java
public static List<String> splitLines(String string) { try {//from ww w . ja v a 2 s. c o m ArrayList<String> list = new ArrayList<String>(); LineNumberReader reader = new LineNumberReader(new StringReader(string)); String s; while ((s = reader.readLine()) != null) { list.add(s); } return list; } catch (IOException e) { // I don't think this can really happen with a StringReader. throw new RuntimeException(e); } }