summaryrefslogtreecommitdiff
path: root/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/BuiltinNative2Ascii.java
blob: f8059fe5571efb84f1a95f81df9517e59d576805 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 *  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.tools.ant.taskdefs.optional.native2ascii;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.function.UnaryOperator;
import java.nio.file.Files;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.optional.Native2Ascii;
import org.apache.tools.ant.util.Native2AsciiUtils;

/**
 * Encapsulates the built-in Native2Ascii implementation.
 *
 * @since Ant 1.9.8
 */
public class BuiltinNative2Ascii implements Native2AsciiAdapter {

    static final String IMPLEMENTATION_NAME = "builtin";

    @Override
    public final boolean convert(Native2Ascii args, File srcFile,
                                 File destFile) throws BuildException {
        boolean reverse = args.getReverse();
        String encoding = args.getEncoding();
        try (BufferedReader input = getReader(srcFile, encoding, reverse);
                Writer output = getWriter(destFile, encoding, reverse)) {

            translate(input, output, reverse ? Native2AsciiUtils::ascii2native
                : Native2AsciiUtils::native2ascii);
            return true;
        } catch (IOException ex) {
            throw new BuildException("Exception trying to translate data", ex);
        }
    }

    private BufferedReader getReader(File srcFile, String encoding,
                                     boolean reverse) throws IOException {
        if (!reverse && encoding != null) {
            return new BufferedReader(new InputStreamReader(
                Files.newInputStream(srcFile.toPath()), encoding));
        }
        return new BufferedReader(new FileReader(srcFile));
    }

    private Writer getWriter(File destFile, String encoding,
                             boolean reverse) throws IOException {
        if (!reverse) {
            encoding = "ASCII";
        }
        if (encoding != null) {
            return new BufferedWriter(
                new OutputStreamWriter(Files.newOutputStream(destFile.toPath()),
                                       encoding));
        }
        return new BufferedWriter(new FileWriter(destFile));
    }

    private void translate(BufferedReader input, Writer output,
        UnaryOperator<String> translation) throws IOException {
        PrintWriter pw = new PrintWriter(output);

        for (String line : (Iterable<String>) () -> input.lines()
            .map(translation).iterator()) {
            pw.println(line);
        }
    }
}