📄 stdxmlreader.java
字号:
String encoding = this.getEncoding(charsRead.toString()); if (encoding == null) { return new InputStreamReader(pbstream, "UTF-8"); } charsRead.setLength(0); try { return new InputStreamReader(pbstream, encoding); } catch (UnsupportedEncodingException e) { return new InputStreamReader(pbstream, "UTF-8"); } default: charsRead.append((char) b); return new InputStreamReader(pbstream, "UTF-8"); } } /** * Initializes the XML reader. * * @param stream the input for the XML data. * * @throws java.io.IOException * if an I/O error occurred */ public StdXMLReader(InputStream stream) throws IOException { PushbackInputStream pbstream = new PushbackInputStream(stream); StringBuffer charsRead = new StringBuffer(); Reader reader = this.stream2reader(stream, charsRead); this.currentReader = new StackedReader(); this.readers = new Stack(); this.currentReader.lineReader = new LineNumberReader(reader); this.currentReader.pbReader = new PushbackReader(this.currentReader.lineReader, 2); this.currentReader.publicId = ""; try { this.currentReader.systemId = new URL("file:."); } catch (MalformedURLException e) { // never happens } this.startNewStream(new StringReader(charsRead.toString())); } /** * Reads a character. * * @return the character * * @throws java.io.IOException * if no character could be read */ public char read() throws IOException { int ch = this.currentReader.pbReader.read(); while (ch < 0) { if (this.readers.empty()) { throw new IOException("Unexpected EOF"); } this.currentReader.pbReader.close(); this.currentReader = (StackedReader) this.readers.pop(); ch = this.currentReader.pbReader.read(); } return (char) ch; } /** * Returns true if the current stream has no more characters left to be * read. * * @throws java.io.IOException * if an I/O error occurred */ public boolean atEOFOfCurrentStream() throws IOException { int ch = this.currentReader.pbReader.read(); if (ch < 0) { return true; } else { this.currentReader.pbReader.unread(ch); return false; } } /** * Returns true if there are no more characters left to be read. * * @throws java.io.IOException * if an I/O error occurred */ public boolean atEOF() throws IOException { int ch = this.currentReader.pbReader.read(); while (ch < 0) { if (this.readers.empty()) { return true; } this.currentReader.pbReader.close(); this.currentReader = (StackedReader) this.readers.pop(); ch = this.currentReader.pbReader.read(); } this.currentReader.pbReader.unread(ch); return false; } /** * Pushes the last character read back to the stream. * * @param ch the character to push back. * * @throws java.io.IOException * if an I/O error occurred */ public void unread(char ch) throws IOException { this.currentReader.pbReader.unread(ch); } /** * Opens a stream from a public and system ID. * * @param publicID the public ID, which may be null * @param systemID the system ID, which is never null * * @throws java.net.MalformedURLException * if the system ID does not contain a valid URL * @throws java.io.FileNotFoundException * if the system ID refers to a local file which does not exist * @throws java.io.IOException * if an error occurred opening the stream */ public Reader openStream(String publicID, String systemID) throws MalformedURLException, FileNotFoundException, IOException { URL url = new URL(this.currentReader.systemId, systemID); if (url.getRef() != null) { String ref = url.getRef(); if (url.getFile().length() > 0) { url = new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile()); url = new URL("jar:" + url + '!' + ref); } else { url = StdXMLReader.class.getResource(ref); } } this.currentReader.publicId = publicID; this.currentReader.systemId = url; StringBuffer charsRead = new StringBuffer(); Reader reader = this.stream2reader(url.openStream(), charsRead); if (charsRead.length() == 0) { return reader; } String charsReadStr = charsRead.toString(); PushbackReader pbreader = new PushbackReader(reader, charsReadStr.length()); for (int i = charsReadStr.length() - 1; i >= 0; i--) { pbreader.unread(charsReadStr.charAt(i)); } return pbreader; } /** * Starts a new stream from a Java reader. The new stream is used * temporary to read data from. If that stream is exhausted, control * returns to the parent stream. * * @param reader the non-null reader to read the new data from */ public void startNewStream(Reader reader) { this.startNewStream(reader, false); } /** * Starts a new stream from a Java reader. The new stream is used * temporary to read data from. If that stream is exhausted, control * returns to the parent stream. * * @param reader the non-null reader to read the new data from * @param isInternalEntity true if the reader is produced by resolving * an internal entity */ public void startNewStream(Reader reader, boolean isInternalEntity) { StackedReader oldReader = this.currentReader; this.readers.push(this.currentReader); this.currentReader = new StackedReader(); if (isInternalEntity) { this.currentReader.lineReader = null; this.currentReader.pbReader = new PushbackReader(reader, 2); } else { this.currentReader.lineReader = new LineNumberReader(reader); this.currentReader.pbReader = new PushbackReader(this.currentReader.lineReader, 2); } this.currentReader.systemId = oldReader.systemId; this.currentReader.publicId = oldReader.publicId; } /** * Returns the current "level" of the stream on the stack of streams. */ public int getStreamLevel() { return this.readers.size(); } /** * Returns the line number of the data in the current stream. */ public int getLineNr() { if (this.currentReader.lineReader == null) { StackedReader sr = (StackedReader) this.readers.peek(); if (sr.lineReader == null) { return 0; } else { return sr.lineReader.getLineNumber() + 1; } } return this.currentReader.lineReader.getLineNumber() + 1; } /** * Sets the system ID of the current stream. * * @param systemID the system ID * * @throws java.net.MalformedURLException * if the system ID does not contain a valid URL */ public void setSystemID(String systemID) throws MalformedURLException { this.currentReader.systemId = new URL(this.currentReader.systemId, systemID); } /** * Sets the public ID of the current stream. * * @param publicID the public ID */ public void setPublicID(String publicID) { this.currentReader.publicId = publicID; } /** * Returns the current system ID. */ public String getSystemID() { return this.currentReader.systemId.toString(); } /** * Returns the current public ID. */ public String getPublicID() { return this.currentReader.publicId; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -