src/demo/share/jpackager/JNLPConverter/src/jnlp/converter/parser/xml/XMLEncoding.java
author herrick
Fri, 12 Oct 2018 19:00:51 -0400
branchJDK-8200758-branch
changeset 56963 eaca4369b068
permissions -rw-r--r--
8198472: Add support for creating bundles from JNLP files Submitten-by: almatvee Reviewed-by: herrick, kcr, prr, asemenyuk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56963
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
     1
/*
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
     2
 * Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
     4
 *
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
     7
 * published by the Free Software Foundation.
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
     8
 *
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    13
 * accompanied this code).
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    14
 *
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    18
 *
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    21
 * questions.
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    22
 */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    23
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    24
package jnlp.converter.parser.xml;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    25
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    26
import java.io.ByteArrayInputStream;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    27
import java.io.EOFException;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    28
import java.io.InputStreamReader;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    29
import java.io.IOException;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    30
import java.io.Reader;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    31
import java.io.UnsupportedEncodingException;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    32
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    33
public class XMLEncoding {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    34
    /**
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    35
     * Decodes a byte stream into a String by testing for a Byte Order Mark
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    36
     * (BOM) or an XML declaration.
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    37
     * <br />
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    38
     * Detection begins by examining the first four octets of the stream for a
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    39
     * BOM. If a BOM is not found, then an encoding declaration is looked for
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    40
     * at the beginning of the stream. If the encoding still can not be
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    41
     * determined at this point, then UTF-8 is assumed.
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    42
     *
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    43
     * @param data  an array of bytes containing an encoded XML document.
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    44
     *
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    45
     * @return A string containing the decoded XML document.
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    46
     */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    47
    public static String decodeXML(byte [] data) throws IOException {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    48
        int start = 0;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    49
        String encoding;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    50
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    51
        if (data.length < BOM_LENGTH) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    52
            throw (new EOFException("encoding.error.not.xml"));
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    53
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    54
        // no else required; successfully read stream
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    55
        int firstFour = ((0xff000000 & ((int) data[0] << 24)) |
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    56
                         (0x00ff0000 & ((int) data[1] << 16)) |
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    57
                         (0x0000ff00 & ((int) data[2] <<  8)) |
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    58
                         (0x000000ff &  (int) data[3]));
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    59
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    60
        // start by examining the first four bytes for a BOM
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    61
        switch (firstFour) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    62
            case EBCDIC:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    63
                // examine the encoding declaration
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    64
                encoding = examineEncodingDeclaration(data, IBM037_ENC);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    65
                break;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    66
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    67
            case XML_DECLARATION:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    68
                // assume UTF-8, but examine the encoding declaration
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    69
                encoding = examineEncodingDeclaration(data, UTF_8_ENC);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    70
                break;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    71
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    72
            case UTF_16BE:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    73
                encoding = UTF_16BE_ENC;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    74
                break;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    75
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    76
            case UTF_16LE:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    77
                encoding = UTF_16LE_ENC;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    78
                break;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    79
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    80
            case UNUSUAL_OCTET_1:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    81
            case UNUSUAL_OCTET_2:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    82
                throw (new UnsupportedEncodingException("encoding.error.unusual.octet"));
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    83
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    84
            case UTF_32_BE_BOM:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    85
            case UTF_32_LE_BOM:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    86
                encoding = UTF_32_ENC;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    87
                break;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    88
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    89
            default:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    90
                int firstThree = firstFour & 0xffffff00;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    91
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    92
                switch (firstThree) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    93
                    case UTF_8_BOM:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    94
                        // the InputStreamReader class doen't properly handle
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    95
                        // the Byte Order Mark (BOM) in UTF-8 streams, so don't
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    96
                        // putback those 3 bytes.
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    97
                        start    = 3;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    98
                        encoding = UTF_8_ENC;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
    99
                        break;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   100
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   101
                    default:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   102
                        int firstTwo = firstFour & 0xffff0000;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   103
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   104
                        switch (firstTwo) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   105
                            case UTF_16_BE_BOM:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   106
                            case UTF_16_LE_BOM:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   107
                                encoding = UTF_16_ENC;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   108
                                break;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   109
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   110
                            default:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   111
                                // this is probably UTF-8 without the encoding
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   112
                                // declaration
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   113
                                encoding = UTF_8_ENC;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   114
                                break;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   115
                        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   116
                        break;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   117
                }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   118
                break;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   119
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   120
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   121
        return (new String(data, start, data.length - start, encoding));
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   122
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   123
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   124
    /**
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   125
     * [3]  S            ::= ( #x20 | #x09 | #x0d | #x0a )
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   126
     * [23] XMLDecl      ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   127
     * [24] VersionInfo  ::= S 'version' Eq ( '"' VersionNum '"' |
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   128
     *                                        "'" VersionNum "'" )
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   129
     * [25] Eq           ::= S? '=' S?
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   130
     * [26] VersionNum   ::= ([a-zA-Z0-9_.:] | '-')+
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   131
     * [80] EncodingDecl ::= S 'encoding' Eq ( '"' EncName '"' |
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   132
     *                                         "'" EncName "'" )
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   133
     * [81] EncName      ::= [a-zA-Z] ([a-zA-Z0-9_.] | '-')*
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   134
     */
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   135
    private static String examineEncodingDeclaration(byte [] data,
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   136
                          String    encoding) throws IOException {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   137
        boolean loop       = false;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   138
        boolean recognized = false;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   139
        boolean almost     = false;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   140
        boolean question   = false;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   141
        boolean done       = false;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   142
        boolean found      = false;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   143
        int     pos        = 0;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   144
        int     ch         = -1;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   145
        Reader  reader     = null;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   146
        String  result     = ((encoding != null) ? encoding : UTF_8_ENC);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   147
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   148
        reader = new InputStreamReader(new ByteArrayInputStream(data), result);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   149
        ch     = reader.read();
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   150
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   151
        // if this is an XML declaration, it will start with the text '<?xml'
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   152
        for (int i = 0; ((i < XML_DECL_START.length()) && (done == false)); i++) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   153
            if (ch != XML_DECL_START.charAt(i)) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   154
                // This doesn't look like an XML declaration.  This method
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   155
                // should only be called if the stream contains an XML
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   156
                // declaration in the encoding that is passed into the method.
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   157
                done = true;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   158
                break;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   159
            }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   160
            // no else required; still matches
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   161
            ch = reader.read();
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   162
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   163
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   164
        // there must be at least one whitespace character next.
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   165
        loop = true;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   166
        while ((loop == true) && (done == false)) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   167
            switch (ch) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   168
                case SPACE:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   169
                case TAB:         // intentional
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   170
                case LINEFEED:    // fall
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   171
                case RETURN:      // through
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   172
                    ch = reader.read();
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   173
                    break;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   174
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   175
                case -1:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   176
                    // unexpected EOF
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   177
                    done = true;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   178
                    break;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   179
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   180
                default:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   181
                    // non-whitespace
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   182
                    loop = false;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   183
                    break;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   184
            }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   185
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   186
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   187
        // now look for the text 'encoding', but if the end of the XML
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   188
        // declaration (signified by the text '?>') comes first, then
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   189
        // assume the encoding is UTF-8
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   190
        loop = true;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   191
        while ((loop == true) && (done == false)) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   192
            if (ch == -1) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   193
                // unexpected EOF
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   194
                done = true;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   195
                break;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   196
            } else if (recognized == true) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   197
                // this is the encoding declaration as long as the next few
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   198
                // characters are whitespace and/or the equals ('=') sign
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   199
                switch (ch) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   200
                    case SPACE:       // intentional
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   201
                    case TAB:         // fall
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   202
                    case LINEFEED:    // through
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   203
                    case RETURN:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   204
                        // don't need to do anything
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   205
                        break;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   206
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   207
                    case EQUAL:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   208
                        if (almost == false) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   209
                            // got the equal, now find a quote
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   210
                            almost = true;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   211
                        } else {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   212
                            // this is not valid XML, so punt
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   213
                            recognized = false;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   214
                            done       = true;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   215
                        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   216
                        break;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   217
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   218
                    case DOUBLE_QUOTE:    // intentional
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   219
                    case SINGLE_QUOTE:    // fall through
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   220
                        if (almost == true) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   221
                            // got the quote, so move on to get the value
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   222
                            loop = false;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   223
                        } else {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   224
                            // got a quote before the equal; this is not valid
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   225
                            // XML, so punt
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   226
                            recognized = false;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   227
                            done       = true;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   228
                        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   229
                        break;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   230
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   231
                    default:
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   232
                        // non-whitespace
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   233
                        recognized = false;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   234
                        if (almost == true) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   235
                            // this is not valid XML, so punt
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   236
                            done = true;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   237
                        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   238
                        // no else required; this wasn't the encoding
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   239
                        // declaration
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   240
                        break;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   241
                }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   242
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   243
                if (recognized == false) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   244
                    // this isn't the encoding declaration, so go back to the
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   245
                    // top without reading the next character
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   246
                    pos = 0;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   247
                    continue;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   248
                }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   249
                // no else required; still looking good
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   250
            } else if (ch == ENCODING_DECL.charAt(pos++)) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   251
                if (ENCODING_DECL.length() == pos) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   252
                    // this looks like the encoding declaration
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   253
                    recognized = true;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   254
                }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   255
                // no else required; this might be the encoding declaration
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   256
            } else if (ch == '?') {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   257
                question = true;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   258
                pos      = 0;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   259
            } else if ((ch == '>') && (question == true)) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   260
                // there is no encoding declaration, so assume that the initial
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   261
                // encoding guess was correct
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   262
                done   = true;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   263
                continue;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   264
            } else {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   265
                // still searching for the encoding declaration
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   266
                pos = 0;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   267
            }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   268
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   269
            ch = reader.read();
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   270
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   271
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   272
        if (done == false) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   273
            StringBuilder buffer = new StringBuilder(MAX_ENC_NAME);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   274
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   275
            if (((ch >= 'a') && (ch <= 'z')) |
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   276
                ((ch >= 'A') && (ch <= 'Z'))) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   277
                // add the character to the result
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   278
                buffer.append((char) ch);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   279
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   280
                loop = true;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   281
                while ((loop == true) && (done == false)) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   282
                    ch = reader.read();
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   283
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   284
                    if (((ch >= 'a') && (ch <= 'z')) ||
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   285
                        ((ch >= 'A') && (ch <= 'Z')) ||
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   286
                        ((ch >= '0') && (ch <= '9')) ||
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   287
                        (ch == '_') || (ch == '.') || (ch == '-')) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   288
                        // add the character to the result
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   289
                        buffer.append((char) ch);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   290
                    } else if ((ch == DOUBLE_QUOTE) || (ch == SINGLE_QUOTE)) {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   291
                        // finished!
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   292
                        found  = true;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   293
                        done   = true;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   294
                        result = buffer.toString();
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   295
                    } else {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   296
                        // this is not a valid encoding name, so punt
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   297
                        done = true;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   298
                    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   299
                }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   300
            } else {
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   301
                // this is not a valid encoding name, so punt
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   302
                done = true;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   303
            }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   304
        }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   305
        // no else required; already failed to find the encoding somewhere else
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   306
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   307
        return (result);
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   308
    }
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   309
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   310
    private static final int BOM_LENGTH   = 4;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   311
    private static final int MAX_ENC_NAME = 512;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   312
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   313
    private static final int SPACE        = 0x00000020;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   314
    private static final int TAB          = 0x00000009;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   315
    private static final int LINEFEED     = 0x0000000a;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   316
    private static final int RETURN       = 0x0000000d;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   317
    private static final int EQUAL        = '=';
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   318
    private static final int DOUBLE_QUOTE = '\"';
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   319
    private static final int SINGLE_QUOTE = '\'';
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   320
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   321
    private static final int UTF_32_BE_BOM   = 0x0000feff;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   322
    private static final int UTF_32_LE_BOM   = 0xfffe0000;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   323
    private static final int UTF_16_BE_BOM   = 0xfeff0000;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   324
    private static final int UTF_16_LE_BOM   = 0xfffe0000;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   325
    private static final int UTF_8_BOM       = 0xefbbbf00;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   326
    private static final int UNUSUAL_OCTET_1 = 0x00003c00;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   327
    private static final int UNUSUAL_OCTET_2 = 0x003c0000;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   328
    private static final int UTF_16BE        = 0x003c003f;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   329
    private static final int UTF_16LE        = 0x3c003f00;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   330
    private static final int EBCDIC          = 0x4c6fa794;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   331
    private static final int XML_DECLARATION = 0x3c3f786d;
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   332
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   333
    private static final String UTF_32_ENC   = "UTF-32";
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   334
    private static final String UTF_16_ENC   = "UTF-16";
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   335
    private static final String UTF_16BE_ENC = "UTF-16BE";
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   336
    private static final String UTF_16LE_ENC = "UTF-16LE";
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   337
    private static final String UTF_8_ENC    = "UTF-8";
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   338
    private static final String IBM037_ENC   = "IBM037";
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   339
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   340
    private static final String XML_DECL_START = "<?xml";
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   341
    private static final String ENCODING_DECL  = "encoding";
eaca4369b068 8198472: Add support for creating bundles from JNLP files
herrick
parents:
diff changeset
   342
}