8000 Issue #13012: fix loading with non-ascii symbols in path · checkstyle/checkstyle@8a4b406 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8a4b406

Browse files
strkkknrmancuso
authored andcommitted
Issue #13012: fix loading with non-ascii symbols in path
1 parent 96eeed5 commit 8a4b406

File tree

12 files changed

+62
-27
lines changed

12 files changed

+62
-27
lines changed

config/signatures.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ com.puppycrawl.tools.checkstyle.DefaultConfiguration#getAttribute(java.lang.Stri
33
com.puppycrawl.tools.checkstyle.DefaultConfiguration#addAttribute(java.lang.String,java.lang.String) @ Usage of deprecated API is forbidden. Please use DefaultConfiguration.addProperty(String name, String value) instead.
44
com.puppycrawl.tools.checkstyle.api.AbstractCheck#log(int,int,java.lang.String,java.lang.Object[]) @ Use of this log method is forbidden, please use AbstractCheck#log(DetailAST ast, String key, Object... args)
55
com.puppycrawl.tools.checkstyle.api.AbstractCheck#log(int,java.lang.String,java.lang.Object[]) @ Use of this log method is forbidden, please use AbstractCheck#log(DetailAST ast, String key, Object... args)
6+
java.net.URI#toString() @ This method can return garbage for non-ascii data, please use URI#toASCIIString()

config/suppressions.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,8 @@
194194
<!-- The file is generated -->
195195
<suppress checks="FileTabCharacter"
196196
files="src[\\/]xdocs[\\/]checks[\\/]whitespace[\\/]filetabcharacter\.xml"/>
197+
198+
<!-- Uses non-ascii symbols intentionally -->
199+
<suppress id="checkASCII"
200+
files="[\\/]src[\\/]test[\\/]java[\\/]com[\\/]puppycrawl[\\/]tools[\\/]checkstyle[\\/]ConfigurationLoaderTest.java"/>
197201
</suppressions>

src/main/java/com/puppycrawl/tools/checkstyle/ConfigurationLoader.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package com.puppycrawl.tools.checkstyle;
2121

2222
import java.io.IOException;
23-
import java.net.URI;
2423
import java.util.ArrayDeque;
2524
import java.util.ArrayList;
2625
import java.util.Arrays;
@@ -260,10 +259,7 @@ public static Configuration loadConfiguration(String config,
260259
IgnoredModulesOptions ignoredModulesOptions,
261260
ThreadModeSettings threadModeSettings)
262261
throws CheckstyleException {
263-
// figure out if this is a File or a URL
264-
final URI uri = CommonUtil.getUriByFilename(config);
265-
final InputSource source = new InputSource(uri.toString());
266-
return loadConfiguration(source, overridePropsResolver,
262+
return loadConfiguration(CommonUtil.sourceFromFilename(config), overridePropsResolver,
267263
ignoredModulesOptions, threadModeSettings);
268264
}
269265

src/main/java/com/puppycrawl/tools/checkstyle/checks/header/AbstractHeaderCheck.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.nio.charset.Charset;
3030
import java.nio.charset.UnsupportedCharsetException;
3131
import java.util.ArrayList;
32-
import java.util.Collections;
3332
import java.util.List;
3433
import java.util.Set;
3534
import java.util.regex.Pattern;
@@ -201,10 +200,10 @@ public Set<String> getExternalResourceLocations() {
201200
final Set<String> result;
202201

203202
if (headerFile == null) {
204-
result = Collections.emptySet();
203+
result = Set.of();
205204
}
206205
else {
207-
result = Collections.singleton(headerFile.toString());
206+
result = Set.of(headerFile.toASCIIString());
208207
}
209208

210209
return result;

src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlCheck.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package com.puppycrawl.tools.checkstyle.checks.imports;
2121

2222
import java.net.URI;
23-
import java.util.Collections;
2423
import java.util.Set;
2524
import java.util.regex.Pattern;
2625

@@ -253,7 +252,7 @@ else if (currentImportControl != null) {
253252

254253
@Override
255254
public Set<String> getExternalResourceLocations() {
256-
return Collections.singleton(file.toString());
255+
return Set.of(file.toASCIIString());
257256
}
258257

259258
/**

src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoader.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import java.io.FileNotFoundException;
2323
import java.io.IOException;
24-
import java.net.URI;
2524
import java.util.HashMap;
2625
import java.util.HashSet;
2726
import java.util.Locale;
@@ -218,10 +217,7 @@ private static XpathFilterElement getXpathFilter(Attributes attributes) throws S
218217
*/
219218
public static FilterSet loadSuppressions(String filename)
220219
throws CheckstyleException {
221-
// figure out if this is a File or a URL
222-
final URI uri = CommonUtil.getUriByFilename(filename);
223-
final InputSource source = new InputSource(uri.toString());
224-
return loadSuppressions(source, filename);
220+
return loadSuppressions(CommonUtil.sourceFromFilename(filename), filename);
225221
}
226222

227223
/**
@@ -247,10 +243,7 @@ private static FilterSet loadSuppressions(
247243
*/
248244
public static Set<TreeWalkerFilter> loadXpathSuppressions(String filename)
249245
throws CheckstyleException {
250-
// figure out if this is a File or a URL
251-
final URI uri = CommonUtil.getUriByFilename(filename);
252-
final InputSource source = new InputSource(uri.toString());
253-
return loadXpathSuppressions(source, filename);
246+
return loadXpathSuppressions(CommonUtil.sourceFromFilename(filename), filename);
254247
}
255248

256249
/**

src/main/java/com/puppycrawl/tools/checkstyle/utils/CommonUtil.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import java.util.regex.Pattern;
3737
import java.util.regex.PatternSyntaxException;
3838

39+
import org.xml.sax.InputSource;
40+
3941
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
4042

4143
/**
@@ -330,6 +332,19 @@ public static void close(Closeable closeable) {
330332
}
331333
}
332334

335+
/**
336+
* Creates an input source from a file.
337+
*
338+
* @param filename name of the file.
339+
* @return input source.
340+
* @throws CheckstyleException if an error occurs.
341+
*/
342+
public static InputSource sourceFromFilename(String filename) throws CheckstyleException {
343+
// figure out if this is a File or a URL
344+
final URI uri = getUriByFilename(filename);
345+
return new InputSource(uri.toASCIIString());
346+
}
347+
333348
/**
334349
* Resolve the specified filename to a URI.
335350
*

src/test/java/com/puppycrawl/tools/checkstyle/ConfigurationLoaderTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,21 @@ public void testLoadConfigurationFromClassPath() throws Exception {
654654
.isEqualTo(0);
655655
}
656656

657+
@Test
658+
public void testLoadConfigurationFromClassPathWithNonAsciiSymbolsInPath() throws Exception {
659+
final DefaultConfiguration config =
660+
(DefaultConfiguration) ConfigurationLoader.loadConfiguration(
661+
getResourcePath("棵¥/InputConfigurationLoaderDefaultProperty.xml"),
662+
new PropertiesExpander(new Properties()));
663+
664+
final Properties expectedPropertyValues = new Properties();
665+
expectedPropertyValues.setProperty("tabWidth", "2");
666+
expectedPropertyValues.setProperty("basedir", ".");
667+
// charset property uses 2 variables, one is not defined, so default becomes a result value
668+
expectedPropertyValues.setProperty("charset", "ASCII");
669+
verifyConfigNode(config, "Checker", 0, expectedPropertyValues);
670+
}
671+
657672
@Test
658673
public void testParsePropertyString() throws Exception {
659674
final List<String> propertyRefs = new ArrayList<>();

src/test/java/com/puppycrawl/tools/checkstyle/checks/header/HeaderCheckTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public void testExternalResource() throws Exception {
295295
.isEqualTo(1);
296296
assertWithMessage("Invalid resource location")
297297
.that(results.iterator().next())
298-
.isEqualTo(uri.toString());
298+
.isEqualTo(uri.toASCIIString());
299299
}
300300

301301
@Test

src/test/java/com/puppycrawl/tools/checkstyle/meta/MetadataGeneratorUtilTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79 64EC ,7 +79,7 @@ public void testMetadataFilesGenerationAllFiles(@SystemOutGuard.SysOut Capturabl
7979

8080
final String[] expectedErrorMessages = {
8181
"31: " + getCheckMessage(MSG_DESC_MISSING, "AbstractSuperCheck"),
82-
"44: " + getCheckMessage(MSG_DESC_MISSING, "AbstractHeaderCheck"),
82+
"43: " + getCheckMessage(MSG_DESC_MISSING, "AbstractHeaderCheck"),
8383
"43: " + getCheckMessage(MSG_DESC_MISSING, "AbstractJavadocCheck"),
8484
"45: " + getCheckMessage(MSG_DESC_MISSING, "AbstractClassCouplingCheck"),
8585
"26: " + getCheckMessage(MSG_DESC_MISSING, "AbstractAccessControlNameCheck"),

0 commit comments

Comments
 (0)
0