📄 unmarshallingcontext.java
字号:
* <code>null</code> or the empty string for the empty namespace)
* @param names alphabetical list of attribute names expected (duplicates
* names are ordered by namespace URI)
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public void checkAllowedAttributes(String[] nss, String[] names)
throws JiBXException {
if (m_reader.getEventType() == IXMLReader.START_TAG) {
int count = m_reader.getAttributeCount();
loop: for (int i = 0; i < count; i++) {
String name = m_reader.getAttributeName(i);
String ns = m_reader.getAttributeNamespace(i);
int base = 0;
int limit = names.length - 1;
while (base <= limit) {
int cur = (base + limit) >> 1;
int diff = name.compareTo(names[cur]);
if (diff == 0) {
String comp = nss[cur];
if (comp == null) {
diff = ns.compareTo("");
} else {
diff = ns.compareTo(comp);
}
if (diff == 0) {
continue loop;
}
}
if (diff < 0) {
limit = cur - 1;
} else if (diff > 0) {
base = cur + 1;
}
}
throwStartTagException("Illegal attribute " +
buildNameString(ns, name));
}
} else {
throw new JiBXException("Error parsing document " +
buildPositionString());
}
}
/**
* Internal parse to expected start tag. Ignores character data seen prior
* to a start tag, but throws exception if an end tag or the end of the
* document is seen before a start tag. Leaves the parser positioned at the
* start tag.
*
* @param ns namespace URI for expected element (may be <code>null</code>
* or the empty string for the empty namespace)
* @param name element name expected
* @throws JiBXException on any error (possibly wrapping other exception)
*/
private void matchStart(String ns, String name) throws JiBXException {
if (toTag() == IXMLReader.START_TAG) {
if (!m_reader.getName().equals(name) || !verifyNamespace(ns)) {
throwStartTagNameError(ns, name);
}
} else {
throw new JiBXException("Expected " + buildNameString(ns, name)
+ " start tag, found " + currentNameString() + " end tag "
+ buildPositionString());
}
}
/**
* Parse to start of element. Ignores character data to next start or end
* tag, but throws exception if an end tag is seen before a start tag, or if
* the start tag seen does not match the expected name. Leaves the parse
* positioned at the start tag.
*
* @param ns namespace URI for expected element (may be <code>null</code>
* or the empty string for the empty namespace)
* @param name element name expected
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public void parseToStartTag(String ns, String name) throws JiBXException {
matchStart(ns, name);
}
/**
* Parse past start of element. Ignores character data to next start or end
* tag, but throws exception if an end tag is seen before a start tag, or if
* the start tag seen does not match the expected name. Leaves the parse
* positioned following the start tag.
*
* @param ns namespace URI for expected element (may be <code>null</code>
* or the empty string for the empty namespace)
* @param name element name expected
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public void parsePastStartTag(String ns, String name) throws JiBXException {
matchStart(ns, name);
advance();
}
/**
* Parse past start of element. Ignores character data to next start or end
* tag, but throws exception if an end tag is seen before a start tag, or if
* the start tag seen does not match the expected name. Leaves the parse
* positioned following the start tag.
*
* @param ns namespace URI for expected element (may be <code>null</code>
* or the empty string for the empty namespace)
* @param name element name expected
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public boolean parseIfStartTag(String ns, String name)
throws JiBXException {
if (isAt(ns, name)) {
advance();
return true;
} else {
return false;
}
}
/**
* Parse past current end of element. Ignores character data to next start
* or end tag, but throws exception if a start tag is seen before a end tag,
* or if the end tag seen does not match the expected name. Leaves the parse
* positioned following the end tag.
*
* @param ns namespace URI for expected element (may be <code>null</code>
* or the empty string for the empty namespace)
* @param name element name expected
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public void parsePastCurrentEndTag(String ns, String name)
throws JiBXException {
// move parse to start or end tag
int event = toTag();
// check for match on expected end tag
if (event == IXMLReader.END_TAG) {
if (m_reader.getName().equals(name) && verifyNamespace(ns)) {
advance();
} else {
throwEndTagNameError(ns, name);
}
} else {
throw new JiBXException("Expected " + buildNameString(ns, name)
+ " end tag, found " + currentNameString() + " start tag "
+ buildPositionString());
}
}
/**
* Parse past end of element. If currently at a start tag parses past that
* start tag, then ignores character data to next start or end tag, and
* throws exception if a start tag is seen before a end tag, or if
* the end tag seen does not match the expected name. Leaves the parse
* positioned following the end tag.
*
* @param ns namespace URI for expected element (may be <code>null</code>
* or the empty string for the empty namespace)
* @param name element name expected
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public void parsePastEndTag(String ns, String name) throws JiBXException {
// most past current tag if start
int event = m_reader.getEventType();
if (event == IXMLReader.START_TAG) {
advance();
}
// handle as current tag
parsePastCurrentEndTag(ns, name);
}
/**
* Check if next tag is a start tag. If not currently positioned at a
* start or end tag this first advances the parse to the next start or
* end tag.
*
* @return <code>true</code> if at start of element, <code>false</code> if
* at end
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public boolean isStart() throws JiBXException {
int type = m_reader.getEventType();
while (type != IXMLReader.START_TAG &&
type != IXMLReader.END_TAG) {
type = m_reader.next();
}
return m_reader.getEventType() == IXMLReader.START_TAG;
}
/**
* Check if next tag is an end tag. If not currently positioned at a
* start or end tag this first advances the parse to the next start or
* end tag.
*
* @return <code>true</code> if at end of element, <code>false</code> if
* at start
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public boolean isEnd() throws JiBXException {
int type = m_reader.getEventType();
while (type != IXMLReader.START_TAG &&
type != IXMLReader.END_TAG) {
type = m_reader.next();
}
return m_reader.getEventType() == IXMLReader.END_TAG;
}
/**
* Accumulate text content. This skips past comments and processing
* instructions, and consolidates text and entities to a single string. Any
* unexpanded entity references found are treated as errors.
*
* @return consolidated text string (empty string if no text components)
* @exception JiBXException on error in unmarshalling
*/
public String accumulateText() throws JiBXException {
String text = null;
StringBuffer buff = null;
loop: while (true) {
switch (m_reader.getEventType()) {
case IXMLReader.ENTITY_REF:
if (m_reader.getText() == null) {
throw new JiBXException
("Unexpanded entity reference in text at " +
buildPositionString());
}
// fall through into text accumulation
case IXMLReader.CDSECT:
case IXMLReader.TEXT:
if (text == null) {
text = m_reader.getText();
} else {
if (buff == null) {
buff = new StringBuffer(text);
}
buff.append(m_reader.getText());
}
break;
case IXMLReader.END_TAG:
case IXMLReader.START_TAG:
case IXMLReader.END_DOCUMENT:
break loop;
default:
break;
}
m_reader.nextToken();
}
if (buff == null) {
return (text == null) ? "" : text;
} else {
return buff.toString();
}
}
/**
* Parse required text content. Assumes the parse is already positioned at
* the text content, so just returns the text.
*
* @return content text found
* @throws JiBXException on any error (possible wrapping other exception)
*/
public String parseContentText() throws JiBXException {
return accumulateText();
}
/**
* Parse past end of element, returning optional text content. Assumes
* you've already parsed past the start tag of the element, so it just looks
* for text content followed by the end tag, and returns with the parser
* positioned after the end tag.
*
* @param ns namespace URI for expected element (may be <code>null</code>
* or the empty string for the empty namespace)
* @param tag element name expected
* @return content text from element
* @throws JiBXException on any error (possible wrapping other exception)
*/
public String parseContentText(String ns, String tag)
throws JiBXException {
String text = accumulateText();
switch (m_reader.getEventType()) {
case IXMLReader.END_TAG:
if (m_reader.getName().equals(tag) &&
verifyNamespace(ns)) {
m_reader.nextToken();
return text;
} else {
throwEndTagNameError(ns, tag);
}
case IXMLReader.START_TAG:
throw new JiBXException("Expected " +
buildNameString(ns, tag) + " end tag, " +
"found " + currentNameString() + " start tag " +
buildPositionString());
case IXMLReader.END_DOCUMENT:
throw new JiBXException("Expected " +
buildNameString(ns, tag) + " end tag, " +
"found end of document " + buildPositionString());
}
return null;
}
/**
* Parse past end of element, returning integer value of
* content. Assumes you've already parsed past the start tag of the
* element, so it just looks for text content followed by the end tag.
*
* @param ns namespace URI for expected element (may be <code>null</code>
* or the empty string for the empty namespace)
* @param tag element name expected
* @return converted value from element text
* @throws JiBXException on any error (possible wrapping other exception)
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -