/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2004 Danet GmbH (www.danet.de), BU BTS.
* All rights reserved.
*
* 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
*
* $Id: ParseTagSupport.java 2326 2007-03-27 21:59:44Z mlipp $
*
* $Log$
* Revision 1.4 2007/02/27 13:20:39 drmlipp
* Fixed logger.
*
* Revision 1.3 2006/09/29 12:32:12 drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.2 2005/09/05 09:41:48 drmlipp
* Synchronized with 1.3.2.
*
* Revision 1.1.2.1 2005/09/01 11:36:57 drmlipp
* Insert Tag workfing.
*
*/
package de.danet.an.util.jellytags;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.commons.jelly.JellyTagException;
import org.apache.commons.jelly.Script;
import org.apache.commons.jelly.TagSupport;
import org.apache.commons.jelly.XMLOutput;
import org.apache.commons.jelly.parser.XMLParser;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/**
* Provides an abstract base class for tags that use XML as input data.
*/
public abstract class ParseTagSupport extends TagSupport {
private static final org.apache.commons.logging.Log logger
= org.apache.commons.logging.LogFactory
.getLog(ParseTagSupport.class);
private XMLReader xmlReaderCache = null;
private XMLParser jellyParserCache = null;
private String text = null;
private Script script = null;
/**
* @return the XMLReader used for parsing, creating one lazily if need be
*/
public XMLReader getXMLReader() throws ParserConfigurationException, SAXException {
if (xmlReaderCache == null) {
xmlReaderCache = createXMLReader();
}
return xmlReaderCache;
}
/**
* @return XMLParser
*/
public XMLParser getJellyParser() {
if (jellyParserCache == null) {
jellyParserCache = createJellyParser();
}
return jellyParserCache;
}
/**
* Return the XML to be process by the tag as SAX events. The XML is
* taken from (in order of prcedence):
* <ul>
* <li>the <code>text</code> attribute</li>
* <li>the excution of a previously parsed script, referenced in
* attribute <code>script</code> (see <code>core:parse</code>)</li>
* <li>the tag's body</li>
* </ul>
* @throws SAXException if a problem occurs while processing
* the events
* @throws JellyTagException if a problem occurs parsing and
* executing the script
*/
public void getXML (XMLOutput output)
throws SAXException, JellyTagException {
// Any text?
if (text != null) {
parseText (output);
return;
}
try {
// a script?
if (script != null) {
script.run(context, output);
}
// Parse script into Jelly parser
ContentHandler handler = getJellyParser();
XMLOutput newOutput = new XMLOutput(handler);
handler.startDocument();
invokeBody(newOutput);
handler.endDocument();
// Run script to generate output
Script script = getJellyParser().getScript();
script.run(context, output);
} catch (JellyTagException e) {
if (e.getCause() instanceof SAXException) {
throw (SAXException)e.getCause();
} else {
throw e;
}
}
}
/**
* Parses the give piece of text as being markup
* @param output where to send the SAX events
*/
protected void parseText(XMLOutput output) throws SAXException {
if ( logger.isDebugEnabled() ) {
logger.debug( "About to parse: " + text );
}
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
SAXParser parser = factory.newSAXParser();
XMLReader reader = getXMLReader();
reader.setContentHandler(output);
reader.parse(new InputSource(new StringReader(text)));
} catch (ParserConfigurationException e) {
throw new SAXException(e);
} catch (IOException e) {
throw new SAXException(e);
}
}
/**
* Sets the XMLReader used for parsing
* @param xmlReader the reader to use.
*/
public void setXMLReader(XMLReader xmlReader) {
xmlReaderCache = xmlReader;
}
/**
* Factory method to create a new XMLReader
*/
protected XMLReader createXMLReader() throws ParserConfigurationException, SAXException {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
SAXParser parser = factory.newSAXParser();
return parser.getXMLReader();
}
/**
* Sets the Jelly parser.
* @param jellyParser The jellyParser to set
*/
public void setJellyParser(XMLParser jellyParser) {
jellyParserCache = jellyParser;
}
/**
* Factory method to create a new Jelly parser
* @return XMLParser
*/
protected XMLParser createJellyParser() {
XMLParser answer = new XMLParser();
answer.setContext(context);
return answer;
}
/**
* Sets the text to be parsed.
* @param text The text to be parsed by this parser
*/
public void setText(String text) {
this.text = text;
}
/**
* @param script The script to set.
*/
public void setScript(Script script) {
this.script = script;
}
}