consolidate charset handling.

This commit is contained in:
Oliver Burn 2008-11-09 10:19:43 +00:00
parent 13b7c63433
commit 2e4762f171
9 changed files with 52 additions and 48 deletions

View File

@ -18,8 +18,9 @@
<allow pkg="org.xml.sax"/>
<!-- The local ones -->
<allow pkg="com.puppycrawl.tools.checkstyle.grammars" local-only="true"/>
<allow class="java.nio.charset.Charset" local-only="true"/>
<allow class="java.security.MessageDigest" local-only="true"/>
<allow pkg="com.puppycrawl.tools.checkstyle.grammars" local-only="true"/>
<allow pkg="org.apache.commons.cli" local-only="true"/>
<allow pkg="org.apache.tools.ant" local-only="true"/>
@ -27,7 +28,6 @@
<allow pkg="java.beans"/>
<allow pkg="java.lang.reflect"/>
<allow pkg="java.text"/>
<allow class="java.nio.charset.Charset"/>
<allow class="com.puppycrawl.tools.checkstyle.grammars.CommentListener"
local-only="true"/>
<allow class="com.puppycrawl.tools.checkstyle.grammars.GeneratedJavaTokenTypes"

View File

@ -38,6 +38,8 @@ import com.puppycrawl.tools.checkstyle.api.Utils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Locale;
import java.util.Set;
@ -98,6 +100,9 @@ public class Checker extends AutomaticBean implements MessageDispatcher
*/
private SeverityLevel mSeverityLevel = SeverityLevel.ERROR;
/** Name of a charset */
private String mCharset = System.getProperty("file.encoding", "UTF-8");
/**
* Creates a new <code>Checker</code> instance.
* The instance needs to be contextualized and configured.
@ -240,7 +245,7 @@ public class Checker extends AutomaticBean implements MessageDispatcher
// Prepare to start
fireAuditStarted();
for (FileSetCheck fsc : mFileSetChecks) {
fsc.beginProcessing();
fsc.beginProcessing(getCharset());
}
// Process each file
@ -249,8 +254,8 @@ public class Checker extends AutomaticBean implements MessageDispatcher
fireFileStarted(fileName);
final TreeSet<LocalizedMessage> fileMessages = Sets.newTreeSet();
try {
// TODO: Need to use , getCharset()
final String[] lines = Utils.getLines(f.getAbsolutePath());
final String[] lines = Utils.getLines(f.getAbsolutePath(),
getCharset());
final List<String> theLines = Lists.newArrayList(lines);
for (FileSetCheck fsc : mFileSetChecks) {
fileMessages.addAll(fsc.process(f, theLines));
@ -567,4 +572,25 @@ public class Checker extends AutomaticBean implements MessageDispatcher
{
mModuleClassLoader = aModuleClassLoader;
}
/** @return the name of the charset */
public String getCharset()
{
return mCharset;
}
/**
* Sets a named charset.
* @param aCharset the name of a charset
* @throws UnsupportedEncodingException if aCharset is unsupported.
*/
public void setCharset(String aCharset)
throws UnsupportedEncodingException
{
if (!Charset.isSupported(aCharset)) {
final String message = "unsupported charset: '" + aCharset + "'";
throw new UnsupportedEncodingException(message);
}
mCharset = aCharset;
}
}

View File

@ -19,8 +19,6 @@
package com.puppycrawl.tools.checkstyle.api;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.TreeSet;
@ -43,9 +41,6 @@ public abstract class AbstractFileSetCheck
/** collects the error messages */
private final LocalizedMessages mMessages = new LocalizedMessages();
/** Name of a charset */
private String mCharset = System.getProperty("file.encoding", "UTF-8");
/**
* Called to process a file that matches the specified file extensions.
* @param aFile the file to be processed
@ -64,7 +59,7 @@ public abstract class AbstractFileSetCheck
}
/** {@inheritDoc} */
public void beginProcessing()
public void beginProcessing(String aCharset)
{
}
@ -85,27 +80,6 @@ public abstract class AbstractFileSetCheck
{
}
/** @return the name of the charset */
public String getCharset()
{
return mCharset;
}
/**
* Sets a named charset.
* @param aCharset the name of a charset
* @throws UnsupportedEncodingException if aCharset is unsupported.
*/
public void setCharset(String aCharset)
throws UnsupportedEncodingException
{
if (!Charset.isSupported(aCharset)) {
final String message = "unsupported charset: '" + aCharset + "'";
throw new UnsupportedEncodingException(message);
}
mCharset = aCharset;
}
/** {@inheritDoc} */
public final void setMessageDispatcher(MessageDispatcher aDispatcher)
{

View File

@ -47,8 +47,11 @@ public interface FileSetCheck
/** Cleans up the object. **/
void destroy();
/** Called when about to be called to process a set of files. */
void beginProcessing();
/**
* Called when about to be called to process a set of files.
* @param aCharset the character set used to read the files.
*/
void beginProcessing(String aCharset);
/**
* Request to process a file. The implementation should use the supplied

View File

@ -70,9 +70,9 @@ public class TranslationCheck
}
@Override
public void beginProcessing()
public void beginProcessing(String aCharset)
{
super.beginProcessing();
super.beginProcessing(aCharset);
mPropertyFiles.clear();
}

View File

@ -193,6 +193,8 @@ public final class StrictDuplicateCodeCheck extends AbstractFileSetCheck
/** total number of duplicates found */
private int mDuplicates;
/** the charset used to load files. */
private String mCharset;
/** Creates a new instance of this class. */
public StrictDuplicateCodeCheck()
@ -221,9 +223,10 @@ public final class StrictDuplicateCodeCheck extends AbstractFileSetCheck
}
@Override
public void beginProcessing()
public void beginProcessing(String aCharset)
{
super.beginProcessing();
super.beginProcessing(aCharset);
mCharset = aCharset;
mFiles.clear();
}
@ -502,7 +505,7 @@ public final class StrictDuplicateCodeCheck extends AbstractFileSetCheck
if (cachedLines != null) {
return cachedLines;
}
final String charset = getCharset();
final String charset = mCharset;
final String[] lines = getTrimmed(Utils.getLines(path, charset));
mTrimmedLineCache.put(path, lines);
return lines;

View File

@ -116,9 +116,9 @@ public final class CrossLanguageRegexpHeaderCheck extends AbstractFileSetCheck
}
@Override
public void beginProcessing()
public void beginProcessing(String aCharset)
{
super.beginProcessing();
super.beginProcessing(aCharset);
mRegexpHeaderChecker = new RegexpHeaderChecker(mHeaderInfo,
new FileSetCheckViolationMonitor());
}

View File

@ -47,9 +47,9 @@ public class JavadocPackageCheck extends AbstractFileSetCheck
}
@Override
public void beginProcessing()
public void beginProcessing(String aCharset)
{
super.beginProcessing();
super.beginProcessing(aCharset);
mDirectoriesChecked.clear();
}
@ -62,7 +62,7 @@ public class JavadocPackageCheck extends AbstractFileSetCheck
return;
}
mDirectoriesChecked.add(dir);
// Check for the preferred file.
final File packageInfo = new File(dir, "package-info.java");
final File packageHtml = new File(dir, "package.html");
@ -71,10 +71,8 @@ public class JavadocPackageCheck extends AbstractFileSetCheck
if (packageHtml.exists()) {
log(0, "javadoc.legacyPackageHtml");
}
}
else if (mAllowLegacy && packageHtml.exists()) {
}
else {
else if (!mAllowLegacy || !packageHtml.exists()) {
log(0, "javadoc.packageInfo");
}
}

View File

@ -68,7 +68,7 @@ public abstract class BaseCheckTestSupport
new DefaultConfiguration("configuration");
final DefaultConfiguration twConf = createCheckConfig(TreeWalker.class);
// make sure that the tests always run with this charset
twConf.addAttribute("charset", "iso-8859-1");
dc.addAttribute("charset", "iso-8859-1");
dc.addChild(twConf);
twConf.addChild(aConfig);
return dc;