?? saxparser1.java~261~
字號:
package xmlsax;import java.util.*;import java.io.*;import java.io.IOException;import org.xml.sax.*;import org.xml.sax.helpers.*;import org.apache.xerces.parsers.SAXParser;public class SaxParser1 extends DefaultHandler { private java.util.Stack tagStack=new java.util.Stack(); private String resultXML=""; //寫成XML的字符串 private String indent=""; private static final java.lang.String NEWLINE=System.getProperty("line.separator"); private PrintWriter printFile=null; private String resultPZ="您所選擇的設備類型如下:" + NEWLINE; //返回所選擇的設備類型 private String resultTXT="";//寫成文本的字符串 String[] res =new String[10]; //類型列表 int len = 0,sel,selport,f=0; String sbname,tag; public static void main(String[] argv) { if (argv.length != 1) { System.out.println("Usage: java SaxParser1 [URI]"); System.exit(0); } String uri = argv[0]; try { XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); SaxParser1 SaxParser1Instance = new SaxParser1(); parser.setContentHandler(SaxParser1Instance); parser.parse(uri); } catch(IOException ioe) { ioe.printStackTrace(); } catch(SAXException saxe) { saxe.printStackTrace(); } }//將字符串中的單詞拆開 public String[] sTOc(String temp){ String[] res =new String[10]; StringTokenizer st = new StringTokenizer(temp); len = 0; while(st.hasMoreTokens()) res[len++] = st.nextToken() + " " + sbname; return res; }//顯示外設選擇菜單 private int selectSb(String[] sb) { int i; String s = ""; String ask = "\n確認請按回車,或選擇其它"; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("請在下列" + sbname + "類型中選擇一種\n"); for(i=0;i<len;i++) System.out.println(" " + i +"."+ sb[i] + "\n"); i=0; System.out.println("請輸入0 -- " + (len-1) + "(直接回車,默認選擇0):"); try { s = in.readLine(); Integer tt=new java.lang.Integer(0); while ( s.length() != 0) { i=tt.valueOf(s).intValue(); if(i<len && i>=0) System.out.println("您選擇的" + sbname + "是: " + sb[i] + "\n" + ask + sbname); else System.out.println("錯誤的選擇!!"); s = in.readLine(); } } catch (IOException e) { e.printStackTrace(); } return i; }//顯示所用端口選擇菜單 private int selectPort(String sbname) { int i; String s = ""; String ask = "\n確認請按回車,或選擇其它"; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("請為" + sbname + "選擇一種輔口\n"); i=0; System.out.println("請輸入1 或 2 或 3 分別代表輔口1、2、3(直接回車,默認選擇1):"); try { s = in.readLine(); Integer tt=new java.lang.Integer(0); while ( s.length() != 0) { i=tt.valueOf(s).intValue(); if(i<3 && i>=0) System.out.println("您為" + sbname + "設備選擇的輔口是: " + i + "\n" + ask + sbname); else System.out.println("錯誤的選擇!!"); s = in.readLine(); } } catch (IOException e) { e.printStackTrace(); } return i; } private String locateValue(String tagName) { return sbname; } /**@todo: Implement this characters method*/ public void characters(char[] ch, int start, int length) throws SAXException { String value = new String(ch, start, length); if(f == 1){ resultTXT=resultTXT+value+"}"+NEWLINE; f = 0; } } /**@todo: Implement this endDocument method*/ public void endDocument() throws SAXException { String ask = "\n確認請按回車,或輸入其它文件名:"; String s = ""; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String filename = "config.ini"; System.out.println(resultPZ); System.out.println("您是否將上面的選擇保存為配置文件Y/N(直接回車,默認選擇N):"); try { s = in.readLine(); if ( s.length() != 0) { if(s.equals("Y")|| s.equals("y")){ System.out.println("您是否保存為:" + filename + "\n" + ask); s = in.readLine(); if ( s.length() != 0) filename = s; try { printFile=new PrintWriter(new FileWriter(filename)); printFile.print(resultTXT); printFile.close(); System.out.println("您已成功保存配置文件:" + filename); } catch(IOException e) { System.out.println("打開 " + filename + " 時出錯:"+e); } } } } catch (IOException e) { e.printStackTrace(); } } /**@todo: Implement this endElement method*/ public void endElement(String uri, String localName, String qName) throws SAXException { if(localName.equals("trnDef")) { } else if(localName.equals("Device")) { String tagName=(String)tagStack.pop(); indent=indent.substring(0,indent.length()-4); resultXML=resultXML+indent+"</"+tagName+">"+NEWLINE; } else if(localName.equals("Name")) { String tagName=(String)tagStack.pop(); indent=indent.substring(0,indent.length()-4); if (res[sel].equals(tag)) resultXML=resultXML+indent+"</"+tagName+">"+NEWLINE; else tag = ""; } } /**@todo: Implement this startDocument method*/ public void startDocument() throws SAXException { } /**@todo: Implement this startElement method*/ public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if(localName.equals("Def")) { } else if(localName.equals("Device")) { String tagName=attributes.getValue(0); String tagValue=attributes.getValue(1); sbname = tagName; res=sTOc(tagValue); sel=selectSb(res); if (tagName.equals("密碼鍵盤") || tagName.equals("讀卡器")) selport=selectPort(tagName); tagStack.push(tagName); tag = tagName + " " + sbname; resultXML=resultXML+indent+"<"+tagName+">"+NEWLINE; indent+=" "; resultPZ=resultPZ+tagName+"="; //返回設備名 resultTXT=resultTXT+"<"+tagName+"="; //返回設備名 } else if(localName.equals("Name")) { String tagName=attributes.getValue(0); tagStack.push(tagName); tag = tagName + " " + sbname; if (res[sel].equals(tag)){ resultXML=resultXML+indent+"<"+tagName+">"+NEWLINE; resultPZ=resultPZ+tagName+NEWLINE; //返回設備類型 resultTXT=resultTXT+tagName+">"+NEWLINE; //返回設備類型 if (sbname.equals("讀卡器")){ resultTXT=resultTXT+"{MSR_PORT="+selport+"}"+NEWLINE; resultPZ=resultPZ+"MSR_PORT="+selport+NEWLINE; //返回設備類型 }else if(sbname.equals("密碼鍵盤")){ resultTXT=resultTXT+"{PINPAD_PORT="+selport+"}"+NEWLINE; resultPZ=resultPZ+"PINPAD_PORT="+selport+NEWLINE; //返回設備類型 } } indent+=" "; } else if(localName.equals("Values")) { String tagName=attributes.getValue(0); String tagValue=locateValue(tagName); if(!tagValue.equals("")) { if (res[sel].equals(tag)){ resultXML=resultXML+indent+"<"+tagName+">"+tagValue+"</"+tagName+">"+NEWLINE; // resultTXT=resultTXT+"{"+tagName+"="+tagValue+"}"+NEWLINE; resultTXT=resultTXT+"{"+tagName+"="; f = 1; } } } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -