?? xmlreader.java
字號:
/* * Copyright 2004 Grzegorz Grasza groz@gryf.info * * This file is part of mobber. Mobber is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) any later version. * Mobber is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. * You should have received a copy of the GNU General Public License along with mobber; * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA . */package utils;import java.io.*;import java.util.*;public class XmlReader extends InputStreamReader{ public final static int START_DOCUMENT = 0; public final static int END_DOCUMENT = 1; public final static int START_TAG = 2; public final static int END_TAG = 3; public final static int TEXT = 4; private Stack tags; private boolean inside_tag; private String tagName; private String text; private Hashtable attributes = new Hashtable(); private int c; private int type = START_DOCUMENT; public XmlReader(InputStream in) throws IOException, UnsupportedEncodingException { super(in, "UTF-8"); tags = new Stack(); inside_tag = false; } public int next() throws IOException { c = read(); if(c <= ' ') // omit white space while((c = read()) <= ' ' && c!=-1); if(c==-1) { type = END_DOCUMENT; return type; } if(c == '<' || (c == '/' && !inside_tag)) { inside_tag = true; // reset all tagName = null; text = null; attributes.clear(); if(c == '<') c = read(); if(c == '/') { type = END_TAG; c = read(); tagName = readName('>'); } else if(c == '?' || c == '!') // ignore xml heading & comments { while((c = read()) != '>'); next(); } else { type = START_TAG; tagName = readName(' '); String attribute = ""; String value = ""; while(c == ' ') { c = read(); attribute = readName('='); c = read(); // ''' c = read(); value = readText('\''); c = read(); attributes.put(attribute, value); } if(c != '/') inside_tag = false; } } else if(c == '>' && inside_tag) // last tag ended { type = END_TAG; inside_tag = false; } else { tagName = null; attributes.clear(); type = TEXT; text = readText('<'); } return type; } public int getType() { return type; } public String getName() { return tagName; } public String getAttribute(String name) { return (String)attributes.get(name); } public Enumeration getAttributes() { return attributes.keys(); } public String getText() { return text; } private String readText(int end) throws IOException { StringBuffer output = new StringBuffer(""); while(c != end) { if(c == '&') { c = read(); switch(c) { case 'l': output.append('<'); break; case 'g': output.append('>'); break; case 'a': if(read()=='m') output.append('&'); else output.append('\''); break; case 'q': output.append('"'); break; case 'n': output.append(' '); break; default: output.append('?'); } while((c = read()) != ';'); } else if(c == '\\') { if((c = read()) == '<') break; else output.append((char) c); } else output.append((char) c); c = read(); } //while((c = read()) != end); return output.toString(); } private String readName(int end) throws IOException { StringBuffer output = new StringBuffer(""); do output.append((char) c); while((c = read()) != end && c != '>' && c != '/'); return output.toString(); }};
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -