📄 basereaderconfigurationhandler.java
字号:
/* * BaseReaderConfigurationHandler.java * Created on February 3, 2003, 10:52 AM */package gov.nist.examples.bps.reader;import java.io.IOException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import org.xml.sax.XMLReader;import org.xml.sax.helpers.DefaultHandler;/** * * @author Deruelle Olivier */public class BaseReaderConfigurationHandler extends DefaultHandler { private Configuration configuration; private String element; private void configurationMessage(String message) { System.out.println(element + ":" + message); } /** Creates a new instance of BaseReaderHandler */ public BaseReaderConfigurationHandler(String confFile) { try{ SAXParserFactory saxParserFactory=SAXParserFactory.newInstance(); SAXParser saxParser = saxParserFactory.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setContentHandler(this); xmlReader.setFeature ("http://xml.org/sax/features/validation",false); // parse the xml specification for the event tags. xmlReader.parse(confFile); } catch (SAXParseException spe) { spe.printStackTrace(); } catch (SAXException sxe) { sxe.printStackTrace(); } catch (IOException ioe) { // I/O error ioe.printStackTrace(); } catch (Exception pce) { // Parser with specified options can't be built pce.printStackTrace(); } } public Configuration getConfiguration() { return configuration; } //=========================================================== // SAX DocumentHandler methods //=========================================================== public void startDocument() throws SAXException { try { BaseReaderDebug.println("---------------------------------------"); BaseReaderDebug.println("Parsing the XML configuration file..."); configuration=new Configuration(); } catch (Exception e) { throw new SAXException("XMLRegistrationsParser error", e); } } public void endDocument() throws SAXException { try { BaseReaderDebug.println ("The XML configuration file has been successfully parsed!"); BaseReaderDebug.println("---------------------------------------"); } catch (Exception e) { throw new SAXException("XMLRegistrationsParser error", e); } } public void startElement(String namespaceURI, String lName, // local name String qName, // qualified name Attributes attrs) throws SAXException { String element=qName; this.element = element; if (element.compareToIgnoreCase("SIP_STACK") ==0 ) { String stackName=attrs.getValue("stack_name"); if (stackName==null || stackName.trim().equals("")) { configurationMessage("ERROR: the name of the stack is not set"); } else { configuration.stackName=stackName.trim(); configurationMessage("INFORMATION: the name of the stack is set to:"+ configuration.stackName); } String stackIPAddress=attrs.getValue("stack_IP_address"); if (stackIPAddress==null || stackIPAddress.trim().equals("")) { configurationMessage ("ERROR: the IP address of the stack is not set"); } else { configuration.stackIPAddress=stackIPAddress.trim(); configurationMessage ("INFORMATION: the stack IP address is set to:" + stackIPAddress); } String outboundProxy=attrs.getValue("outbound_proxy"); if (outboundProxy==null || outboundProxy.trim().equals("")) { configurationMessage ("INFORMATION: the outbound proxy is not set"); } else { configuration.outboundProxy=outboundProxy.trim(); configurationMessage ("INFORMATION: the outbound proxy is " + outboundProxy); } String routerPath=attrs.getValue("router_path"); if (routerPath==null || routerPath.trim().equals("")) { configurationMessage ("WARNING: the path for the router is not set using default"); } else { configuration.routerPath=routerPath.trim(); configurationMessage ("INFORMATION: the path for the router is set to: " + routerPath.trim()); } String extensionMethods=attrs.getValue("extension_methods"); if (extensionMethods==null || extensionMethods.trim().equals("") ) { configurationMessage ("INFORMATION: no extensions are supported"); } else { configuration.extensionMethods=extensionMethods.trim(); configurationMessage ("INFORMATION: extensions supported: " + extensionMethods); } String retransmissionFilter=attrs.getValue("retransmission_filter"); if (retransmissionFilter!=null && !retransmissionFilter.trim().equals("")) { configurationMessage ("INFORMATION: the retransmission filter is set to:" + retransmissionFilter); configuration.retransmissionFilter=retransmissionFilter.trim(); } else configurationMessage ("INFORMATION: no default retransmission filter"); String stopTime=attrs.getValue("stop_after"); if (stopTime!=null && !stopTime.trim().equals("")) { configurationMessage ("INFORMATION: The proxy will stop automatically after " +stopTime+" ms"); configuration.stopTime=stopTime.trim(); } String maxConnections=attrs.getValue("max_connections"); if (maxConnections!=null && !maxConnections.trim().equals("")) { configurationMessage ("INFORMATION: The proxy will authorize only " +maxConnections+" connections"); configuration.maxConnections=maxConnections.trim(); } String maxServerTransactions=attrs.getValue("max_server_transactions"); if (maxServerTransactions!=null && !maxServerTransactions.trim().equals("")) { configurationMessage ("INFORMATION: The proxy will authorize only " +maxServerTransactions+" server transactions"); configuration.maxServerTransactions=maxServerTransactions.trim(); } String threadPoolSize=attrs.getValue("thread_pool_size"); if (threadPoolSize!=null && !threadPoolSize.trim().equals("")) { configurationMessage ("INFORMATION: The thread pool size is set to " +threadPoolSize); configuration.threadPoolSize=threadPoolSize.trim(); } } if (element.compareToIgnoreCase("LISTENING_POINT") ==0 ) { String stackPort=attrs.getValue("port"); if (stackPort==null || stackPort.trim().equals("")) { configurationMessage ("ERROR: the port of the stack is not set"); } else { String stackTransport=attrs.getValue("transport"); if (stackTransport==null || stackTransport.trim().equals("")) { configurationMessage ("ERROR: the transport is not set"); } else { configuration.addListeningPoint(stackPort,stackTransport); } } } if (element.compareToIgnoreCase("BASE_READER") ==0 ) { String serialPortIdentifier=attrs.getValue("serial_port_identifier"); if (serialPortIdentifier==null || serialPortIdentifier.trim().equals("")) { configurationMessage ("ERROR: specify COM1 or COM2 for the serial port identifier"); } else { String serialLocalPort=attrs.getValue("serial_local_port"); if (serialLocalPort==null || serialLocalPort.trim().equals("")) { configurationMessage ("ERROR: specify a port of the serial API to connect to"); } else { try{ configuration.serialLocalPort= Integer.valueOf(serialLocalPort.trim()).intValue(); configuration.serialPortIdentifier=serialPortIdentifier; } catch(Exception e) { configurationMessage ("ERROR: the serial local port is not set."); } } } String serialDebug=attrs.getValue("serial_debug"); if (serialDebug!=null && !serialDebug.trim().equals("")) { try{ Boolean b=Boolean.valueOf (serialDebug.trim()); if ( b.booleanValue() ) { configuration.serialDebug=true; configurationMessage ("INFORMATION: Serial debug feature is enabled"); } else { configuration.serialDebug=false; configurationMessage ("WARNING: Serial debug feature is disabled"); } } catch(Exception e) { configurationMessage ("WARNING: Serial debug feature is disabled"); } } String baseReaderIdentifier=attrs.getValue("base_reader_identifier"); if (baseReaderIdentifier==null || baseReaderIdentifier.trim().equals("")) { configurationMessage ("ERROR: specify base reader identifier"); } else { configuration.baseReaderIdentifier=baseReaderIdentifier; } String baseReaderLocation=attrs.getValue("base_reader_location"); if (baseReaderLocation==null || baseReaderLocation.trim().equals("")) { configurationMessage ("ERROR: specify base reader Location"); } else { configuration.baseReaderLocation=baseReaderLocation; } String baseReaderRange=attrs.getValue("base_reader_range"); if (baseReaderRange==null || baseReaderRange.trim().equals("")) { configurationMessage ("WARNING: the base reader range is not set."); } else { configuration.baseReaderRange= baseReaderRange; } } if (element.compareToIgnoreCase("LOGGING") ==0 ) { String enableDebug=attrs.getValue("enable_debug"); if (enableDebug==null || enableDebug.trim().equals("") ) { configuration.enableDebug=false; configurationMessage ("INFORMATION: the Debug feature is not set"); } else { try{ boolean deb=Boolean.valueOf (enableDebug.trim()).booleanValue(); if ( deb) { configurationMessage ("INFORMATION: the Debug feature is set"); configuration.enableDebug=deb; } else { configuration.enableDebug=false; configurationMessage ("INFORMATION: the Debug feature is not set"); } } catch(Exception e) { configuration.enableDebug=false; configurationMessage ("INFORMATION: the Debug feature is not set"); } } String serverLogFile=attrs.getValue("server_log"); if (serverLogFile==null || serverLogFile.trim().equals("")) { configurationMessage ("INFORMATION: the server message log file is not set"); } else { configuration.serverLogFile=serverLogFile.trim(); configurationMessage ("INFORMATION: the server message log file is: " + configuration.serverLogFile); } String badMessageLog=attrs.getValue("bad_message_log"); if (badMessageLog==null || badMessageLog.trim().equals("")) { configurationMessage ("INFORMATION: the bad message log file is not set"); } else { configuration.badMessageLogFile= badMessageLog.trim(); configurationMessage ("INFORMATION: the bad message log is: " + configuration.badMessageLogFile); } String debugLog=attrs.getValue("debug_log"); if (debugLog==null || debugLog.trim().equals("")) { configurationMessage ("INFORMATION: the debug log file is not set"); } else { configuration.debugLogFile=debugLog.trim(); configurationMessage ("INFORMATION: the debug log file is: " + configuration.debugLogFile); } } } public void endElement(String namespaceURI, String sName, // simple name String qName // qualified name ) throws SAXException { String element=qName; } public void characters(char buf[], int offset, int len) throws SAXException { String str = new String(buf, offset, len); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -