Skip to content

Commit

Permalink
[ARCHETYPE-691] Remove FileCharsetDetector.java and moved its methods…
Browse files Browse the repository at this point in the history
… to the usage side.
  • Loading branch information
Stellar1999 committed Dec 15, 2024
1 parent e3e970b commit bac982f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 70 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.inject.Named;
import javax.inject.Singleton;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -36,17 +37,19 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import com.ibm.icu.text.CharsetDetector;
import com.ibm.icu.text.CharsetMatch;
import org.apache.commons.collections.CollectionUtils;
import org.apache.maven.archetype.ArchetypeCreationRequest;
import org.apache.maven.archetype.ArchetypeCreationResult;
import org.apache.maven.archetype.common.ArchetypeFilesResolver;
import org.apache.maven.archetype.common.Constants;
import org.apache.maven.archetype.common.PomManager;
import org.apache.maven.archetype.common.util.FileCharsetDetector;
import org.apache.maven.archetype.common.util.ListScanner;
import org.apache.maven.archetype.common.util.PathUtils;
import org.apache.maven.archetype.metadata.ArchetypeDescriptor;
Expand Down Expand Up @@ -1260,9 +1263,7 @@ private void processFileSet(

File inputFile = new File(basedir, inputFileName);

FileCharsetDetector detector = new FileCharsetDetector(inputFile);

String fileEncoding = detector.isFound() ? detector.getCharset() : defaultEncoding;
String fileEncoding = getFileCharsetEncoding(inputFile, defaultEncoding);

String initialcontent = IOUtil.toString(Files.newInputStream(inputFile.toPath()), fileEncoding);

Expand Down Expand Up @@ -1632,6 +1633,17 @@ private FileSet getUnpackagedFileSet(
return createFileSet(excludes, false, filtered, group, includes, defaultEncoding);
}

private String getFileCharsetEncoding(File detectedFile, String defaultEncoding) {
try (InputStream in = new BufferedInputStream(Files.newInputStream(detectedFile.toPath()))) {
CharsetDetector detector = new CharsetDetector();
detector.setText(in);
CharsetMatch match = detector.detect();
return match.getName().toUpperCase(Locale.ENGLISH);
} catch (IOException e) {
return defaultEncoding;
}
}

private FileSet getUnpackagedFileSet(
final boolean filtered,
final Set<String> unpackagedExtensions,
Expand Down

0 comments on commit bac982f

Please sign in to comment.