jaxws/make/tools/StripProperties/StripProperties.java
author ohair
Mon, 27 Apr 2009 20:18:40 -0700
changeset 2681 583fd6b10e17
parent 8 474761f14bca
permissions -rw-r--r--
6831225: Upgrade JPRT jobs to use newer Linux 2.6 (e.g. Fedora 9) Reviewed-by: tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
474761f14bca Initial load
duke
parents:
diff changeset
     1
/*
474761f14bca Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001 Sun Microsystems, Inc.  All Rights Reserved.
474761f14bca Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
474761f14bca Initial load
duke
parents:
diff changeset
     4
 *
474761f14bca Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
474761f14bca Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
474761f14bca Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
474761f14bca Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
474761f14bca Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
474761f14bca Initial load
duke
parents:
diff changeset
    10
 *
474761f14bca Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
474761f14bca Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
474761f14bca Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
474761f14bca Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
474761f14bca Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
474761f14bca Initial load
duke
parents:
diff changeset
    16
 *
474761f14bca Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
474761f14bca Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
474761f14bca Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
474761f14bca Initial load
duke
parents:
diff changeset
    20
 *
474761f14bca Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
474761f14bca Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
474761f14bca Initial load
duke
parents:
diff changeset
    23
 * have any questions.
474761f14bca Initial load
duke
parents:
diff changeset
    24
 */
474761f14bca Initial load
duke
parents:
diff changeset
    25
474761f14bca Initial load
duke
parents:
diff changeset
    26
import java.io.BufferedInputStream;
474761f14bca Initial load
duke
parents:
diff changeset
    27
import java.io.BufferedWriter;
474761f14bca Initial load
duke
parents:
diff changeset
    28
import java.io.FileInputStream;
474761f14bca Initial load
duke
parents:
diff changeset
    29
import java.io.FileNotFoundException;
474761f14bca Initial load
duke
parents:
diff changeset
    30
import java.io.FileOutputStream;
474761f14bca Initial load
duke
parents:
diff changeset
    31
import java.io.OutputStream;
474761f14bca Initial load
duke
parents:
diff changeset
    32
import java.io.OutputStreamWriter;
474761f14bca Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
474761f14bca Initial load
duke
parents:
diff changeset
    34
import java.io.InputStream;
474761f14bca Initial load
duke
parents:
diff changeset
    35
import java.util.ArrayList;
474761f14bca Initial load
duke
parents:
diff changeset
    36
import java.util.Enumeration;
474761f14bca Initial load
duke
parents:
diff changeset
    37
import java.util.List;
474761f14bca Initial load
duke
parents:
diff changeset
    38
import java.util.Properties;
474761f14bca Initial load
duke
parents:
diff changeset
    39
474761f14bca Initial load
duke
parents:
diff changeset
    40
/**
474761f14bca Initial load
duke
parents:
diff changeset
    41
 * Reads a properties file from standard input and writes an equivalent
474761f14bca Initial load
duke
parents:
diff changeset
    42
 * properties file without comments to standard output.
474761f14bca Initial load
duke
parents:
diff changeset
    43
 */
474761f14bca Initial load
duke
parents:
diff changeset
    44
public class StripProperties {
474761f14bca Initial load
duke
parents:
diff changeset
    45
474761f14bca Initial load
duke
parents:
diff changeset
    46
    public static void main(String[] args) {
474761f14bca Initial load
duke
parents:
diff changeset
    47
        StripProperties sp = new StripProperties();
474761f14bca Initial load
duke
parents:
diff changeset
    48
        boolean ok = sp.run(args);
474761f14bca Initial load
duke
parents:
diff changeset
    49
        if ( !ok ) {
474761f14bca Initial load
duke
parents:
diff changeset
    50
            System.exit(1);
474761f14bca Initial load
duke
parents:
diff changeset
    51
        }
474761f14bca Initial load
duke
parents:
diff changeset
    52
    }
474761f14bca Initial load
duke
parents:
diff changeset
    53
474761f14bca Initial load
duke
parents:
diff changeset
    54
    static interface Log {
474761f14bca Initial load
duke
parents:
diff changeset
    55
        void info(String msg);
474761f14bca Initial load
duke
parents:
diff changeset
    56
        void verbose(String msg);
474761f14bca Initial load
duke
parents:
diff changeset
    57
        void error(String msg, Exception e);
474761f14bca Initial load
duke
parents:
diff changeset
    58
    }
474761f14bca Initial load
duke
parents:
diff changeset
    59
474761f14bca Initial load
duke
parents:
diff changeset
    60
    private String propfiles[];
474761f14bca Initial load
duke
parents:
diff changeset
    61
    private String outfiles[] ;
474761f14bca Initial load
duke
parents:
diff changeset
    62
    private int stripCount = 0;
474761f14bca Initial load
duke
parents:
diff changeset
    63
    private boolean quiet = false;
474761f14bca Initial load
duke
parents:
diff changeset
    64
    private Log log;
474761f14bca Initial load
duke
parents:
diff changeset
    65
474761f14bca Initial load
duke
parents:
diff changeset
    66
    public void setLog(Log log) {
474761f14bca Initial load
duke
parents:
diff changeset
    67
        this.log = log;
474761f14bca Initial load
duke
parents:
diff changeset
    68
    }
474761f14bca Initial load
duke
parents:
diff changeset
    69
474761f14bca Initial load
duke
parents:
diff changeset
    70
    private boolean parseOptions(String args[]) {
474761f14bca Initial load
duke
parents:
diff changeset
    71
        boolean ok = true;
474761f14bca Initial load
duke
parents:
diff changeset
    72
        if ( stripCount > 0 ) {
474761f14bca Initial load
duke
parents:
diff changeset
    73
            String new_propfiles[] = new String[stripCount + args.length];
474761f14bca Initial load
duke
parents:
diff changeset
    74
            String new_outfiles[]  = new String[stripCount + args.length];
474761f14bca Initial load
duke
parents:
diff changeset
    75
            System.arraycopy(propfiles, 0, new_propfiles, 0, stripCount);
474761f14bca Initial load
duke
parents:
diff changeset
    76
            System.arraycopy(outfiles, 0, new_outfiles, 0, stripCount);
474761f14bca Initial load
duke
parents:
diff changeset
    77
            propfiles = new_propfiles;
474761f14bca Initial load
duke
parents:
diff changeset
    78
            outfiles  = new_outfiles;
474761f14bca Initial load
duke
parents:
diff changeset
    79
        } else {
474761f14bca Initial load
duke
parents:
diff changeset
    80
            propfiles = new String[args.length];
474761f14bca Initial load
duke
parents:
diff changeset
    81
            outfiles  = new String[args.length];
474761f14bca Initial load
duke
parents:
diff changeset
    82
        }
474761f14bca Initial load
duke
parents:
diff changeset
    83
474761f14bca Initial load
duke
parents:
diff changeset
    84
        for ( int i = 0; i < args.length ; i++ ) {
474761f14bca Initial load
duke
parents:
diff changeset
    85
            if ( "-strip".equals(args[i]) && i+2 < args.length ) {
474761f14bca Initial load
duke
parents:
diff changeset
    86
                propfiles[stripCount] = args[++i];
474761f14bca Initial load
duke
parents:
diff changeset
    87
                outfiles[stripCount]    = args[++i];
474761f14bca Initial load
duke
parents:
diff changeset
    88
                stripCount++;
474761f14bca Initial load
duke
parents:
diff changeset
    89
            } else if ( "-optionsfile".equals(args[i]) && i+1 < args.length ) {
474761f14bca Initial load
duke
parents:
diff changeset
    90
                String filename = args[++i];
474761f14bca Initial load
duke
parents:
diff changeset
    91
                FileInputStream finput = null;
474761f14bca Initial load
duke
parents:
diff changeset
    92
                byte contents[] = null;
474761f14bca Initial load
duke
parents:
diff changeset
    93
                try {
474761f14bca Initial load
duke
parents:
diff changeset
    94
                    finput = new FileInputStream(filename);
474761f14bca Initial load
duke
parents:
diff changeset
    95
                    int byteCount = finput.available();
474761f14bca Initial load
duke
parents:
diff changeset
    96
                    if ( byteCount <= 0 ) {
474761f14bca Initial load
duke
parents:
diff changeset
    97
                        log.error("The -optionsfile file is empty", null);
474761f14bca Initial load
duke
parents:
diff changeset
    98
                        ok = false;
474761f14bca Initial load
duke
parents:
diff changeset
    99
                    } else {
474761f14bca Initial load
duke
parents:
diff changeset
   100
                        contents = new byte[byteCount];
474761f14bca Initial load
duke
parents:
diff changeset
   101
                        int bytesRead = finput.read(contents);
474761f14bca Initial load
duke
parents:
diff changeset
   102
                        if ( byteCount != bytesRead ) {
474761f14bca Initial load
duke
parents:
diff changeset
   103
                            log.error("Cannot read all of -optionsfile file", null);
474761f14bca Initial load
duke
parents:
diff changeset
   104
                            ok = false;
474761f14bca Initial load
duke
parents:
diff changeset
   105
                        }
474761f14bca Initial load
duke
parents:
diff changeset
   106
                    }
474761f14bca Initial load
duke
parents:
diff changeset
   107
                } catch ( IOException e ) {
474761f14bca Initial load
duke
parents:
diff changeset
   108
                    log.error("cannot open " + filename, e);
474761f14bca Initial load
duke
parents:
diff changeset
   109
                    ok = false;
474761f14bca Initial load
duke
parents:
diff changeset
   110
                }
474761f14bca Initial load
duke
parents:
diff changeset
   111
                if ( finput != null ) {
474761f14bca Initial load
duke
parents:
diff changeset
   112
                    try {
474761f14bca Initial load
duke
parents:
diff changeset
   113
                        finput.close();
474761f14bca Initial load
duke
parents:
diff changeset
   114
                    } catch ( IOException e ) {
474761f14bca Initial load
duke
parents:
diff changeset
   115
                        ok = false;
474761f14bca Initial load
duke
parents:
diff changeset
   116
                        log.error("cannot close " + filename, e);
474761f14bca Initial load
duke
parents:
diff changeset
   117
                    }
474761f14bca Initial load
duke
parents:
diff changeset
   118
                }
474761f14bca Initial load
duke
parents:
diff changeset
   119
                if ( ok = true && contents != null ) {
474761f14bca Initial load
duke
parents:
diff changeset
   120
                    String tokens[] = (new String(contents)).split("\\s+");
474761f14bca Initial load
duke
parents:
diff changeset
   121
                    if ( tokens.length > 0 ) {
474761f14bca Initial load
duke
parents:
diff changeset
   122
                        ok = parseOptions(tokens);
474761f14bca Initial load
duke
parents:
diff changeset
   123
                    }
474761f14bca Initial load
duke
parents:
diff changeset
   124
                }
474761f14bca Initial load
duke
parents:
diff changeset
   125
                if ( !ok ) {
474761f14bca Initial load
duke
parents:
diff changeset
   126
                    break;
474761f14bca Initial load
duke
parents:
diff changeset
   127
                }
474761f14bca Initial load
duke
parents:
diff changeset
   128
        } else if ( "-quiet".equals(args[i]) ) {
474761f14bca Initial load
duke
parents:
diff changeset
   129
                quiet = true;
474761f14bca Initial load
duke
parents:
diff changeset
   130
        } else {
474761f14bca Initial load
duke
parents:
diff changeset
   131
                log.error("argument error", null);
474761f14bca Initial load
duke
parents:
diff changeset
   132
                ok = false;
474761f14bca Initial load
duke
parents:
diff changeset
   133
        }
474761f14bca Initial load
duke
parents:
diff changeset
   134
        }
474761f14bca Initial load
duke
parents:
diff changeset
   135
        return ok;
474761f14bca Initial load
duke
parents:
diff changeset
   136
    }
474761f14bca Initial load
duke
parents:
diff changeset
   137
474761f14bca Initial load
duke
parents:
diff changeset
   138
    private boolean stripFiles(String propertiesPath, String outputPath) {
474761f14bca Initial load
duke
parents:
diff changeset
   139
        boolean ok = true;
474761f14bca Initial load
duke
parents:
diff changeset
   140
        Properties prop = new Properties();
474761f14bca Initial load
duke
parents:
diff changeset
   141
        InputStream in = null;
474761f14bca Initial load
duke
parents:
diff changeset
   142
        try {
474761f14bca Initial load
duke
parents:
diff changeset
   143
                in = new BufferedInputStream(new FileInputStream(propertiesPath));
474761f14bca Initial load
duke
parents:
diff changeset
   144
                prop.load(in);
474761f14bca Initial load
duke
parents:
diff changeset
   145
        } catch ( FileNotFoundException e ) {
474761f14bca Initial load
duke
parents:
diff changeset
   146
                log.error("Cannot access file " + propertiesPath, e);
474761f14bca Initial load
duke
parents:
diff changeset
   147
                ok = false;
474761f14bca Initial load
duke
parents:
diff changeset
   148
        } catch ( IOException e ) {
474761f14bca Initial load
duke
parents:
diff changeset
   149
                log.error("IO exception processing file " + propertiesPath, e);
474761f14bca Initial load
duke
parents:
diff changeset
   150
                ok = false;
474761f14bca Initial load
duke
parents:
diff changeset
   151
        }
474761f14bca Initial load
duke
parents:
diff changeset
   152
        if ( in != null ) {
474761f14bca Initial load
duke
parents:
diff changeset
   153
                try {
474761f14bca Initial load
duke
parents:
diff changeset
   154
                    in.close();
474761f14bca Initial load
duke
parents:
diff changeset
   155
                } catch ( IOException e ) {
474761f14bca Initial load
duke
parents:
diff changeset
   156
                    log.error("IO exception closing file " + propertiesPath, e);
474761f14bca Initial load
duke
parents:
diff changeset
   157
                    ok = false;
474761f14bca Initial load
duke
parents:
diff changeset
   158
                }
474761f14bca Initial load
duke
parents:
diff changeset
   159
        }
474761f14bca Initial load
duke
parents:
diff changeset
   160
474761f14bca Initial load
duke
parents:
diff changeset
   161
        OutputStream out = null;
474761f14bca Initial load
duke
parents:
diff changeset
   162
        try {
474761f14bca Initial load
duke
parents:
diff changeset
   163
                out = new FileOutputStream(outputPath);
474761f14bca Initial load
duke
parents:
diff changeset
   164
                storeProperties(prop, out);
474761f14bca Initial load
duke
parents:
diff changeset
   165
                out.flush();
474761f14bca Initial load
duke
parents:
diff changeset
   166
        } catch ( IOException e ) {
474761f14bca Initial load
duke
parents:
diff changeset
   167
                log.error("IO exception processing file " + outputPath, e);
474761f14bca Initial load
duke
parents:
diff changeset
   168
                e.printStackTrace();
474761f14bca Initial load
duke
parents:
diff changeset
   169
                ok = false;
474761f14bca Initial load
duke
parents:
diff changeset
   170
        }
474761f14bca Initial load
duke
parents:
diff changeset
   171
        if ( out != null ) {
474761f14bca Initial load
duke
parents:
diff changeset
   172
           try {
474761f14bca Initial load
duke
parents:
diff changeset
   173
                    out.close();
474761f14bca Initial load
duke
parents:
diff changeset
   174
           } catch ( IOException e ) {
474761f14bca Initial load
duke
parents:
diff changeset
   175
                    log.error("IO exception closing file " + outputPath, e);
474761f14bca Initial load
duke
parents:
diff changeset
   176
                    ok = false;
474761f14bca Initial load
duke
parents:
diff changeset
   177
           }
474761f14bca Initial load
duke
parents:
diff changeset
   178
        }
474761f14bca Initial load
duke
parents:
diff changeset
   179
        return ok;
474761f14bca Initial load
duke
parents:
diff changeset
   180
    }
474761f14bca Initial load
duke
parents:
diff changeset
   181
474761f14bca Initial load
duke
parents:
diff changeset
   182
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   183
     * Strip the properties filenames supplied, replacing their contents.
474761f14bca Initial load
duke
parents:
diff changeset
   184
     * @param args Names of properties files to process and replace contents
474761f14bca Initial load
duke
parents:
diff changeset
   185
     */
474761f14bca Initial load
duke
parents:
diff changeset
   186
    public boolean run(String args[]) {
474761f14bca Initial load
duke
parents:
diff changeset
   187
        if (log == null) {
474761f14bca Initial load
duke
parents:
diff changeset
   188
            log = new Log() {
474761f14bca Initial load
duke
parents:
diff changeset
   189
                public void error(String msg, Exception e) {
474761f14bca Initial load
duke
parents:
diff changeset
   190
                    System.err.println("ERROR: StripProperties: " + msg);
474761f14bca Initial load
duke
parents:
diff changeset
   191
                    if ( e != null ) {
474761f14bca Initial load
duke
parents:
diff changeset
   192
                        System.err.println("EXCEPTION: " + e.toString());
474761f14bca Initial load
duke
parents:
diff changeset
   193
                        e.printStackTrace();
474761f14bca Initial load
duke
parents:
diff changeset
   194
                    }
474761f14bca Initial load
duke
parents:
diff changeset
   195
                }
474761f14bca Initial load
duke
parents:
diff changeset
   196
                public void info(String msg) {
474761f14bca Initial load
duke
parents:
diff changeset
   197
                    System.out.println(msg);
474761f14bca Initial load
duke
parents:
diff changeset
   198
                }
474761f14bca Initial load
duke
parents:
diff changeset
   199
                public void verbose(String msg) {
474761f14bca Initial load
duke
parents:
diff changeset
   200
                    if (!quiet)
474761f14bca Initial load
duke
parents:
diff changeset
   201
                        System.out.println(msg);
474761f14bca Initial load
duke
parents:
diff changeset
   202
                }
474761f14bca Initial load
duke
parents:
diff changeset
   203
            };
474761f14bca Initial load
duke
parents:
diff changeset
   204
        }
474761f14bca Initial load
duke
parents:
diff changeset
   205
474761f14bca Initial load
duke
parents:
diff changeset
   206
        boolean ok = true;
474761f14bca Initial load
duke
parents:
diff changeset
   207
        ok = parseOptions(args);
474761f14bca Initial load
duke
parents:
diff changeset
   208
        if ( ok && stripCount == 0 ) {
474761f14bca Initial load
duke
parents:
diff changeset
   209
                log.error("options parsed but no files to compile", null);
474761f14bca Initial load
duke
parents:
diff changeset
   210
                ok = false;
474761f14bca Initial load
duke
parents:
diff changeset
   211
        }
474761f14bca Initial load
duke
parents:
diff changeset
   212
        /* Need at least one file. */
474761f14bca Initial load
duke
parents:
diff changeset
   213
        if ( !ok ) {
474761f14bca Initial load
duke
parents:
diff changeset
   214
            //usage(log);
474761f14bca Initial load
duke
parents:
diff changeset
   215
        } else {
474761f14bca Initial load
duke
parents:
diff changeset
   216
            /* Process files */
474761f14bca Initial load
duke
parents:
diff changeset
   217
            for ( int i = 0; i < stripCount && ok ; i++ ) {
474761f14bca Initial load
duke
parents:
diff changeset
   218
                    ok = stripFiles(propfiles[i], outfiles[i]);
474761f14bca Initial load
duke
parents:
diff changeset
   219
            }
474761f14bca Initial load
duke
parents:
diff changeset
   220
        }
474761f14bca Initial load
duke
parents:
diff changeset
   221
        return ok;
474761f14bca Initial load
duke
parents:
diff changeset
   222
    }
474761f14bca Initial load
duke
parents:
diff changeset
   223
474761f14bca Initial load
duke
parents:
diff changeset
   224
    // --- code below here is adapted from java.util.Properties ---
474761f14bca Initial load
duke
parents:
diff changeset
   225
474761f14bca Initial load
duke
parents:
diff changeset
   226
    private static final String specialSaveChars = "=: \t\r\n\f#!";
474761f14bca Initial load
duke
parents:
diff changeset
   227
474761f14bca Initial load
duke
parents:
diff changeset
   228
    /*
474761f14bca Initial load
duke
parents:
diff changeset
   229
     * Converts unicodes to encoded &#92;uxxxx
474761f14bca Initial load
duke
parents:
diff changeset
   230
     * and writes out any of the characters in specialSaveChars
474761f14bca Initial load
duke
parents:
diff changeset
   231
     * with a preceding slash
474761f14bca Initial load
duke
parents:
diff changeset
   232
     */
474761f14bca Initial load
duke
parents:
diff changeset
   233
    private static String saveConvert(String theString, boolean escapeSpace) {
474761f14bca Initial load
duke
parents:
diff changeset
   234
        int len = theString.length();
474761f14bca Initial load
duke
parents:
diff changeset
   235
        StringBuffer outBuffer = new StringBuffer(len*2);
474761f14bca Initial load
duke
parents:
diff changeset
   236
474761f14bca Initial load
duke
parents:
diff changeset
   237
        for(int x=0; x<len; x++) {
474761f14bca Initial load
duke
parents:
diff changeset
   238
            char aChar = theString.charAt(x);
474761f14bca Initial load
duke
parents:
diff changeset
   239
            switch(aChar) {
474761f14bca Initial load
duke
parents:
diff changeset
   240
                case ' ':
474761f14bca Initial load
duke
parents:
diff changeset
   241
                    if (x == 0 || escapeSpace) {
474761f14bca Initial load
duke
parents:
diff changeset
   242
                        outBuffer.append('\\');
474761f14bca Initial load
duke
parents:
diff changeset
   243
                    }
474761f14bca Initial load
duke
parents:
diff changeset
   244
                    outBuffer.append(' ');
474761f14bca Initial load
duke
parents:
diff changeset
   245
                    break;
474761f14bca Initial load
duke
parents:
diff changeset
   246
                case '\\':
474761f14bca Initial load
duke
parents:
diff changeset
   247
                    outBuffer.append('\\');
474761f14bca Initial load
duke
parents:
diff changeset
   248
                    outBuffer.append('\\');
474761f14bca Initial load
duke
parents:
diff changeset
   249
                    break;
474761f14bca Initial load
duke
parents:
diff changeset
   250
                case '\t':
474761f14bca Initial load
duke
parents:
diff changeset
   251
                    outBuffer.append('\\');
474761f14bca Initial load
duke
parents:
diff changeset
   252
                    outBuffer.append('t');
474761f14bca Initial load
duke
parents:
diff changeset
   253
                    break;
474761f14bca Initial load
duke
parents:
diff changeset
   254
                case '\n':
474761f14bca Initial load
duke
parents:
diff changeset
   255
                    outBuffer.append('\\');
474761f14bca Initial load
duke
parents:
diff changeset
   256
                    outBuffer.append('n');
474761f14bca Initial load
duke
parents:
diff changeset
   257
                    break;
474761f14bca Initial load
duke
parents:
diff changeset
   258
                case '\r':
474761f14bca Initial load
duke
parents:
diff changeset
   259
                    outBuffer.append('\\');
474761f14bca Initial load
duke
parents:
diff changeset
   260
                    outBuffer.append('r');
474761f14bca Initial load
duke
parents:
diff changeset
   261
                    break;
474761f14bca Initial load
duke
parents:
diff changeset
   262
                case '\f':
474761f14bca Initial load
duke
parents:
diff changeset
   263
                    outBuffer.append('\\');
474761f14bca Initial load
duke
parents:
diff changeset
   264
                    outBuffer.append('f');
474761f14bca Initial load
duke
parents:
diff changeset
   265
                    break;
474761f14bca Initial load
duke
parents:
diff changeset
   266
                default:
474761f14bca Initial load
duke
parents:
diff changeset
   267
                    if ((aChar < 0x0020) || (aChar == 0x007e) || (aChar > 0x00ff)) {
474761f14bca Initial load
duke
parents:
diff changeset
   268
                        outBuffer.append('\\');
474761f14bca Initial load
duke
parents:
diff changeset
   269
                        outBuffer.append('u');
474761f14bca Initial load
duke
parents:
diff changeset
   270
                        outBuffer.append(toHex((aChar >> 12) & 0xF));
474761f14bca Initial load
duke
parents:
diff changeset
   271
                        outBuffer.append(toHex((aChar >>  8) & 0xF));
474761f14bca Initial load
duke
parents:
diff changeset
   272
                        outBuffer.append(toHex((aChar >>  4) & 0xF));
474761f14bca Initial load
duke
parents:
diff changeset
   273
                        outBuffer.append(toHex( aChar        & 0xF));
474761f14bca Initial load
duke
parents:
diff changeset
   274
                    } else {
474761f14bca Initial load
duke
parents:
diff changeset
   275
                        if (specialSaveChars.indexOf(aChar) != -1) {
474761f14bca Initial load
duke
parents:
diff changeset
   276
                            outBuffer.append('\\');
474761f14bca Initial load
duke
parents:
diff changeset
   277
                        }
474761f14bca Initial load
duke
parents:
diff changeset
   278
                        outBuffer.append(aChar);
474761f14bca Initial load
duke
parents:
diff changeset
   279
                    }
474761f14bca Initial load
duke
parents:
diff changeset
   280
            }
474761f14bca Initial load
duke
parents:
diff changeset
   281
        }
474761f14bca Initial load
duke
parents:
diff changeset
   282
        return outBuffer.toString();
474761f14bca Initial load
duke
parents:
diff changeset
   283
    }
474761f14bca Initial load
duke
parents:
diff changeset
   284
474761f14bca Initial load
duke
parents:
diff changeset
   285
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   286
     * Writes the content of <code>properties</code> to <code>out</code>.
474761f14bca Initial load
duke
parents:
diff changeset
   287
     * The format is that of Properties.store with the following modifications:
474761f14bca Initial load
duke
parents:
diff changeset
   288
     * <ul>
474761f14bca Initial load
duke
parents:
diff changeset
   289
     * <li>No header or date is written
474761f14bca Initial load
duke
parents:
diff changeset
   290
     * <li>Latin-1 characters are written as single bytes, not escape sequences
474761f14bca Initial load
duke
parents:
diff changeset
   291
     * <li>Line breaks are indicated by a single \n independent of platform
474761f14bca Initial load
duke
parents:
diff changeset
   292
     * <ul>
474761f14bca Initial load
duke
parents:
diff changeset
   293
     */
474761f14bca Initial load
duke
parents:
diff changeset
   294
    private static void storeProperties(Properties properties, OutputStream out)
474761f14bca Initial load
duke
parents:
diff changeset
   295
    throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   296
        BufferedWriter awriter;
474761f14bca Initial load
duke
parents:
diff changeset
   297
        awriter = new BufferedWriter(new OutputStreamWriter(out, "8859_1"));
474761f14bca Initial load
duke
parents:
diff changeset
   298
        for (Enumeration e = properties.keys(); e.hasMoreElements();) {
474761f14bca Initial load
duke
parents:
diff changeset
   299
            String key = (String)e.nextElement();
474761f14bca Initial load
duke
parents:
diff changeset
   300
            String val = (String)properties.get(key);
474761f14bca Initial load
duke
parents:
diff changeset
   301
            key = saveConvert(key, true);
474761f14bca Initial load
duke
parents:
diff changeset
   302
474761f14bca Initial load
duke
parents:
diff changeset
   303
            /* No need to escape embedded and trailing spaces for value, hence
474761f14bca Initial load
duke
parents:
diff changeset
   304
             * pass false to flag.
474761f14bca Initial load
duke
parents:
diff changeset
   305
             */
474761f14bca Initial load
duke
parents:
diff changeset
   306
            val = saveConvert(val, false);
474761f14bca Initial load
duke
parents:
diff changeset
   307
            writeln(awriter, key + "=" + val);
474761f14bca Initial load
duke
parents:
diff changeset
   308
        }
474761f14bca Initial load
duke
parents:
diff changeset
   309
        awriter.flush();
474761f14bca Initial load
duke
parents:
diff changeset
   310
    }
474761f14bca Initial load
duke
parents:
diff changeset
   311
474761f14bca Initial load
duke
parents:
diff changeset
   312
    private static void writeln(BufferedWriter bw, String s) throws IOException {
474761f14bca Initial load
duke
parents:
diff changeset
   313
        bw.write(s);
474761f14bca Initial load
duke
parents:
diff changeset
   314
        bw.write("\n");
474761f14bca Initial load
duke
parents:
diff changeset
   315
    }
474761f14bca Initial load
duke
parents:
diff changeset
   316
474761f14bca Initial load
duke
parents:
diff changeset
   317
    /**
474761f14bca Initial load
duke
parents:
diff changeset
   318
     * Convert a nibble to a hex character
474761f14bca Initial load
duke
parents:
diff changeset
   319
     * @param   nibble  the nibble to convert.
474761f14bca Initial load
duke
parents:
diff changeset
   320
     */
474761f14bca Initial load
duke
parents:
diff changeset
   321
    private static char toHex(int nibble) {
474761f14bca Initial load
duke
parents:
diff changeset
   322
        return hexDigit[(nibble & 0xF)];
474761f14bca Initial load
duke
parents:
diff changeset
   323
    }
474761f14bca Initial load
duke
parents:
diff changeset
   324
474761f14bca Initial load
duke
parents:
diff changeset
   325
    /** A table of hex digits */
474761f14bca Initial load
duke
parents:
diff changeset
   326
    private static final char[] hexDigit = {
474761f14bca Initial load
duke
parents:
diff changeset
   327
        '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
474761f14bca Initial load
duke
parents:
diff changeset
   328
    };
474761f14bca Initial load
duke
parents:
diff changeset
   329
}