【转】JTidy学习笔记

 

转自:http://hedong.3322.org/archives/000252.html
JTidyHTML Tidy(一个HTML语法检查器和优雅的打印编排工具)的Java移植,除了本身具有的清除HTML文件难看或错误内容的功能外,还提供了一个DOM接口,程序员可以将JTidy当作一个处理HTML文件的DOM解析器来使用。

一、下载及编译


jtidy
是个sourceforge的开源项目,可不知为什么,不能通过CVS来下载其最新代码,只好下载了2001年8月1日的发布版
下载之后,运行一下ant就可以了,不用加什么参数,结果是重新生成了doc文档和build/Tidy.jar.

二、使用Tidy.jar


1
、整理HTML文件
在jtidy项目的文档中,有一个例程Test16.java,用以演示将一个URL所对应的HTML文件进行整理后以xml格式输出。这个例程在处理汉字时有问题,主要是汉字成了乱码。用tidy.setRawOut(true)方法强制输出也不能成功。经摸索,发现需对char-encoding 进行设置方可。文后一个附表,是HTMLtidy支持的参数,我没有全部测试,估计能全部被JTidy支持。由此,将此例程进行修改名为 Test17.java,并重新定义命令参数如下:

java Test17 <html_file> <new_html_file> <log_file> <config_file>

,此时会根据配置文件的设定,直接处理HTML文件。
源码如下

import org.w3c.tidy.Tidy;

import java.io.BufferedInputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;




/**

 * HTML
文件整理

 *

 * 从Test16.java修改而来

 *

 * 运行参数

 * java Test17 <html_file> <new_html_file> <log_file> <config_file>

 *

 */

public class Test17 implements Runnable {

    private String srcFileName;

    private String outFileName;

    private String errOutFileName;

    private String configFileName;

 

public Test17(String srcFileName, String outFileName, String errOutFileName,

        String confName) {

        this.srcFileName = srcFileName;

        this.outFileName = outFileName;

        this.errOutFileName = errOutFileName;

        this.configFileName= confName;

    }

 

public void run() {

        BufferedInputStream in;

        FileOutputStream out;

        Tidy tidy = new Tidy();

 tidy.setConfigurationFromFile(configFileName);

        try {

            tidy.setErrout(new PrintWriter(new FileWriter(errOutFileName), true));

            in = new BufferedInputStream(new FileInputStream(srcFileName));

            out = new FileOutputStream(outFileName);

            tidy.parse(in, out);

        } catch (IOException e) {

            System.out.println(this.toString() + e.toString());

        }

    }

 

public static void main(String[] args) {

        Test17 t1 = new Test17(args[0], args[1], args[2], args[3]);

        Thread th1 = new Thread(t1);

        th1.start();

    }

}


JTidy
设置文件参考如下:

// sample config file for HTML tidy
indent: auto
indent-spaces: 2
wrap: 72
markup: yes
output-xml: yes
input-xml: no
show-warnings: yes
numeric-entities: yes
quote-marks: yes
quote-nbsp: yes
quote-ampersand: no
break-before-br: no
uppercase-tags: no
uppercase-attributes: no
char-encoding: raw
new-inline-tags: cfif, cfelse, math, mroot,
mrow, mi, mn, mo, msqrt, mfrac, msubsup, munderover,
munder, mover, mmultiscripts, msup, msub, mtext,
mprescripts, mtable, mtr, mtd, mth
new-blocklevel-tags: cfoutput, cfquery
new-empty-tags: cfelse


2
、利用其JDOM接口处理HTML文件内容
文档中的例程TestDOM.java,通过DOM接口实现了对HTML内容进行显示。有两个问题需要注意一下,一是对输入的HTML文件要求比较严格,我试用时只有经过整理的XHTML文件才被接受;二是汉字显示的问题,我用以下面的函数进行显示前处理。

public static String converttoGB(String s)
{
try
{
byte abyte0[] = s.getBytes("ISO-8859-1");
return new String(abyte0, "gb2312");
}
catch(Exception exception)
{
return s;
}
}

三、JTidy与NekoHTML


对这两者进行比较,似乎勉为其难。不过,我看NekoHTML时,发现JTidy也有解析HTML的功能,这就是我看JTidy的原因。
JTidy最新的班本是2001年8月发布的,一看日期就让我失望。所以,如果要解析HTML的话,用NekoHTML应是个不错的选择。这也是众多开源项目解析HTML时,少用JTidy的缘故吧。
毕竟,JTidy主要是整理(tidy)HTML文件的,更多的是关注HTML的规范,尤其是当我看到那一堆设置参数时,不由得喜欢起来。要想整理HTML,用JTidy没错。 附表:HTML Tidy设置参考列表。

 

选项

值域

解释

tidy-mark

bool

If set to yes (the default) Tidy will add a meta element to the document head to indicate that the document has been tidied. To suppress this, set tidy-mark to no. Tidy won't add a meta element if one is already present.

markup

bool

Determines whether Tidy generates a pretty printed version of the markup. Bool values are either yes or no. Note that Tidy won't generate a pretty printed version if it finds unknown tags, or missing trailing quotes on attribute values, or missing trailing '>' on tags. The default is yes.

