Skip to content

Commit 7fbef10

Browse files
committed
Merge pull request #74 from adangel:remove-commons-io
Remove commons-io #74
2 parents 529d92f + ba58d6a commit 7fbef10

File tree

6 files changed

+47
-45
lines changed

6 files changed

+47
-45
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
* [#61](https://github.com/pmd/pmd-designer/issues/61) Remove dependency to jcommander
1010
* [#62](https://github.com/pmd/pmd-designer/issues/62) Exceptions and errors are not always logged
1111
* [#63](https://github.com/pmd/pmd-designer/issues/63) Update to PMD 7.0.0-rc3
12+
* [#73](https://github.com/pmd/pmd-designer/issues/73) Remove commons-io dependency
1213

1314
**Merged pull requests:**
1415

1516
* [#68](https://github.com/pmd/pmd-designer/pull/68) Expose all properties with default values by [@jsotuyod](https://github.com/jsotuyod)
1617
* [#69](https://github.com/pmd/pmd-designer/pull/69) Perform the persistence asynchronously to not block the main (UI) thread #69 by [@adangel](https://github.com/adangel)
1718
* [#70](https://github.com/pmd/pmd-designer/pull/70) Fix drag and drop for tests case violations by [@adangel](https://github.com/adangel)
19+
* [#74](https://github.com/pmd/pmd-designer/pull/74) Remove commons-io by [@adangel](https://github.com/adangel)
1820

1921
See https://github.com/pmd/pmd-designer/milestone/11
2022

pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -644,12 +644,6 @@
644644
<artifactId>commons-logging</artifactId>
645645
<version>1.2</version>
646646
</dependency>
647-
<dependency>
648-
<groupId>commons-io</groupId>
649-
<artifactId>commons-io</artifactId>
650-
<version>2.7</version>
651-
</dependency>
652-
653647

654648
<!-- Icon packs -->
655649
<!-- Unfortunately these can't be updated as the author now uses Java 11...-->

src/main/java/net/sourceforge/pmd/util/fxdesigner/MainDesignerController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.Stack;
2424
import java.util.stream.Collectors;
2525

26-
import org.apache.commons.io.IOUtils;
2726
import org.checkerframework.checker.nullness.qual.NonNull;
2827
import org.reactfx.Subscription;
2928
import org.reactfx.value.Var;
@@ -37,6 +36,7 @@
3736
import net.sourceforge.pmd.util.fxdesigner.util.AuxLanguageRegistry;
3837
import net.sourceforge.pmd.util.fxdesigner.util.DesignerUtil;
3938
import net.sourceforge.pmd.util.fxdesigner.util.LimitedSizeStack;
39+
import net.sourceforge.pmd.util.fxdesigner.util.ResourceUtil;
4040
import net.sourceforge.pmd.util.fxdesigner.util.SoftReferenceCache;
4141
import net.sourceforge.pmd.util.fxdesigner.util.beans.SettingsPersistenceUtil.PersistentProperty;
4242
import net.sourceforge.pmd.util.fxdesigner.util.controls.DynamicWidthChoicebox;
@@ -218,7 +218,7 @@ private void onOpenFileClicked() {
218218
private void loadSourceFromFile(File file) {
219219
if (file != null) {
220220
try {
221-
String source = IOUtils.toString(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8);
221+
String source = ResourceUtil.readToString(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8);
222222
sourceEditorController.setText(source);
223223
LanguageVersion guess = getLanguageVersionFromExtension(file.getName());
224224
if (guess == null) {

src/main/java/net/sourceforge/pmd/util/fxdesigner/SourceEditorController.java

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.Objects;
1919
import java.util.stream.Collectors;
2020

21-
import org.apache.commons.io.IOUtils;
2221
import org.apache.commons.lang3.StringUtils;
2322
import org.checkerframework.checker.nullness.qual.NonNull;
2423
import org.controlsfx.control.PopOver;
@@ -331,31 +330,10 @@ private void handleTestOpenRequest(@NonNull LiveTestCase oldValue, @NonNull Live
331330

332331

333332
private String getDefaultText() {
334-
try {
335-
// TODO this should take language into account
336-
// it doesn't handle the case where java is not on the classpath
333+
// TODO this should take language into account
334+
// it doesn't handle the case where java is not on the classpath
337335

338-
return IOUtils.resourceToString(ResourceUtil.resolveResource("placeholders/editor.java"), StandardCharsets.UTF_8);
339-
} catch (IOException e) {
340-
e.printStackTrace();
341-
return "class Foo {\n"
342-
+ "\n"
343-
+ " /*\n"
344-
+ " Welcome to the PMD Rule designer :)\n"
345-
+ "\n"
346-
+ " Type some code in this area\n"
347-
+ " \n"
348-
+ " On the right, the Abstract Syntax Tree is displayed\n"
349-
+ " On the left, you can examine the XPath attributes of\n"
350-
+ " the nodes you select\n"
351-
+ " \n"
352-
+ " You can set the language you'd like to work in with\n"
353-
+ " the cog icon above this code area\n"
354-
+ " */\n"
355-
+ "\n"
356-
+ " int i = 0;\n"
357-
+ "}";
358-
}
336+
return ResourceUtil.readToString(SourceEditorController.class.getResourceAsStream(ResourceUtil.resolveResource("placeholders/editor.java")), StandardCharsets.UTF_8);
359337
}
360338

361339

src/main/java/net/sourceforge/pmd/util/fxdesigner/popups/SimplePopups.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
import static java.lang.Double.max;
88
import static java.lang.Math.min;
99

10-
import java.io.IOException;
1110
import java.nio.charset.StandardCharsets;
1211
import java.util.Locale;
1312
import java.util.stream.Collectors;
1413

15-
import org.apache.commons.io.IOUtils;
1614
import org.checkerframework.checker.nullness.qual.NonNull;
1715
import org.checkerframework.checker.nullness.qual.Nullable;
1816
import org.kordamp.ikonli.javafx.FontIcon;
@@ -25,6 +23,7 @@
2523
import net.sourceforge.pmd.util.fxdesigner.app.DesignerRoot;
2624
import net.sourceforge.pmd.util.fxdesigner.util.AuxLanguageRegistry;
2725
import net.sourceforge.pmd.util.fxdesigner.util.DesignerUtil;
26+
import net.sourceforge.pmd.util.fxdesigner.util.ResourceUtil;
2827

2928
import javafx.animation.Animation;
3029
import javafx.animation.Interpolator;
@@ -192,11 +191,7 @@ public static void showLicensePopup() {
192191
licenseAlert.setHeaderText("License");
193192

194193
ScrollPane scroll = new ScrollPane();
195-
try {
196-
scroll.setContent(new TextArea(IOUtils.toString(SimplePopups.class.getResourceAsStream(LICENSE_FILE_PATH), StandardCharsets.UTF_8)));
197-
} catch (IOException e) {
198-
e.printStackTrace();
199-
}
194+
scroll.setContent(new TextArea(ResourceUtil.readToString(SimplePopups.class.getResourceAsStream(LICENSE_FILE_PATH), StandardCharsets.UTF_8)));
200195

201196
licenseAlert.getDialogPane().setContent(scroll);
202197
licenseAlert.showAndWait();

src/main/java/net/sourceforge/pmd/util/fxdesigner/util/ResourceUtil.java

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66

77
import java.io.File;
88
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.InputStreamReader;
11+
import java.io.UncheckedIOException;
912
import java.net.MalformedURLException;
1013
import java.net.URI;
1114
import java.net.URISyntaxException;
1215
import java.net.URL;
16+
import java.nio.charset.Charset;
1317
import java.nio.file.FileSystem;
1418
import java.nio.file.FileSystemNotFoundException;
1519
import java.nio.file.FileSystems;
@@ -24,9 +28,6 @@
2428
import java.util.stream.Collectors;
2529
import java.util.stream.Stream;
2630

27-
import org.apache.commons.io.FilenameUtils;
28-
29-
3031
/**
3132
* Deals with resource fetching and the hardcore details of when we're in a Jar
3233
* vs when we're exploded in the IDE.
@@ -93,10 +94,10 @@ private static Stream<Path> pathsInResource(ClassLoader classLoader,
9394
/** Maps paths to classes. */
9495
private static Class<?> toClass(Path path, String packageName) {
9596
return Optional.of(path)
96-
.filter(p -> "class".equalsIgnoreCase(FilenameUtils.getExtension(path.toString())))
97+
.filter(p -> "class".equalsIgnoreCase(getFilenameExtension(path.toString())))
9798
.<Class<?>>map(p -> {
9899
try {
99-
return Class.forName(packageName + "." + FilenameUtils.getBaseName(path.getFileName().toString()));
100+
return Class.forName(packageName + "." + getFilenameBase(path.getFileName().toString()));
100101
} catch (ClassNotFoundException e) {
101102
e.printStackTrace();
102103
return null;
@@ -105,6 +106,24 @@ private static Class<?> toClass(Path path, String packageName) {
105106
.orElse(null);
106107
}
107108

109+
private static String getFilenameExtension(String name) {
110+
String filename = Paths.get(name).getFileName().toString();
111+
int lastDot = filename.lastIndexOf('.');
112+
if (lastDot > -1) {
113+
return filename.substring(lastDot + 1);
114+
}
115+
return filename;
116+
}
117+
118+
private static String getFilenameBase(String name) {
119+
String filename = Paths.get(name).getFileName().toString();
120+
int lastDot = filename.lastIndexOf('.');
121+
if (lastDot > -1) {
122+
return filename.substring(0, lastDot);
123+
}
124+
return filename;
125+
}
126+
108127
private static String getJarRelativePath(URI uri) {
109128
if ("jar".equals(uri.getScheme())) {
110129
// we have to cut out the path to the jar + '!'
@@ -176,4 +195,18 @@ public static FileSystem getFileSystem(URI uri) throws IOException {
176195
}
177196
}
178197
}
198+
199+
public static String readToString(InputStream stream, Charset charset) {
200+
try (InputStreamReader reader = new InputStreamReader(stream, charset)) {
201+
StringBuilder result = new StringBuilder(8192);
202+
int count;
203+
char[] buffer = new char[8192];
204+
while ((count = reader.read(buffer)) != -1) {
205+
result.append(buffer, 0, count);
206+
}
207+
return result.toString();
208+
} catch (IOException e) {
209+
throw new UncheckedIOException(e);
210+
}
211+
}
179212
}

0 commit comments

Comments
 (0)