org.apache.cocoon.generation
Class XPathTraversableGenerator

java.lang.Object
  extended by org.apache.avalon.framework.logger.AbstractLogEnabled
      extended by org.apache.cocoon.xml.AbstractXMLProducer
          extended by org.apache.cocoon.generation.AbstractGenerator
              extended by org.apache.cocoon.generation.ServiceableGenerator
                  extended by org.apache.cocoon.generation.TraversableGenerator
                      extended by org.apache.cocoon.generation.XPathTraversableGenerator
All Implemented Interfaces:
Poolable, Recyclable, Disposable, Component, LogEnabled, Serviceable, CacheableProcessingComponent, Generator, SitemapModelComponent, 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:
CVS $Id: XPathTraversableGenerator.html 1304258 2012-03-23 10:09:27Z ilgrosso $
Author:
Gianugo Rabellino, Daniele Madama

Field Summary
protected  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.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  org.apache.regexp.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()
          Release all resources.
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)
          Set the current ServiceManager instance used by this Serviceable.
 void setup(SourceResolver resolver, Map objectModel, String src, Parameters par)
          Set the request parameters.
 
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.avalon.framework.logger.AbstractLogEnabled
enableLogging, getLogger, setupLogger, setupLogger, setupLogger
 
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 org.apache.regexp.RE xmlRE
The regular expression for the XML files pattern.


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 Context context
The cocoon context used for mime-type mappings

Constructor Detail

XPathTraversableGenerator

public XPathTraversableGenerator()
Method Detail

setup

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

Specified by:
setup in interface 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:
ProcessingException
SAXException
IOException

service

public void service(ServiceManager manager)
             throws ServiceException
Description copied from class: ServiceableGenerator
Set the current ServiceManager instance used by this Serviceable.

Specified by:
service in interface Serviceable
Overrides:
service in class ServiceableGenerator
Throws:
ServiceException

dispose

public void dispose()
Description copied from class: ServiceableGenerator
Release all resources.

Specified by:
dispose in interface Disposable
Overrides:
dispose in class ServiceableGenerator

addContent

protected void addContent(TraversableSource source)
                   throws SAXException,
                          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
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.

recycle

public void recycle()
Recycle resources

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


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