📄 contenthandlerimpl.java
字号:
// Handle language fragments
Fragment languageFragment = null;
try {
if (namespaceURI.equals(Constants.XBRLAPILanguagesNamespace)) {
if (lName.equals("language")) {
languageFragment = new LanguageImpl();
}
}
if (languageFragment != null) {
languageFragment.setFragmentIndex(getLoader().getNextFragmentId());
getLoader().addFragment(languageFragment,depth,getState());
}
} catch (XBRLException e) {
throw new SAXException("The languages fragment could not be processed.",e);
}
// Add a generic fragment for a document root element if we have not already done so
if (! getLoader().addedAFragment()) {
if (depth == 1) {
try {
Fragment root = new FragmentImpl();
root.setFragmentIndex(getLoader().getNextFragmentId());
getLoader().addFragment(root,1,state);
} catch (XBRLException e) {
throw new SAXException("The default root element fragment could not be created.",e);
}
}
}
// Extend the child count for an new element if we have not started a new fragment
try {
if (! getLoader().getFragment().isNewFragment()) {
getLoader().extendChildren();
}
} catch (XBRLException e) {
throw new SAXException("Could not handle children tracking at the fragment level.",e);
}
// Update the namespace data structure and insert namespace mappings into metadata.
HashMap<String,String> inheritedMap = nsStack.peek();
HashMap<String,String> myMap = (HashMap<String,String>) inheritedMap.clone();
for (int i = 0; i < attrs.getLength(); i++) {
if ((attrs.getQName(i).equals("xmlns") || attrs.getQName(i).startsWith("xmlns:"))) {
myMap.put(attrs.getValue(i),attrs.getQName(i));
}
}
nsStack.push(myMap);
try {
if (getLoader().getFragment().isNewFragment()) {
Fragment f = getLoader().getFragment();
for (String key: inheritedMap.keySet()) {
f.setMetaAttribute(inheritedMap.get(key),key);
}
}
} catch (XBRLException e) {
throw new SAXException("The loader is not building a fragment so something is badly amiss.",e);
}
// Insert the element into the fragment being built
try {
getLoader().getFragment().getBuilder().appendElement(namespaceURI, lName, qName, attrs);
} catch (XBRLException e) {
throw new SAXException("The element could not be appended to the fragment.",e);
}
}
/**
* The end of an element triggers processing of an extended link
* if we have reached the end of an extended link.
* Otherwise, we step up to the parent element in the composite document
* unless the element that is ending did not ever become the current element
* in the composite document.
*/
public void endElement(
String namespaceURI,
String sName,
String qName) throws SAXException {
// Handle the ending of an element in the fragment builder
try {
getLoader().getFragment().getBuilder().endElement(namespaceURI, sName, qName);
} catch (XBRLException e) {
throw new SAXException("The XBRLAPI fragment endElement failed.",e);
}
// Handle the ending of an element in the XLink processor
try {
getLoader().getXlinkProcessor().endElement(namespaceURI, sName, qName);
} catch (XLinkException e) {
throw new SAXException("The XLink processor endElement failed.",e);
}
// Give the loader a chance to update its state
try {
getLoader().updateState(depth);
} catch (XBRLException e) {
throw new SAXException("The state of the loader could not be updated at the end of element " + namespaceURI + ":" + sName + "." + e.getMessage(),e);
}
// Identify if an XBRL instance has been finished.
if (namespaceURI.equals(Constants.XBRL21Namespace) && sName.equals("xbrl")) {
this.parsingAnXBRLInstance = false;
} else if (namespaceURI.equals(Constants.XBRL21LinkNamespace)) {
if (sName.equals("footnoteLink")) {
this.canBeATuple = true;
} else if (sName.equals("schemaRef")) {
this.canBeATuple = true;
} else if (sName.equals("linkbaseRef")) {
this.canBeATuple = true;
} else if (sName.equals("arcroleRef")) {
this.canBeATuple = true;
} else if (sName.equals("roleRef")) {
this.canBeATuple = true;
} else if (sName.equals("reference")) {
this.parsingAReferenceResource = false;
}
} else if (namespaceURI.equals(Constants.XBRL21Namespace)) {
if (sName.equals("context")) {
this.canBeATuple = true;
} else if (sName.equals("true")) {
this.canBeATuple = false;
}
}
// We have finished with an element so move one step up the document tree, reducing the depth of the current element by 1
depth--;
// Update the information about the state of the current element
state = state.getParent();
// Revert to Namespace declarations of the parent element.
nsStack.pop();
}
/**
* Copy characters (trimming white space as required) to the DTSImpl.
*/
public void characters(char buf[], int offset, int len)
throws SAXException
{
try {
getLoader().getXlinkProcessor().titleCharacters(buf, offset, len);
} catch (XLinkException e) {
throw new SAXException("The XLink processor startElement failed.",e);
}
try {
String s = new String(buf, offset, len);
getLoader().getFragment().getBuilder().appendText(s);
} catch (XBRLException e) {
throw new SAXException("The characters could not be appended to the fragment." + inputErrorInformation());
}
}
/**
* Ignore ignorable whitespace
*/
public void ignorableWhitespace(char buf[], int offset, int len)
throws SAXException {
try {
String s = new String(buf, offset, len);
if (!s.trim().equals(""))
getLoader().getFragment().getBuilder().appendText(s);
} catch (XBRLException e) {
throw new SAXException("Failed to handle ignorable white space." + inputErrorInformation());
}
}
/**
* Copy across processing instructions to the DTSImpl
*/
public void processingInstruction(String target, String data)
throws SAXException
{
try {
if (getLoader().getFragment() != null)
getLoader().getFragment().getBuilder().appendProcessingInstruction(target,data);
// Figure out how to capture processing instructions that occur before the document root element.
} catch (XBRLException e) {
e.printStackTrace();
}
}
// TODO Tighten up the XML Schema validation error handling.
public void error(SAXParseException exception) throws SAXException {
System.out.println(exception);
}
public void fatalError(SAXParseException exception) throws SAXException {
System.out.println(exception);
}
public void warning(SAXParseException exception) throws SAXException {
System.out.println(exception);
}
private String publicId() {
return locator.getPublicId();
}
private String systemId() {
return locator.getSystemId();
}
private int lineNumber() {
return locator.getLineNumber();
}
private int columnNumber() {
return locator.getColumnNumber();
}
private String inputErrorInformation() {
StringBuffer s = new StringBuffer(" The problem occurred in ");
if (!(systemId() == null))
s.append(systemId() + ". ");
else
s.append("a document without a URL. All DTS documents must have a URL but one being parsed into the DTS does not. ");
s.append("The problem seems to be on line" + lineNumber() + " at column " + columnNumber() + ".");
return s.toString();
}
/**
* get the XML Schema grammar model for a particular
* XML Schema.
* Modified on 13 February, 2007 by Howard Ungar to use the static grammar pool
* provided by the GrammarCache implementation.
* TODO Determine why a static grammar pool is needed to use included anonymous schemas.
* @throws XBRLException
*/
private XSModel getXSModel() throws XBRLException {
try {
XMLGrammarPreparser preparser = new XMLGrammarPreparser();
preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
//preparser.setProperty("http://apache.org/xml/properties/internal/grammar-pool", GrammarCacheImpl.getGrammarPool());
preparser.setGrammarPool(GrammarCacheImpl.getGrammarPool());
preparser.setFeature("http://xml.org/sax/features/namespaces", true);
preparser.setFeature("http://xml.org/sax/features/validation", true);
preparser.setFeature("http://apache.org/xml/features/validation/schema", true);
preparser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
XMLEntityResolver entityResolver = (XMLEntityResolver) this.getLoader().getEntityResolver();
preparser.setEntityResolver(entityResolver);
// TODO make sure that this XML Resource Identifier is being initialised correctly.
XMLResourceIdentifier xri = new XMLResourceIdentifierImpl("", url.toString(), url.toString(), url.toString());
XMLInputSource xmlInputSource = entityResolver.resolveEntity(xri);
/* if (xml == null) {
xmlInputSource = new XMLInputSource(null, url.toString(), null);
} else {
xmlInputSource = new XMLInputSource(null, url.toString(), null, new StringReader(xml), null);
}
*/
XSGrammar grammar = (XSGrammar) preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, xmlInputSource);
return grammar.toXSModel();
} catch (IOException e) {
throw new XBRLException("Grammar model construction for schema at URL: " + url + " failed.",e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -