📄 unmarshallingcontext.java
字号:
private void advance() throws JiBXException {
m_reader.nextToken();
}
/**
* Verify namespace. This is a simple utility method that allows multiple
* representations for the empty namespace as a convenience for generated
* code.
*
* @param ns namespace URI expected (may be <code>null</code>
* or the empty string for the empty namespace)
* @return <code>true</code> if the current namespace matches that
* expected, <code>false</code> if not
*/
private boolean verifyNamespace(String ns) {
if (ns == null || "".equals(ns)) {
return m_reader.getNamespace().length() == 0;
} else {
return ns.equals(m_reader.getNamespace());
}
}
/**
* Get attribute value from parser.
*
* @param ns namespace URI for expected attribute (may be <code>null</code>
* or the empty string for the empty namespace)
* @param name attribute name expected
* @return attribute value text, or <code>null</code> if missing
*/
private String getAttributeValue(String ns, String name) {
return m_reader.getAttributeValue(ns, name);
}
/**
* Set document to be parsed from stream. This call is not part of the
* interface definition, but is supplied to allow direct control of the
* namespace processing by the compiler. The option of disabling namespaces
* should be considered experimental and may not be supported in the future.
*
* @param ins stream supplying document data
* @param name document name (<code>null</code> if unknown)
* @param enc document input encoding, or <code>null</code> if to be
* determined by parser
* @param nsa enable namespace processing for parser flag
* @throws JiBXException if error creating parser
*/
public void setDocument(InputStream ins, String name, String enc,
boolean nsa) throws JiBXException {
if (m_reader == null) {
m_reader = s_readerFactory.createReader(ins, name, enc, nsa);
} else {
m_reader = s_readerFactory.recycleReader(m_reader, ins, name, enc);
}
reset();
}
/**
* Set document to be parsed from stream.
*
* @param ins stream supplying document data
* @param enc document input encoding, or <code>null</code> if to be
* determined by parser
* @throws JiBXException if error creating parser
*/
public void setDocument(InputStream ins, String enc) throws JiBXException {
setDocument(ins, null, enc, true);
}
/**
* Set document to be parsed from reader. This call is not part of the
* interface definition, but is supplied to allow direct control of the
* namespace processing by the compiler. The option of disabling namespaces
* should be considered experimental and may not be supported in the future.
*
* @param rdr reader supplying document data
* @param name document name (<code>null</code> if unknown)
* @param nsa enable namespace processing for parser flag
* @throws JiBXException if error creating parser
*/
public void setDocument(Reader rdr, String name, boolean nsa)
throws JiBXException {
if (m_reader == null) {
m_reader = s_readerFactory.createReader(rdr, name, nsa);
} else {
m_reader = s_readerFactory.recycleReader(m_reader, rdr, name);
}
reset();
}
/**
* Set document to be parsed from reader.
*
* @param rdr reader supplying document data
* @throws JiBXException if error creating parser
*/
public void setDocument(Reader rdr) throws JiBXException {
setDocument(rdr, null, true);
}
/**
* Set named document to be parsed from stream.
*
* @param ins stream supplying document data
* @param name document name
* @param enc document input encoding, or <code>null</code> if to be
* determined by parser
* @throws JiBXException if error creating parser
*/
public void setDocument(InputStream ins, String name, String enc)
throws JiBXException {
setDocument(ins, name, enc, true);
}
/**
* Set named document to be parsed from reader.
*
* @param rdr reader supplying document data
* @param name document name
* @throws JiBXException if error creating parser
*/
public void setDocument(Reader rdr, String name) throws JiBXException {
setDocument(rdr, name, true);
}
/**
* Set input document parse source directly.
*
* @param rdr document parse event reader
*/
public void setDocument(IXMLReader rdr) {
m_reader = rdr;
}
/**
* Initializes the context to use the same parser and document as another
* unmarshalling context. This method is designed for use when an initial
* context needs to create and invoke a secondary context in the course of
* an unmarshalling operation.
*
* @param parent context supplying parser and document to be unmarshalled
*/
public void setFromContext(UnmarshallingContext parent) {
m_factory = parent.m_factory;
m_reader = parent.m_reader;
}
/**
* Reset unmarshalling information. This releases all references to
* unmarshalled objects and prepares the context for potential reuse.
* It is automatically called when input is set.
*/
public void reset() {
for (int i = 0; i < m_idMaps.length; i++) {
m_idMaps[i] = null;
}
for (int i = m_globalCount; i < m_unmarshallers.length; i++) {
m_namespaces[i] = null;
m_names[i] = null;
m_unmarshallers[i] = null;
}
m_unmarshalMap = null;
m_idref = null;
for (int i = 0; i < m_objectStack.length; i++) {
m_objectStack[i] = null;
}
m_stackDepth = 0;
m_userContext = null;
}
/**
* Parse to 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.
*
* @return element name of start tag found
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public String toStart() throws JiBXException {
if (m_reader.getEventType() == IXMLReader.START_TAG) {
return m_reader.getName();
}
while (true) {
m_reader.next();
switch (m_reader.getEventType()) {
case IXMLReader.START_TAG:
return m_reader.getName();
case IXMLReader.END_TAG:
throw new JiBXException("Expected start tag, " +
"found end tag " + currentNameString() +
" " + buildPositionString());
case IXMLReader.END_DOCUMENT:
throw new JiBXException("Expected start tag, " +
"found end of document " + buildPositionString());
}
}
}
/**
* Parse to end tag. Ignores character data seen prior to an end tag, but
* throws exception if a start tag or the end of the document is seen before
* an end tag. Leaves the parser positioned at the end tag.
*
* @return element name of end tag found
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public String toEnd() throws JiBXException {
if (m_reader.getEventType() == IXMLReader.END_TAG) {
return m_reader.getName();
}
while (true) {
m_reader.next();
switch (m_reader.getEventType()) {
case IXMLReader.START_TAG:
throw new JiBXException("Expected end tag, " +
"found start tag " + currentNameString() +
" " + buildPositionString());
case IXMLReader.END_TAG:
return m_reader.getName();
case IXMLReader.END_DOCUMENT:
throw new JiBXException("Expected end tag, " +
"found end of document " + buildPositionString());
}
}
}
/**
* Parse to start or 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 parser event type for start tag or end tag
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public int toTag() throws JiBXException {
int type = m_reader.getEventType();
while (type != IXMLReader.START_TAG &&
type != IXMLReader.END_TAG) {
type = m_reader.next();
}
return m_reader.getEventType();
}
/**
* Check if next tag is start of element. If not currently positioned at a
* start or end tag this first advances the parse to the next start or 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
* @return <code>true</code> if at start of element with supplied name,
* <code>false</code> if not
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public boolean isAt(String ns, String name) 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 &&
m_reader.getName().equals(name) && verifyNamespace(ns);
}
/**
* Check if attribute is present on current start tag. Throws an exception
* if not currently positioned on a start tag.
*
* @param ns namespace URI for expected attribute (may be <code>null</code>
* or the empty string for the empty namespace)
* @param name attribute name expected
* @return <code>true</code> if named attribute is present,
* <code>false</code> if not
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public boolean hasAttribute(String ns, String name) throws JiBXException {
if (m_reader.getEventType() == IXMLReader.START_TAG) {
return getAttributeValue(ns, name) != null;
} else {
throw new JiBXException("Error parsing document " +
buildPositionString());
}
}
/**
* Check if any of several attributes is present on current start tag.
* Throws an exception if not currently positioned on a start tag.
*
* @param nss namespace URIs for expected attributes (each may be
* <code>null</code> or the empty string for the empty namespace)
* @param names attribute names expected
* @return <code>true</code> if at least one of the named attributes is
* present, <code>false</code> if not
* @throws JiBXException on any error (possibly wrapping other exception)
*/
public boolean hasAnyAttribute(String[] nss, String[] names)
throws JiBXException {
if (m_reader.getEventType() == IXMLReader.START_TAG) {
for (int i = 0; i < names.length; i++) {
if (getAttributeValue(nss[i], names[i]) != null) {
return true;
}
}
return false;
} else {
throw new JiBXException("Error parsing document " +
buildPositionString());
}
}
/**
* Check that only allowed attributes are present on current start tag.
* Throws an exception if not currently positioned on a start tag, or if
* an attribute is present which is not in the list.
*
* @param nss namespace URIs for allowed attributes (each may be
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -