public static void main(String[] args) throws Exception { ingenias.editor.Log.initInstance(new PrintWriter(System.err)); ObtainInstantiationStructure obtainInstantiationStructure1 = new ObtainInstantiationStructure(); if (args.length != 1) { System.err.println( "Wrong format!!!!\n ObtainInstantiationStructure filename"); } else { FileInputStream fis = new FileInputStream(args[0]); int read = fis.read(); StringBuffer sb = new StringBuffer(); while (read != -1) { sb.append( (char) read); read = fis.read(); } fis.close(); System.out.println(getStructure(Conversor.convertArrobaFormat(sb.toString()))); } }
public static String getNodes(int depth, Node n) { StringBuffer result = new StringBuffer(); NodeList nl = n.getChildNodes(); for (int k = 0; k < nl.getLength(); k++) { if (nl.item(k).getNodeName().toLowerCase().equals("repeat")) { String indentation = " "; for (int j = 0; j < depth; j++) { indentation = indentation + " "; } result.append(indentation + nl.item(k).getNodeName() + " id = " + nl.item(k).getAttributes().getNamedItem("id"). getNodeValue()+"\n"); } if (nl.item(k).getNodeName().toLowerCase().equals("v")) { String indentation = " "; for (int j = 0; j < depth; j++) { indentation = indentation + " "; } result.append(indentation + " v " + nl.item(k).getChildNodes().item(0).getNodeValue()+"\n"); } if (nl.item(k).hasChildNodes()) result.append(getNodes(depth + 1, nl.item(k))); } return result.toString(); }
public static String getStructure(String program) throws Exception { DOMParser parser = new DOMParser(); // Parse the Document // and traverse the DOM parser.parse(new InputSource(new java.io.StringBufferInputStream(program))); Document doc = parser.getDocument(); NodeList nl = doc.getElementsByTagName("program"); if (nl.getLength() == 0) { throw new RuntimeException("There is no <program> tag"); } Node programNode = nl.item(0); return getNodes(0, programNode); }