Moved all the Localization stuff into the api directory. The plan is reused

the localisation support in the checks.
This commit is contained in:
Oliver Burn 2002-09-17 12:33:30 +00:00
parent ffb17cde0f
commit 672b27dd55
14 changed files with 98 additions and 55 deletions

View File

@ -18,6 +18,8 @@
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
import java.util.EventObject;
/**

View File

@ -31,6 +31,7 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import org.apache.regexp.RESyntaxException;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
/**
* This class provides the functionality to check a set of files.

View File

@ -25,18 +25,18 @@ import java.util.TreeMap;
/**
* Checks can query the CommentManager if they want to analyse comments.
* <p>
* The AST tree does not contain comments as they can occur at arbitrary places.
* Adding them to the AST tree would make it very hard to write checks, as you would
* always have to beware of comment nodes.
* </p>
* <p>
* Instead, during Java parsing comments are registered with the CommentManager, and
* Checks that need to analyse comments can use the CommentManager to access that data.
* </p>
* <p>
* Sample Checks that use the CommentManager are the to-do check and the javadoc check.
* </p>
*
* <p> The AST tree does not contain comments as they can occur at arbitrary
* places. Adding them to the AST tree would make it very hard to write
* checks, as you would always have to beware of comment nodes. </p>
*
* <p> Instead, during Java parsing comments are registered with the
* CommentManager, and Checks that need to analyse comments can use the
* CommentManager to access that data. </p>
*
* <p> Sample Checks that use the CommentManager are the to-do check and the
* javadoc check. </p>
*
* @author Lars Kühne
*/
public class CommentManager
@ -150,8 +150,8 @@ public class CommentManager
/**
* An iterator over all comments.
* @return an iterator that contains entries of type String (CPP style comments)
* or String[] (C style comments)
* @return an iterator that contains entries of type String (CPP style
* comments) or String[] (C style comments)
*/
public Iterator iterator()
{

View File

@ -39,6 +39,7 @@ import java.util.TreeSet;
import org.apache.regexp.RE;
import org.apache.regexp.RESyntaxException;
import com.puppycrawl.tools.checkstyle.api.Utils;
/**
* Represents the configuration that checkstyle uses when checking. The

View File

@ -20,6 +20,7 @@ package com.puppycrawl.tools.checkstyle;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessages;
import java.util.Map;
import java.util.HashMap;
@ -46,9 +47,11 @@ class TreeWalker
private static final Map TOKEN_VALUE_TO_NAME = new HashMap();
/** maps from token name to checks */
private Map mTokenToChecks = new HashMap();
private final Map mTokenToChecks = new HashMap();
/** all the registered checks */
private Set mAllChecks = new HashSet();
private final Set mAllChecks = new HashSet();
/** collects the error messages */
private final LocalizedMessages mMessages;
// initialise the constants
static {
@ -75,6 +78,11 @@ class TreeWalker
}
public TreeWalker(LocalizedMessages aMessages)
{
mMessages = aMessages;
}
/**
* Returns the name of a token for a given ID.
* @param aID the ID of the token name to get
@ -143,12 +151,14 @@ class TreeWalker
* @param aLines the lines of the file the AST was generated from
* @param aFilename the file name of the file the AST was generated from
*/
void walk(DetailAST aAST, String[] aLines, String aFilename)
LocalizedMessages walk(DetailAST aAST, String[] aLines, String aFilename)
{
mMessages.reset();
notifyBegin(aLines, aFilename);
aAST.setParent(null);
process(aAST);
notifyEnd();
return mMessages;
}
/**

View File

@ -31,6 +31,9 @@ import java.util.Stack;
import org.apache.regexp.RE;
import org.apache.regexp.RESyntaxException;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessages;
import com.puppycrawl.tools.checkstyle.api.Utils;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
/**
* Verifier of Java rules. Each rule verifier takes the form of

View File

@ -35,6 +35,11 @@ public abstract class Check
/** the global context for the check */
private Map mGlobalContext;
/**
* the object for collecting messages. decided to not put in the global
* context for performance and ease of use.
*/
private LocalizedMessages mMessages;
/** the context for the check across an AST */
private Map mTreeContext;
/** the context for a check across a token. */
@ -59,11 +64,20 @@ public abstract class Check
/**
* Set the global context for the check.
* @param aGlobalContext the global context
* @param aContext the context
*/
public void setGlobalContext(Map aGlobalContext)
public void setGlobalContext(Map aContext)
{
mGlobalContext = aGlobalContext;
mGlobalContext = aContext;
}
/**
* Set the global object used to collect messages.
* @param aMessages the messages to log with
*/
public void setMessages(LocalizedMessages aMessages)
{
mMessages = aMessages;
}
/**

View File

@ -16,7 +16,7 @@
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;
package com.puppycrawl.tools.checkstyle.api;
import java.util.ResourceBundle;
import java.util.Locale;
@ -59,7 +59,10 @@ public class LocalizedMessage
* @param aKey the key to locate the translation
* @param aArgs arguments for the translation
*/
LocalizedMessage(int aLineNo, int aColNo, String aKey, Object[] aArgs)
public LocalizedMessage(int aLineNo,
int aColNo,
String aKey,
Object[] aArgs)
{
mLineNo = aLineNo;
mColNo = aColNo;
@ -75,7 +78,7 @@ public class LocalizedMessage
* @param aKey the key to locate the translation
* @param aArgs arguments for the translation
*/
LocalizedMessage(int aLineNo, String aKey, Object[] aArgs)
public LocalizedMessage(int aLineNo, String aKey, Object[] aArgs)
{
this(aLineNo, 0, aKey, aArgs);
}
@ -106,7 +109,7 @@ public class LocalizedMessage
}
/** @param aLocale the locale to use for localization **/
static void setLocale(Locale aLocale)
public static void setLocale(Locale aLocale)
{
sLocale = aLocale;
}

View File

@ -16,7 +16,7 @@
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;
package com.puppycrawl.tools.checkstyle.api;
import java.util.Collections;
import java.util.ArrayList;
@ -26,7 +26,7 @@ import java.util.ArrayList;
* @author <a href="mailto:checkstyle@puppycrawl.com">Oliver Burn</a>
* @version 1.0
*/
class LocalizedMessages
public class LocalizedMessages
{
/** contains the messages logged **/
private final ArrayList mMessages = new ArrayList();
@ -40,19 +40,19 @@ class LocalizedMessages
*
* @param aTabWidth the tab width to calculate columns with
*/
LocalizedMessages(int aTabWidth)
public LocalizedMessages(int aTabWidth)
{
mTabWidth = aTabWidth;
}
/** @param aLines the lines to record messages against **/
void setLines(String[] aLines)
public void setLines(String[] aLines)
{
mLines = aLines;
}
/** @return the logged messages **/
LocalizedMessage[] getMessages()
public LocalizedMessage[] getMessages()
{
Collections.sort(mMessages);
return (LocalizedMessage[])
@ -60,7 +60,7 @@ class LocalizedMessages
}
/** Reset the object **/
void reset()
public void reset()
{
mMessages.clear();
mLines = null;
@ -70,7 +70,7 @@ class LocalizedMessages
* Logs a message to be reported
* @param aMsg the message to log
**/
void add(LocalizedMessage aMsg)
public void add(LocalizedMessage aMsg)
{
mMessages.add(aMsg);
}
@ -82,7 +82,7 @@ class LocalizedMessages
* @param aKey key to locale message format
* @param aArgs arguments for message
*/
void add(int aLineNo, String aKey, Object[] aArgs)
public void add(int aLineNo, String aKey, Object[] aArgs)
{
add(new LocalizedMessage(aLineNo, 0, aKey, aArgs));
}
@ -93,7 +93,7 @@ class LocalizedMessages
* @param aLineNo line number to associate with the message
* @param aKey key to locale message format
*/
void add(int aLineNo, String aKey)
public void add(int aLineNo, String aKey)
{
add(aLineNo, aKey, new Object[0]);
}
@ -105,7 +105,7 @@ class LocalizedMessages
* @param aKey key to locale message format
* @param aArg0 first argument
*/
void add(int aLineNo, String aKey, Object aArg0)
public void add(int aLineNo, String aKey, Object aArg0)
{
add(aLineNo, aKey, new Object[] {aArg0});
}
@ -118,7 +118,7 @@ class LocalizedMessages
* @param aArg0 first argument
* @param aArg1 second argument
*/
void add(int aLineNo, String aKey, Object aArg0, Object aArg1)
public void add(int aLineNo, String aKey, Object aArg0, Object aArg1)
{
add(aLineNo, aKey, new Object[] {aArg0, aArg1});
}
@ -132,7 +132,7 @@ class LocalizedMessages
* @param aArg1 second argument
* @param aArg2 third argument
*/
void add(int aLineNo, String aKey,
public void add(int aLineNo, String aKey,
Object aArg0, Object aArg1, Object aArg2)
{
add(aLineNo, aKey, new Object[] {aArg0, aArg1, aArg2});
@ -146,7 +146,7 @@ class LocalizedMessages
* @param aKey key to locale message format
* @param aArgs arguments for message
*/
void add(int aLineNo, int aColNo, String aKey, Object[] aArgs)
public void add(int aLineNo, int aColNo, String aKey, Object[] aArgs)
{
final int col = 1 + Utils.lengthExpandedTabs(
mLines[aLineNo - 1], aColNo, mTabWidth);
@ -160,7 +160,7 @@ class LocalizedMessages
* @param aColNo column number to associate with the message
* @param aKey key to locale message format
*/
void add(int aLineNo, int aColNo, String aKey)
public void add(int aLineNo, int aColNo, String aKey)
{
add(aLineNo, aColNo, aKey, new Object[0]);
}
@ -173,7 +173,7 @@ class LocalizedMessages
* @param aKey key to locale message format
* @param aArg0 first argument
*/
void add(int aLineNo, int aColNo, String aKey, Object aArg0)
public void add(int aLineNo, int aColNo, String aKey, Object aArg0)
{
add(aLineNo, aColNo, aKey, new Object[] {aArg0});
}
@ -187,7 +187,7 @@ class LocalizedMessages
* @param aArg0 first argument
* @param aArg1 second argument
*/
void add(int aLineNo, int aColNo, String aKey,
public void add(int aLineNo, int aColNo, String aKey,
Object aArg0, Object aArg1)
{
add(aLineNo, aColNo, aKey, new Object[] {aArg0, aArg1});

View File

@ -16,7 +16,7 @@
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;
package com.puppycrawl.tools.checkstyle.api;
import java.util.HashMap;
import java.util.Iterator;
@ -33,7 +33,7 @@ import org.apache.regexp.RESyntaxException;
* @author <a href="mailto:oliver@puppycrawl.com">Oliver Burn</a>
* @version 1.0
*/
final class Utils
public final class Utils
{
/** Map of all created regular expressions **/
private static final Map CREATED_RES = new HashMap();
@ -51,7 +51,7 @@ final class Utils
* @param aLine the line to check
* @return whether there is only whitespace
*/
static boolean whitespaceBefore(int aIndex, String aLine)
public static boolean whitespaceBefore(int aIndex, String aLine)
{
for (int i = 0; i < aIndex; i++) {
if (!Character.isWhitespace(aLine.charAt(i))) {
@ -68,7 +68,7 @@ final class Utils
* @param aLine the string to process
* @return the length of the string ignoring all trailing whitespace
**/
static int lengthMinusTrailingWhitespace(String aLine)
public static int lengthMinusTrailingWhitespace(String aLine)
{
int len = aLine.length();
for (int i = len - 1; i >= 0; i--) {
@ -89,7 +89,9 @@ final class Utils
* @param aTabWidth the distance betweeen tab stop position.
* @return the length of aString.substring(0, aToIdx) with tabs expanded.
*/
static int lengthExpandedTabs(String aString, int aToIdx, int aTabWidth)
public static int lengthExpandedTabs(String aString,
int aToIdx,
int aTabWidth)
{
int len = 0;
for (int idx = 0; idx < aToIdx; idx++) {
@ -111,7 +113,7 @@ final class Utils
* @param aPattern the regular expression pattern
* @throws RESyntaxException an invalid pattern was supplied
**/
static RE getRE(String aPattern)
public static RE getRE(String aPattern)
throws RESyntaxException
{
RE retVal = (RE) CREATED_RES.get(aPattern);
@ -128,7 +130,9 @@ final class Utils
* @param aKey the key to add the property under
* @param aValue if not null, then the value to add the property with
*/
static void addObjectString(Properties aProps, String aKey, Object aValue)
public static void addObjectString(Properties aProps,
String aKey,
Object aValue)
{
if (aValue != null) {
aProps.put(aKey, aValue.toString());
@ -142,7 +146,7 @@ final class Utils
* @param aKey the key to add the property under
* @param aSet the Set to encode
*/
static void addSetString(Properties aProps, String aKey, Set aSet)
public static void addSetString(Properties aProps, String aKey, Set aSet)
{
final StringBuffer buf = new StringBuffer();
final Iterator it = aSet.iterator();

View File

@ -24,13 +24,13 @@ import com.puppycrawl.tools.checkstyle.api.DetailAST;
/**
* Check that finds import statement that use the * notation.
* <p>
* Rationale: Importing all classes from a package leads to tight coupling between
* packages and might lead to problems when a new version of a library introduces
* name clashes.
*
* <p> Rationale: Importing all classes from a package leads to tight coupling
* between packages and might lead to problems when a new version of a library
* introduces name clashes.
*
* @author Oliver Burn
* @version $Id: AvoidStarImport.java,v 1.2 2002-09-15 16:55:59 lkuehne Exp $
* @version $Id: AvoidStarImport.java,v 1.3 2002-09-17 12:33:29 oburn Exp $
*/
public class AvoidStarImport extends ImportCheck
{

View File

@ -24,7 +24,10 @@ import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.JavaTokenTypes;
/**
* Abstract base class that provides functionality that is used in import checks.
* Abstract base class that provides functionality that is used in import
* checks.
* @author <a href="mailto:checkstyle@puppycrawl.com">Oliver Burn</a>
* @version 1.0
*/
public abstract class ImportCheck
extends Check

View File

@ -19,7 +19,6 @@
package com.puppycrawl.tools.checkstyle.checks;
import com.puppycrawl.tools.checkstyle.checks.ImportCheck;
import com.puppycrawl.tools.checkstyle.JavaTokenTypes;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
@ -29,6 +28,8 @@ import java.util.Iterator;
/**
* Checks for unused import statements.
* @author <a href="mailto:checkstyle@puppycrawl.com">Oliver Burn</a>
* @version 1.0
*/
public class UnusedImportsCheck extends ImportCheck
{

View File

@ -2,6 +2,7 @@ package com.puppycrawl.tools.checkstyle;
import junit.framework.TestCase;
import org.apache.regexp.RE;
import com.puppycrawl.tools.checkstyle.api.Utils;
public class UtilsTest
extends TestCase