Created
June 21, 2012 14:13
-
-
Save simonharrer/2965971 to your computer and use it in GitHub Desktop.
Counting the nodes in an xml file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class XmlAnalyzer { | |
public static final String[] commonNodes = ["process", "partnerLinks", "partnerLink", "variable", "variables", "import"] | |
public static void main(String[] args) { | |
if(args.length != 2){ | |
throw new IllegalArgumentException("Usage: PATH EXTENSION") | |
} | |
analyzeFilesInDirectory(new File(args[0]),args[1]) | |
} | |
File file | |
/** | |
* Computes a set of all node names | |
* | |
* @return a list of all node names | |
*/ | |
SortedSet<String> getNodes() { | |
def xml = new XmlSlurper(false, false).parse(file) | |
xml.depthFirst().collect { it.name().trim() }.unique().sort() as SortedSet | |
} | |
/** | |
* Computes a set of all node names except the ones in the <code>commonNodes</code> | |
* | |
* @return a list of all nodes except the ones in the <code>commonNodes</code> | |
*/ | |
SortedSet<String> getNodesWithoutCommonNodes() { | |
def nodes = getNodes() | |
nodes.removeAll(commonNodes) | |
nodes | |
} | |
/** | |
* Returns a map which maps nodes to | |
* | |
* @return | |
*/ | |
SortedMap<String, Integer> getNodesWithOccurences() { | |
def xml = new XmlSlurper(false, false).parse(file) | |
Map<String, Integer> result = new TreeMap<String, Integer>() | |
xml.depthFirst().collect { it.name().trim() }.each { | |
if(result.containsKey(it)){ | |
result.put(it, result.get(it) + 1) | |
} else { | |
result.put(it, 1) | |
} | |
} | |
result | |
} | |
public static void analyzeFilesInDirectory(File dir, String extension){ | |
dir.eachFileRecurse { file -> | |
if(file.isFile() && file.name.endsWith(extension)){ | |
println "------------------------------------------" | |
println file.name | |
XmlAnalyzer xmlAnalyzer = new XmlAnalyzer(file: file) | |
println "All Nodes: " +xmlAnalyzer.getNodes() | |
println "All Nodes and their Occurences: " + xmlAnalyzer.getNodesWithOccurences() | |
println "Uncommon Nodes: " + xmlAnalyzer.getNodesWithoutCommonNodes() | |
println "------------------------------------------" | |
println "" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment