⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basestoreimpl.java

📁 xbrlapi的源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    /**     * @see org.xbrlapi.data.Store#getStub(String stubId)     */    public void removeStub(String stubId) throws XBRLException {        if (hasFragment(stubId)) removeFragment(stubId);    }                        /**     * @see org.xbrlapi.data.Store#getDocumentsToDiscover()     */    public List<URL> getDocumentsToDiscover() throws XBRLException {        FragmentList<Fragment> stubs = getStubs();        LinkedList<URL> list = new LinkedList<URL>();        for (Fragment stub: stubs) {            String url = stub.getMetaAttribute("url");            try {                list.add(new URL(url));            } catch (MalformedURLException e) {                throw new XBRLException("URL " + url + " is malformed", e);            }        }        return list;    }    	/**	 * Saves the individual documents in the data store 	 * into a directory structure that is placed into	 * the specified directory.  The directory structure that is 	 * created mirrors the structure of the URLs of the documents. 	 * Note that the URLs of the documents that are written out	 * will be reflected in the paths to those documents	 * using the same rules as those applied for document caching.	 * @param destination The folder in which the directory structure and	 * the documents in the data store are to be saved.  	 * @throws XBRLException If the root folder does not exist or 	 * is not a directory or if the documents in the store cannot 	 * be saved to the local file system.	 */	public void saveDocuments(File destination) throws XBRLException {		saveDocuments(destination, "");	}		/**	 * Serializes those documents in the data store with a URL that	 * begins with the specified URL prefix. They are saved to the local	 * file system in the same manner as is applied for the saveDocuments	 * method that operates on all documents in the data store.	 * @param destination The folder in which the directory structure and	 * the documents in the data store are to be saved.	 * @param urlPrefix All documents in the data store with a URL that begins 	 * with the string specified by urlPrefix will be saved to the local	 * file system.	 * @throws XBRLException If the root folder does not exist or 	 * is not a directory or if the documents in the store cannot 	 * be saved to the local file system.	 */	public void saveDocuments(File destination, String urlPrefix) throws XBRLException {				try {			if (! destination.exists()) throw new XBRLException("The specified directory does not exist.");						if (! destination.isDirectory()) throw new XBRLException("A directory rather than a file must be specified.");						List<String> urls = getStoredURLs();			Iterator<String> iterator = urls.iterator();			while (iterator.hasNext()) {							String url = iterator.next();				if (url.startsWith(urlPrefix)) {					CacheImpl cache = new CacheImpl(destination);					File file = cache.getCacheFile(new URL(url));					Element e = getDocumentAsDOM(url);					serialize(e,file);				}			}		} catch (MalformedURLException e) {			throw new XBRLException("Document could not be saved because the URL could not be formed.", e);		}			}		/**	 * Creates a single DOM structure from all documents in the 	 * data store and saves this single XML structure in the	 * specified file.	 * @param file The file to save the Store content to.	 * @throws XBRLException if the documents in the store cannot be	 * saved to the single file.	 */	public void saveStoreAsSingleDocument(File file) throws XBRLException {		serialize(getStoreAsDOM().getDocumentElement(),file);	}        /**     * Convert a DOM element (and its descendents) to a string.     * @param element The element to convert to a string.     * @return The string that is the serialised element.     * @throws XBRLException if an IO exception occurs.     */    protected String DOM2String(Element element) {        StringWriter sw = new StringWriter();        String out = null;    	try {			org.apache.xml.serialize.OutputFormat format = new org.apache.xml.serialize.OutputFormat("xml", "UTF-8", true);			org.apache.xml.serialize.XMLSerializer output = new org.apache.xml.serialize.XMLSerializer(sw, format);			output.setNamespaces(true);		    output.serialize(element);	        sw.flush();	        out = sw.toString();	        	        sw.close();    	} catch (IOException e) {            // StringWriter does not generate any IOExceptions; it can create only OutOfMemoryError    	}        return out;    }     /**     * @see org.xbrlapi.data.Store#getFragments(String)     */    public <F extends Fragment> FragmentList<F> getFragments(String interfaceName) throws XBRLException {    	return this.<F>query("/"+ Constants.XBRLAPIPrefix+ ":" + "fragment[@type='org.xbrlapi.impl." + interfaceName + "Impl']");    }        /**     * @see org.xbrlapi.data.Store#getChildFragments(String, String)     */    public <F extends Fragment> FragmentList<F> getChildFragments(String interfaceName, String parentIndex) throws XBRLException {    	return this.<F>query("/"+ Constants.XBRLAPIPrefix+ ":" + "fragment[@type='org.xbrlapi.impl." + interfaceName + "Impl' and @parentIndex='" + parentIndex + "']");    }            /**     * @see org.xbrlapi.data.Store#getNetworks(String)     */    public Networks getNetworks(String arcRole) throws XBRLException {    	Networks networks = new NetworksImpl();    	    	// First get the set of arcs using the arc role		FragmentList<Arc> arcs = getArcs(arcRole);		logger.info(arcs.getLength());    	for (Arc arc: arcs) {    		FragmentList<ArcEnd> sources = arc.getSourceFragments();    		FragmentList<ArcEnd> targets = arc.getTargetFragments();    		for (ArcEnd source: sources) {    			for (ArcEnd target: targets) {    				Fragment s = null;    				Fragment t = null;            		if (source.getType().equals("org.xbrlapi.impl.LocatorImpl"))             			s = ((Locator) source).getTargetFragment();            		else s = source;            		if (target.getType().equals("org.xbrlapi.impl.LocatorImpl"))             			t = ((Locator) target).getTargetFragment();            		else t = target;        			Relationship relationship = new RelationshipImpl(arc,s,t);        			networks.addRelationship(relationship);    			}    		}    	}    	return networks;    	    }        /**     * TODO Index xlink:role and xlink:arcrole attributes.     * @param arcrole The arcrole to use to identify the arcs to retrieve.     * @return the list of arc fragments with a given arc role value.     * @throws XBRLException     */    private FragmentList<Arc> getArcs(String arcrole) throws XBRLException {    	String query = "/"+ Constants.XBRLAPIPrefix+ ":" + "fragment["+ Constants.XBRLAPIPrefix+ ":" + "data/*[@xlink:arcrole='" + arcrole + "' and @xlink:type='arc']]";    	return this.<Arc>query(query);    }            /**     * Utility method to return a list of fragments in a data store     * that have a type corresponding to the specified fragment interface name and     * that are in the document with the specified URL.     * @param url The URL of the document to get the fragments from.     * @param interfaceName The name of the interface.  EG: If a list of     *  org.xbrlapi.impl.ReferenceArcImpl fragments is required then     *  this parameter would have a value of "ReferenceArc".     *  Note that this method does not yet recognise fragment subtypes so      *  a request for an Arc would not return all ReferenceArcs as well as other     *  types of arcs.     * @return a list of fragments with the given fragment type and in the given document.     * @throws XBRLException     */    public <F extends Fragment> FragmentList<F> getFragmentsFromDocument(URL url, String interfaceName) throws XBRLException {        URL matchURL = getMatcher().getMatch(url);        return this.<F>query("/"+ Constants.XBRLAPIPrefix+ ":" + "fragment[@url='"+ matchURL + "' and @type='org.xbrlapi.impl." + interfaceName + "Impl']");    }            /**     * @return a list of all of the root-level facts in the data store (those facts     * that are children of the root element of an XBRL instance).  Returns an empty list      * if no facts are found.     * @throws XBRLException     */    public FragmentList<Fact> getFacts() throws XBRLException {    	FragmentList<Instance> instances = this.<Instance>getFragments("Instance");    	return getFactsFromInstances(instances);    }        /**     * This method is provided as a helper method for the getFact methods.     * @param instances The list of instance fragments to extract facts from.     * @return The list of facts in the instances.     * @throws XBRLException     */    private FragmentList<Fact> getFactsFromInstances(FragmentList<Instance> instances) throws XBRLException {    	FragmentList<Fact> facts = new FragmentListImpl<Fact>();    	for (int i=0; i<instances.getLength(); i++) {    		Instance instance = instances.getFragment(i);    		facts.addAll(instance.getFacts());    	}    	return facts;    }        /**     * Helper method for common code in the getItem methods.     * @param instances The instances to retrieve items for.     * @return a list of root items in the instances.     * @throws XBRLException     */    private FragmentList<Item> getItemsFromInstances(FragmentList<Instance> instances) throws XBRLException {    	FragmentList<Fact> facts = getFactsFromInstances(instances);    	FragmentList<Item> items = new FragmentListImpl<Item>();    	for (Fact fact: facts) {    		if (! fact.getType().equals("org.xbrlapi.org.impl.TupleImpl"))    			items.addFragment((Item) fact);    	}    	return items;    }        /**     * Helper method for common code in the getTuple methods.     * @param instances The instances to retrieve tuples for.     * @return a list of root tuples in the instances.     * @throws XBRLException     */    private FragmentList<Tuple> getTuplesFromInstances(FragmentList<Instance> instances) throws XBRLException {    	FragmentList<Fact> facts = getFactsFromInstances(instances);    	FragmentList<Tuple> tuples = new FragmentListImpl<Tuple>();    	for (Fact fact: facts) {    		if (fact.getType().equals("org.xbrlapi.org.impl.TupleImpl"))    			tuples.addFragment((Tuple) fact);    	}    	return tuples;    }            /**     * @return a list of all of the root-level items in the data store(those items     * that are children of the root element of an XBRL instance).     * TODO eliminate the redundant retrieval of tuples from the getItems methods.     * @throws XBRLException     */    public FragmentList<Item> getItems() throws XBRLException {    	FragmentList<Instance> instances = this.getFragments("Instance");    	return getItemsFromInstances(instances);    }        /**     * @param url The URL of the document to get the facts from.     * @return a list of all of the root-level facts in the specified document.     * @throws XBRLException     */    public FragmentList<Fact> getFacts(URL url) throws XBRLException {    	FragmentList<Instance> instances = this.<Instance>getFragmentsFromDocument(url,"Instance");    	return this.getFactsFromInstances(instances);    }        /**     * @param url The URL of the document to get the items from.     * @return a list of all of the root-level items in the data store.     * @throws XBRLException     */    public FragmentList<Item> getItems(URL url) throws XBRLException {    	FragmentList<Instance> instances = this.<Instance>getFragmentsFromDocument(url,"Instance");    	return this.getItemsFromInstances(instances);    }        /**     * @see org.xbrlapi.data.Store#getRootFragmentForDocument(String)     */    public <F extends Fragment> F getRootFragmentForDocument(String url) throws XBRLException {    	FragmentList<F> fragments = this.<F>query("/"+ Constants.XBRLAPIPrefix+ ":" + "fragment[@url='" + url + "' and @parentIndex='none']");    	if (fragments.getLength() == 0) return null;    	if (fragments.getLength() > 1) throw new XBRLException("Two fragments identify themselves as roots of the one document.");    	return fragments.getFragment(0);    }        /**     * @see org.xbrlapi.data.Store#getRootFragments()     */    public <F extends Fragment> FragmentList<F> getRootFragments() throws XBRLException {    	return this.<F>query("/"+ Constants.XBRLAPIPrefix+ ":" + "fragment[@parentIndex='none']");    }    /**     * @see org.xbrlapi.data.Store#getLanguage(String, String)     */    public Language getLanguage(String encoding, String code) throws XBRLException {        if (encoding == null) return null;        if (code == null) return null;        String query = "/"+ Constants.XBRLAPIPrefix + ":" + "fragment[@type='org.xbrlapi.impl.LanguageImpl' and "+ Constants.XBRLAPIPrefix+ ":" + "data/lang:language/lang:encoding='" + encoding.toUpperCase() + "' and " + Constants.XBRLAPIPrefix + ":" + "data/lang:language/lang:code='" + code.toUpperCase() + "']";        FragmentList<Language> languages = this.<Language>query(query);        if (languages.getLength() == 0) return null;        return languages.get(0);    }            /**     * @see org.xbrlapi.data.Store#setNamespaceBinding(String,String)     */    public void setNamespaceBinding(String namespace, String prefix) throws XBRLException {        this.namespaceBindings.put(namespace,prefix);    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -