FileDocCategorySizeDatePackage
ParseSupport.javaAPI DocGlassfish v2 API11907Sat May 05 19:17:54 BST 2007org.apache.taglibs.standard.tag.common.xml

ParseSupport

public abstract class ParseSupport extends javax.servlet.jsp.tagext.BodyTagSupport

Support for tag handlers for <parse>, the XML parsing tag.

author
Shawn Bayern

Fields Summary
protected Object
xml
protected String
systemId
protected XMLFilter
filter
private String
var
private String
varDom
private int
scope
private int
scopeDom
private DocumentBuilderFactory
dbf
private DocumentBuilder
db
private TransformerFactory
tf
private TransformerHandler
th
Constructors Summary
public ParseSupport()

	super();
	init();
    
Methods Summary
public intdoEndTag()

      try {
	
	// set up our DocumentBuilder
        if (dbf == null) {
            dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            dbf.setValidating(false);
        }
        db = dbf.newDocumentBuilder();

	// if we've gotten a filter, set up a transformer to support it
	if (filter != null) {
            if (tf == null)
                tf = TransformerFactory.newInstance();
            if (!tf.getFeature(SAXTransformerFactory.FEATURE))
                throw new JspTagException(
		    Resources.getMessage("PARSE_NO_SAXTRANSFORMER"));
            SAXTransformerFactory stf = (SAXTransformerFactory) tf;
            th = stf.newTransformerHandler();
	}

	// produce a Document by parsing whatever the attributes tell us to use
	Document d;
	Object xmlText = this.xml;
	if (xmlText == null) {
	    // if the attribute was specified, use the body as 'xml'
	    if (bodyContent != null && bodyContent.getString() != null)
		xmlText = bodyContent.getString().trim();
	    else
		xmlText = "";
	}
	if (xmlText instanceof String)
	    d = parseStringWithFilter((String) xmlText, filter);
	else if (xmlText instanceof Reader)
	    d = parseReaderWithFilter((Reader) xmlText, filter);
	else
	    throw new JspTagException(
	        Resources.getMessage("PARSE_INVALID_SOURCE"));

	// we've got a Document object; store it out as appropriate
	// (let any exclusivity or other constraints be enforced by TEI/TLV)
	if (var != null)
	    pageContext.setAttribute(var, d, scope);
	if (varDom != null)
	    pageContext.setAttribute(varDom, d, scopeDom);

	return EVAL_PAGE;
      } catch (SAXException ex) {
	throw new JspException(ex);
      } catch (IOException ex) {
	throw new JspException(ex);
      } catch (ParserConfigurationException ex) {
	throw new JspException(ex);
      } catch (TransformerConfigurationException ex) {
	throw new JspException(ex);
      }
    
private voidinit()

	var = varDom = null;
	xml = null;
	systemId = null;
	filter = null;
	dbf = null;
	db = null;
	tf = null;
	th = null;
	scope = PageContext.PAGE_SCOPE;
	scopeDom = PageContext.PAGE_SCOPE;
    
private org.w3c.dom.DocumentparseInputSource(org.xml.sax.InputSource s)
Parses the given InputSource into a Document.

	db.setEntityResolver(new JstlEntityResolver(pageContext));

        // normalize URIs so they can be processed consistently by resolver
        if (systemId == null)
            s.setSystemId("jstl:");
	else if (ImportSupport.isAbsoluteUrl(systemId))
            s.setSystemId(systemId);
        else
            s.setSystemId("jstl:" + systemId);
	return db.parse(s);
    
private org.w3c.dom.DocumentparseInputSourceWithFilter(org.xml.sax.InputSource s, org.xml.sax.XMLFilter f)
Parses the given InputSource after, applying the given XMLFilter.

	if (f != null) {
            // prepare an output Document
            Document o = db.newDocument();

            // use TrAX to adapt SAX events to a Document object
            th.setResult(new DOMResult(o));
            XMLReader xr = XMLReaderFactory.createXMLReader();
	    xr.setEntityResolver(new JstlEntityResolver(pageContext));
            //   (note that we overwrite the filter's parent.  this seems
            //    to be expected usage.  we could cache and reset the old
            //    parent, but you can't setParent(null), so this wouldn't
            //    be perfect.)
            f.setParent(xr);
            f.setContentHandler(th);
            f.parse(s);
            return o;
	} else
	    return parseInputSource(s);	
    
private org.w3c.dom.DocumentparseReader(java.io.Reader r)
Parses the given Reader into a Document.

        return parseInputSource(new InputSource(r));
    
private org.w3c.dom.DocumentparseReaderWithFilter(java.io.Reader r, org.xml.sax.XMLFilter f)
Parses the given Reader after applying the given XMLFilter.

	return parseInputSourceWithFilter(new InputSource(r), f);
    
private org.w3c.dom.DocumentparseString(java.lang.String s)
Parses the given String into a Document.

        StringReader r = new StringReader(s);
        return parseReader(r);
    
private org.w3c.dom.DocumentparseStringWithFilter(java.lang.String s, org.xml.sax.XMLFilter f)
Parses the given String after applying the given XMLFilter.

        StringReader r = new StringReader(s);
        return parseReaderWithFilter(r, f);
    
private org.w3c.dom.DocumentparseURL(java.lang.String url)
Parses the URL (passed as a String) into a Document.

	return parseInputSource(new InputSource(url));
    
private org.w3c.dom.DocumentparseURLWithFilter(java.lang.String url, org.xml.sax.XMLFilter f)
Parses the given Reader after applying the given XMLFilter.

	return parseInputSourceWithFilter(new InputSource(url), f);
    
public voidrelease()

	init();
    
public voidsetScope(java.lang.String scope)

	this.scope = Util.getScope(scope);
    
public voidsetScopeDom(java.lang.String scopeDom)

	this.scopeDom = Util.getScope(scopeDom);
    
public voidsetVar(java.lang.String var)

	this.var = var;
    
public voidsetVarDom(java.lang.String varDom)

	this.varDom = varDom;