wrap

number

Sets the right margin for line wrapping. Tidy tries to wrap lines so that they do not exceed this length. The default is 66. Set wrap to zero if you want to disable line wrapping.

wrap-attributes

bool

If set to yes, attribute values may be wrapped across lines for easier editing. The default is no. This option can be set independently of wrap-scriptlets

wrap-script-literals

bool

If set to yes, this allows lines to be wrapped within string literals that appear in script attributes. The default is no. The example shows how Tidy wraps a really really long script string literal inserting a backslash character before the linebreak:

wrap-asp

bool

If set to no, this prevents lines from being wrapped within ASP pseudo elements, which look like: <%?? ...?? %>. The default is yes.

wrap-jste

bool

If set to no, this prevents lines from being wrapped within JSTE pseudo elements, which look like: <#?? ...?? #>. The default is yes.

wrap-php

bool

If set to no, this prevents lines from being wrapped within PHP pseudo elements. The default is yes.

literal-attributes

bool

If set to yes, this ensures that whitespace characters within attribute values are passed through unchanged. The default is no.

tab-size

number

Sets the number of columns between successive tab stops. The default is 4. It is used to map tabs to spaces when reading files. Tidy never outputs files with tabs.

indent

no, yes or auto

If set to yes, Tidy will indent block-level tags. The default is no. If set to auto Tidy will decide whether or not to indent the content of tags such as title, h1-h6, li, td, th, or p depending on whether or not the content includes a block-level element. You are advised to avoid setting indent to yes as this can expose layout bugs in some browsers.

indent-spaces

number

Sets the number of spaces to indent content when indentation is enabled. The default is 2 spaces.

indent-attributes

bool

If set to yes, each attribute will begin on a new line. The default is no.

hide-endtags

bool

If set to yes, optional end-tags will be omitted when generating the pretty printed markup. This option is ignored if you are outputting to XML. The default is no.

input-xml

bool

If set to yes, Tidy will use the XML parser rather than the error correcting HTML parser. The default is no.

output-xml

bool

If set to yes, Tidy will generate the pretty printed output writing it as well-formed XML. Any entities not defined in XML 1.0 will be written as numeric entities to allow them to be parsed by an XML parser. The tags and attributes will be in the case used in the input document, regardless of other options. The default is no.

add-xml-pi

bool

 

add-xml-decl

bool

If set to yes, Tidy will add the XML declatation when outputting XML or XHTML. The default is no. Note that if the input document includes an <?xml?> declaration then it will appear in the output independent of the value of this option.

output-xhtml

bool

If set to yes, Tidy will generate the pretty printed output writing it as extensible HTML. The default is no. This option causes Tidy to set the doctype and default namespace as appropriate to XHTML. If a doctype or namespace is given they will checked for consistency with the content of the document. In the case of an inconsistency, the corrected values will appear in the output. For XHTML, entities can be written as named or numeric entities according to the value of the "numeric-entities" property. The tags and attributes will be output in the case used in the input document, regardless of other options.

doctype

omit, auto, strict, loose or <fpi>

This property controls the doctype declaration generated by Tidy. If set to omit the output file won't contain a doctype declaration. If set to auto (the default) Tidy will use an educated guess based upon the contents of the document. If set to strict, Tidy will set the doctype to the strict DTD. If set to loose, the doctype is set to the loose (transitional) DTD. Alternatively, you can supply a string for the formal public identifier (fpi) for example: If you specify the fpi for an XHTML document, Tidy will set the system identifier to the empty string. Tidy leaves the document type for generic XML documents unchanged.

char-encoding

raw, ascii, latin1, utf8 or iso2022

Determines how Tidy interprets character streams. For ascii, Tidy will accept Latin-1 character values, but will use entities for all characters whose value > 127. For raw, Tidy will output values above 127 without translating them into entities. For latin1 characters above 255 will be written as entities. For utf8, Tidy assumes that both input and output is encoded as UTF-8. You can use iso2022 for files encoded using the ISO2022 family of encodings e.g. ISO 2022-JP. The default is ascii.

numeric-entities

bool

Causes entities other than the basic XML 1.0 named entities to be written in the numeric rather than the named entity form. The default is no

quote-marks

bool

If set to yes, this causes " characters to be written out as &quot; as is preferred by some editing environments. The apostrophe character ' is written out as ' since many web browsers don't yet support &apos;. The default is no.

quote-nbsp

bool

If set to yes, this causes non-breaking space characters to be written out as entities, rather than as the Unicode character value 160 (decimal). The default is yes.

quote-ampersand

bool

If set to yes, this causes unadorned & characters to be written out as &amp;. The default is yes.

assume-xml-procins

bool

If set to yes, this changes the parsing of processing instructions to require ?> as the terminator rather than >. The default is no. This option is automatically set if the input is in XML.

fix-backslash

bool

