Gridarta Editor
FixedDtdInputStream.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.utils.xml;
21 
22 import java.io.ByteArrayInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.nio.charset.StandardCharsets;
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
28 
34 public class FixedDtdInputStream extends InputStream {
35 
39  @NotNull
40  private final InputStream inputStream;
41 
45  @NotNull
46  private final String rootElement;
47 
51  @NotNull
52  private final String systemId;
53 
57  private int state;
58 
63  @Nullable
64  private InputStream inject;
65 
73  public FixedDtdInputStream(@NotNull final InputStream inputStream, @NotNull final String rootElement, @NotNull final String systemId) {
74  this.inputStream = inputStream;
75  this.rootElement = rootElement;
76  this.systemId = systemId;
77  }
78 
79  @Override
80  public int read() throws IOException {
81  if (inject != null) {
82  final int ch = inject.read();
83  if (ch != -1) {
84  return ch;
85  }
86  inject = null;
87  }
88 
89  final int ch = inputStream.read();
90  if (ch == -1) {
91  return -1;
92  }
93 
94  switch (state) {
95  case 0:
96  if (ch == '<') {
97  state = 1;
98  }
99  break;
100 
101  case 1:
102  if (ch == '?') {
103  state = 2;
104  } else if (ch == '!') {
105  state = 4;
106  } else {
107  final String doctypeDeclaration = "DOCTYPE " + rootElement + " SYSTEM \"" + systemId + "\"><" + (char) ch;
108  inject = new ByteArrayInputStream(doctypeDeclaration.getBytes(StandardCharsets.US_ASCII));
109  state = 12;
110  return '!';
111  }
112  break;
113 
114  case 2:
115  if (ch == '?') {
116  state = 3;
117  }
118  break;
119 
120  case 3:
121  if (ch == '>') {
122  state = 0;
123  } else if (ch != '?') {
124  state = 2;
125  }
126  break;
127 
128  case 4:
129  if (ch == 'D') {
130  state = 5;
131  } else {
132  state = 11;
133  }
134  break;
135 
136  case 5:
137  if (ch == 'O') {
138  state = 6;
139  } else {
140  state = 11;
141  }
142  break;
143 
144  case 6:
145  if (ch == 'C') {
146  state = 7;
147  } else {
148  state = 11;
149  }
150  break;
151 
152  case 7:
153  if (ch == 'T') {
154  state = 8;
155  } else {
156  state = 11;
157  }
158  break;
159 
160  case 8:
161  if (ch == 'Y') {
162  state = 9;
163  } else {
164  state = 11;
165  }
166  break;
167 
168  case 9:
169  if (ch == 'P') {
170  state = 10;
171  } else {
172  state = 11;
173  }
174  break;
175 
176  case 10:
177  if (ch == 'E') {
178  state = 12;
179  } else {
180  state = 11;
181  }
182  break;
183 
184  case 11:
185  if (ch == '>') {
186  state = 0;
187  }
188  break;
189 
190  case 12:
191  break;
192  }
193 
194  return ch;
195  }
196 
197 }
net.sf.gridarta.utils.xml.FixedDtdInputStream
Injects a <!DOCTYPE...> declaration into an.
Definition: FixedDtdInputStream.java:34
net.sf.gridarta.utils.xml.FixedDtdInputStream.systemId
final String systemId
The system ID for the DOCTYPE declaration.
Definition: FixedDtdInputStream.java:52
net.sf.gridarta.utils.xml.FixedDtdInputStream.inject
InputStream inject
The InputStream that injects the <!DOCTYPE...> declaration.
Definition: FixedDtdInputStream.java:64
net.sf.gridarta.utils.xml.FixedDtdInputStream.read
int read()
Definition: FixedDtdInputStream.java:80
net.sf.gridarta.utils.xml.FixedDtdInputStream.FixedDtdInputStream
FixedDtdInputStream(@NotNull final InputStream inputStream, @NotNull final String rootElement, @NotNull final String systemId)
Creates a new instance.
Definition: FixedDtdInputStream.java:73
net.sf.gridarta.utils.xml.FixedDtdInputStream.inputStream
final InputStream inputStream
The InputStream that is modified.
Definition: FixedDtdInputStream.java:40
net.sf.gridarta.utils.xml.FixedDtdInputStream.state
int state
The current state.
Definition: FixedDtdInputStream.java:57
net.sf.gridarta.utils.xml.FixedDtdInputStream.rootElement
final String rootElement
The tag name of the root element in the DOCTYPE declaration.
Definition: FixedDtdInputStream.java:46