Created
October 15, 2018 04:13
-
-
Save mutsune/6b0eede3bcd561eab0421442674d831a to your computer and use it in GitHub Desktop.
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
package sax; | |
import org.xml.sax.Attributes; | |
import org.xml.sax.ErrorHandler; | |
import org.xml.sax.SAXException; | |
import org.xml.sax.SAXParseException; | |
import org.xml.sax.XMLReader; | |
import org.xml.sax.helpers.DefaultHandler; | |
import java.io.File; | |
import java.io.PrintStream; | |
import java.util.Enumeration; | |
import java.util.Hashtable; | |
import javax.xml.parsers.SAXParser; | |
import javax.xml.parsers.SAXParserFactory; | |
public class SAXLocalNameCount extends DefaultHandler { | |
private Hashtable<String, Integer> tags; | |
@Override | |
public void startDocument() { | |
tags = new Hashtable<>(); | |
} | |
@Override | |
public void startElement(String namespaceURI, String localName, String qName, Attributes attributes) { | |
Integer value = tags.get(localName); | |
if (value == null) { | |
tags.put(localName, 1); | |
} else { | |
int count = value; | |
count++; | |
tags.put(localName, count); | |
} | |
} | |
@Override | |
public void endDocument() { | |
Enumeration<String> e = tags.keys(); | |
while (e.hasMoreElements()) { | |
String tag = e.nextElement(); | |
int count = tags.get(tag); | |
System.out.println("Local Name \"" + tag + "\" occurs " + count + " times"); | |
} | |
} | |
private static String convertToFileURL(String filename) { | |
String path = new File(filename).getAbsolutePath(); | |
if (File.separatorChar != '/') { | |
path = path.replace(File.separatorChar, '/'); | |
} | |
if (!path.startsWith("/")) { | |
path = "/" + path; | |
} | |
return "file:" + path; | |
} | |
static public void main(String[] args) throws Exception { | |
String filename = null; | |
for (int i = 0; i < args.length; i++) { | |
filename = args[i]; | |
if (i != args.length - 1) { | |
usage(); | |
} | |
} | |
if (filename == null) { | |
usage(); | |
} | |
SAXParserFactory spf = SAXParserFactory.newInstance(); | |
spf.setNamespaceAware(true); | |
SAXParser saxParser = spf.newSAXParser(); | |
XMLReader xmlReader = saxParser.getXMLReader(); | |
xmlReader.setContentHandler(new SAXLocalNameCount()); | |
xmlReader.setErrorHandler(new MyErrorHandler(System.err)); | |
xmlReader.parse(convertToFileURL(filename)); | |
} | |
private static void usage() { | |
System.err.println("Usage: SAXLocalNameCount <file.xml>"); | |
System.err.println(" -usage or -help = this message"); | |
System.exit(1); | |
} | |
private static class MyErrorHandler implements ErrorHandler { | |
private PrintStream out; | |
MyErrorHandler(PrintStream out) { | |
this.out = out; | |
} | |
private String getParseExceptionInfo(SAXParseException spe) { | |
String systemId = spe.getSystemId(); | |
if (systemId == null) { | |
systemId = "null"; | |
} | |
return "URI=" + systemId + " Line=" + spe.getLineNumber() + ": " + spe.getMessage(); | |
} | |
public void warning(SAXParseException spe) { | |
out.println("Warning: " + getParseExceptionInfo(spe)); | |
} | |
public void error(SAXParseException spe) throws SAXException { | |
String message = "Error: " + getParseExceptionInfo(spe); | |
throw new SAXException(message); | |
} | |
public void fatalError(SAXParseException spe) throws SAXException { | |
String message = "Fatal Error: " + getParseExceptionInfo(spe); | |
throw new SAXException(message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
data