If set to yes, this causes backslash characters """ in URLs to be replaced by forward slashes "/". The default is yes.

break-before-br

bool

If set to yes, Tidy will output a line break before each <br> element. The default is no.

uppercase-tags

bool

Causes tag names to be output in upper case. The default is no resulting in lowercase, except for XML input where the original case is preserved.

uppercase-attributes

bool

If set to yes attribute names are output in upper case. The default is no resulting in lowercase, except for XML where the original case is preserved.

word-2000

bool

If set to yes, Tidy will go to great pains to strip out all the surplus stuff Microsoft Word 2000 inserts when you save Word documents as "Web pages". The default is no. Note that Tidy doesn't yet know what to do with VML markup from Word, but in future I hope to be able to map VML to SVG. Microsoft has developed its own optional filter for exporting to HTML, and the 2.0 version is much improved. You can download the filter free from the Microsoft Office Update site.

clean

bool

If set to yes, causes Tidy to strip out surplus presentational tags and attributes replacing them by style rules and structural markup as appropriate. It works well on the html saved from Microsoft Office'97. The default is no.

logical-emphasis

bool

If set to yes, causes Tidy to replace any occurrence of i by em and any occurrence of b by strong. In both cases, the attributes are preserved unchanged. The default is no. This option can now be set independently of the clean and drop-font-tags options.

drop-empty-paras

bool

If set to yes, empty paragraphs will be discarded. If set to no, empty paragraphs are replaced by a pair of br elements as HTML4 precludes empty paragraphs. The default is yes.

drop-font-tags

bool

If set to yes together with the clean option (see above), Tidy will discard font and center tags rather than creating the corresponding style rules. The default is no.

enclose-text

bool

If set to yes, this causes Tidy to enclose any text it finds in the body element within a p element. This is useful when you want to take an existing html file and use it with a style sheet. Any text at the body level will screw up the margins, but wrap the text within a p element and all is well! The default is no.

enclose-block-text

bool

If set to yes, this causes Tidy to insert a p element to enclose any text it finds in any element that allows mixed content for HTML transitional but not HTML strict. The default is no.

fix-bad-comments

bool

If set to yes, this causes Tidy to replace unexpected hyphens with "=" characters when it comes across adjacent hyphens. The default is yes. This option is provided for users of Cold Fusion which uses the comment syntax: <!---?? --->

add-xml-space

bool

If set to yes, this causes Tidy to add xml:space="preserve" to elements such as pre, style and script when generating XML. This is needed if the whitespace in such elements is to be parsed appropriately without having access to the DTD. The default is no.

alt-text

string

This allows you to set the default alt text for img attributes. This feature is dangerous as it suppresses further accessibility warnings. YOU ARE RESPONSIBLE FOR MAKING YOUR DOCUMENTS ACCESSIBLE TO PEOPLE WHO CAN'T SEE THE IMAGES!!!

write-back

bool

If set to yes, Tidy will write back the tidied markup to the same file it read from. The default is no. You are advised to keep copies of important files before tidying them as on rare occasions the result may not always be what you expect.

keep-time

bool

If set to yes, Tidy won't alter the last modified time for files it writes back to. The default is yes. This allows you to tidy files without effecting which ones will be uploaded to the Web server when using a tool such as 'SiteCopy'. Note that this feature may not work on some platforms.

error-file

filename

Writes errors and warnings to the named file rather than to stderr.

show-warnings

bool

If set to no, warnings are suppressed. This can be useful when a few errors are hidden in a flurry of warnings. The default is yes.

quiet

bool

If set to yes, Tidy won't output the welcome message or the summary of the numbers of errors and warnings. The default is no.

gnu-emacs

bool

If set to yes, Tidy changes the format for reporting errors and warnings to a format that is more easily parsed by GNU Emacs. The default is no.

split

bool

If set to yes Tidy will use the input file to create a sequence of slides, splitting the markup prior to each successive <h2>. You can see an example of the results in a recent talk I made on XHTML. The slides are written to "slide1.html", "slide2.html" etc. The default is no.

new-empty-tags

tag1, tag2, tag3

Use this to declare new empty inline tags. The option takes a space or comma separated list of tag names. Unless you declare new tags, Tidy will refuse to generate a tidied file if the input includes previously unknown tags. Remember to also declare empty tags as either inline or blocklevel, see below.

new-inline-tags

tag1, tag2, tag3

Use this to declare new non-empty inline tags. The option takes a space or comma separated list of tag names. Unless you declare new tags, Tidy will refuse to generate a tidied file if the input includes previously unknown tags.

new-blocklevel-tags

tag1, tag2, tag3

Use this to declare new block-level tags. The option takes a space or comma separated list of tag names. Unless you declare new tags, Tidy will refuse to generate a tidied file if the input includes previously unknown tags. Note you can't change the content model for elements such as table, ul, ol and dl. This is explained in more detail in the release notes.

new-pre-tags

tag1, tag2, tag3

Use this to declare new tags that are to be processed in exactly the same way as HTML's pre element. The option takes a space or comma separated list of tag names. Unless you declare new tags, Tidy will refuse to generate a tidied file if the input includes previously unknown tags. Note you can't as yet add new CDATA elements (similar to script).

转载于:https://www.cnblogs.com/xiaoman_890/archive/2009/01/16/1376997.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值