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

📄 basestoreimpl.java

📁 xbrlapi的源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package org.xbrlapi.data;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.io.Serializable;import java.io.StringWriter;import java.net.MalformedURLException;import java.net.URL;import java.util.Comparator;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.TreeSet;import org.apache.log4j.Logger;import org.apache.xml.serialize.OutputFormat;import org.apache.xml.serialize.XMLSerializer;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xbrlapi.Arc;import org.xbrlapi.ArcEnd;import org.xbrlapi.Fact;import org.xbrlapi.Fragment;import org.xbrlapi.FragmentList;import org.xbrlapi.Instance;import org.xbrlapi.Item;import org.xbrlapi.Language;import org.xbrlapi.Locator;import org.xbrlapi.Networks;import org.xbrlapi.Relationship;import org.xbrlapi.Tuple;import org.xbrlapi.cache.CacheImpl;import org.xbrlapi.data.resource.DefaultMatcherImpl;import org.xbrlapi.data.resource.Matcher;import org.xbrlapi.impl.FragmentComparator;import org.xbrlapi.impl.FragmentListImpl;import org.xbrlapi.impl.MockFragmentImpl;import org.xbrlapi.impl.NetworksImpl;import org.xbrlapi.impl.RelationshipImpl;import org.xbrlapi.utilities.Constants;import org.xbrlapi.utilities.XBRLException;import org.xbrlapi.utilities.XMLDOMBuilder;/** * Abstract base implementation of the data store * providing all methods of the store interface that * do not depend on the nature of the underlying data store * implementation. *  * @author Geoffrey Shuetrim (geoff@galexy.net) */public abstract class BaseStoreImpl implements Store, Serializable {	protected static Logger logger = Logger.getLogger(BaseStoreImpl.class);	    /**     * The DOM document used to construct DOM representations     * of subtrees of documents in the store.     */        protected Document storeDOM = null;    /**     * Resource matcher     */    protected Matcher matcher = new DefaultMatcherImpl();        /**     * @see org.xbrlapi.data.Store#setMatcher(Matcher)     */    public void setMatcher(Matcher matcher) throws XBRLException {        if (matcher == null) throw new XBRLException("The matcher cannot be null");        this.matcher = matcher;    }    /**     * @see org.xbrlapi.data.Store#getMatcher()     */    public Matcher getMatcher() {        return this.matcher;    }            /**     * Namespace bindings     */    protected HashMap<String,String> namespaceBindings = new HashMap<String,String>();        	public BaseStoreImpl() {		super();	}	/**	 * Close the data store.	 * Throws XBRLException if the data store cannot be closed. 	 */	public void close() throws XBRLException {	}	    /**     * @see org.xbrlapi.data.Store#storeLoaderState(List)     */    public void storeLoaderState(List<String> documents) throws XBRLException {        try {            for (String document: documents) {                storeStub(document);            }        } catch (XBRLException e) {            throw new XBRLException("The loader state could not be stored.",e);        }    }    /**     * @see org.xbrlapi.data.Store#storeStub(String)     */    public void storeStub(String document) throws XBRLException {        if (this.hasDocument(document)) return;        if (this.getStub(document) != null) return;                String documentId = getDocumentId(document);        Fragment stub = new MockFragmentImpl(documentId);        stub.setFragmentIndex(documentId);        stub.setURL(document);        stub.setMetaAttribute("stub","");        this.storeFragment(stub);    }    /**     * @see org.xbrlapi.data.Store#getDocumentId(String)     */        public String getDocumentId(String document) throws XBRLException {        Fragment stub = getStub(document);        if (stub != null) {            return stub.getFragmentIndex();        }        int i = 1;        String iString = (new Integer(i)).toString();        String randomString = random();        while (this.hasFragment(randomString + "_" + iString + "_1")) {            i++;            iString = (new Integer(i)).toString();        }        return (randomString + "_" + iString);    }        /**     * Generate a random string.     * @return a randomly generated string consisting of digits and     * a-z or A-Z only.     */    private String random() {        String random = "";        for (int i=0; i<6; i++) {            int code = (new Long(Math.round(Math.random()*61))).intValue();            code = code + 48;            if (code < 58) {                random = random + new Character((char)code).toString();            } else {                code = code + 7;                if (code < 91) {                    random = random + new Character((char)code).toString();                } else {                    code = code + 6;                    random = random + new Character((char)code).toString();                }            }        }        return random;    }        /**     * @param bs The given byte array.     * @return a hex string representation of the given byte array.     */    private String bytesToHex(byte[] bs) {        StringBuffer ret = new StringBuffer(bs.length);        for (int i = 0; i < bs.length; i++) {            String hex = Integer.toHexString(0x0100 + (bs[i] & 0x00FF)).substring(1);            ret.append((hex.length() < 2 ? "0" : "") + hex);        }        return ret.toString();    }        /**     * Serialize the specified XML DOM to the specified destination.     * @param what the root element of the DOM to be serialised.     * @param destination The destination output stream to be serialised to.     * @throws XBRLException if the DOM cannot be serialised     * because the destination cannot be written to or some other     * different problem occurs during serialisation.     */    public void serialize(Element what, OutputStream destination) throws XBRLException {		try {			OutputFormat format = new OutputFormat(what.getOwnerDocument(), "UTF-8", true);						// TODO Make sure that BaseStoreImpl serialization uses the latest Xalan code.			XMLSerializer output = new XMLSerializer(destination, format);			output.setNamespaces(true);			output.serialize(what);		} catch (IOException e) {			throw new XBRLException("The information could not be serialised.", e);		}    }        /**     * Serialize the specified XML DOM to System.out.     * @param what the root element of the DOM to be serialised.     * @throws XBRLException     */    public void serialize(Element what) throws XBRLException {		serialize(what,System.out);    }        /**     * Serialize the specified fragment.     * @param fragment The fragment to be serialised.     * @throws XBRLException     */    public void serialize(Fragment fragment) throws XBRLException {        serialize(fragment.getMetadataRootElement(),System.out);    }            /**     * Serialize the specified XML DOM node.     * @param what the root element of the DOM to be serialised.     * @return a string containing the serialized XML.     * @throws XBRLException     */    public String serializeToString(Element what) throws XBRLException {		ByteArrayOutputStream baos = new ByteArrayOutputStream();		serialize(what, baos);		return baos.toString();    }            /**	  * @see org.xbrlapi.data.Store#deleteDocument(String)     */    public void deleteDocument(String url) throws XBRLException {    	try {    	    URL matchURL = this.getMatcher().getMatch(new URL(url));    	    String query = "/*[@url='"+ matchURL + "']";            FragmentList<Fragment> fragments = this.<Fragment>query(query);                  for (Fragment fragment: fragments) {                this.removeFragment(fragment.getFragmentIndex());            }    	} catch (MalformedURLException e) {    	    throw new XBRLException("Malformed URL.",e);    	}    }        /**	  * @see org.xbrlapi.data.Store#deleteRelatedDocuments(String)	  */	private static HashMap<String,Boolean> documentsToDelete = new HashMap<String,Boolean>();     public void deleteRelatedDocuments(String url) throws XBRLException {    	deleteDocument(url);    	FragmentList<Fragment> fragments = this.<Fragment>query("/"+ Constants.XBRLAPIPrefix+ ":" + "fragment[@targetDocumentURL='"+ url + "']");    	for (Fragment fragment: fragments) {            if (! documentsToDelete.containsKey(fragment.getURL())) {                documentsToDelete.put(fragment.getURL(),new Boolean(true));            }    		Iterator<String> iterator = documentsToDelete.keySet().iterator();    		while (iterator.hasNext()) {    			String myURL = iterator.next();    			if (documentsToDelete.get(myURL)) {        			deleteRelatedDocuments(myURL);        			documentsToDelete.put(myURL,new Boolean(false));    			}    		}    	}    }        /**     * Serialize the specified XML DOM to the specified destination.     * create the necessary directory if it does not exist.  Use      * the file to create a file outputstream.     * @param what the root element of the DOM to be serialised.     * @param destination The destination file to be serialised to.     * @throws XBRLException if the DOM cannot be serialised     * because the destination cannot be written to or some other     * different problem occurs during serialisation.	 */	public void serialize(Element what, File destination) throws XBRLException {				File parentFile = destination.getParentFile();				if (parentFile != null) parentFile.mkdirs();		try {			FileOutputStream fileOutputStream = new FileOutputStream(destination.toString());			serialize(what, fileOutputStream);		} catch (FileNotFoundException e) {			throw new XBRLException("The file to be written to cannot be found.", e);		}			}        /**     * Serialize the specified XML DOM to the specified destination file.     * @param what the root element of the DOM to be serialised.     * @param destination The destination file to be serialised to.     * @throws XBRLException if the DOM cannot be serialised     * because the destination cannot be written to or some other     * different problem occurs during serialisation.	 */	public void serialize(Element what, String destination) throws XBRLException {		serialize(what, new File(destination));	}	    /**     * Get a list of the URLs that have been stored.

⌨️ 快捷键说明

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