2006-06-19

Generator in Cocoon

Cocoon 中事先定义了一系列的Generator,可以直接使用。这是一个简单的介绍,摘自Professional XML Development with Apache Tools: Xerces, Xalan, FOP, Cocoon, Axis, Xindice。




  • Directory generator—This generator produces an event stream that represents an XML representation of a file system directory. The document has a <directory> element as the root element, with children that are either <directory> or <file> elements. These elements contain attributes for the name, last modified time, and size of the file. The elements use the namespace URI http://apache.org/cocoon/directory/2.0 and the prefix dir. When you use this generator in the sitemap, you can control how deep the directory traversal goes, the format used for dates, the sort order, and a number of other options. This generator is in org.apache.coccon.generation.DirectoryGenerator and is named directory.


  • File generator—You’ve already seen the use of the file generator. It simply reads an XML document from a URL and produces the SAX event stream for it. The namespace URI and prefix are determined by the document that is read. This is the default generator for Cocoon. It’s in org.apache.org.cocoon.generation.FileGenerator and is named file.


  • Image directory generator—This is a subclass of the directory generator, which ensures that all the files in the directory are images. It adds attributes containing the image dimensions to the event stream and uses the same namespace URI and prefix as the directory generator. This generator is in org.apache.cocoon.generation.ImageDirectoryGenerator and is available with the name imagedirectory.


  • Linkstatus generator—The linkstatus Generator takes a URL as its src attribute and recursively traverses all the links embedded in the HTML found at that URL. The event stream contains <link> elements whose attributes contain the URL for the link, the content-type of the link (if traversal was successful), and the HTTP status code. The elements provided by the linkstatus generator use the namespace URI http://apache.org/cocoon/linkstatus/2.0 and the prefix linkstatus. This generator is useful for forcing a traversal of all pages in a Cocoon site. You can use it to preload pages into the Cocoon cache or to precompile eXstensible Server Pages (XSPs). The linkstatus generator is in org.apache.cocoon. generation.LinkStatusGenerator and is assigned the name linkstatus.


  • Notifying generator—You’ve seen the use of this generator in the <handle-errors> section of the sample sitemap. When an error occurs in the pipeline, the error (which is usually a Java Throwable object) is passed to the notifying generator. Nothing further happens until you access the notifying generator from a pipeline. When you do that, the information in the Throwable object is placed into the event stream. You get a <notify> element as the root element, and there will be a <title> element whose content gives the title of the error notification, a <source> element whose content gives the name of the class of the error object, a <message> element whose content is the result of calling getMessage on the error object, and a <description> element whose content is the result of calling toString on the error object. These elements use the namespace URI http://apache.org/cocoon/error/2.0 and the prefix error. The notifying generator is in org.apache.coccon.sitemap.NotifyingGenerator and is named notifier.


  • Request generator—This generator takes information in the HttpServletRequest and converts it to XML. The namespace URI for the elements is http://apache.org/cocoon/request/2.0. The root element is <request>, and it has four children. The <requestHeaders> element contains a sequence of <header> elements that represent the HTTP headers. Each <header> has a name attribute that contains the header name, and the content of the element is the value of the header. After that a <requestParameters> element that contains the parameters to the request. These are represented by a sequence of <parameter> elements. Again, a name attribute supplies the name of the parameter. However, because parameters can have multiple values, the content of a <parameter> element is a <value> element whose content is the parameter value. The next child element is <requestAttributes>. This element is supplied only if the sitemap parameter generate-attributes is set to true in the <generator> or <generate> element. The content of <requestAttributes> is a sequence of <attribute> elements. Each has a name attribute, and the attribute values appear as child <value> elements, just as for <requestParameters>. The last child element is <configurationParameters>, which contains a sequence of <parameter> elements, one for each parameter specified in the <generate> or <generator> element. These <parameter> elements are different from the <parameter> elements for the <requestParameter> elements. They use a name attribute for the parameter name, but the value is the content of the <parameter> element. No child <value> element is needed. The request generator is available under the name request and is in org.apache.cocoon.generation.RequestGenerator.


  • Server pages generator—The server pages generator uses an XSP page as the generator for the event stream. We’ll talk about this in more detail when we discuss XSPs. For now, you need to know that the generator is in org.apache.cocoon.generation.ServerPagesGenerator and uses the name serverpages.


  • Status generator—The status generator takes Cocoon’s internal status and renders it as a SAX event stream. It uses the namespace URI http://apache.org/cocoon/status/2.0. The root element is called <statusinfo>, and it can have two kinds of children: <group>s and <value>s. <statusinfo> has attributes that give the date and hostname of the server. A <group> can have another <group> or a <value> as a child. A <value> can have a sequence of <line>s as a child, and each line’s content is character data. Both <group> and <value> have a name attribute. This generator is in org.apche.cocoon.generation.StatusGenerator under the name status.


  • Stream generator—The stream generator generates a SAX event stream from an HttpRequest’s InputStream. It can deal with either GET or POST requests. When you use it in a pipeline, it uses the HttpRequest that caused the pipeline to execute. The stream generator is available under the name stream and is in org.apache.cocoon.generation.StreamGenerator.


  • XPath directory generator—The XPath directory generator works the like the directory generator, but it allows you to specify two additional parameters. If you specify an xmlFiles parameter with a regular expression value, then the files that match the regular expression are treated as XML files. If you specify an xpath parameter, then the XPath you provide as the parameter value is used to filter XML files (as specified by the xmlFiles parameter). Only those portions of each XML file that match the XPath expression are in the event stream. This generator is in org.apche.cocoon.generation.XPathDirectoryGenerator and is assigned the name xpathdirectory.

没有评论:

发表评论