This is an automated email from the ASF dual-hosted git repository.

sjaranowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-archetype.git


The following commit(s) were added to refs/heads/master by this push:
     new 7dee5ee0 [ARCHETYPE-691] Remove FileCharsetDetector.java and moved its 
methods to the usage side.
7dee5ee0 is described below

commit 7dee5ee0f2727ff086edd46d02a358272d14ac4e
Author: Stellar <[email protected]>
AuthorDate: Thu Dec 12 13:20:53 2024 +0800

    [ARCHETYPE-691] Remove FileCharsetDetector.java and moved its methods to 
the usage side.
---
 .../archetype/common/util/FileCharsetDetector.java | 66 ----------------------
 .../archetype/creator/FilesetArchetypeCreator.java | 20 +++++--
 2 files changed, 16 insertions(+), 70 deletions(-)

diff --git 
a/archetype-common/src/main/java/org/apache/maven/archetype/common/util/FileCharsetDetector.java
 
b/archetype-common/src/main/java/org/apache/maven/archetype/common/util/FileCharsetDetector.java
deleted file mode 100644
index b8af7b26..00000000
--- 
a/archetype-common/src/main/java/org/apache/maven/archetype/common/util/FileCharsetDetector.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.maven.archetype.common.util;
-
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Locale;
-
-import com.ibm.icu.text.CharsetDetector;
-import com.ibm.icu.text.CharsetMatch;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * @author rafale
- */
-public class FileCharsetDetector {
-    private static final Logger LOGGER = 
LoggerFactory.getLogger(FileCharsetDetector.class);
-    private final String charset;
-
-    public FileCharsetDetector(File detectedFile) throws IOException {
-        try (FileInputStream fileInputStream = new 
FileInputStream(detectedFile);
-                BufferedInputStream is = new 
BufferedInputStream(fileInputStream)) {
-            CharsetDetector detector = new CharsetDetector();
-            detector.setText(is);
-            CharsetMatch match = detector.detect();
-
-            charset = match.getName().toUpperCase(Locale.ENGLISH);
-        }
-    }
-
-    public FileCharsetDetector(InputStream detectedStream) throws IOException {
-        CharsetDetector detector = new CharsetDetector();
-        detector.setText(detectedStream);
-        CharsetMatch match = detector.detect();
-
-        charset = match.getName().toUpperCase(Locale.ENGLISH);
-    }
-
-    public String getCharset() {
-        return charset;
-    }
-
-    public boolean isFound() {
-        return true;
-    }
-}
diff --git 
a/archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java
 
b/archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java
index 989e97a5..bf3fa085 100644
--- 
a/archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java
+++ 
b/archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java
@@ -22,6 +22,7 @@ import javax.inject.Inject;
 import javax.inject.Named;
 import javax.inject.Singleton;
 
+import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -36,17 +37,19 @@ import java.util.HashMap;
 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;
@@ -1261,9 +1264,7 @@ public class FilesetArchetypeCreator implements 
ArchetypeCreator {
 
             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);
 
@@ -1633,6 +1634,17 @@ public class FilesetArchetypeCreator implements 
ArchetypeCreator {
         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,

Reply via email to