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