/*
* $Id: DocbookListener.java,v 1.4 2006/03/16 03:04:06 ian Exp $
* $Name: gxparse-sf-alpha-2_0 $
*
* This file is part of Docbook Utilities, a set of XML tools for Docbook
* Copyright (C) 2003, 2004 Ian E. Gorman
*
* 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
* or go to http://www.gnu.org/
*
* Ian E. Gorman http://gorman.ca/
*/
package tohtml.common;
import ca.gorman.xml.parse.*;
import ca.gorman.xml.parse.util.PackageNamespaceElementMapper;
import java.io.*;
import java.util.ArrayList;
import javax.xml.namespace.QName;
import javax.xml.XMLConstants;
/**
*
*/
public class DocbookListener
extends AbstractErrorListener>
{
// element handler to send elements to appropriate methods in this class
private ElementMapper> elementMapper;
public DocbookListener(Writer writer)
throws IOException
{
super(writer);
ElementListener> unmappedElementHandler
= new UnmappedElementHandler>();
// unchecked conversion because cannot create array of generic type
PackageNamespaceElementMapper.NamespaceEntry>[]
namespaceEntry
= new PackageNamespaceElementMapper.NamespaceEntry[1];
namespaceEntry[0]
= new PackageNamespaceElementMapper
.NamespaceEntry>(
new QName(XMLConstants.NULL_NS_URI,""),
"tohtml.noname",
new tohtml.noname.DefaultHandler(),
"Element_");
try {
elementMapper
= new PackageNamespaceElementMapper>(
unmappedElementHandler,
namespaceEntry);
} catch(PackageNamespaceElementMapper.DuplicateNamespaceException dx) {
// won't happen in normal use
throw new ListenerRuntimeException(dx);
}
}
/**
* {@inheritDoc}.
*
* @throws ListenerException {@inheritDoc}
* @throws IOException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws ElementMapper.NotValidMethodException if a method is found with
* the correct name, but the method cannot be invoked as an
* element method.
*/
public void doElement(Parser parser, CurrentElement element)
throws ListenerException, IOException {
// find and invoke one of the element methods in this class
assert (elementMapper != null);
// generates "unchecked call" warning. PackageNamespaceElementMapper
// uses Java reflection to find the element method, and the generic
// type parameter cannot be checked at runtime.
elementMapper.doElement(parser, element);
}
// inherit javadoc
public void characters(final Parser parser,
final CharSequence charSequence)
throws ListenerException, IOException
{
Docbook docbook = parser.getGlobal();
// for HTML, convert '&', '<', '>' to entities, write
// other characters as themselves
for (int i = 0 ; i < charSequence.length() ; ++i) {
char chr = charSequence.charAt(i);
switch (chr) {
case '>':
docbook.writerStack.write(">");
break;
case '<':
docbook.writerStack.write("<");
break;
case '&':
docbook.writerStack.write("&");
break;
default:
docbook.writerStack.write(chr);
break;
}
}
}
}