/*
* (C) Copyright IBM Corp. 1999-2000 All rights reserved.
*
* US Government Users Restricted Rights Use, duplication or
* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*
* The program is provided "as is" without any warranty express or
* implied, including the warranty of non-infringement and the implied
* warranties of merchantibility and fitness for a particular purpose.
* IBM will not be liable for any damages suffered by you as a result
* of using the Program. In no event will IBM be liable for any
* special, indirect or consequential damages or lost profits even if
* IBM has been advised of the possibility of their occurrence. IBM
* will not be liable for any third party claims against you.
*/
package asn1;
import com.ibm.xml.asn1.SAXEventDumper;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
/**
* Sample of converting object identifiers into readable forms.
*
* @author Hiroshi Maruyama
* @author Takeshi Imamura
*/
public class OidTranslator extends DefaultHandler {
static boolean opt_d = false;
static String oidElementName = "_OBJECT_IDENTIFIER";
static String tableFileName = "oidtable.dat";
static Hashtable oidTable;
StringBuffer buffer = null;
ContentHandler handler;
void prepareTable() {
oidTable = new Hashtable();
try {
InputStream i = (OidTranslator.class).getResourceAsStream(tableFileName);
BufferedReader r = new BufferedReader(new InputStreamReader(i));
String line;
int lineno = 0;
while ((line=r.readLine())!=null) {
lineno++;
StringTokenizer st = new StringTokenizer(line);
if (!st.hasMoreTokens()) continue;
if (!st.hasMoreTokens()) {
System.err.println("Error in line "+lineno+" in "+tableFileName);
System.exit(-1);
}
String key = st.nextToken();
if (!st.hasMoreTokens()) {
System.err.println("Error in line "+lineno+" in "+tableFileName);
System.exit(-1);
}
String dash = st.nextToken(); // "-" to be ignored
StringBuffer sb = new StringBuffer();
while (st.hasMoreTokens()) {
sb.append(st.nextToken());
if (st.hasMoreTokens()) sb.append(" ");
}
oidTable.put(key,sb.toString());
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
void dumpTable() {
for (Enumeration keys = oidTable.keys(); keys.hasMoreElements(); ) {
String key = (String) keys.nextElement();
String value = (String) oidTable.get(key);
System.out.println(key+"="+value);
}
}
void start(String inf) throws IOException, SAXException {
prepareTable();
// if (opt_d) dumpTable();
SAXEventDumper dump = new SAXEventDumper();
Writer out = new OutputStreamWriter(System.out);
dump.setWriter(out);
dump.setXMLDeclaration("1.0", null);
dump.setFormat(false);
handler = dump;
SAXParser parser = new SAXParser();
parser.setContentHandler(this);
parser.setErrorHandler(this);
parser.parse(inf);
}
//
// The following five methods implements ContentHandler
//
public void startDocument() throws SAXException {
handler.startDocument();
}
public void endDocument() throws SAXException {
handler.endDocument();
}
public void startElement(String uri, String localName, String name, Attributes atts) throws SAXException {
handler.startElement(uri, localName, name, atts);
if (name.equals("_OBJECT_IDENTIFIER")) {
this.buffer = new StringBuffer();
}
}
public void endElement(String uri, String localName, String name) throws SAXException {
if (name.equals("_OBJECT_IDENTIFIER")) {
String oid = this.buffer.toString();
// System.err.println("ContentType:"+this.ContentType);
String oidName = (String) oidTable.get(oid);
if (oidName != null) {
handler.characters(oidName.toCharArray(),0,oidName.length());
} else {
System.err.println("OID "+oid+" not found.");
}
this.buffer = null;
}
handler.endElement(uri, localName, name);
}
public void characters(char chars[], int start, int length) throws SAXException {
if (this.buffer != null) {
this.buffer.append(chars, start, length);
} else {
handler.characters(chars,start,length);
}
}
//
// The following three methods implements ErrorHandler
//
/**
* Receive notification of a warning. The warning information is
* printed.
*
* @param exc the warning information encapsulated in a
* SAX parse exception.
*/
public void warning(SAXParseException exc) {
System.err.println("[Warning] "+getLocation(exc)+": "+exc.getMessage());
}
/**
* Receive notification of a recoverable error. The error
* information is printed.
*
* @param exc the error information encapsulated in a
* SAX parse exception.
*/
public void error(SAXParseException exc) {
System.err.println("[Error] "+getLocation(exc)+": "+exc.getMessage());
}
/**
* Receive notification of a non-recoverable error. The error
* information is printed and the exception is thrown again.
*
* @param exc the error information encapsulated in a
* SAX parse exception.
*
* @exception org.xml.sax.SAXException -
*/
public void fatalError(SAXParseException exc) throws SAXException {
System.err.println("[Fatal error] "+getLocation(exc)+": "+exc.getMessage());
throw exc;
}
/**
* Gets the location where the exception is thrown.
*
* @param exc the warning or error information
* encapsulated in a SAX parse exception.
*/
private String getLocation(SAXParseException exc) {
StringBuffer buf = new StringBuffer();
String id = exc.getSystemId();
if (id != null) {
int i = id.lastIndexOf('/');
if (i != -1) {
id = id.substring(i+1);
}
if (!id.equals("")) {
buf.append(id+", ");
}
}
buf.append(exc.getLineNumber()+", ");
buf.append(exc.getColumnNumber());
return buf.toString();
}
//
// Main
//
public static void main(String args[]) {
int i=0;
if (args.length<1) {
usage(-1);
} else if (args[0].equals("-d")) {
opt_d = true;
i++;
}
if (args.length");
System.exit(stat);
}
}