//
// SearchXMLHandler.java
// VideoAnnotate
//
// Created by Michael D. Fischer on 21/10/2006.
// Copyright (c) 2006, Centre for Social Anthropology and Computing,
// University of Kent. All rights reserved.
//
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// Neither the name of the Centre for Social Anthropology and Computing,
// University of Kent nor the names of its contributors may be used
// to endorse or promote products derived from this software without
// specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE
// COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
// BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
// OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// HandlerBase.java: Simple base class for AElfred processors.
// NO WARRANTY! See README, and copyright below.
// $Id: HandlerBase.java,v 2.2 1998/05/02 17:17:10 david Exp $
// package com.microstar.xml;
import com.microstar.xml.XmlHandler;
import com.microstar.xml.XmlParser;
import com.microstar.xml.XmlException;
import java.io.Reader;
import java.util.*; // Stack, Vector
/**
* Convenience base class for AElfred handlers.
* <p>This base class implements the XmlHandler interface with
* (mostly empty) default handlers. You are not required to use this,
* but if you need to handle only a few events, you might find
* it convenient to extend this class rather than implementing
* the entire interface. This example overrides only the
* <code>charData</code> method, using the defaults for the others:
* <pre>
* import com.microstar.xml.HandlerBase;
*
* public class MyHandler extends HandlerBase {
* public void charData (char ch[], int start, int length)
* {
* System.out.println("Data: " + new String (ch, start, length));
* }
* }
* </pre>
* <p>This class is optional, but if you use it, you must also
* include the <code>XmlException</code> class.
* <p>Do not extend this if you are using SAX; extend
* <code>org.xml.sax.HandlerBase</code> instead.
* @author Copyright (c) 1998 by Microstar Software Ltd.
* @author written by David Megginson <dmeggins@microstar.com>
* @version 1.1
* @see XmlHandler
* @see XmlException
* @see org.xml.sax.HandlerBase
*/
public class SearchXMLHandler implements XmlHandler {
//Stack termListStack = new Stack();
//Stack termStack = new Stack();
//TransferKinInfoVector kinTermVector=null;
public Preferences prefs=null;
boolean attributes=false;
SearchXML master = null;
Stack masters = new Stack();
Stack data = new Stack();
//Term rootTerm = null;
public SearchXMLHandler(SearchXML m) {
super();
master = m;
}
public void pushMaster(SearchXML d) {
masters.push(master);
master = d;
}
public SearchXML popMaster() {
master = (SearchXML) masters.pop();
return master;
}
public void processResult(String tag, Object result) { // dispatch to next element above present master
popMaster().searchXMLProcess(tag,result);
}
public void dispatchTo(SearchXML newMaster, String tag, Hashtable attributes) { // dispatch to next element above present master
pushMaster(newMaster);
newMaster.searchXMLStart(this, tag, attributes);
}
public void pushData(Object d) {
data.push(d);
}
public Object popData() {
return data.pop();
}
/**
* Handle the start of the document.
* <p>The default implementation does nothing.
* @see com.microstar.xml.XmlHandler#startDocument
* @exception java.lang.Exception Derived methods may throw exceptions.
*/
public void startDocument ()
throws java.lang.Exception
{
}
/**
* Handle the end of the document.
* <p>The default implementation does nothing.
* @see com.microstar.xml.XmlHandler#endDocument
* @exception java.lang.Exception Derived methods may throw exceptions.
*/
public void endDocument () throws java.lang.Exception {
}
/**
* Resolve an external entity.
* <p>The default implementation simply returns the supplied
* system identifier.
* @see com.microstar.xml.XmlHandler#resolveEntity
* @exception java.lang.Exception Derived methods may throw exceptions.
*/
public Object resolveEntity (String publicId, String systemId)
throws java.lang.Exception
{
return null;
}
/**
* Handle the start of an external entity.
* <p>The default implementation does nothing.
* @see com.microstar.xml.XmlHandler#startExternalEntity
* @exception java.lang.Exception Derived methods may throw exceptions.
*/
public void startExternalEntity (String systemId)
throws java.lang.Exception
{
}
/**
* Handle the end of an external entity.
* <p>The default implementation does nothing.
* @see com.microstar.xml.XmlHandler#endExternalEntity
* @exception java.lang.Exception Derived methods may throw exceptions.
*/
public void endExternalEntity (String systemId)
throws java.lang.Exception
{
}
/**
* Handle a document type declaration.
* <p>The default implementation does nothing.
* @see com.microstar.xml.XmlHandler#doctypeDecl
* @exception java.lang.Exception Derived methods may throw exceptions.
*/
public void doctypeDecl (String name, String publicId, String systemId)
throws java.lang.Exception
{
}
/**
* Handle an attribute assignment.
* <p>The default implementation does nothing.
* @see com.microstar.xml.XmlHandler#attribute
* @exception java.lang.Exception Derived methods may throw exceptions.
*/
private static String attributeLabel = "attributes";
Vector attributeNames = new Vector(1);
Vector attributeValues = new Vector(1);
Hashtable attributeTable = new Hashtable();
public void attribute (String aname, String value, boolean isSpecified)
throws java.lang.Exception
{
attributeNames.addElement(aname);
attributeValues.addElement(value);
attributeTable.put(aname,value);
attributes = true;
}
//TransferKinInfo currentEntry=null;
String currKey=null;
StringVector sv = null;
boolean inStringVector = false;
/**
* Handle the start of an element.
* <p>The default implementation does nothing.
* @see com.microstar.xml.XmlHandler#startElement
* @exception java.lang.Exception Derived methods may throw exceptions.
*/
public void startElement (String elname)
throws java.lang.Exception
{
if (contentData != null && !contentData.trim().equals("")) attributeTable.put("strayText",contentData.trim());
contentData = "";
master.searchXMLStart(this,elname,attributeTable);
attributeNames = new Vector(1);
attributeValues = new Vector(1);
attributeTable = new Hashtable();
attributes = false;
}
/**
* Handle the end of an element.
* <p>The default implementation does nothing.
* @see com.microstar.xml.XmlHandler#endElement
* @exception java.lang.Exception Derived methods may throw exceptions.
*/
public void endElement (String elname)
throws java.lang.Exception
{
contentData = contentData.trim();
master.searchXMLEnd(this,elname,contentData);
contentData = "";
}
String contentData = "";
/**
* Handle character data.
* <p>The default implementation does nothing.
* @see com.microstar.xml.XmlHandler#charData
* @exception java.lang.Exception Derived methods may throw exceptions.
*/
public void charData (char ch[], int start, int length)
throws java.lang.Exception
{
String tdata = new String(ch,start,length);
contentData += tdata;
}
/**
* Handle ignorable whitespace.
* <p>The default implementation does nothing.
* @see com.microstar.xml.XmlHandler#ignorableWhitespace
* @exception java.lang.Exception Derived methods may throw exceptions.
*/
public void ignorableWhitespace (char ch[], int start, int length)
throws java.lang.Exception
{
}
/**
* Handle a processing instruction.
* <p>The default implementation does nothing.
* @see com.microstar.xml.XmlHandler#processingInstruction
* @exception java.lang.Exception Derived methods may throw exceptions.
*/
public void processingInstruction (String target, String data)
throws java.lang.Exception
{
}
/**
* Throw an exception for a fatal error.
* <p>The default implementation throws <code>XmlException</code>.
* @see com.microstar.xml.XmlHandler#error
* @exception com.microstar.xml.XmlException A specific parsing error.
* @exception java.lang.Exception Derived methods may throw exceptions.
*/
public void error (String message, String systemId, int line, int column)
throws XmlException, java.lang.Exception
{
throw new XmlException(message, systemId, line, column);
}
public void getRootTerm() {
//return rootTerm;
}
/**
* Start a parse in application mode.
* <p>Output will go to STDOUT.
* @see #displayText
* @see com.microstar.xml.XmlParser#run
*/
void doParse (XFile xr)
throws java.lang.Exception
{
//String docURL = makeAbsoluteURL(url);
// create the parser
// System.out.println("File: "+xr.aFile.toString());
XmlParser parser = new XmlParser();
parser.setHandler(this);
parser.parse(null, null, xr.diStream);
}
}