我是程序员,多数时间和代码打交道,所以我的博客可能充满了代码。
开篇,XML解析器:
//===================EnumXmlEventType.java=========================//
- package jiamu.base.xml;
- public enum EnumXmlEventType {
- START_READ_HEADER(0),
- START_READ_ATTRIBUTER(1),
- START_READ_NODER(2),
- FINISH_READ_HEADER(3),
- FINISH_READ_ATTRIBUTER(4),
- FINISH_READ_NODER(5);
- public static EnumXmlStatus getEnum(int value) {
- for (EnumXmlStatus enumValue : EnumXmlStatus.values()) {
- if (enumValue.getValue() == value) {
- return enumValue;
- }
- }
- return null;
- }
- private int value = 0;
- private EnumXmlEventType(int value) {
- this.value = value;
- }
- /**
- * Gets the enumerate object's value.
- *
- * @return the position of the enumerate object
- */
- public int getValue() {
- return this.value;
- }
- }
//===================EnumXmlStatus.java============================//
- package jiamu.base.xml;
- public enum EnumXmlStatus {
- UNKNOWN(0),
- STARTED(1),
- FINISHED(2),
- READVALUE(3),
- READTAG(4);
- public static EnumXmlStatus getEnum(int value) {
- for (EnumXmlStatus enumValue : EnumXmlStatus.values()) {
- if (enumValue.getValue() == value) {
- return enumValue;
- }
- }
- return null;
- }
- private int value = 0;
- private EnumXmlStatus(int value) {
- this.value = value;
- }
- /**
- * Gets the enumerate object's value.
- *
- * @return the position of the enumerate object
- */
- public int getValue() {
- return this.value;
- }
- }
//===================QXmlElement.java==============================//
- package jiamu.base.xml;
- public class QXmlElement {
- public String name = null;
- public EnumXmlStatus status = EnumXmlStatus.UNKNOWN;
- public QXmlElement() {
- this.name = "";
- this.status = EnumXmlStatus.UNKNOWN;
- }
- }
//===================QXmlAttributer.java===========================//
- package jiamu.base.xml;
- public class QXmlAttributer extends QXmlElement {
- public String value = null;
- public boolean lastAttributer = false;
- public QXmlAttributer() {
- super();
- this.value = "";
- }
- @Override
- public String toString() {
- return this.name + "=/"" + this.value + "/"";
- }
- }
//===================QXmlHeader.java===============================//
- package jiamu.base.xml;
- import java.util.Vector;
- import jiamu.base.functions.SystemFunctions;
- public class QXmlHeader extends QXmlElement {
- public Vector<QXmlAttributer> attributers = null;
- public QXmlHeader() {
- super();
- this.attributers = new Vector<QXmlAttributer>();
- }
- @Override
- public String toString() {
- StringBuilder builder = new StringBuilder();
- builder.append("<?" + this.name);
- for (int i = 0; i < this.attributers.size(); i++) {
- builder.append(" ");
- builder.append(this.attributers.get(i));
- }
- builder.append(" ?>");
- builder.append(SystemFunctions.getLineFeed());
- return builder.toString();
- }
- }
//===================QXmlNoder.java================================//
- package jiamu.base.xml;
- import java.util.Vector;
- import jiamu.base.functions.StringFunctions;
- import jiamu.base.functions.SystemFunctions;
- public class QXmlNoder extends QXmlElement {
- public Vector<QXmlAttributer> attributers = null;
- public Vector<QXmlNoder> noders = null;
- public Vector<QXmlHeader> headers = null;
- public String value = null;
- public QXmlNoder() {
- super();
- this.value = "";
- this.attributers = new Vector<QXmlAttributer>();
- this.noders = new Vector<QXmlNoder>();
- this.headers = new Vector<QXmlHeader>();
- }
- public String getAttributerValue(String name) {
- String result = "";
- for (int i = 0; i < this.attributers.size(); i++) {
- QXmlAttributer xmlAttributer = this.attributers.get(i);
- if (xmlAttributer.name.compareToIgnoreCase(name) == 0) {
- result = xmlAttributer.value;
- break;
- }
- }
- return result;
- }
- public QXmlNoder getNoder(String name) {
- QXmlNoder xmlNoder = null;
- for (int i = 0; i < this.noders.size(); i++) {
- if (this.noders.get(i).name.compareToIgnoreCase(name) == 0) {
- xmlNoder = this.noders.get(i);
- break;
- }
- }
- return xmlNoder;
- }
- public int getNoderCount() {
- return this.noders.size();
- }
- @Override
- public String toString() {
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < this.headers.size(); i++) {
- builder.append(this.headers.get(i));
- }
- builder.append("<" + this.name);
- for (int i = 0; i < this.attributers.size(); i++) {
- builder.append(" ");
- builder.append(this.attributers.get(i));
- }
- builder.append(">");
- builder.append(SystemFunctions.getLineFeed());
- for (int i = 0; i < this.noders.size(); i++) {
- builder.append(this.noders.get(i));
- }
- if (!StringFunctions.isEmpty(this.value)) {
- builder.append(this.value);
- builder.append(SystemFunctions.getLineFeed());
- }
- builder.append("</" + this.name + ">");
- builder.append(SystemFunctions.getLineFeed());
- return builder.toString();
- }
- }
//===================QXmlStreamEvent.java==========================//
- package jiamu.base.xml;
- public class QXmlStreamEvent {
- public EnumXmlEventType xmlEventType = null;
- public QXmlNoder currentNoder = null;
- public QXmlAttributer currentAttributer = null;
- public QXmlHeader currentHeader = null;
- public QXmlNoder wholeNoder = null;
- public QXmlStreamEvent(EnumXmlEventType xmlEventType, QXmlNoder currentNoder, QXmlAttributer currentAttributer, QXmlHeader currentHeader, QXmlNoder wholeNoder) {
- this.xmlEventType = xmlEventType;
- this.currentNoder = currentNoder;
- this.currentAttributer = currentAttributer;
- this.currentHeader = currentHeader;
- this.wholeNoder = wholeNoder;
- }
- @Override
- public String toString() {
- return this.currentNoder.toString();
- }
- }
//===================QXmlStreamEventListener.java==================//
- package jiamu.base.xml;
- public interface QXmlStreamEventListener {
- public void eventHappened(QXmlStreamEvent xmlFileEvent);
- }
//===================QXmlStream.java===============================//
- package jiamu.base.xml;
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.net.URL;
- import java.util.Vector;
- import jiamu.base.exceptions.QException;
- public class QXmlStream {
- private static int ReadAheadLimit = 10;
- private Vector<QXmlStreamEventListener> listeners = null;
- private QXmlNoder baseNoder = null;
- private BufferedReader in = null;
- public QXmlStream(BufferedReader bufferedReader) {
- this.listeners = new Vector<QXmlStreamEventListener>();
- this.in = bufferedReader;
- }
- public QXmlStream(String fileName) throws UnsupportedEncodingException, FileNotFoundException {
- this(new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8")));
- }
- public QXmlStream(URL url) throws IOException {
- this(new BufferedReader(new InputStreamReader(url.openConnection().getInputStream(), "UTF-8")));
- }
- public void addQXmlFileEventListener(QXmlStreamEventListener l) {
- this.listeners.add(l);
- }
- public QXmlNoder getBaseNoder() {
- return this.baseNoder;
- }
- private void invokeEvent(QXmlStreamEvent xmlFileEvent) {
- for (int i = 0; i < this.listeners.size(); i++) {
- this.listeners.get(i).eventHappened(xmlFileEvent);
- }
- }
- public boolean parse() throws QException {
- try {
- this.baseNoder = new QXmlNoder();
- this.baseNoder.name = "QXMLFILE";
- int readCharacter = this.read();
- while (readCharacter != -1) {
- if (readCharacter == '<') {
- // it a starter of tag, follow is the tag.
- // try to read the next to see if it's an XML starter.
- readCharacter = this.read(true);
- if (readCharacter == '?') {
- // It's an XML header.
- QXmlHeader xmlHeader = this.parseHeader();
- if (xmlHeader != null) {
- this.baseNoder.headers.add(xmlHeader);
- }
- } else {
- // It's an XML node.
- this.reset();
- QXmlNoder xmlNoder = this.parseNoder();
- if (xmlNoder != null) {
- this.baseNoder.noders.add(xmlNoder);
- }
- }
- }
- readCharacter = this.read();
- }
- this.in.close();
- } catch (IOException ex) {
- throw new QException(ex, "QXmlFile", "parse", "");
- }
- return true;
- }
- private QXmlAttributer parseAttributer(QXmlHeader xmlHeader, QXmlNoder xmlNoder) throws QException {
- QXmlAttributer xmlAttributer = new QXmlAttributer();
- this.invokeEvent(new QXmlStreamEvent(EnumXmlEventType.START_READ_ATTRIBUTER, xmlNoder, xmlAttributer, xmlHeader, this.baseNoder));
- int readCharacter = this.read();
- boolean bQuoteSeparator = false;
- while ((readCharacter != -1) && (xmlAttributer.status != EnumXmlStatus.FINISHED)) {
- switch (xmlAttributer.status) {
- case UNKNOWN: {
- if (readCharacter != ' ') {
- xmlAttributer.name += (char) readCharacter;
- xmlAttributer.status = EnumXmlStatus.STARTED;
- }
- }
- break;
- case STARTED: {
- if (readCharacter != '=') {
- xmlAttributer.name += (char) readCharacter;
- } else {
- xmlAttributer.status = EnumXmlStatus.READVALUE;
- readCharacter = this.read();
- if (readCharacter == '"') {
- bQuoteSeparator = true;
- } else {
- xmlAttributer.value += (char) readCharacter;
- }
- }
- }
- break;
- case FINISHED:
- break;
- case READVALUE: {
- if ((readCharacter == '"') && bQuoteSeparator) {
- xmlAttributer.status = EnumXmlStatus.FINISHED;
- } else if ((readCharacter == ' ') && !bQuoteSeparator) {
- xmlAttributer.status = EnumXmlStatus.FINISHED;
- } else if (readCharacter == '?') {
- readCharacter = this.read();
- if (readCharacter == '>') {
- xmlAttributer.lastAttributer = true;
- }
- xmlAttributer.status = EnumXmlStatus.FINISHED;
- } else {
- xmlAttributer.value += (char) readCharacter;
- }
- }
- break;
- }
- if (xmlAttributer.status != EnumXmlStatus.FINISHED) {
- readCharacter = this.read();
- }
- }
- this.invokeEvent(new QXmlStreamEvent(EnumXmlEventType.FINISH_READ_ATTRIBUTER, xmlNoder, xmlAttributer, xmlHeader, this.baseNoder));
- return xmlAttributer;
- }
- private QXmlHeader parseHeader() throws QException {
- QXmlHeader xmlHeader = new QXmlHeader();
- this.invokeEvent(new QXmlStreamEvent(EnumXmlEventType.START_READ_HEADER, null, null, xmlHeader, this.baseNoder));
- int readCharacter = this.read();
- while ((readCharacter != -1) && (xmlHeader.status != EnumXmlStatus.FINISHED)) {
- switch (xmlHeader.status) {
- case UNKNOWN: {
- xmlHeader.name += (char) readCharacter;
- xmlHeader.status = EnumXmlStatus.READTAG;
- }
- break;
- case STARTED: {
- if (readCharacter == '?') {
- readCharacter = this.read();
- if (readCharacter == '>') {
- xmlHeader.status = EnumXmlStatus.FINISHED;
- break;
- }
- } else if (readCharacter != ' ') {
- this.reset();
- QXmlAttributer attributer = this.parseAttributer(xmlHeader, null);
- if (attributer != null) {
- xmlHeader.attributers.add(attributer);
- if (attributer.lastAttributer) {
- xmlHeader.status = EnumXmlStatus.FINISHED;
- }
- }
- }
- }
- break;
- case READTAG: {
- if (readCharacter == ' ') {
- xmlHeader.status = EnumXmlStatus.STARTED;
- } else {
- xmlHeader.name += (char) readCharacter;
- }
- }
- break;
- case FINISHED:
- break;
- case READVALUE:
- break;
- }
- if (xmlHeader.status != EnumXmlStatus.FINISHED) {
- readCharacter = this.read(true);
- }
- }
- this.invokeEvent(new QXmlStreamEvent(EnumXmlEventType.FINISH_READ_HEADER, null, null, xmlHeader, this.baseNoder));
- return xmlHeader;
- }
- private QXmlNoder parseNoder() throws QException {
- QXmlNoder xmlNoder = new QXmlNoder();
- this.invokeEvent(new QXmlStreamEvent(EnumXmlEventType.START_READ_NODER, xmlNoder, null, null, this.baseNoder));
- int readCharacter = this.read();
- while ((readCharacter != -1) && (xmlNoder.status != EnumXmlStatus.FINISHED)) {
- switch (xmlNoder.status) {
- case UNKNOWN: {
- xmlNoder.name += (char) readCharacter;
- xmlNoder.status = EnumXmlStatus.READTAG;
- }
- break;
- case READTAG: {
- if (readCharacter == ' ') {
- xmlNoder.status = EnumXmlStatus.STARTED;
- } else if (readCharacter == '>') {
- xmlNoder.status = EnumXmlStatus.READVALUE;
- break;
- } else {
- xmlNoder.name += (char) readCharacter;
- }
- }
- break;
- case STARTED: {
- if (readCharacter == '/') {
- readCharacter = this.read();
- if (readCharacter == '>') {
- xmlNoder.status = EnumXmlStatus.FINISHED;
- break;
- }
- } else if (readCharacter == '>') {
- xmlNoder.status = EnumXmlStatus.READVALUE;
- break;
- }
- this.reset();
- QXmlAttributer attributer = this.parseAttributer(null, xmlNoder);
- if (attributer != null) {
- xmlNoder.attributers.add(attributer);
- if (attributer.lastAttributer) {
- xmlNoder.status = EnumXmlStatus.READVALUE;
- }
- }
- }
- break;
- case FINISHED:
- break;
- case READVALUE: {
- if (readCharacter == '<') {
- readCharacter = this.read(true);
- if (readCharacter == '/') {
- while (readCharacter != '>') {
- readCharacter = this.read();
- if (readCharacter == -1) {
- break;
- }
- }
- xmlNoder.status = EnumXmlStatus.FINISHED;
- break;
- } else if (readCharacter == '!') {
- String cdataStart = "";
- cdataStart += (char) this.read();
- cdataStart += (char) this.read();
- cdataStart += (char) this.read();
- cdataStart += (char) this.read();
- cdataStart += (char) this.read();
- cdataStart += (char) this.read();
- cdataStart += (char) this.read();
- if (cdataStart.compareToIgnoreCase("[CDATA[") == 0) {
- // begin reading the CDATA
- readCharacter = this.read(true);
- while (readCharacter != -1) {
- if (readCharacter == ']') {
- String cdataEnd = "";
- cdataEnd += (char) this.read();
- cdataEnd += (char) this.read();
- if (cdataEnd.compareToIgnoreCase("]>") == 0) {
- // End reading the CDATA
- break;
- } else {
- this.reset();
- readCharacter = this.read(true);
- }
- }
- xmlNoder.value += (char) readCharacter;
- readCharacter = this.read(true);
- }
- break;
- }
- }
- this.reset();
- QXmlNoder xmlSubNoder = this.parseNoder();
- if (xmlSubNoder != null) {
- xmlNoder.noders.add(xmlSubNoder);
- }
- } else {
- xmlNoder.value += (char) readCharacter;
- }
- }
- break;
- }
- if (xmlNoder.status != EnumXmlStatus.FINISHED) {
- readCharacter = this.read(true);
- }
- }
- this.invokeEvent(new QXmlStreamEvent(EnumXmlEventType.FINISH_READ_NODER, xmlNoder, null, null, this.baseNoder));
- return xmlNoder;
- }
- private int read() throws QException {
- return this.read(false);
- }
- private int read(boolean marked) throws QException {
- try {
- if (marked) {
- this.in.mark(QXmlStream.ReadAheadLimit);
- }
- int result = this.in.read();
- return result;
- } catch (IOException ex) {
- throw new QException(ex, "QXmlFile", "read", "");
- }
- }
- public void removeXmlFileEventListener(QXmlStreamEventListener l) {
- this.listeners.remove(l);
- }
- private void reset() throws QException {
- try {
- this.in.reset();
- } catch (IOException ex) {
- throw new QException(ex, "QXmlFile", "reset", "");
- }
- }
- }
完。
这个博客编辑器真不好使。。难道和我的IE8有关。