⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xml2objectparser.java

📁 著名的开源仿真软件yale
💻 JAVA
字号:
/* *  YALE - Yet Another Learning Environment *  Copyright (C) 2002, 2003 *      Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,  *          Katharina Morik, Oliver Ritthoff *      Artificial Intelligence Unit *      Computer Science Department *      University of Dortmund *      44221 Dortmund,  Germany *  email: yale@ls8.cs.uni-dortmund.de *  web:   http://yale.cs.uni-dortmund.de/ * *  This program 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.  * *  This program 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 this program; if not, write to the Free Software *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *  USA. */package edu.udo.cs.yale.tools.xml;import edu.udo.cs.yale.tools.LogService;import java.io.*;import org.kxml.Attribute;import org.kxml.Xml;import org.kxml.parser.XmlParser;import org.kxml.parser.ParseEvent;import org.kxml.parser.AbstractXmlParser;import java.util.*;/** This class is for parsing XML strings. Therefore it uses the kxml package of *  Stephan Haustein which is enclosed in YALE. <br> *  The constructor get a set of string pairs, whereby the first item of the pair is the tag's *  name and the second the proper class name. The classes must extend TagObject. *  <BR> *  <UL> *  <LI>During parsing a XML string for each tag the constructor of the proper class is  *      invoked. Thereafter, the set <CODE>setTagName()</CODE> is called with the name  *      of the tag.</LI> *  <LI>For each attribute the <CODE>setAttribute()</CODE> is invoked.</LI> *  <LI>For each inner tag recursively a new TagObject is created and added with *      <CODE>addInnerTag()</CODE></LI> *  <LI>For text the <CODE>appendText()</CODE> method is invoked. *  </UL> *  @see edu.udo.cs.yale.tools.xml.TagObject */public class XML2ObjectParser {    /** Verbosity Level for XML errors. */    public int XML_VERBOSITY = LogService.ERROR;    /** The classes for the tag names. */    private Class[] tagClass;    /** The tag names for the classes. */    private String[] tagnames;    /** Current line number, only used for error messages. */    private int currentLineNumber;    /** Creates a new XML2ObjectParser. */    public  XML2ObjectParser(String[] tagnames, Class[] classes) {	this.tagnames = tagnames;	this.tagClass = classes;	for (int i = 0; i < tagnames.length; i++) {	    if (!TagObject.class.isAssignableFrom(tagClass[i])) {		throw new IllegalArgumentException(tagClass[i].getName()+" is not a TagObject!");	    }	}    }    /** Parse a tag object from the reader and returns the first tag object. */    public TagObject parseXML(Reader reader) throws XMLException, IOException {	AbstractXmlParser parser = null;	parser = new XmlParser(reader);	return parseTag(parser);    }    /** Parse a all tag objects from the reader and returns them as list. */    public List parseAll(Reader reader) throws XMLException, IOException {	AbstractXmlParser parser = null;	parser = new XmlParser(reader);	List result = new LinkedList();	TagObject tag = null;	do {	    tag = parseTag(parser);	    if (tag != null)		result.add(tag);	} while (tag != null);	return result;    }    /** Changes the parser to the TagObject suitable to the first containing Tag.     *  Comments are ignored. */    private TagObject parseTag(AbstractXmlParser parser) throws XMLException, IOException {	ParseEvent event = null;	event = parser.peek();	currentLineNumber = parser.getLineNumber();		switch (event.getType()) {	case Xml.TEXT:	    parser.read();	    currentLineNumber = parser.getLineNumber();		return new TextTagObject(event.getText());	case Xml.START_TAG:	    parser.read();	    currentLineNumber = parser.getLineNumber();	    for (int i = 0; i < tagnames.length; i++) {		if (event.getName().equals(tagnames[i])) {		    TagObject tagObject = null;		    try {			tagObject = (TagObject)tagClass[i].newInstance();		    } catch (Exception e) {			throw new XMLException(currentLineNumber+": Cannot instantiate '"+tagClass[i].getName()+"': " + e.getMessage());		    }		    tagObject.setTagName(event.getName());		    for (int j = 0; j < event.getAttributeCount(); j++) {			Attribute a = event.getAttribute(j);			tagObject.setAttribute(a.getName(), a.getValue());		    }		    TagObject inner = null;		    do {			inner = parseTag(parser);			if (inner != null) {			    if (inner instanceof TextTagObject) {				tagObject.appendText(((TextTagObject)inner).getText());				    			    } else {				tagObject.addInnerTag(inner);			    }			}		    } while (inner != null);		    event = parser.read();		    currentLineNumber = parser.getLineNumber();		    if (event.getType() != Xml.END_TAG) {			throw new XMLException(currentLineNumber+ ": Missing end tag!");		    }		    return tagObject;		}	    }	    throw new XMLException(currentLineNumber+": Unknown tag: "+event.getName());	    	case Xml.END_TAG:	    return null;	case Xml.WHITESPACE:	case Xml.COMMENT:	    parser.read();	    currentLineNumber = parser.getLineNumber();	    return parseTag(parser);	default:	    return null;	}    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -