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