142 lines
5.5 KiB
HTML
142 lines
5.5 KiB
HTML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!DOCTYPE html
|
|
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
|
"DTD/xhtml1-strict.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<title>XInclude Processing</title>
|
|
<link rel="stylesheet" type="text/css" href="../../../docs/mystyle.css"/>
|
|
</head>
|
|
|
|
<body>
|
|
<h1>XInclude Processing</h1>
|
|
<h3>Description</h3>
|
|
<p>This is an example of how you can separate a Checkstyle configuration file
|
|
into several files and process the configuration using
|
|
<a href="http://www.w3.org/TR/2003/WD-xinclude-20031110/">XInclude processing</a>.
|
|
This requires a SAX parser that supports XML namespaces.
|
|
First we give an example a SAX parser factory that produces parsers supporting XML namespaces
|
|
and indicate how to configure your system to use this factory.
|
|
Then we give an example XML configuration files with XInclude processing and
|
|
an ant target that uses the <a href="../../../docs/anttask.html">Checkstyle ant task</a>
|
|
to check a Java source file with the configuration files.
|
|
</p>
|
|
<h3>Parsers</h3>
|
|
<p>SAX parser factory <a href="NamespacesSAXParserFactoryImpl.java"><span class="code">NamespacesSAXParserFactoryImpl</span></a>
|
|
is an example of a factory that produces parsers
|
|
supporting XML namespaces:
|
|
<pre>
|
|
package com.puppycrawl.tools.checkstyle;
|
|
|
|
import org.apache.xerces.jaxp.SAXParserFactoryImpl;
|
|
|
|
/**
|
|
* A parser factory that produces parsers that support XML namespaces.
|
|
* @author Rick Giles
|
|
* @version May 28, 2004
|
|
*/
|
|
public class NamespacesSAXParserFactoryImpl extends SAXParserFactoryImpl
|
|
{
|
|
/**
|
|
* Constructs a NamespacesSAXParserFactoryImpl. Initializes
|
|
* it to produce parsers that support XML namespaces.
|
|
*/
|
|
public NamespacesSAXParserFactoryImpl()
|
|
{
|
|
super();
|
|
setNamespaceAware(true);
|
|
}
|
|
}
|
|
</pre>
|
|
</p>
|
|
<p>
|
|
In order to use <span class="code">NamespacesSAXParserFactoryImpl</span> as the
|
|
SAX parser factory, place <span class="code">NamespacesSAXParserFactoryImpl</span>
|
|
in the classpath and
|
|
<a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/xml/parsers/SAXParserFactory.html#newInstance()">configure your system</a>
|
|
to load
|
|
<span class="code">NamespacesSAXParserFactoryImpl</span>
|
|
as the <span class="code">SAXParserFactory</span>.
|
|
For example, you can create a file called <span class="code">jaxp.properties</span>
|
|
in the lib subdirectory of the JRE installation with contents
|
|
<pre>
|
|
javax.xml.parsers.SAXParserFactory=com.puppycrawl.tools.checkstyle.NamespacesSAXParserFactoryImpl
|
|
</pre>
|
|
</p>
|
|
<p>
|
|
XInclude processing requires an XML parser that implements XML inclusions.
|
|
Here we use the <a href="http://xml.apache.org/xerces2-j/index.html">Xerces parser</a>
|
|
that is in the ant distribution. In order
|
|
to <a href="http://xml.apache.org/xerces2-j/faq-xinclude.html">enable Xinclude processing</a>,
|
|
you can change the parser configuration by creating a file called <span class="code">xerces.properties</span>
|
|
in the lib subdirectory of the JRE installation with contents
|
|
<pre>
|
|
org.apache.xerces.xni.parser.XMLParserConfiguration=org.apache.xerces.parsers.XIncludeParserConfiguration
|
|
</pre>
|
|
<h3>Checkstyle Configuration</h3>
|
|
</p>
|
|
<p>The Checkstyle configuration of this example is in two files.
|
|
File <a href="config.xml"><span class="code">config.xml</span></a> has an internal DTD that supports
|
|
<span class="code">xi:include</span> elements:
|
|
<pre>
|
|
<?xml version="1.0"?>
|
|
<!DOCTYPE module [
|
|
<!ELEMENT module (module|property|xi:include)*>
|
|
<!ATTLIST module name NMTOKEN #REQUIRED>
|
|
|
|
<!ELEMENT xi:include EMPTY>
|
|
<!ATTLIST xi:include
|
|
href CDATA #REQUIRED
|
|
xmlns:xi CDATA #REQUIRED
|
|
>
|
|
|
|
<!ELEMENT property EMPTY>
|
|
<!ATTLIST property
|
|
name NMTOKEN #REQUIRED
|
|
value CDATA #REQUIRED
|
|
default CDATA #IMPLIED
|
|
>
|
|
]>
|
|
<module name="Checker">
|
|
<module name="TreeWalker">
|
|
<xi:include
|
|
href="treewalker.xml"
|
|
xmlns:xi="http://www.w3.org/2003/XInclude"/>
|
|
</module>
|
|
</module>
|
|
</pre>
|
|
</p>
|
|
<p>
|
|
The configuration in <span class="code">config.xml</span> includes a second
|
|
configuration file, <a href="treewalker.xml"><span class="code">treewalker.xml</span></a>, that applies
|
|
the <span class="code">TypeName</span> module:
|
|
<pre>
|
|
<?xml version="1.0"?>
|
|
<!DOCTYPE module PUBLIC
|
|
"-//Puppy Crawl//DTD Check Configuration 1.2//EN"
|
|
"http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
|
|
<module name="TypeName">
|
|
<property name="format" value="${typename.format}"/>
|
|
</module>
|
|
</pre>
|
|
</p>
|
|
<p>
|
|
Notice that the configuration of <span class="code">treewalker.xml</span>
|
|
applies property <span class="code">${typename.format}</span>.
|
|
That propery is set in the following segment of an ant build file that uses the
|
|
Checkstyle ant task to check file <span class="code">InputHeader.java</span>
|
|
with the configuration of <span class="code">config.xml</span>:
|
|
<pre>
|
|
<taskdef
|
|
resource="checkstyletask.properties"
|
|
classpath="/path/to/checkstyle-all-@CHECKSTYLE_VERSION@.jar" />
|
|
<target name="checkstyle" description="run checkstyle">
|
|
<checkstyle file="InputHeader.java" config="config.xml">
|
|
<property key="typename.format" value="xyz" />
|
|
</checkstyle>
|
|
</target>
|
|
</pre>
|
|
</p>
|
|
</body>
|
|
</html> |