📄 basestoreimpl.java
字号:
* @return a list of the URLs in the data store. * @throws XBRLException if the list cannot be constructed. */ public List<String> getStoredURLs() throws XBRLException { LinkedList<String> urls = new LinkedList<String>(); FragmentList<Fragment> rootFragments = this.query("/"+ Constants.XBRLAPIPrefix+ ":" + "fragment[@parentIndex='none']"); for (int i=0; i<rootFragments.getLength(); i++) { urls.add(rootFragments.getFragment(i).getURL()); } return urls; } /** * @see org.xbrlapi.data.Store#hasDocument(String) */ public boolean hasDocument(String url) throws XBRLException { try { URL matchURL = getMatcher().getMatch(new URL(url)); FragmentList<Fragment> rootFragments = this.<Fragment>query("/"+ Constants.XBRLAPIPrefix+ ":" + "fragment[@url='" + matchURL + "' and @parentIndex='none']"); return (rootFragments.getLength() > 0) ? true : false; } catch (MalformedURLException e) { throw new XBRLException("Malformed URL.",e); } } /** * Get a single document in the store as a DOM. Note that this will * not reflect the original document in some ways. Importantly, * entities will be resolved and document type declarations will be missing. * Document encodings may also differ. If the original document is required, * simply use the supplied URL to get a copy of the original document. * @param url The string representation of the URL of the * document to be retrieved. * @return a DOM Document containing the XML representation of the * file at the specified URL. Returns null if the store does not * contain a document with the given URL. * @throws XBRLException if the document cannot be constructed as a DOM. */ public Element getDocumentAsDOM(String url) throws XBRLException { return getSubtree(this.getRootFragmentForDocument(url)); } /** * Get a single document in the store as a DOM including annotations. * @param url The string representation of the URL of the * document to be retrieved. * @return an annotated DOM Document containing the XML representation of the * file at the specified URL. Returns null if the store does not * contain a document with the given URL. * @throws XBRLException if more or less than one document is found in the store matching * the supplied URL. */ private Element getAnnotatedDocumentAsDOM(String url) throws XBRLException { try { URL matchURL = getMatcher().getMatch(new URL(url)); FragmentList<Fragment> fragments = query("/"+ Constants.XBRLAPIPrefix+ ":" + "fragment[@url='" + matchURL + "' and @parentIndex='none']"); if (fragments.getLength() > 1) throw new XBRLException("More than one document was found in the data store."); if (fragments.getLength() == 0) throw new XBRLException("No documents were found in the data store."); Fragment fragment = fragments.getFragment(0); Element document = this.getAnnotatedSubtree(fragment); document.setAttributeNS(Constants.CompNamespace,Constants.CompPrefix + ":index",fragment.getFragmentIndex()); return document; } catch (MalformedURLException e) { throw new XBRLException("Malformed URL.",e); } } /** * Returns the root element of the subtree starting with the * fragment with the specified index. All subtrees for a given store * instance are produced from the one XML DOM and so can be appended * to eachother as required. * @param f The fragment at the root of the subtree. * @return The root element of the subtree headed by the fragment * with the specified index. * @throws XBRLException if the subtree cannot be constructed. */ public Element getSubtree(Fragment f) throws XBRLException { // Make sure that the DOM is initialised. if (storeDOM == null) { storeDOM = XMLDOMBuilder.newDocument(); } // Get the DOM representation of the fragment Element d = null; try { d = (Element) storeDOM.importNode(f.getDataRootElement(), true); } catch (Exception e) { f.getStore().serialize(f.getMetadataRootElement()); throw new XBRLException("The data could not be plugged into the DOM for fragment " + f.getFragmentIndex(),e); } // Get the child fragment IDs FragmentList<Fragment> fs = this.query("/"+ Constants.XBRLAPIPrefix + ":" + "fragment[@parentIndex='" + f.getFragmentIndex() + "']"); // With no children, just return the fragment if (fs.getLength() == 0) { return d; } // Sort the child fragments into insertion order Comparator<Fragment> comparator = new FragmentComparator(); TreeSet<Fragment> fragments = new TreeSet<Fragment>(comparator); for (int i=0; i<fs.getLength(); i++) { fragments.add(fs.getFragment(i)); } // Iterate child fragments in insertion order, inserting them Iterator<Fragment> iterator = fragments.iterator(); while (iterator.hasNext()) { //Get child fragment Fragment childFragment = iterator.next(); // Get XML DOM for child fragment using recursion Element child = getSubtree(childFragment); // Get parent element of child fragment in current fragment Element parentElement = childFragment.getParentElement(d); // Determine the number of preceding siblings int precedingSiblings = (new Integer(childFragment.getPrecedingSiblings())).intValue(); // Get the following sibling of this child fragment Element followingSibling = getFollowingSibling(parentElement, precedingSiblings); // Do the fragment insertion parentElement.insertBefore(child, followingSibling); } return d; } /** * Get the following sibling of this fragment's root in the parent fragment's data. * @param parentElement The parent element in the parent fragment's data. * @param precedingSiblings The number of sibling elements preceding the element of interest. * @return the following sibling of this fragment's root (or null if there is no preceding sibling). */ protected Element getFollowingSibling(Element parentElement, int precedingSiblings) { // Traverse the parent data DOM to find the relevant child node int siblingCount = 0; NodeList children = parentElement.getChildNodes(); for (int i=0; i<children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { siblingCount++; // We have found a sibling element if (siblingCount > precedingSiblings) return (Element) child; } } return null; } /** * Returns the root element of the annotated subtree starting with the * fragment with the specified index. All subtrees for a given store * instance are produced from the one XML DOM and so can be appended * to eachother as required. * @param f The fragment at the root of the subtree. * @return the root element of the subtree headed by the fragment * with the specified index. * @throws XBRLException if the subtree cannot be constructed. */ private Element getAnnotatedSubtree(Fragment f) throws XBRLException { //logger.debug((new Date()) + ":Getting fragment " + f.getFragmentIndex()); // Make sure that the DOM is initialised. if (storeDOM == null) { storeDOM = XMLDOMBuilder.newDocument(); } // Get the DOM representation of the fragment Element d = (Element) storeDOM.importNode(f.getDataRootElement(), true); // Get the child fragment IDs FragmentList<Fragment> fs = this.query("/"+ Constants.XBRLAPIPrefix+ ":" + "fragment[@parentIndex='" + f.getFragmentIndex() + "']"); // With no children, just return the fragment if (fs.getLength() == 0) { return d; } // Sort the child fragments into insertion order TreeSet<Fragment> fragments = new TreeSet<Fragment>(new FragmentComparator()); for (int i=0; i<fs.getLength(); i++) { fragments.add(fs.getFragment(i)); } // Iterate child fragments in insertion order, inserting them Iterator<Fragment> iterator = fragments.iterator(); while (iterator.hasNext()) { // Get child fragment Fragment childFragment = iterator.next(); // Get XML DOM for child fragment using recursion Element child = getAnnotatedSubtree(childFragment); child.setAttributeNS(Constants.CompNamespace,Constants.CompPrefix + ":index",childFragment.getFragmentIndex()); // Get parent element of child fragment in current fragment Element parentElement = childFragment.getParentElement(d); // Determine the number of preceding siblings int precedingSiblings = (new Integer(childFragment.getPrecedingSiblings())).intValue(); // Get the following sibling of this child fragment Element followingSibling = getFollowingSibling(parentElement, precedingSiblings); // Do the fragment insertion parentElement.insertBefore(child, followingSibling); } return d; } /** * Get all documents in the store as a single DOM. Note that this will * not reflect the original documents in some ways. Importantly, * entities will be resolved and document type declarations will be missing. * Document encodings may also differ. If the original documents are required, * simply use the URLs captured in the data store to get a copy of the original * document. @see org.xbrlapi.data.Store#getStoredURLs() for more details. * Get all data in the store as a single XML DOM object. * @return the XML DOM representation of the XBRL information in the * data store. * @throws XBRLException if the DOM cannot be constructed. */ public Document getStoreAsDOM() throws XBRLException { if (storeDOM == null) { storeDOM = XMLDOMBuilder.newDocument(); } Element root = storeDOM.createElementNS(Constants.XBRLAPINamespace,Constants.XBRLAPIPrefix + ":dts"); List<String> urls = getStoredURLs(); Iterator<String> iterator = urls.iterator(); while (iterator.hasNext()) { String url = iterator.next(); Element e = getDocumentAsDOM(url); root.appendChild(e); } storeDOM.appendChild(root); return storeDOM; } /** * Get all data in the store as a single XML DOM object including * the annotations used in the * <a href="http://www.sourceforge.net/xbrlcomposer/">XBRLComposer</a> project. * @return the composed data store as a DOM object. * @throws XBRLException if the composed data store cannot be constructed. */ public Document formCompositeDocument() throws XBRLException { if (storeDOM == null) { storeDOM = XMLDOMBuilder.newDocument(); } Element root = storeDOM.createElementNS(Constants.CompNamespace,Constants.CompPrefix + ":dts"); List<String> urls = getStoredURLs(); Iterator<String> iterator = urls.iterator(); while (iterator.hasNext()) { String url = iterator.next(); Element file = storeDOM.createElementNS(Constants.CompNamespace,Constants.CompPrefix + ":file"); file.setAttributeNS(Constants.CompNamespace,Constants.CompPrefix + ":uri", url); file.setAttributeNS(Constants.CompNamespace,Constants.CompPrefix + ":index",this.getNextFragmentId()); root.appendChild(file); file.appendChild(getAnnotatedDocumentAsDOM(url)); } root.setAttributeNS(Constants.CompNamespace,Constants.CompPrefix + ":index",this.getNextFragmentId()); storeDOM.appendChild(root); return storeDOM; } /** * @see org.xbrlapi.data.Store#getNextFragmentId() */ public String getNextFragmentId() throws XBRLException { return "1";/* Fragment summary = this.getFragment("summary"); String maxId = summary.getMetaAttribute("maximumFragmentId"); if (maxId == null) { return "1"; } else { int value = (new Integer(maxId)).intValue(); return (new Integer(value+1)).toString(); }*/ } /** * @see org.xbrlapi.data.Store#getStubs() */ public FragmentList<Fragment> getStubs() throws XBRLException { return this.<Fragment>query("/"+ Constants.XBRLAPIPrefix+ ":" + "fragment[@stub]"); } /** * @see org.xbrlapi.data.Store#getStub(String url) */ public Fragment getStub(String url) throws XBRLException { try { URL matchURL = getMatcher().getMatch(new URL(url)); FragmentList<Fragment> stubs = this.<Fragment>query("/"+ Constants.XBRLAPIPrefix + ":" + "fragment[@stub and @url='" + matchURL + "']"); if (stubs.getLength() == 0) return null; if (stubs.getLength() > 1) throw new XBRLException("There are " + stubs.getLength() + " stubs for " + url); return stubs.get(0); } catch (MalformedURLException e) { throw new XBRLException("Malformed URL.",e); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -