OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
182 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils.system;
15
 
16
import java.nio.charset.StandardCharsets;
17
import java.util.Iterator;
18
import java.util.List;
19
import java.util.regex.Matcher;
20
import java.util.regex.Pattern;
21
 
22
public final class Powershell {
23
 
24
    private static final Powershell INSTANCE = new Powershell();
25
 
26
    // No static methods to future-proof if the syntax changes in the future
27
    static public final Powershell getInstance() {
28
        return INSTANCE;
29
    }
30
 
31
    static private final Pattern PS_CHARS = Pattern.compile("\\R+|'");
32
 
33
    private Powershell() {
34
    }
35
 
36
    public final String quote(String s) {
37
        final StringBuffer sb = new StringBuffer((int) (s.length() * 1.1));
38
        quote(s, sb);
39
        return sb.toString();
40
    }
41
 
42
    public final void quote(String s, final StringBuffer sb) {
43
        sb.append("'");
44
        final Matcher m = PS_CHARS.matcher(s);
45
        while (m.find()) {
46
            final String replacement;
47
            if (m.group().equals("'")) {
48
                replacement = "''";
49
            } else {
50
                // `n isn't expanded in single quoted strings and it's complicated to handle all
51
                // needed escaping in double quoted strings, so just concatenate a single newline.
52
                replacement = "' + \"`n\" + '";
53
            }
54
            m.appendReplacement(sb, replacement);
55
        }
56
        m.appendTail(sb);
57
        sb.append("'");
58
    }
59
 
60
    // @("C:\Users\ILM\Documents", "2")
61
    public final String quoteArray(List<String> l) {
62
        final StringBuffer sb = new StringBuffer(l.size() * 64);
63
        sb.append("@(");
64
        final Iterator<String> iter = l.iterator();
65
        while (iter.hasNext()) {
66
            final String s = iter.next();
67
            quote(s, sb);
68
            if (iter.hasNext())
69
                sb.append(", ");
70
        }
71
        sb.append(")");
72
        return sb.toString();
73
    }
74
 
75
    public final String getEncodedCommand(final String s) {
76
        return java.util.Base64.getEncoder().encodeToString(s.getBytes(StandardCharsets.UTF_16LE));
77
    }
78
}