📄 xmlutil.java
字号:
/* XMLUtil.java NanoXML/Java * * $Revision: 1.5 $ * $Date: 2002/02/03 21:19:38 $ * $Name: RELEASE_2_2_1 $ * * This file is part of NanoXML 2 for Java. * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved. * * This software is provided 'as-is', without any express or implied warranty. * In no event will the authors be held liable for any damages arising from the * use of this software. * * Permission is granted to anyone to use this software for any purpose, * including commercial applications, and to alter it and redistribute it * freely, subject to the following restrictions: * * 1. The origin of this software must not be misrepresented; you must not * claim that you wrote the original software. If you use this software in * a product, an acknowledgment in the product documentation would be * appreciated but is not required. * * 2. Altered source versions must be plainly marked as such, and must not be * misrepresented as being the original software. * * 3. This notice may not be removed or altered from any source distribution. */package net.n3.nanoxml;import java.io.IOException;import java.io.Reader;import java.io.CharArrayReader;/** * Utility methods for NanoXML. * * @author Marc De Scheemaecker * @version $Name: RELEASE_2_2_1 $, $Revision: 1.5 $ */class XMLUtil{ /** * Skips the remainder of a comment. * It is assumed that <!- is already read. * * @param reader the reader * * @throws java.io.IOException * if an error occurred reading the data */ static void skipComment(IXMLReader reader) throws IOException, XMLParseException { if (reader.read() != '-') { XMLUtil.errorExpectedInput(reader.getSystemID(), reader.getLineNr(), "<!--"); } int dashesRead = 0; for (;;) { char ch = reader.read(); switch (ch) { case '-': dashesRead++; break; case '>': if (dashesRead == 2) { return; } default: dashesRead = 0; } } } /** * Skips the remainder of the current XML tag. * * @param reader the reader * * @throws java.io.IOException * if an error occurred reading the data */ static void skipTag(IXMLReader reader) throws IOException, XMLParseException { int level = 1; while (level > 0) { char ch = reader.read(); switch (ch) { case '<': ++level; break; case '>': --level; break; } } } /** * Scans a public ID. * * @param publicID will contain the public ID * @param reader the reader * * @return the system ID * * @throws java.io.IOException * if an error occurred reading the data */ static String scanPublicID(StringBuffer publicID, IXMLReader reader) throws IOException, XMLParseException { if (! XMLUtil.checkLiteral(reader, "UBLIC")) { return null; } XMLUtil.skipWhitespace(reader, null); publicID.append(XMLUtil.scanString(reader, '\0', null)); XMLUtil.skipWhitespace(reader, null); return XMLUtil.scanString(reader, '\0', null); } /** * Scans a system ID. * * @param reader the reader * * @return the system ID * * @throws java.io.IOException * if an error occurred reading the data */ static String scanSystemID(IXMLReader reader) throws IOException, XMLParseException { if (! XMLUtil.checkLiteral(reader, "YSTEM")) { return null; } XMLUtil.skipWhitespace(reader, null); return XMLUtil.scanString(reader, '\0', null); } /** * Retrieves an identifier from the data. * * @param reader the reader * * @throws java.io.IOException * if an error occurred reading the data */ static String scanIdentifier(IXMLReader reader) throws IOException, XMLParseException { StringBuffer result = new StringBuffer(); for (;;) { char ch = reader.read(); if ((ch == '_') || (ch == ':') || (ch == '-') || (ch == '.') || ((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) || (ch > '\u007E')) { result.append(ch); } else { reader.unread(ch); break; } } return result.toString(); } /** * Retrieves a delimited string from the data. * * @param reader the reader * @param entityChar the escape character (& or %) * @param entityResolver the entity resolver * * @throws java.io.IOException * if an error occurred reading the data */ static String scanString(IXMLReader reader, char entityChar, IXMLEntityResolver entityResolver) throws IOException, XMLParseException { StringBuffer result = new StringBuffer(); int startingLevel = reader.getStreamLevel(); char delim = reader.read(); if ((delim != '\'') && (delim != '"')) { XMLUtil.errorExpectedInput(reader.getSystemID(), reader.getLineNr(), "delimited string"); } for (;;) { String str = XMLUtil.read(reader, entityChar); char ch = str.charAt(0); if (ch == entityChar) { if (str.charAt(1) == '#') { result.append(XMLUtil.processCharLiteral(str)); } else { XMLUtil.processEntity(str, reader, entityResolver); } } else if (ch == '&') { reader.unread(ch); str = XMLUtil.read(reader, '&'); if (str.charAt(1) == '#') { result.append(XMLUtil.processCharLiteral(str)); } else { result.append(str); } } else if (reader.getStreamLevel() == startingLevel) { if (ch == delim) { break; } else if ((ch == 9) || (ch == 10) || (ch == 13)) { result.append(' '); } else { result.append(ch); } } else { result.append(ch); } } return result.toString(); } /** * Processes an entity. * * @param entity the entity * @param reader the reader * @param entityResolver the entity resolver * * @throws java.io.IOException * if an error occurred reading the data */ static void processEntity(String entity, IXMLReader reader, IXMLEntityResolver entityResolver) throws IOException, XMLParseException { entity = entity.substring(1, entity.length() - 1); Reader entityReader = entityResolver.getEntity(reader, entity); if (entityReader == null) { XMLUtil.errorInvalidEntity(reader.getSystemID(), reader.getLineNr(), entity); } boolean externalEntity = entityResolver.isExternalEntity(entity); reader.startNewStream(entityReader, !externalEntity); } /** * Processes a character literal. * * @param entity the entity * * @throws java.io.IOException * if an error occurred reading the data */ static char processCharLiteral(String entity) throws IOException, XMLParseException { if (entity.charAt(2) == 'x') { entity = entity.substring(3, entity.length() - 1); return (char) Integer.parseInt(entity, 16); } else { entity = entity.substring(2, entity.length() - 1); return (char) Integer.parseInt(entity, 10); } } /** * Skips whitespace from the reader. * * @param reader the reader * @param buffer where to put the whitespace; null if the * whitespace does not have to be stored. * * @throws java.io.IOException * if an error occurred reading the data */ static void skipWhitespace(IXMLReader reader, StringBuffer buffer) throws IOException { char ch; if (buffer == null) { do { ch = reader.read(); } while ((ch == ' ') || (ch == '\t') || (ch == '\n')); } else { for (;;) { ch = reader.read(); if ((ch != ' ') && (ch != '\t') && (ch != '\n')) { break; } if (ch == '\n') { buffer.append('\n'); } else { buffer.append(' '); } } } reader.unread(ch); } /** * Reads a character from the reader. * * @param reader the reader * @param entityChar the escape character (& or %) used to indicate * an entity * * @return the character, or an entity expression (like e.g. &lt;) * * @throws java.io.IOException * if an error occurred reading the data */ static String read(IXMLReader reader, char entityChar) throws IOException, XMLParseException { char ch = reader.read(); StringBuffer buf = new StringBuffer(); buf.append(ch); if (ch == entityChar) { while (ch != ';') { ch = reader.read(); buf.append(ch); } } return buf.toString(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -