org.apache.cocoon.generation
Class XPathTraversableGenerator

java.lang.Object
  extended byorg.apache.cocoon.util.AbstractLogEnabled
      extended byorg.apache.cocoon.xml.AbstractXMLProducer
          extended byorg.apache.cocoon.generation.AbstractGenerator
              extended byorg.apache.cocoon.generation.ServiceableGenerator
                  extended byorg.apache.cocoon.generation.TraversableGenerator
                      extended byorg.apache.cocoon.generation.XPathTraversableGenerator
All Implemented Interfaces:
org.apache.cocoon.caching.CacheableProcessingComponent, Disposable, org.apache.cocoon.generation.Generator, Poolable, Recyclable, Serviceable, org.apache.cocoon.sitemap.SitemapModelComponent, org.apache.cocoon.xml.XMLProducer

public class XPathTraversableGenerator
extends TraversableGenerator

Generates an XML collection listing performing XPath queries on XML sources.

It can be used both as a plain TraversableGenerator or, if an XPath is specified, it will perform an XPath query on every XML resource, where "xml resource" is, by default, any resource ending with ".xml", which can be overriden by setting the (regexp) pattern "xmlFiles as a sitemap parameter, or where the name of the resource has a container-wide mime-type mapping to 'text/xml' such as specified by mime-mapping elements in a web.xml descriptor file. The XPath can be specified in two ways:

  1. By using an XPointerish syntax in the URL: everything following the pound sign (possiby preceding query string arguments) will be treated as the XPath;
  2. Specifying it as a sitemap parameter named "xpath"
Sample usage: Sitemap: <map:match pattern="documents/**"> <map:generate type="xpathdirectory" src=" docs/{1}#/article/title|/article/abstract" > < map:parameter name="xmlFiles" value="\.xml$"/> </map:generate> <map: serialize type="xml" /> </map:match> Request: http://www.some.host/documents/test Result: <collection:collection name="test" lastModified="1010400942000" date="1/7/02 11:55 AM" requested="true" xmlns:collection="http://apache.org/cocoon/collection/1.0"> <collection:collection name="subdirectory" lastModified="1010400942000" date="1/7/02 11:55 AM" /> <collection:resource name="test.xml" lastModified="1011011579000" date="1/14/02 1:32 PM"> <collection:xpath docid="test.xml" query="/article/title"> <title>This is a test document</title> <abstract> <para>Abstract of my test article</para> </abstract> </collection:xpath> </collection:resource> <collection:resource name="test.gif" lastModified="1011011579000" date="1/14/02 1:32 PM"> </collection:collection> If you need to use namespaces, you can set them as sitemap parameters in the form: lt;map:parameter name="xmlns:your prefix" value="nsURI"/**">

Version:
$Id: XPathTraversableGenerator.html 1304280 2012-03-23 11:18:01Z ilgrosso $

Field Summary
protected  org.apache.cocoon.environment.Context context
          The cocoon context used for mime-type mappings
protected  Document doc
          The document that should be parsed and (partly) included.
protected  org.apache.cocoon.core.xml.DOMParser parser
          The parser for the XML snippets to be included.
protected  org.apache.cocoon.generation.XPathTraversableGenerator.XPathPrefixResolver prefixResolver
          The prefix resolver for namespaced queries
protected  XPathProcessor processor
          The XPath processor.
protected static String QUERY_ATTR_NAME
          Attribute for the XPath query.
protected static String RESULT_DOCID_ATTR
          The document containing a successful XPath query
protected  RE xmlRE
          The regular expression for the XML files pattern.
protected  String xpath
          The XPath.
protected static String XPATH_NODE_NAME
          Local name for the element that contains the included XML snippet.
 
Fields inherited from class org.apache.cocoon.generation.TraversableGenerator
attributes, cacheKeyParList, COL_NODE_NAME, DATE_ATTR_NAME, dateFormatter, depth, excludeRE, includeRE, isRequestedSource, LASTMOD_ATTR_NAME, PREFIX, refreshDelay, RES_NAME_ATTR_NAME, RESOURCE_NODE_NAME, reverse, rootRE, SIZE_ATTR_NAME, sort, URI, URI_ATTR_NAME, validity
 
Fields inherited from class org.apache.cocoon.generation.ServiceableGenerator
manager
 
Fields inherited from class org.apache.cocoon.generation.AbstractGenerator
objectModel, parameters, resolver, source
 
Fields inherited from class org.apache.cocoon.xml.AbstractXMLProducer
contentHandler, EMPTY_CONTENT_HANDLER, lexicalHandler, xmlConsumer
 
Fields inherited from interface org.apache.cocoon.generation.Generator
ROLE
 
Constructor Summary
XPathTraversableGenerator()
           
 
Method Summary
protected  void addContent(TraversableSource source)
          Allow subclasses a chance to generate additional elements within collection and resource elements.
 void dispose()
           
protected  boolean isXML(TraversableSource path)
          Determines if a given TraversableSource shall be handled as XML.
protected  void performXPathQuery(TraversableSource in)
          Performs an XPath query on the source.
 void recycle()
          Recycle resources
 void service(ServiceManager manager)
           
 void setParser(org.apache.cocoon.core.xml.DOMParser parser)
           
 void setup(org.apache.cocoon.environment.SourceResolver resolver, Map objectModel, String src, Parameters par)
          Set the request parameters.
 void setXPathProcessor(XPathProcessor processor)
           
 
Methods inherited from class org.apache.cocoon.generation.TraversableGenerator
addAncestorPath, addPath, endNode, generate, getAncestors, getKey, getValidity, isExcluded, isIncluded, isRoot, setNodeAttributes, startNode
 
Methods inherited from class org.apache.cocoon.xml.AbstractXMLProducer
setConsumer, setContentHandler, setLexicalHandler
 
Methods inherited from class org.apache.cocoon.util.AbstractLogEnabled
getLogger, setLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.cocoon.xml.XMLProducer
setConsumer
 

Field Detail

XPATH_NODE_NAME

protected static final String XPATH_NODE_NAME
Local name for the element that contains the included XML snippet.

See Also:
Constant Field Values

QUERY_ATTR_NAME

protected static final String QUERY_ATTR_NAME
Attribute for the XPath query.

See Also:
Constant Field Values

RESULT_DOCID_ATTR

protected static final String RESULT_DOCID_ATTR
The document containing a successful XPath query

See Also:
Constant Field Values

xmlRE

protected RE xmlRE
The regular expression for the XML files pattern.


parser

protected org.apache.cocoon.core.xml.DOMParser parser
The parser for the XML snippets to be included.


doc

protected Document doc
The document that should be parsed and (partly) included.


xpath

protected String xpath
The XPath.


processor

protected XPathProcessor processor
The XPath processor.


prefixResolver

protected org.apache.cocoon.generation.XPathTraversableGenerator.XPathPrefixResolver prefixResolver
The prefix resolver for namespaced queries


context

protected org.apache.cocoon.environment.Context context
The cocoon context used for mime-type mappings

Constructor Detail

XPathTraversableGenerator

public XPathTraversableGenerator()
Method Detail

setParser

public void setParser(org.apache.cocoon.core.xml.DOMParser parser)

setXPathProcessor

public void setXPathProcessor(XPathProcessor processor)

setup

public void setup(org.apache.cocoon.environment.SourceResolver resolver,
                  Map objectModel,
                  String src,
                  Parameters par)
           throws org.apache.cocoon.ProcessingException,
                  SAXException,
                  IOException
Description copied from class: TraversableGenerator
Set the request parameters. Must be called before the generate method.

Specified by:
setup in interface org.apache.cocoon.sitemap.SitemapModelComponent
Overrides:
setup in class TraversableGenerator
Parameters:
resolver - the SourceResolver object
objectModel - a Map containing model object
src - the Traversable Source to be XMLized specified as src attribute on <map:generate/>
par - configuration parameters
Throws:
org.apache.cocoon.ProcessingException
SAXException
IOException

service

public void service(ServiceManager manager)
             throws ServiceException
Throws:
ServiceException

recycle

public void recycle()
Recycle resources

Specified by:
recycle in interface Recyclable
Overrides:
recycle in class TraversableGenerator

dispose

public void dispose()

addContent

protected void addContent(TraversableSource source)
                   throws SAXException,
                          org.apache.cocoon.ProcessingException
Description copied from class: TraversableGenerator
Allow subclasses a chance to generate additional elements within collection and resource elements.

Overrides:
addContent in class TraversableGenerator
Parameters:
source - the source to generate additional data for.
Throws:
SAXException
org.apache.cocoon.ProcessingException

isXML

protected boolean isXML(TraversableSource path)
Determines if a given TraversableSource shall be handled as XML.

Parameters:
path - the TraversableSource to check
Returns:
true if the given TraversableSource shall handled as XML, false otherwise.

performXPathQuery

protected void performXPathQuery(TraversableSource in)
                          throws SAXException
Performs an XPath query on the source.

Parameters:
in - the Source the XPath is performed on.
Throws:
SAXException - if something goes wrong while adding the XML snippet.


Copyright © 1999-2008 The Apache Software Foundation. All Rights Reserved.