make/jdk/src/classes/build/tools/dtdbuilder/DTDInputStream.java
author mgronlun
Tue, 03 Sep 2019 13:31:54 +0200
branchJEP-349-branch
changeset 57989 338ea33db84d
parent 47216 71c04702a3d5
permissions -rw-r--r--
macro removals
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21805
diff changeset
     2
 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package build.tools.dtdbuilder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.text.html.parser.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.Reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.CharArrayReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.FilterReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * A stream for reading HTML files. This stream takes care
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * of \r\n conversions and parameter entity expansion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @see DTD
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @see DTDParser
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Steven B. Byrne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
class DTDInputStream extends FilterReader implements DTDConstants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public DTD dtd;
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
    51
    public Stack<Object> stack = new Stack<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public char str[] = new char[64];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public int replace = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public int ln = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public int ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Create the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public DTDInputStream(InputStream in, DTD dtd) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        super(new InputStreamReader(in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.dtd = dtd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this.ch = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Error
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public void error(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        System.out.println("line " + ln + ": dtd input error: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Push a single character
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public void push(int ch) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        char data[] = {(char)ch};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        push(new CharArrayReader(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Push an array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public void push(char data[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (data.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            push(new CharArrayReader(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Push an entire input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    void push(Reader in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        stack.push(new Integer(ln));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        stack.push(new Integer(ch));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        stack.push(this.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        this.in = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        ch = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Read a character from the input. Automatically pop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * a stream of the stack if the EOF is reached. Also replaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * parameter entities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * [60] 350:22
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
   108
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
          case '%': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            ch = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (replace > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                return '%';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            while (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                   ((ch >= '0') && (ch <= '9')) || (ch == '.') || (ch == '-')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                str[pos++] = (char)ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                ch = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (pos == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                return '%';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            String nm = new String(str, 0, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            Entity ent = dtd.getEntity(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if (ent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                error("undefined entity reference: " + nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                return read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            // Skip ; or RE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
              case '\r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                ln++;
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
   138
                /* fall through */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
              case ';':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                ch = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
              case '\n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                ln++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                if ((ch = in.read()) == '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    ch = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            // Push the entity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                push(getEntityInputReader(ent));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                error("entity data not found: " + ent + ", " + ent.getString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
          case '\n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            ln++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if ((ch = in.read()) == '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                ch = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            return '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
          case '\r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            ln++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            ch = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
          case -1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (stack.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                in = (Reader)stack.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                ch = ((Integer)stack.pop()).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                ln = ((Integer)stack.pop()).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                return read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            int c = ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            ch = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    * Return the data as a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private Reader getEntityInputReader(Entity ent) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if ((ent.type & Entity.PUBLIC) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            // InputStream is = DTDBuilder.mapping.get(ent.getString()).openStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            // return new InputStreamReader(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            String path = DTDBuilder.mapping.get(ent.getString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            return new InputStreamReader(new FileInputStream(path));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if ((ent.type & Entity.SYSTEM) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            //InputStream is =  new URL(DTDBuilder.mapping.base, ent.getString()).openStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            String path = DTDBuilder.mapping.baseStr +  ent.getString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            return new InputStreamReader(new FileInputStream(path));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return new CharArrayReader(ent.data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
}