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

📄 filehandler.java

📁 一款即时通讯软件
💻 JAVA
字号:
/*
 *   License
 *
 * The contents of this file are subject to the Jabber Open Source License
 * Version 1.0 (the "License").  You may not copy or use this file, in either
 * source code or executable form, except in compliance with the License.  You
 * may obtain a copy of the License at http://www.jabber.com/license/ or at
 * http://www.opensource.org/.  
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 *   Copyrights
 *
 * Portions created by or assigned to Jabber.com, Inc. are 
 * Copyright (c) 2000 Jabber.com, Inc.  All Rights Reserved.  Contact
 * information for Jabber.com, Inc. is available at http://www.jabber.com/.
 *
 * Portions Copyright (c) 1999-2000 David Waite 
 *
 *   Acknowledgements
 * 
 * Special thanks to the Jabber Open Source Contributors for their
 * suggestions and support of Jabber.
 * 
 *   Changes
 *
 * dwaite 20001007
 * Changed reliance on ProtocolHandler to be a reliance on ConnectionBean.
 * This will probably change in the near future so that it instead receives
 * a reference to an accessor class. 
 * @author  David Waite <a href="mailto:dwaite@jabber.com">
 *                      <i>&lt;dwaite@jabber.com&gt;</i></a>
 * @author  $Author: chris $
 * @version $Revision: 1.1 $
 *
 * j.komzak
 * Changed to provide access to files.
 */

package edu.ou.kmi.buddyspace.xml;

/*
 * FileHandler.java
 *
 * Project: BuddySpace
 * (C) Copyright Knowledge Media Institute 2002
 *
 *
 * Created on 28 August 2002, 10:50
 */

import java.io.*;

import org.jabber.jabberbeans.*;
import org.jabber.jabberbeans.sax.*;

import org.xml.sax.*;

/**
 * <code>FileHandler</code> provides XML storing/reading in/from files.
 * It relies on <code>XMLStreamDocumentHandler</code> and
 * a <code>Parser</code>.
 *
 * @author  Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
 */
public class FileHandler implements XMLFileDocInterface {
    
    private InputStream in = null;
    private String parsername;
    private boolean parsingDone;
    private static final String CHANGE_PARSER_TOO_LATE =
	"You must set the parser before connecting.";
    private static final String DEFAULT_PARSER = "com.microstar.xml.SAXDriver";
    private XMLFileDocInterface listener = null;
    
    /** Constructor */
    public FileHandler() {
	parsername = DEFAULT_PARSER;
    }
    
    /** Sets input stream */
    public void setInputStream(InputStream in)
    {
	this.in = in;
        parsingDone = false;
    }
    
    /** Sets listener */
    public void setListener(XMLFileDocInterface listener) {
        this.listener = listener;
    }
    
    /** Runs the handler */
    public final void run()
    {
	Parser parser;
	XMLFileDocHandler docHandler;
	
	if (in == null)
	    throw new RuntimeException
		("attempting to start InputStreamHandler without input to " +
		 "monitor");
	try {
	    parser = (Parser)Class.forName(parsername).newInstance();
	}
	catch (Exception e) {
	    throw new RuntimeException("InputStreamHandler:"+e.toString());
	}

	docHandler=new XMLFileDocHandler();
	docHandler.setDataHandler(this);

	((SubHandler)docHandler).setParser(parser);

	parser.setDocumentHandler(docHandler);

	try
	{
	    final InputSource is = new InputSource(new InputStreamReader(in,"UTF8"));

	    parser.parse(is);
	}
	catch (IOException e)
	{
	    if (parsingDone == true)
		return;

	    unexpectedThreadDeath(e);
	}
	catch (SAXException e)
	{
	    if (parsingDone == true)
		return;
	    unexpectedThreadDeath(e);
	}

	parsingDone = true;
    }
    
    /** Shuts down */
    public void shutdown() {
	parsingDone = true;
    }

    /** Sets parser */
    public void setParser(String parsername) {
	if (in != null)
	    throw new RuntimeException(CHANGE_PARSER_TOO_LATE);
	this.parsername = parsername;
    }

    /** Returns parser */
    public String getParser() {
	return parsername;
    }
    
    // *** implementation of interface ***
    
    /** Called when xml data received - sets data in listener */
    public void received(XMLData d) {
        if (listener != null)
            listener.received(d);
    }
    
    /** Called on unexpected thread death */
    public void unexpectedThreadDeath(Exception e) {
        if (listener != null)
            listener.unexpectedThreadDeath(e);
    }
    
}

⌨️ 快捷键说明

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