<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 13,  comments - 3,  trackbacks - 0

    DocBook與Eclipse - 教程

    Lars Vogel

    李墨耘


    0. 翻譯說明

    本文翻譯自Lars Vogel的DocBook with Eclipse教程, http://www.vogella.de/articles/DocBook/article.html

    在翻譯過程中,本人保留原文的一切鏈接。

    原文一切權利歸原作者所有,譯文一切權利歸本人所有。如欲轉載原文,請征得作者授權。如欲轉載譯文,請注明本文原始鏈接。

    本文的翻譯已獲得原作者授權。

    1. DocBook介紹

    1.1. 概要

    DocBook是一種文檔標準,用以創建結構化的純文本文檔。用DocBook創建的文檔能夠方便的在不同的操作系統之間以及不同的文本處理 工具之間進行移植,并可以通過XSLT 轉為其他的輸出格式。XSLT是EXtensible Stylesheet Language Transformation的縮寫。 由于DocBook是使用純文本編輯的,因此你可以使用任何一個文本 編輯器來編寫DocBook,并納入版本控制系統的管理。

    目前,有多種不同的樣式表,能夠把DocBook文件轉換為不同的輸出格式,例如轉換為HTML,pdf,java help以及Unix man pages.

    DocBook有兩種主要的文檔,一種是book,另一種是article。其中

    • Article: 用來寫一些技術文章,下文都以article為例。article通常是由一系列的section組成。

    • Book: 用來寫一些更長的描述。book比article多了一種結構:chapter。

     

    1.2. 實例

    下面就是一個DocBook文檔的例子。

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "../docbook-xml-4.5/docbookx.dtd">
    <article>
    <articleinfo>
    <title>DocBook Intro</title>
    <author>
    <firstname>Lars</firstname>
    <surname>Vogel</surname>
    </author>
    </articleinfo>
    <sect1 label="1.0">
    <title>An introduction to DocBook</title>
    <para>
    This is text.
    </para>
    </sect1>
    </article>
    

    Note

    請注意,在上面的例子中,DTD文件的位置:當前目錄的上一級目錄,其中的docbook-xml-4.5文件夾內。

    1.3. 所需的工具

    想要創建DocBook文件并轉換成其他格式,你需要以下工具:

    • DocBook的DTD文件。這個文件定義了DocBook文檔的格式。

    • XSLT樣式表,用來把DocBook文檔轉換成其他格式。

    • XSLT解析器

    我們使用Eclipse 作為XML編輯器,Xalan作為XSLT解析器,并使用 Apache Ant 來進行XSLT的轉換。

    2. 安裝

    2.1. Eclipse

    你需要安裝Eclipse,可以參看這篇文章 Eclipse IDE 來學習Eclipse的安裝和使用。我們需要用到的Ant已經被集成到Eclipse里面了,因此關于Ant我們不需要安裝任何額外的東西。

    2.2. Docbook和樣式表

    你還需要下載Docbook的DTD,以及用來轉換的Docbook文檔的樣式表。Docbook的DTD可以在 http://www.oasis-open.org/docbook/xml/4.5下載, 而XSLT樣式表可以在 http://docbook.sourceforge.net/ 下載。在寫這篇文檔時,最新的版本是“1.75.2”(譯者注:在翻譯這篇文檔時,最新的版本是1.76.1)。你需要下載的docbook-xsl的發布文件, 例如“docbook-xsl-1.75.2.zip”。

    2.3. XSL處理器

    要命的是JVM自帶的XSL處理器在處理XSLT樣式表的時候有問題……所以我們需要從 http://xml.apache.org/xalan-j/ 下載Xalan,用來作為我們的XSL處理器。

    3. 把Docbook轉換為HTML

    3.1. 工程設置

    可以在Eclipse中,創建一個"de.vogella.docbook.first"的新工程,方法是File -> New -> Project,并從彈出的 窗口中選擇General -> Projects.

    在工程中創建如下的目錄結構:

    • output : Docbook轉換成其他格式時的輸出目錄

    • docbook-xml-4.5: 用來放Docbook的DTD定義文件

    • docbook-xsl: 用來放進行Docbook轉換的樣式表文件

    • lib: 用來包含你需要的庫文件(用來創建pdf)

    • documents: 用來放你的DocBook文件

    把DocBook的DTD和XSLT的樣式表放入相應的文件夾中。

    在lib文件夾下創建xalan文件夾,并把xalan相關的jar包拷入這個文件夾中。 結果應該看起來是這樣的:

    project

    3.2. 第一個Docbook文檔

    在“documents”文件夾里面,創建一個“book.xml”文件,并輸入下面的xml文件

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "../docbook-xml-4.5/docbookx.dtd">
    <article>
    <articleinfo>
    <title>DocBook Intro</title>
    <author>
    <firstname>Lars</firstname>
    <surname>Vogel</surname>
    </author>
    </articleinfo>
    <sect1 label="1.0">
    <title>An introduction to DocBook</title>
    <para>
    This is text.
    </para>
    </sect1>
    </article>
    

    在xml文件中,“../docbook-xml-4.5/docbook.dtd”對應于我們剛剛創建的文件夾以及放入的DTD文件。

    3.3. 使用Ant把Docbook轉換為HTML格式

    接下來我們配置ANT。在工程目錄中,創建build.xml文件如下:

    <?xml version="1.0"?>
    <!--
    - Author: Lars Vogel
    -->
    <project name="docbook-src" default="build-html">
    <description>
    This Ant buildhtml.xml file is used to transform DocBook XML to html output
    </description>
    <!--
    - Configure basic properties that will be used in the file.
    -->
    <property name="docbook.xsl.dir" value="docbook-xsl" />
    <property name="doc.dir" value="output" />
    <property name="documents" value="documents" />
    <property name="html.stylesheet" value="${docbook.xsl.dir}/html/docbook.xsl" />
    <!-- Making xalan available -->
    <path id="xalan.class.path">
    <pathelement location="lib/xalan/serializer.jar" />
    <pathelement location="lib/xalan/xalan.jar" />
    <pathelement location="lib/xalan/xercesImpl.jar" />
    <pathelement location="lib/xalan/xml-apis.jar" />
    </path>
    <!--
    - target: usage
    -->
    <target name="usage" description="Prints the Ant build.xml usage">
    <echo message="Use -projecthelp to get a list of the available targets." />
    </target>
    <!--
    - target: clean
    -->
    <target name="clean" description="Cleans up generated files.">
    <delete dir="${doc.dir}" />
    </target>
    <!--
    - target: depends
    -->
    <target name="depends">
    <mkdir dir="${doc.dir}" />
    </target>
    <!--
    - target: build-html
    - description: Iterates through a directory and transforms
    - .xml files into .html files using the DocBook XSL.
    -->
    <target name="build-html" depends="depends" description="Generates HTML files from DocBook XML">
    <xslt style="${html.stylesheet}" extension=".html" basedir="${documents}" destdir="${doc.dir}">
    <include name="**/*book.xml" />
    <include name="**/*article.xml" />
    <param name="html.stylesheet" expression="style.css" />
    <classpath refid="xalan.class.path" />
    </xslt>
    <!-- Copy the stylesheet to the same directory as the HTML files -->
    <copy todir="${doc.dir}">
    <fileset dir="lib">
    <include name="style.css" />
    </fileset>
    </copy>
    </target>
    </project>
    

    運行build.xml文件(右鍵 -> Run As -> Ant Build)。運行之后,在你的output文件夾里面, 應該已經有一個“book.html”了。

    恭喜你完成了第一個Docbook文檔,并順利的轉成了HTML格式!

    4. Docbook examples

    下面是一些使用Docbook標簽的概覽。

    4.1. 標簽

    Table 1. Docbook一些重要的標簽

    Tag 說明
    <![CDATA[ 此處可輸入特殊字符,e.g. & ]]> 在標簽中可以輸入某些特殊字符,例如某些xml以及Docbook的特殊字符。
    <programlisting> </programlisting> 表示該文本是程序代碼
    <emphasis> </emphasis> 表示用強調(Highlight)該文本
    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="example1.txt" /> 包含example1.xml的內容。該文件可以是一個獨立的xml文件
    <ulink url="http://www.heise.de/newsticker">German IT News</ulink> [a] 在文檔中創建一個超鏈接
    &amp; 在文檔中插入“&”符號

    [a] 譯者注:Docbook5改變了超鏈接的格式。關于Docbook5以及更詳細的Eclipse配置Docbook教程,會很快放出。 不過,即使是在Docbook5的環境下,使用ulink一樣可以順利的用XSLT轉換,只不過Eclipse的xml編輯器會提示有錯誤。

    4.2. 表格

    下面是一個創建表格的例子

    <table frame='all'>
    <title>Sample Table</title>
    <tgroup cols='2' align='left' colsep='1' rowsep='1'>
    <colspec colname='c1' />
    <colspec colname='c2' />
    <thead>
    <row>
    <entry>a4</entry>
    <entry>a5</entry>
    </row>
    </thead>
    <tfoot>
    <row>
    <entry>f4</entry>
    <entry>f5</entry>
    </row>
    </tfoot>
    <tbody>
    <row>
    <entry>b1</entry>
    <entry>b2</entry>
    </row>
    <row>
    <entry>d1</entry>
    <entry>d5</entry>
    </row>
    </tbody>
    </tgroup>
    </table>
    

    生成的表格看起來是這樣的

    Table 2. Sample Table

    a4 a5
    f4 f5
    b1 b2
    d1 d5

    4.3. 列表

    沒有序號的列表可以這樣創建:

    <itemizedlist>
    <listitem>
    <para>Item1</para>
    </listitem>
    <listitem>
    <para>Item2</para>
    </listitem>
    <listitem>
    <para>Item3</para>
    </listitem>
    <listitem>
    <para>Item4</para>
    </listitem>
    </itemizedlist>
    

    輸出結果如下:

    • Item1

    • Item2

    • Item3

    • Item4

    而帶編號的列表可以這樣寫:

    <orderedlist>
    <listitem>
    <para>This is a list entry</para>
    </listitem>
    <listitem>
    <para>This is another list entry</para>
    </listitem>
    </orderedlist>
    

    輸出結果如下:

    1. This is a list entry

    2. This is another list entry

    4.4. 鏈接

    鏈接可以用下面的方法來創建:[a]

    <para>
    We use the Ant integrated into Eclipse. See
    <ulink url="http://www.vogella.de/articles/ApacheAnt/article.html"> Apache Ant Tutorial</ulink>
    for an introduction into Apache Ant.
    </para>
    

    4.5. 插入圖片

    插入圖片可以使用下面的方式。

    <para>
    <mediaobject>
    <imageobject>
    <imagedata fileref="images/antview10.gif" format="gif">
    </imagedata>
    </imageobject>
    </mediaobject>
    </para>
    

    5. 創建pdf輸出

    5.1. 安裝

    Docbook轉成pdf的過程是:先由docbook轉成XSL-FO格式,再利用Apache FOP把FO轉成 pdf。因此,我們首先需要Apache FOP相關的庫。

    XML FO,是XML Formating Object的意思,FO格式是一種處理打印、印刷介質的XML標準。

    可以從http://xmlgraphics.apache.org/fop/下載FOP的最新版本。

    從下載的FOP發行版中,把所有的jar文件都拷貝到你的lib文件夾中,并把這些庫都加入到ant 的build path中。可以參考 Apach Ant Tutorial來修改ant的build path。

    5.2. 定義Ant Task

    要在ant中使用fop,我們首先應當定義一個fop相關的ant task,然后在后面的腳本中使用這個任務。 下面的這個例子演示了怎樣定義一個ant task并怎樣調用。第二個例子是一個完整的build.xml文件的例子。

    <!--
    - Defines the ant task for fop
    -->
    <taskdef name="fop" classname="org.apache.fop.tools.anttasks.Fop" />
    <!-- Transformation into pdf
    - Two steps
    - 1.) First create the FO files
    - 2.) Then transform the FO files into pdf files
    -->
    <!--
    - target: build-pdf
    - description: Iterates through a directory and transforms
    - .xml files into .fo files using the DocBook XSL.
    -->
    <target name="build-pdf" depends="depends, xinclude"
    description="Generates HTML files from DocBook XML">
    <!-- Convert DocBook Files into FO -->
    <xslt style="${fo.stylesheet}" extension=".fo" basedir="${src.tmp}"
    destdir="${src.tmp}">
    <include name="**/*book.xml" />
    <include name="**/*article.xml" />
    <param name="section.autolabel" expression="1" />
    </xslt>
    <!-- Convert FO Files into pdf -->
    <fop format="application/pdf" outdir="${doc.dir}">
    <fileset dir="${src.tmp}">
    <include name="**/*.fo" />
    </fileset>
    </fop>
    </target>
    
    <?xml version="1.0"?>
    <!--
    - Author: Lars Vogel
    -->
    <project name="docbook-src" default="all">
    <description>
    This Ant build.xml file is used to transform DocBook XML to
    various output formats
    </description>
    <!--
    - Defines the ant task for xinclude
    -->
    <taskdef name="xinclude" classname="de.vogella.xinclude.XIncludeTask" />
    <!--
    - Defines the ant task for xinclude
    -->
    <taskdef name="fop" classname="org.apache.fop.tools.anttasks.Fop" />
    <!--
    - Configure basic properties that will be used in the file.
    -->
    <property name="javahelp.dir" value="${basedir}/../Documentation/output/vogella/javahelp" />
    <property name="src" value="${basedir}/documentation" />
    <property name="output.dir" value="${basedir}/../Documentation/output/vogella/articles" />
    <property name="output.tmp" value="${basedir}/output.tmp" />
    <property name="lib" value="${basedir}/lib/" />
    <property name="docbook.xsl.dir" value="${basedir}/docbook-xsl-1.72.0" />
    <property name="xinclude.lib.dir" value="${basedir}/lib/" />
    <!--
    - Usage of the differect style sheets which will be used for the transformation
    -->
    <property name="eclipse.stylesheet" value="${docbook.xsl.dir}/eclipse/eclipse.xsl" />
    <property name="html.stylesheet" value="${docbook.xsl.dir}/html/docbook.xsl" />
    <property name="fo.stylesheet" value="${docbook.xsl.dir}/fo/docbook.xsl" />
    <property name="javahelp.stylesheet" value="${docbook.xsl.dir}/javahelp/javahelp.xsl" />
    <property name="chunk-html.stylesheet" value="${docbook.xsl.dir}/html/chunk.xsl" />
    <!--
    - target: usage
    -->
    <target name="usage" description="Prints the Ant build.xml usage">
    <echo message="Use -projecthelp to get a list of the available targets." />
    </target>
    <!--
    - target: clean
    -->
    <target name="clean" description="Cleans up generated files.">
    <delete dir="${output.dir}" />
    </target>
    <!--
    - target: depends
    -->
    <target name="depends">
    <mkdir dir="${output.dir}" />
    </target>
    <!--
    - target: copy
    - Copies the images from the subdirectories to the target folder
    -->
    <target name="copy">
    <echo message="Copy the images" />
    <copy todir="${output.dir}">
    <fileset dir="${src}">
    <include name="**/images/*.*" />
    </fileset>
    </copy>
    </target>
    <!--
    - target: xinclude
    - description: Creates one combined temporary files for the different inputs files.
    - The combined file will then be processed via different ant tasks
    -->
    <target name="xinclude">
    <xinclude in="${src}/DocBook/article.xml" out="${output.tmp}/DocBook/article.xml" />
    <xinclude in="${src}/JavaConventions/article.xml" out="${output.tmp}/JavaConventions/article.xml" />
    <xinclude in="${src}/JUnit/article.xml" out="${output.tmp}/JUnit/article.xml" />
    <xinclude in="${src}/EclipseReview/article.xml" out="${output.tmp}/EclipseReview/article.xml" />
    <xinclude in="${src}/HTML/article.xml" out="${output.tmp}/HTML/article.xml" />
    <xinclude in="${src}/Eclipse/article.xml" out="${output.tmp}/Eclipse/article.xml" />
    <xinclude in="${src}/Logging/article.xml" out="${output.tmp}/Logging/article.xml" />
    <!--
    <xinclude in="${src}/ant/article.xml" out="${src.tmp}/ant/article.xml" />
    -->
    </target>
    <!--
    - target: build-html
    - description: Iterates through a directory and transforms
    - .xml files into .html files using the DocBook XSL.
    -->
    <target name="build-html" depends="depends, xinclude" description="Generates HTML files from DocBook XML">
    <xslt style="${html.stylesheet}" extension=".html" basedir="${output.tmp}" destdir="${output.dir}">
    <include name="**/*book.xml" />
    <include name="**/*article.xml" />
    <param name="html.stylesheet" expression="styles.css" />
    <param name="section.autolabel" expression="1" />
    <param name="html.cleanup" expression="1" />
    <outputproperty name="indent" value="yes" />
    </xslt>
    <!-- Copy the stylesheet to the same directory as the HTML files -->
    <copy todir="${output.dir}">
    <fileset dir="lib">
    <include name="styles.css" />
    </fileset>
    </copy>
    </target>
    <!--
    - target: build-javahelp
    - description: Iterates through a directory and transforms
    - .xml files into .html files using the DocBook XSL.
    -->
    <target name="build-javahelp" depends="depends, xinclude" description="Generates HTML files from DocBook XML">
    <xslt style="${javahelp.stylesheet}" extension=".html" basedir="${output.tmp}" destdir="${javahelp.dir}">
    <include name="**/*book.xml" />
    <include name="**/*article.xml" />
    <outputproperty name="indent" value="yes" />
    </xslt>
    </target>
    <!--
    - target: chunks-html
    - description: Iterates through a directory and transforms
    - .xml files into seperate .html files using the DocBook XSL.
    -->
    <target name="build-chunks" depends="depends, xinclude" description="Generates chunk HTML files from DocBook XML">
    <xslt style="${html.stylesheet}" extension=".html" basedir="${output.tmp}" destdir="${output.dir}">
    <include name="**/*book.xml" />
    <include name="**/*article.xml" />
    <param name="html.stylesheet" expression="styles.css" />
    <param name="section.autolabel" expression="1" />
    <param name="html.cleanup" expression="1" />
    <param name="chunk.first.selection" expression="1" />
    </xslt>
    <!-- Copy the stylesheet to the same directory as the HTML files -->
    <copy todir="${output.dir}">
    <fileset dir="lib">
    <include name="styles.css" />
    </fileset>
    </copy>
    </target>
    <!-- Transformation into pdf
    - Two steps
    - 1.) First create the FO files
    - 2.) Then transform the FO files into pdf files
    -->
    <!--
    - target: build-pdf
    - description: Iterates through a directory and transforms
    - .xml files into .fo files using the DocBook XSL.
    - Relativebase is set to true to enable FOP to find the graphics which are included
    - in the images directory
    -->
    <target name="build-pdf" depends="depends, xinclude" description="Generates HTML files from DocBook XML">
    <!-- Convert DocBook Files into FO -->
    <xslt style="${fo.stylesheet}" extension=".fo" basedir="${output.tmp}" destdir="${output.tmp}">
    <include name="**/*book.xml" />
    <include name="**/*article.xml" />
    <param name="section.autolabel" expression="1" />
    </xslt>
    <!-- Convert FO Files into pdf -->
    <fop format="application/pdf" outdir="${output.dir}" relativebase="true">
    <fileset dir="${output.tmp}">
    <include name="**/*.fo" />
    </fileset>
    </fop>
    </target>
    <!--
    - target: chunks-html
    - description: Iterates through a directory and transforms
    - .xml files into seperate .html files using the DocBook XSL.
    -->
    <target name="build-eclipse" depends="depends, xinclude" description="Generates Eclipse help files from DocBook XML">
    <xslt style="${eclipse.stylesheet}" basedir="${output.tmp}" destdir="${output.dir}">
    <include name="**/*book.xml" />
    <include name="**/*article.xml" />
    </xslt>
    </target>
    <target name="all" depends="copy, build-html, build-pdf, build-chunks, build-eclipse">
    </target>
    </project>
    

    7. 控制輸出格式

    我們可以通過修改XSLT樣式表的參數來影響輸出的結果。下面其中一些參數的介紹。

    7.1. HTML參數

    Table 3. HTML參數

    參數 說明
    name="section.autolabel" expression="1" 為section自動編號(例如,第一個section是1,其下一集的section是1.1,以此類推)
    name="chapter.autolabel" expression="1" 為chapter自動編號
    name="html.stylesheet" expression="styles.css" 定義html使用的樣式表
    name="html.cleanup" expression="1" 清理html使之更具可讀性

    7.2. 為html增加內容

    Docbook允許在轉換成html的時候,導入并包含一個外部的html文件。你可以使用這種技術,在生成 html的時候向其中插入javascript代碼。

    下面是一個包含html文件的例子。

     <?dbhtml-include href="../../myadditonalcontent.html"?>
    

    Inserting external HTML code 有更多的描述。

    7. 用Eclipse XSL完成XInclude功能

    7.1. 概述

    XInclude技術能幫你重新組織你的docbook文件。你可以在書寫每一個章節的時候,都使用 一個單獨的xml文件,然后用一個總的xml文件把這些章節都組合起來。簡單的說,XInclude 能把不同的xml文件組合成為一個大的xml文件。

    例如,假設你要引入一個“foo.xml”文件,則可以寫成:

     <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="foo.xml" />
    

    下面這個例子是要把導入的文件當做文本: [1]

    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="bar.xml" />

    Eclipse XSL project提供一個XInclude的ant task。在此,我很自豪的告訴各位:這個ant task是我提供給XSL project 的:)

    7.2. Eclipse XSL工具

    Eclipse XSL工具提供了對XSLT的支持,包括XSL的編輯以及debug的支持。雖然我們這里僅僅使用其中的ant task, 但還是得完整的安裝整個包。

    安裝XSL工具可以通過Eclipse的update manager完成。 [2] 你可以通過 Using the Eclipse update manager 來獲得更多信息。

    installxsl

    7.3. 在Ant中使用XInclude

    在你的Eclipse安裝路徑中找到“org.eclipse.wst.xsl.core.jar”并把這個jar包加入到ANT的classpath中。 這樣,你應該就可以創建和運行xinclude tast了。下面是一個build.xml文件的例子:

    <?xml version="1.0"?>
    <!--
    - Author: Lars Vogel
    -->
    <project name="docbook-src" default="usage">
    <description>
    This Ant build.xml file is used to transform DocBook XML to various
    output formats
    </description>
    <!--
    - Configure basic properties that will be used in the file.
    -->
    <property name="doc.dir" value="${basedir}/output" />
    <property name="src" value="${basedir}/src" />
    <property name="src.tmp" value="${basedir}/src.tmp" />
    <property name="lib" value="${basedir}/lib/" />
    <property name="docbook.xsl.dir" value="${basedir}/docbook-xsl-1.72.0" />
    <property name="html.stylesheet" value="${docbook.xsl.dir}/html/docbook.xsl" />
    <property name="xinclude.lib.dir" value="${basedir}/lib/" />
    <!--
    - target: usage
    -->
    <target name="usage" description="Prints the Ant build.xml usage">
    <echo message="Use -projecthelp to get a list of the available targets." />
    </target>
    <!--
    - target: clean
    -->
    <target name="clean" description="Cleans up generated files.">
    <delete dir="${doc.dir}" />
    </target>
    <!--
    - target: depends
    -->
    <target name="depends">
    <mkdir dir="${doc.dir}" />
    </target>
    <!--
    - target: xinclude
    - description: Creates one combined temporary files for the different inputs files.
    - The combined file will then be processed via different ant tasks
    -->
    <target name="xinclude">
    <xsl.xinclude in="${src}/DocBook/article.xml" out="${src.tmp}/DocBook/article.xml" />
    </target>
    <!--
    - target: build-html
    - description: Iterates through a directory and transforms
    - .xml files into .html files using the DocBook XSL.
    -->
    <target name="build-html" depends="depends, xinclude" description="Generates HTML files from DocBook XML">
    <xslt style="${html.stylesheet}" extension=".html" basedir="${src.tmp}" destdir="${doc.dir}">
    <include name="**/*book.xml" />
    <include name="**/*article.xml" />
    <param name="html.stylesheet" expression="styles.css" />
    </xslt>
    <!-- Copy the stylesheet to the same directory as the HTML files -->
    <copy todir="${doc.dir}">
    <fileset dir="lib">
    <include name="styles.css" />
    </fileset>
    </copy>
    </target>
    </project>
    


    [1] 譯者注:這塊兒沒看明白,哪位高人指點一下?

    [2] 譯者注:XSL工具被包含在WTP(Web Develop Platform)中。 如果你裝的Eclipse是J2EE版本,很可能這個工具在你的Eclipse上已經有了。

    posted @ 2011-08-07 02:00 Antony Lee 閱讀(2104) | 評論 (2)編輯 收藏
         摘要: Core Python Programming學習筆記7 - if, 循環,迭代器,列表解析,生成器解析  閱讀全文
    posted @ 2011-06-13 15:40 Antony Lee 閱讀(199) | 評論 (0)編輯 收藏
         摘要: Core Python Programming 學習筆記6 - Mapping and Set  閱讀全文
    posted @ 2011-06-10 15:23 Antony Lee 閱讀(458) | 評論 (1)編輯 收藏
         摘要: Core Python Programming學習筆記5 - 字符串,列表,元組  閱讀全文
    posted @ 2011-06-09 12:20 Antony Lee 閱讀(805) | 評論 (0)編輯 收藏
         摘要: Core Python Programming 學習筆記4 - 數字相關  閱讀全文
    posted @ 2011-06-09 12:17 Antony Lee 閱讀(225) | 評論 (0)編輯 收藏
         摘要: Core Python Programming 學習筆記(3)  閱讀全文
    posted @ 2011-03-15 22:15 Antony Lee 閱讀(314) | 評論 (0)編輯 收藏
         摘要: Core Python Programming 2 學習筆記(2)  閱讀全文
    posted @ 2011-03-13 22:30 Antony Lee 閱讀(234) | 評論 (0)編輯 收藏
         摘要: Core Python學習筆記(一) : Python基礎  閱讀全文
    posted @ 2011-03-13 20:50 Antony Lee 閱讀(291) | 評論 (0)編輯 收藏
         摘要: 速成Java與XML系列文章之一
    本文介紹XML的基礎知識和概念,是用Java進行XML解析的基礎。  閱讀全文
    posted @ 2011-01-09 16:12 Antony Lee 閱讀(525) | 評論 (0)編輯 收藏

    我會爭取在近期用Python寫個圖形界面。之所以現在不寫,是因為現在我還不會……

    原理很簡單,就是用程序去獲得騰訊星座網站的源碼,然后簡單解析一下就可以了。原來試圖用dom解析,結果發現騰訊和新浪的網站都會解析出錯,一狠心干脆直接用字符串的替換。

    Java那個程序的功能多一些,寫了讀取“明天”、“本周”、“下周”、“本月”、“下月”的功能,主函數中提供了讀取“明天”的功能,默認會在當前工作目錄下生成“yuncheng_XXX.txt”文件。實在懶得寫客戶端了,甚至于命令行客戶端都不想寫了,因為昨天寫這個破東西寫到了11點半,原以為很簡單的呢,結果郁悶壞了。真是水平大幅度下滑啊。

    今天晚上用python寫了一個程序,這是我用Python寫的第一個程序喲~~(如果不算helloworld的話。。。)這個程序功能比較簡單,只能獲得明日運程。

    剛剛寫出來而已,注釋很亂,代碼很亂。寫這篇博客只是因為母雞心態:好歹有了個新東西了,怎么也得讓我叫喚兩聲吧。

    下載地址:

    http://www.rayfile.com/files/bc9485dc-88e8-11de-b777-0014221f469f/
    posted @ 2009-08-15 00:32 Antony Lee 閱讀(546) | 評論 (0)編輯 收藏
    最近在學習Spring。某大人跟我說,Spring的AOP其實就是Java反射中的動態代理。OK,那我就從動態代理開始看起。

    一、基本概念
    所謂動態代理,基本上是如下場景:假設我有個接口IHelloWorld

    public interface IHelloWorld{
    void sayHello();
    }


    我再有一個實現類HelloWorldImpl實現了IHelloWorld接口
    public class HelloWorldImpl implements IHelloWorld{
    public void sayHello(){
    System.out.println(
    "Hello, World");
    }
    }


    這樣,我就可以創建一個HelloWorldImpl對象,來實現IHelloWorld中定義的服務。

     問題是,現在,我打算為HelloWorldImpl增強功能,需要在調用sayHello方法前后各執行一些操作。在有些情況下,你無法修改HelloWorldImpl的源代碼,那怎么辦呢?
    從道理上來說,我們可以攔截對HelloWorldImpl對象里sayHello()函數的調用。也就是說,每當有代碼調用sayHello函數時,我們都把這種調用請求攔截下來之后,做自己想做的事情。

     那怎么攔截呢?

     首先,需要開發一個InvocationHandler。這個東東表示的是,你攔截下函數調用之后,究竟想干什么。InvocationHandler是一個接口,里面的聲明的函數只有一個:
    Object invoke(Object proxy, Method method, Object[] args) throws Throwable
    這個函數表示一次被攔截的函數調用。因此,proxy表示這個被攔截的調用,原本是對哪個對象調用的;method表示這個被攔截的調用,究竟是調用什么方法;args表示這個被攔截的調用里,參數分別是什么。

     我們下面寫一個攔截器,讓他在函數調用之前和之后分別輸出一句話。

    import java.lang.reflect.*;

    public class HelloHandler implements InvocationHandler{
    Object oriObj;

    public HelloProxy(Object obj){
    oriObj 
    = obj;
    }

    public Object invoke(Object proxy, Method m, Object[] args) throws Throwable{
    Object result 
    = null;

    //在函數調用前輸出一些信息
        System.out.println("################################");
    String methodName 
    = m.getName();
    System.out.println(
    "method name : " + methodName);
    doBefore();

    //利用反射,進行真正的調用
        result = m.invoke(oriObj, args);

    //在函數調用后執行
        doAfter();
    System.out.println(
    "################################");
    return result;
    }

    public void doBefore(){
    System.out.println(
    "Do Before");
    }
    public void doAfter(){
    System.out.println(
    "Do After");
    }
    }

     有了這個Handler之后,下面要做的,就是把這個Handler和一個IHelloWorld類型的對象裝配起來。重點的函數只有一個,那就是java.lang.reflect.Proxy類中的一個靜態工廠方法:
    public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException
    這個方法返回一個對象,我們稱返回的對象為代理對象(proxy)。
    而后,我們就不把真正的原對象暴露給外接,而使用這個代理對象。這個代理對象接受對源對象的一切函數調用(也就是把所有調用都攔截了),然后根據我們寫的InvocationHandler,來對函數進行處理。

     產生代理對象的過程,我把它理解成一個裝配的過程:由源對象、源對象實現的接口、InvocationHandler裝配產生一個代理對象。

     相應的測試代碼如下:

    public class TestHello{
    public static void main(String args[])throws Exception{
    HelloWorldImpl h 
    = new HelloWorldImpl();

    Object proxy 
    = Proxy.newProxyInstance(
    h.getClass().getClassLoader(),
    new Class[]{IHelloWorld.class},
    new HelloProxy(h)
    );
    ((IHelloWorld)proxy).sayHello();
    }
    }

     利用ant編譯運行的結果:
    [java] ################################
    [java] method name : sayHello
    [java] Do Before
    [java] Hello, World
    [java] Do After
    [java] ################################


    二、更多理解
    我們看產生代理對象的newProxyInstance函數的聲明:
    public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException

     這個函數的第一個參數是ClassLoader,第三個參數是InvocationHandler,基本都沒什么問題。
    第二個參數是一個Class類型的數組,名字叫interfaces,表示的是產生的動態代理對象實現的接口。

     仔細想想,有兩個問題。第一,產生一個代理對象,需要源對象么?第二,我能不能產生一個動態代理對象,來實現源對象沒有實現的接口?

     第一個問題和第二個問題其實是一致的。我們完全可以脫離源對象,而直接產生一個代理對象,也可以利用動態代理,讓源對象實現更多的接口,為源對象增強功能。

     例如,假設我們希望讓源對象實現java.io.Closeable接口,則首先修改一下我們的Handler的invoke方法,讓他在獲取colse方法時,不要傳遞給源對象(因為源對象沒有實現該方法):

    public Object invoke(Object proxy, Method m, Object[] args) throws Throwable{
    Object result 
    = null;

    //在函數調用前輸出一些信息
      System.out.println("################################");
    String methodName 
    = m.getName();
    System.out.println(
    "method name : " + methodName);
    doBefore();

    //判斷是否是Closeabled的方法
      if (m.getDeclaringClass().isAssignableFrom(java.io.Closeable.class)){
    System.out.println(
    "I got the close() method!");
    }
    else{
    //傳遞給源對象
    //利用反射,進行真正的調用
        result = m.invoke(oriObj, args);
    }

    //在函數調用后執行
      doAfter();
    System.out.println(
    "################################");
    return result;
    }

     然后,我們在裝配的過程中,改變一下參數,并強轉之后調用一下close方法:

    Object proxy = Proxy.newProxyInstance(
    h.getClass().getClassLoader(),
    new Class[]{IHelloWorld.class,java.io.Closeable.class},
    new HelloProxy(h)
    );
    ((Closeable)proxy).close();

     ant運行結果:
    [java] ################################
    [java] method name : close
    [java] Do Before
    [java] I got the close() method!
    [java] Do After
    [java] ################################

    三、更多的代理~
    我們現在能夠讓sayHello()函數執行之前和之后,輸出一些內容了。那如果我還想在裝配一個Handler呢?
    最簡單的方法:

    Object proxy = Proxy.newProxyInstance(
    h.getClass().getClassLoader(),
    new Class[]{IHelloWorld.class, java.io.Closeable.class},
    new HelloProxy(h)
    );
    Object proxy2 
    = Proxy.newProxyInstance(
    h.getClass().getClassLoader(),
    new Class[]{IHelloWorld.class, java.io.Closeable.class},
    new HelloProxy(proxy)
    );
    ((IHelloWorld)proxy2).sayHello();

    ant運行結果:
    [java] ################################
    [java] method name : sayHello
    [java] Do Before
    [java] ################################
    [java] method name : sayHello
    [java] Do Before
    [java] Hello, World
    [java] Do After
    [java] ################################
    [java] Do After
    [java] ################################

    不用我解釋了吧!

    posted @ 2009-03-02 22:28 Antony Lee 閱讀(515) | 評論 (0)編輯 收藏

    <2009年3月>
    22232425262728
    1234567
    891011121314
    15161718192021
    22232425262728
    2930311234

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    文章分類

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 182tv免费视频在线观看| 国产免费一区二区视频| 国国内清清草原免费视频99| 亚洲韩国—中文字幕| 免费视频精品一区二区三区 | 无码视频免费一区二三区| 久久久久久久亚洲Av无码| 热re99久久6国产精品免费| 久久久久久亚洲精品成人| 精品一区二区三区无码免费视频| 亚洲丝袜美腿视频| 18禁免费无码无遮挡不卡网站 | av无码国产在线看免费网站 | 亚洲砖码砖专无区2023| AA免费观看的1000部电影| 久久亚洲国产最新网站| 影音先锋在线免费观看| 黄色免费网址大全| 日韩一卡2卡3卡4卡新区亚洲| 国产一精品一AV一免费| 亚洲精品中文字幕乱码影院| 毛片A级毛片免费播放| 日韩在线视频免费 | 亚洲人成网男女大片在线播放| 成年性午夜免费视频网站不卡| 国产精品亚洲精品久久精品| 亚洲午夜成人精品电影在线观看| 嫩草成人永久免费观看| 亚洲一级片在线观看| 亚洲av区一区二区三| 182tv免费视频在线观看 | 亚洲熟伦熟女专区hd高清| mm1313亚洲精品无码又大又粗| 最近2019中文免费字幕在线观看| 亚洲国产成a人v在线| 亚洲成a人片在线观看日本麻豆| 日本免费一区二区久久人人澡| 亚洲国产精品一区二区三区在线观看 | 久久精品国产亚洲AV未满十八| 在线观看亚洲精品国产| 最新猫咪www免费人成|