example of how to configure the XML parser factory for Checkstyle,
and how to support XInclude processing
This commit is contained in:
parent
1ae462f726
commit
114baaccac
|
|
@ -0,0 +1,21 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
<?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="T:/checkstyle/test/xiinclude/treewalker.xml"
|
||||
xmlns:xi="http://www.w3.org/2003/XInclude"/>
|
||||
</module>
|
||||
</module>
|
||||
|
|
@ -0,0 +1,142 @@
|
|||
<?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>
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
<?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>
|
||||
|
|
@ -76,6 +76,10 @@
|
|||
<li class="body">Added check that checks for a modified control variable in a for loop,
|
||||
contributed by Daniel Grenner
|
||||
(module ModifiedControlVariable, patch 927680).</li>
|
||||
|
||||
<li class="body">Added example of how to configure the XML parser factory
|
||||
for Checkstyle, and how to support XInclude processing
|
||||
(contrib/examples/XInclude, request 905169).</li>
|
||||
|
||||
</ul>
|
||||
|
||||
|
|
@ -821,7 +825,7 @@
|
|||
<li class="body">Added DTD for XML output (request 622157).</li>
|
||||
<li class="body">Added an XSL stylesheet to convert XML output to plain text, contributed by Jon Scott Stevens.</li>
|
||||
<li class="body">Added portuguese localization, contributed by Pedro Morais.</li>
|
||||
<li class="body">Added finnish localization, contributed by Ville Skyttä.</li>
|
||||
<li class="body">Added finnish localization, contributed by Ville Skyttä.</li>
|
||||
<li class="body">Added french localization, contributed by Pierre Dittgen.</li>
|
||||
</ul>
|
||||
|
||||
|
|
@ -877,7 +881,7 @@
|
|||
<li class="body">Incorporate patch 566855 from Rob Worth to optionally check that parenthesis are padded with spaces.</li>
|
||||
<li class="body">Incorporate patch 590931 from Vijay Aravamudhan to improve documentation of the build.xml file.</li>
|
||||
<li class="body">Incorporate patch from Vijay Aravamudhan to enforce requiring @version tag (request 543964).</li>
|
||||
<li class="body">Incorporate patch 607481 from Ville Skyttä to enforce wrap on operator at EOL.</li>
|
||||
<li class="body">Incorporate patch 607481 from Ville Skyttä to enforce wrap on operator at EOL.</li>
|
||||
</ul>
|
||||
|
||||
<p class="body">
|
||||
|
|
@ -1007,5 +1011,5 @@
|
|||
</table>
|
||||
<hr />
|
||||
<div><a href="index.html">Back to the Checkstyle Home Page</a></div>
|
||||
<p class="copyright">Copyright © 2002-2004 Oliver Burn. All rights Reserved.</p>
|
||||
</body> </html>
|
||||
<p class="copyright">Copyright © 2002-2004 Oliver Burn. All rights Reserved.</p>
|
||||
</body> </html>
|
||||
Loading…
Reference in New Issue