Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/dtdbuilder/DTDInputStream.java
32287 views
/*1* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package build.tools.dtdbuilder;2627import javax.swing.text.html.parser.*;28import java.io.IOException;29import java.io.FileInputStream;30import java.io.InputStream;31import java.io.Reader;32import java.io.InputStreamReader;33import java.io.CharArrayReader;34import java.io.FilterReader;35import java.util.Stack;36import java.net.URL;3738/**39* A stream for reading HTML files. This stream takes care40* of \r\n conversions and parameter entity expansion.41*42* @see DTD43* @see DTDParser44* @author Arthur van Hoff45* @author Steven B. Byrne46*/47public final48class DTDInputStream extends FilterReader implements DTDConstants {49public DTD dtd;50public Stack<Object> stack = new Stack<>();51public char str[] = new char[64];52public int replace = 0;53public int ln = 1;54public int ch;5556/**57* Create the stream.58*/59public DTDInputStream(InputStream in, DTD dtd) throws IOException {60super(new InputStreamReader(in));61this.dtd = dtd;62this.ch = in.read();63}6465/**66* Error67*/68public void error(String msg) {69System.out.println("line " + ln + ": dtd input error: " + msg);70}7172/**73* Push a single character74*/75public void push(int ch) throws IOException {76char data[] = {(char)ch};77push(new CharArrayReader(data));78}798081/**82* Push an array of bytes.83*/84public void push(char data[]) throws IOException {85if (data.length > 0) {86push(new CharArrayReader(data));87}88}8990/**91* Push an entire input stream92*/93void push(Reader in) throws IOException {94stack.push(new Integer(ln));95stack.push(new Integer(ch));96stack.push(this.in);97this.in = in;98ch = in.read();99}100101/**102* Read a character from the input. Automatically pop103* a stream of the stack if the EOF is reached. Also replaces104* parameter entities.105* [60] 350:22106*/107@SuppressWarnings("fallthrough")108public int read() throws IOException {109switch (ch) {110case '%': {111ch = in.read();112if (replace > 0) {113return '%';114}115116int pos = 0;117while (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) ||118((ch >= '0') && (ch <= '9')) || (ch == '.') || (ch == '-')) {119str[pos++] = (char)ch;120ch = in.read();121}122if (pos == 0) {123return '%';124}125126String nm = new String(str, 0, pos);127Entity ent = dtd.getEntity(nm);128if (ent == null) {129error("undefined entity reference: " + nm);130return read();131}132133// Skip ; or RE134switch (ch) {135case '\r':136ln++;137/* fall through */138case ';':139ch = in.read();140break;141case '\n':142ln++;143if ((ch = in.read()) == '\r') {144ch = in.read();145}146break;147}148149// Push the entity.150try {151push(getEntityInputReader(ent));152} catch (Exception e) {153error("entity data not found: " + ent + ", " + ent.getString());154}155return read();156}157158case '\n':159ln++;160if ((ch = in.read()) == '\r') {161ch = in.read();162}163return '\n';164165case '\r':166ln++;167ch = in.read();168return '\n';169170case -1:171if (stack.size() > 0) {172in = (Reader)stack.pop();173ch = ((Integer)stack.pop()).intValue();174ln = ((Integer)stack.pop()).intValue();175return read();176}177return -1;178179default:180int c = ch;181ch = in.read();182return c;183}184}185186/**187* Return the data as a stream.188*/189private Reader getEntityInputReader(Entity ent) throws IOException {190if ((ent.type & Entity.PUBLIC) != 0) {191// InputStream is = DTDBuilder.mapping.get(ent.getString()).openStream();192// return new InputStreamReader(is);193String path = DTDBuilder.mapping.get(ent.getString());194195return new InputStreamReader(new FileInputStream(path));196}197if ((ent.type & Entity.SYSTEM) != 0) {198//InputStream is = new URL(DTDBuilder.mapping.base, ent.getString()).openStream();199String path = DTDBuilder.mapping.baseStr + ent.getString();200return new InputStreamReader(new FileInputStream(path));201}202return new CharArrayReader(ent.data);203}204205}206207208