【Java开源代码栏目提醒】:网学会员为广大网友收集整理了,XmlParser.java,希望对大家有所帮助!
package com.mortbay.Util;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import org.xml.sax.AttributeList;
import org.xml.sax.HandlerBase;
import org.xml.sax.InputSource;
import org.xml.sax.Parser;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.ParserFactory;
/*--------------------------------------------------------------*/
/** XML Parser wrapper.
* This class wraps any standard sax parser with convieniant error and
* entity handlers and a mini dom-like document tree.
*
* @version $Id: XmlParser.java,v 1.14 2000/12/14 22:50:48 gregwilkins Exp $
* @author Greg Wilkins (gregw)
*/
public class XmlParser
{
private Map _redirectMap = new HashMap();
private Parser _parser;
/* ------------------------------------------------------------ */
/** Constructor.
* @exception ParserConfigurationException
*/
public XmlParser()
{
try
{
_parser =ParserFactory.makeParser
(System.getProperty("org.xml.sax.parser",
"com.microstar.xml.SAXDriver"));
}
catch(Exception e)
{
Code.warning(e);
throw new Error(e.toString());
}
}
/* ------------------------------------------------------------ */
/**
* @param url
* @return
* @exception IOException
* @exception SAXException
*/
public synchronized Node parse(String url)
throws IOException,SAXException
{
Handler handler= new Handler();
_parser.setDocumentHandler(handler);
_parser.setErrorHandler(handler);
_parser.setEntityResolver(handler);
_parser.parse(url);
if (handler._error!=null)
throw handler._error;
Node doc=(Node)handler._top.get(0);
handler.clear();
return doc;
}
/* ------------------------------------------------------------ */
/**
* @param file
* @return
* @exception IOException
* @exception SAXException
*/
public synchronized Node parse(File file)
throws IOException,SAXException
{
return parse(file.toURL().toString());
}
/* ------------------------------------------------------------ */
/**
* @param url
* @return
* @exception IOException
* @exception SAXException
*/
public synchronized Node parse(InputStream in)
throws IOException,SAXException
{
Handler handler= new Handler();
_parser.setDocumentHandler(handler);
_parser.setErrorHandler(handler);
_parser.setEntityResolver(handler);
_parser.parse(new InputSource(in));
if (handler._error!=null)
throw handler._error;
Node doc=(Node)handler._top.get(0);
handler.clear();
return doc;
}
/* ------------------------------------------------------------ */
/**
* @param name
* @param local
*/
public synchronized void redirectEntity(String name,Resource entity)
{
if (entity!=null)
_redirectMap.put(name,entity);
}
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
private class Handler extends HandlerBase
{
Node _top = new Node(null,null,null);
SAXParseException _error;
private Node _context = _top;
/* ------------------------------------------------------------ */
void clear()
{
_top=null;
_error=null;
_context=null;
}
/* ------------------------------------------------------------ */
public void startElement (String tag, AttributeList attrs)
throws SAXException
{
Node node= new Node(_context,tag,attrs);
_context.add(node);
_context=node;
}
/* ------------------------------------------------------------ */
public void endElement (String tag)
throws SAXException
{
_context=_context._parent;
}
/* ------------------------------------------------------------ */
public void ignorableWhitespace (char buf [], int offset, int len)
throws SAXException
{
// XXX - for testing
// characters(buf,offset,len);
}
/* ------------------------------------------------------------ */
public void characters (char buf [], int offset, int len)
throws SAXException
{
_context.add(new String(buf,offset,len));
}
/* ------------------------------------------------------------ */
public void warning(SAXParseException ex)
{
Code.debug(ex);
Code.warning("WARNING@"+getLocationString(ex)+
" : "+ex.toString());
}
/* ------------------------------------------------------------ */
public void error(SAXParseException ex)
throws SAXException
{
// Save error and continue to report other errors
if(_error==null)
_error=ex;
Code.debug(ex);
Code.warning("ERROR@"+getLocationString(ex)+
" : "+ex.toString());
}
/* ------------------------------------------------------------ */
public void fatalError(SAXParseException ex)
throws SAXException
{
_error=ex;
Code.debug(ex);
Code.warning("FATAL@"+getLocationString(ex)+
" : "+ex.toString());
throw ex;
}
/* ------------------------------------------------------------ */
private String getLocationString(SAXParseException ex)
{
return ex.getSystemId()+
" line:"+ex.getLineNumber()+
" col:"+ex.getColumnNumber();
}
/* ------------------------------------------------------------ */
public InputSource resolveEntity
(String pid, String sid)
{
Resource resource=null;
if(pid!=null)
resource = (Resource)_redirectMap.get(pid);
if(resource==null)
resource = (Resource)_redirectMap.get(sid);
if (resource==null)
{
String dtd = sid;
if (dtd.lastIndexOf("/")>=0)
dtd=dtd.substring(dtd.lastIndexOf("/")+1);
resource = (Resource)_redirectMap.get(dtd);
}
if (resource!=null && resource.exists())
{
try
{
InputStream in= resource.getInputStream();
Code.debug("Redirected entity ",
sid," --> ",resource);
return new InputSource(in);
}
catch(IOException e){Code.ignore(e);}
}
return null;
}
}
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/** XML Attribute.
*/
public static class Attribute
{
private String _name;
private String _value;
Attribute(String n,String v)
{
_name=n;
_value=v;
}
public String getName() {return _name;}
public String getValue() {return _value;}
}
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/** XML Node.
上一篇:
XmlParse.java
下一篇:
问卷调查可以自动生成试卷等,并且只能一个用户登录