📄 xmlelement.java
字号:
offset = this.scanOneAttribute(input, offset, end, lineNr);
}
return offset;
}
/**
* Scans the content of the object.
*
* @param input Description of Parameter
* @param offset Description of Parameter
* @param end Description of Parameter
* @param contentOffset Description of Parameter
* @param contentSize Description of Parameter
* @param lineNr Description of Parameter
* @return the offset after the XML element;
* contentOffset points to the start of the content section; contentSize
* is the size of the content section
* @exception XMLParseException Description of Exception
*/
private int scanContent(char[] input,
int offset,
int end,
int[] contentOffset,
int[] contentSize,
int[] lineNr)
throws XMLParseException {
if (input[offset] == '/') {
contentSize[0] = 0;
if (input[offset + 1] != '>') {
throw this.expectedInput("'>'", lineNr[0]);
}
return offset + 2;
}
if (input[offset] != '>') {
throw this.expectedInput("'>'", lineNr[0]);
}
if (this.skipLeadingWhitespace) {
offset = this.skipWhitespace(input, offset + 1, end, lineNr);
}
else {
offset++;
}
// int begin = offset;
contentOffset[0] = offset;
int level = 0;
char[] tag = this.tagName.toCharArray();
end -= (tag.length + 2);
while ((offset < end) && (level >= 0)) {
if (input[offset] == '<') {
boolean ok = true;
if ((offset < (end - 1)) && (input[offset + 1] == '!')
&& (input[offset + 2] == '[')) {
offset++;
continue;
}
for (int i = 0; ok && (i < tag.length); i++) {
ok &= (input[offset + (i + 1)] == tag[i]);
}
ok &= !this.isIdentifierChar(input[offset + tag.length + 1]);
if (ok) {
while ((offset < end) && (input[offset] != '>')) {
offset++;
}
if (input[offset - 1] != '/') {
level++;
}
continue;
}
else if (input[offset + 1] == '/') {
ok = true;
for (int i = 0; ok && (i < tag.length); i++) {
ok &= (input[offset + (i + 2)] == tag[i]);
}
if (ok) {
contentSize[0] = offset - contentOffset[0];
offset += tag.length + 2;
try {
offset = this.skipWhitespace(input, offset,
end + tag.length
+ 2,
lineNr);
} catch (XMLParseException e) {
// ignore
}
if (input[offset] == '>') {
level--;
offset++;
}
continue;
}
}
}
if (input[offset] == '\r') {
lineNr[0]++;
if ((offset != end) && (input[offset + 1] == '\n')) {
offset++;
}
}
else if (input[offset] == '\n') {
lineNr[0]++;
}
offset++;
}
if (level >= 0) {
throw this.unexpectedEndOfData(lineNr[0]);
}
if (this.skipLeadingWhitespace) {
int i = contentOffset[0] + contentSize[0] - 1;
while ((contentSize[0] >= 0) && (input[i] <= ' ')) {
i--;
contentSize[0]--;
}
}
return offset;
}
/**
* Scans an identifier.
*
* @param input Description of Parameter
* @param offset Description of Parameter
* @param end Description of Parameter
* @return the identifier, or <CODE>null</CODE> if offset doesn't point
* to an identifier
*/
private String scanIdentifier(char[] input,
int offset,
int end) {
int begin = offset;
while ((offset < end) && (this.isIdentifierChar(input[offset]))) {
offset++;
}
if ((offset == end) || (offset == begin)) {
return null;
}
else {
return new String(input, begin, offset - begin);
}
}
/**
* Scans one attribute of an object.
*
* @param input Description of Parameter
* @param offset Description of Parameter
* @param end Description of Parameter
* @param lineNr Description of Parameter
* @return the offset after the attribute
* @exception XMLParseException Description of Exception
*/
private int scanOneAttribute(char[] input,
int offset,
int end,
int[] lineNr)
throws XMLParseException {
String key;
String value;
key = this.scanIdentifier(input, offset, end);
if (key == null) {
throw this.syntaxError("an attribute key", lineNr[0]);
}
offset = this.skipWhitespace(input, offset + key.length(), end, lineNr);
if (this.ignoreCase) {
key = key.toUpperCase();
}
if (input[offset] != '=') {
throw this.valueMissingForAttribute(key, lineNr[0]);
}
offset = this.skipWhitespace(input, offset + 1, end, lineNr);
value = this.scanString(input, offset, end, lineNr);
if (value == null) {
throw this.syntaxError("an attribute value", lineNr[0]);
}
if ((value.charAt(0) == '"') || (value.charAt(0) == '\'')) {
value = value.substring(1, (value.length() - 1));
offset += 2;
}
this.attributes.put(key, this.decodeString(value, lineNr[0]));
return offset + value.length();
}
/**
* Scans a string. Strings are either identifiers, or text delimited by double
* quotes.
*
* @param input Description of Parameter
* @param offset Description of Parameter
* @param end Description of Parameter
* @param lineNr Description of Parameter
* @return the string found, without delimiting
* double quotes; or null if offset didn't point to a valid string
* @exception XMLParseException Description of Exception
* @see nanoxml.XMLElement#scanIdentifier
*/
private String scanString(char[] input,
int offset,
int end,
int[] lineNr)
throws XMLParseException {
char delim = input[offset];
if ((delim == '"') || (delim == '\'')) {
int begin = offset;
offset++;
while ((offset < end) && (input[offset] != delim)) {
if (input[offset] == '\r') {
lineNr[0]++;
if ((offset != end) && (input[offset + 1] == '\n')) {
offset++;
}
}
else if (input[offset] == '\n') {
lineNr[0]++;
}
offset++;
}
if (offset == end) {
return null;
}
else {
return new String(input, begin, offset - begin + 1);
}
}
else {
return this.scanIdentifier(input, offset, end);
}
}
/**
* Scans the class (tag) name of the object.
*
* @param input Description of Parameter
* @param offset Description of Parameter
* @param end Description of Parameter
* @param lineNr Description of Parameter
* @return the position after the tag name
* @exception XMLParseException Description of Exception
*/
private int scanTagName(char[] input,
int offset,
int end,
int[] lineNr)
throws XMLParseException {
this.tagName = this.scanIdentifier(input, offset, end);
if (this.tagName == null) {
throw this.syntaxError("a tag name", lineNr[0]);
}
return offset + this.tagName.length();
}
/**
* Skips a tag that don't contain any useful data: <?...?>, <!...>
* and comments.
*
* @param input Description of Parameter
* @param offset Description of Parameter
* @param end Description of Parameter
* @param lineNr Description of Parameter
* @return the position after the tag
* @exception XMLParseException Description of Exception
*/
private int skipPreamble(char[] input,
int offset,
int end,
int[] lineNr)
throws XMLParseException {
char ch;
do {
offset = this.skipWhitespace(input, offset, end, lineNr);
if (input[offset] != '<') {
this.expectedInput("'<'", lineNr[0]);
}
offset++;
if (offset >= end) {
throw this.unexpectedEndOfData(lineNr[0]);
}
ch = input[offset];
if ((ch == '!') || (ch == '?')) {
offset = this.skipBogusTag(input, offset, end, lineNr);
}
} while (!isIdentifierChar(ch));
return offset;
}
/**
* Skips whitespace characters.
*
* @param input Description of Parameter
* @param offset Description of Parameter
* @param end Description of Parameter
* @param lineNr Description of Parameter
* @return the position after the whitespace
*/
private int skipWhitespace(char[] input,
int offset,
int end,
int[] lineNr) {
int startLine = lineNr[0];
while (offset < end) {
if (((offset + 6) < end) && (input[offset + 3] == '-')
&& (input[offset + 2] == '-') && (input[offset + 1] == '!')
&& (input[offset] == '<')) {
offset += 4;
while ((input[offset] != '-') || (input[offset + 1] != '-')
|| (input[offset + 2] != '>')) {
if ((offset + 2) >= end) {
throw this.unexpectedEndOfData(startLine);
}
offset++;
}
offset += 3;
}
else if (input[offset] == '\r') {
lineNr[0]++;
if ((offset != end) && (input[offset + 1] == '\n')) {
offset++;
}
}
else if (input[offset] == '\n') {
lineNr[0]++;
}
else if (input[offset] > ' ') {
break;
}
offset++;
}
if (offset == end) {
throw this.unexpectedEndOfData(startLine);
}
return offset;
}
/**
* Creates a parse exception for when an invalid valueset is given to a
* method.
*
* @param key Description of Parameter
* @return Description of the Returned Value
*/
private XMLParseException invalidValueSet(String key) {
String msg = "Invalid value set (key = \"" + key + "\")";
return new XMLParseException(this.getTagName(), msg);
}
/**
* Creates a parse exception for when an invalid value is given to a method.
*
* @param key Description of Parameter
* @param value Description of Parameter
* @param lineNr Description of Parameter
* @return Description of the Returned Value
*/
private XMLParseException invalidValue(String key,
String value,
int lineNr) {
String msg = "Attribute \"" + key + "\" does not contain a valid "
+ "value (\"" + value + "\")";
return new XMLParseException(this.getTagName(), lineNr, msg);
}
/**
* The end of the data input has been reached.
*
* @param lineNr Description of Parameter
* @return Description of the Returned Value
*/
private XMLParseException unexpectedEndOfData(int lineNr) {
String msg = "Unexpected end of data reached";
return new XMLParseException(this.getTagName(), lineNr, msg);
}
/**
* A syntax error occured.
*
* @param context Description of Parameter
* @param lineNr Description of Parameter
* @return Description of the Returned Value
*/
private XMLParseException syntaxError(String context,
int lineNr) {
String msg = "Syntax error while parsing " + context;
return new XMLParseException(this.getTagName(), lineNr, msg);
}
/**
* A character has been expected.
*
* @param charSet Description of Parameter
* @param lineNr Description of Parameter
* @return Description of the Returned Value
*/
private XMLParseException expectedInput(String charSet,
int lineNr) {
String msg = "Expected: " + charSet;
return new XMLParseException(this.getTagName(), lineNr, msg);
}
/**
* A value is missing for an attribute.
*
* @param key Description of Parameter
* @param lineNr Description of Parameter
* @return Description of the Returned Value
*/
private XMLParseException valueMissingForAttribute(String key,
int lineNr) {
String msg = "Value missing for attribute with key \"" + key + "\"";
return new XMLParseException(this.getTagName(), lineNr, msg);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -