Cocoon的基本组件

eXSP

<?xml version="1.0" encoding="ISO-8859-1"?>



<xsp:page language="java" xmlns:xsp="http://apache.org/xsp">



  <xsp:logic>

  static private int counter = 0;

  private synchronized int count()

  {

    return counter++;

  }

  </xsp:logic>



  <page>

  <p>I have been requested <xsp:expr>count()</xsp:expr> times.</p>

  </page>



</xsp:page>

eXSP其实就是一个xml文件满足了一下几个要求

a.文档的根必须为<xsp:page>

b.必须要有使用语言的声明在<xsp:page>里面

c.<xsp:page>必须有namespace属性

d.如果一个xsp要有一些实际的作用的化,必须包含<xsp:logic><xsp:expr>这两个

Code Generator (XSP Processing)

package org.apache.cocoon.www.docs.samples.xsp;



import java.io.File;

// A bunch of other imports 



public class counter_xsp extends XSPGenerator {

   // .. Bookkeeping stuff commented out.

  /* User Class Declarations */

  static private int counter = 0;

  private synchronized int count() {

    return counter++;

  }

  /* Generate XML data. */

  public void generate() throws SAXException {

    this.contentHandler.startDocument();

    AttributesImpl xspAttr = new AttributesImpl();

    this.contentHandler.startPrefixMapping("xsp", "http://apache.org/xsp");

    this.contentHandler.startElement("", "page", "page", xspAttr);

    // Statements to build the XML document (Omitted)

    this.contentHandler.endElement("", "page", "page");

    this.contentHandler.endPrefixMapping("xsp");

    this.contentHandler.endDocument();

  }

  这里面就是将xsp转换成为xml文件的转换器

XSP的几种架构

Embeded Logic

a.代码都放到一个文件里面

b.内容和逻辑不分开

c.适合小的工程,不适合大的工程

Included Logicsheet

a.代码存储在一个分开了的xsl文件里

b.实现了内容和逻辑的分离

c.更好的实现了xsp

Logicsheet as tag library

a.logicsheet被包装同时注册到了cocoon.xconf里面,实现了重用

b.标签库有名字空间,可以在<xsp:page>里面引用

c.最佳的实现了内容,逻辑和管理的分离

Sitemap

<?xml version="1.0"?>

<map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">



<map:components>

...

</map:components>



<map:views>

...

</map:views>

<map:pipelines>

<map:pipeline>

<map:match> 

...

</map:match>

...

</map:pipeline>

...

</map:pipelines>

...

</map:sitemap>

Sitemap包含了以下的内容

    * list of matchers

    * list of generators

    * list of transformers

    * list of readers

    * list of serializers

    * list of selectors

    * list of processing pipelines with match patterns

    * ...

Matchers

Matcher根据一定的规则匹配uri信息,根据匹配结果分发*request*到不同的处理管道上去

有两种的Matcher

    * wildcard matcher

    * regexp matcher



Sitemap entries for different types of matchers



<map:matchers default="wildcard">

 <map:matcher name="wildcard" factory="org.apache.cocoon.matching.WildcardURIMatcher"/>

 <map:matcher name="regexp" factory="org.apache.cocoon.matching.RegexpURIMatcher"/>

</map:matchers>



Pipeline entries in sitemap file



<map:match pattern="jsp/*">

  <map:generate type="jsp" src="/docs/samples/jsp/{1}.jsp"/>

  ...

  </map:match>

<map:match pattern="hello.pdf">

</map:match

Generators

生成器的目的就是根据不同的输入内容(file,directory,stream等等)生成xml文档

有以下几种生成器

    * file generator

    * directory generator

    * XSP generator

    * JSP generator

    * Request generator

    * ...



Sitemap entries for different types of generators



<map:generators default="file">

 <map:generator name="file"

                src="org.apache.cocoon.generation.FileGenerator"

                label="content"/>

 <map:generator name="directory"

                src="org.apache.cocoon.generation.DirectoryGenerator"

                label="content"/>

 <map:generator name="serverpages"

                src="org.apache.cocoon.generation.ServerPagesGenerator"

                label="content"/>

 <map:generator name="request"

                src="org.apache.cocoon.generation.RequestGenerator"/>

 ...

</map:generators>



A sample generator entries in a pipeline



<map:match pattern="hello.html">

    <map:generate src="docs/samples/hello-page.xml"/>

    <map:transform src="stylesheets/page/simple-page2html.xsl"/>

    <map:serialize type="html"/>

</map:match>

Transformers(转换器)

转换器的作用是将xml的文档格式转换成为另外一种文档格式

有以下几种转换器

    *  XSLT Transformer

    * Log Transformer

    * SQL Transformer

    * I18N Transformer

    * ...



Sitemap entries for different types of transformers



<map:transformers default="xslt">

   <map:transformer name="xslt" src="org.apache.cocoon.transformation.TraxTransformer">

    <use-request-parameters>false</use-request-parameters>

    <use-browser-capabilities-db>false</use-browser-capabilities-db>

   </map:transformer>

   <map:transformer name="log" src="org.apache.cocoon.transformation.LogTransformer"/>

...



</map:transformers>



A sample transformer entry in a pipeline



<map:match pattern="hello.html">

 <map:generate src="docs/samples/hello-page.xml"/>

 <map:transform src="stylesheets/page/simple-page2html.xsl"/>

 <map:serialize type="html"/>

</map:match>

Serializers

用来将xml文件格式转换成为其他格式(例如html,xml,pdf等等)

有以下几种转换器

    * HTML Serializer

    * FOP Serializer

    * Text Serializer

    * XML Serializer

    * ...



Sitemap entries for different types of serializers



<map:serializers default="html">

 <map:serializer name="xml"

                 mime-type="text/xml"

                 src="org.apache.cocoon.serialization.XMLSerializer"/>

 <map:serializer name="html"

                 mime-type="text/html"

                 src="org.apache.cocoon.serialization.HTMLSerializer"/>

 <map:serializer name="fo2pdf"

                 mime-type="application/pdf"

                 src="org.apache.cocoon.serialization.FOPSerializer"/>

 <map:serializer name="vrml"

                 mime-type="model/vrml"

                 src="org.apache.cocoon.serialization.TextSerializer"/>

 ...

</map:serializers>



A sample serializer entry in a pipeline



 <map:match pattern="hello.html">

    <map:generate src="docs/samples/hello-page.xml"/>

    <map:transform src="stylesheets/page/simple-page2html.xsl"/>

    <map:serialize type="html"/>

   </map:match>

Pipeline Processing

哈哈,核心部分。这个组合了上面的各种组件,完成一次的操作

pipeline是由sitemap_xmap.java来配置的,而这个文件是由sitemap.xmap文件生成的。

Logicsheets

Logicsheet就是有相对应的名字空间的xsl文件。哈哈,很有意思。

这是主要的向xsp当中添加逻辑处理的方式

当然了,这个也需要在cocoon.xconf注册才可以使用

Logicsheet是被generator用来在生成程序前转换xml文件格式的。

Cocoon自带了以下几种Logicsheet

    * request.xsl

    * response.xsl

    * session.xsl

    * cookie.xsl

    * esql.xsl

    * log.xsl

    * ...



Log.xsl structure



<xsl:stylesheet  version="1.0"

                 xmlns:xsp="http://apache.org/xsp"

                 xmlns:log="http://apache.org/xsp/log"

                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">



<xsl:template match="log:logger">

... variable and xsp:logic statements ...

</xsl:template>



<xsl:template match="log:debug">

  <xsp:logic>

   if(getLogger() != null)

     getLogger().debug("<xsl:value-of select="."/>");    

  </xsp:logic>  

</xsl:template>

<xsl:template match="log:error">

...  

</xsl:template>

</xsl:stylesheet>



A sample use



<xsp:page language="java"

          xmlns:xsp="http://apache.org/xsp"

          xmlns:log="http://apache.org/xsp/log">



  <page>

  <log:logger name="test" filename="test.log"/>

  <log:debug>Test Message</log:debug>

  </page>

</xsp:page>

Apache Cocoon Configuration(Cocoon的配置)

Cocoon高度可配置。这里面我们假设我们的程序是发布到Tomcat下面去。

    * sitemap.xmap: 显然,主要的配置文件

    * cocoon.xconf: 注册Logicsheet,同时也是配置sitemap.xmap等配置文件的地方

    * web.xml: Servlet配置文件,指定cocoon.xconf文件和log等的位置

    * cocoon.roles:用来配置Cocoon核心组件的地方,如果要定制Cocoon,就需要修改她

你可能感兴趣的:(OO)