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

📄 fragmentimpl.java

📁 xbrlapi的源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package org.xbrlapi.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import org.apache.log4j.Logger;
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.Fragment;
import org.xbrlapi.FragmentList;
import org.xbrlapi.LabelResource;
import org.xbrlapi.Locator;
import org.xbrlapi.Networks;
import org.xbrlapi.ReferenceResource;
import org.xbrlapi.Relationship;
import org.xbrlapi.SimpleLink;
import org.xbrlapi.builder.Builder;
import org.xbrlapi.builder.BuilderImpl;
import org.xbrlapi.data.Store;
import org.xbrlapi.utilities.Constants;
import org.xbrlapi.utilities.XBRLException;

/**
 * Implements the functionality that is common to all types of XBRL fragments.
 * @author Geoffrey Shuetrim (geoff@galexy.net)
 */

public class FragmentImpl implements Fragment {
	
	protected static Logger logger = Logger.getLogger(FragmentImpl.class);	
	
	/**
	 * The unique index value for this fragment, within the scope of the
	 * data store that this fragment is in.  This property is immutable 
	 * and is set during construction of the fragment.
	 */
	private String index;
		
	/**
	 * The Fragment builder - used when building fragments during DTS discovery.
	 */
	private Builder builder = null;

	/**
	 * The data store that manages this fragment.
	 */
	private Store store = null;
	
	/**
	 * The DOM instantiation of the 
	 * fragment's root element.
	 */
	private Element rootElement = null;

	/**
	 * No argument constructor
	 * @throws XBRLEception
	 */
	public FragmentImpl() {
	}
	
	/**
	 * @see org.xbrlapi.Fragment#setStore(Store)
	 */
	public void setStore(Store store) throws XBRLException {
		if (this.store != null) {
			throw new XBRLException("The data store has already been specified for this fragment.");
		}
		this.store = store;
	}

	/**
	 * @see org.xbrlapi.Fragment#setBuilder(Builder)
	 */
	public void setBuilder(Builder builder) {
		builder.setMetaAttribute("type",getType());
		this.builder = builder;
	}
	
	/**
	 * @see org.xbrlapi.Fragment#getStore()
	 */
	public Store getStore() {
		return store;
	}
	
	/**
	 * Update this fragment in the data store by storing it again.
	 * @throws XBRLException if this fragment cannot be updated in the data store.
	 */
	private void updateStore() throws XBRLException {
		store.storeFragment(this);
	}
	
    /**
     * @see org.xbrlapi.Fragment#getAncestorOrSelf(String)
     */
    public Fragment getAncestorOrSelf(String type) throws XBRLException {
    	if (getType().equals(type)) return this;
    	Fragment parent = this.getParent();
    	if (parent == null) throw new XBRLException("No ancestor (or self) fragments match the given type: " + type);
        return parent.getAncestorOrSelf(type);
    }
    
    /**
     * Gets the child fragments with the specified fragment type.
     * @param type The required fragment type of the child.
     * @return the fragment list of children fragments that match the specified fragment type
     * @throws XBRLException
     */
    protected <F extends Fragment> FragmentList<F> getChildren(String type) throws XBRLException {
    	String query = "/*[@parentIndex='" + getFragmentIndex() + "' and @type='" + type + "']";
    	FragmentList<F> fragments = getStore().<F>query(query);
    	return fragments;
    }
    
    /**
     * @see org.xbrlapi.Fragment#getSimpleLinks()
     */
    public FragmentList<SimpleLink> getSimpleLinks() throws XBRLException {
    	return this.getStore().<SimpleLink>getChildFragments("SimpleLink",this.getFragmentIndex());
    }
    
    /**
     * @see org.xbrlapi.Fragment#getAllChildren()
     */
    public FragmentList<Fragment> getAllChildren() throws XBRLException {
    	String xpath = "/*[@parentIndex='" + getFragmentIndex() + "']";
    	FragmentList<Fragment> fragments = getStore().query(xpath);
    	return fragments;
    }
    
    /**
     * Get a specific child fragment.
     * @param type The fragment type of the required child
     * @param index The index of the required child fragment (among other children of the same type).
     * @return the child fragment or null if there are no children fragments of the specified type.
     * @throws XBRLException if the index is out of bounds
     */
    protected Fragment getChild(String type, int index) throws XBRLException {
    	FragmentList<Fragment> children = getChildren(type);
    	if (children == null) return null;
    	if (index >= children.getLength()) throw new XBRLException("The index is too high.");
    	if (index < 0) throw new XBRLException("The index is too low.");
    	return children.getFragment(index);   
    }
	
	/**
	 * @see org.xbrlapi.Fragment#getBuilder()
	 */
	public Builder getBuilder() {
		return builder;
	}

	/**
     * @see org.xbrlapi.Fragment#getResource()
     */
    public Element getResource() throws XBRLException {
    	return rootElement;
    }
	
	/**
	 * @see org.xbrlapi.Fragment#setResource(Element)
	 */
	public void setResource(Element rootElement) throws XBRLException {
		builder = null;
		if (rootElement == null) throw new XBRLException("The XML resource is null.");
		this.rootElement = rootElement;
	}

    /**
     * @see org.xbrlapi.Fragment#getDataRootElement()
     */
    public Element getDataRootElement() throws XBRLException {
    	
    	if (builder != null) {
    		return builder.getData();
    	}

    	Element metadata = getMetadataRootElement();
    	Element dataContainer = (Element) metadata.getElementsByTagNameNS(Constants.XBRLAPINamespace,Constants.FragmentDataContainerElementName).item(0);
    	NodeList children = dataContainer.getChildNodes();
    	for (int i=0; i< children.getLength(); i++) 
    		if (children.item(i).getNodeType() == Node.ELEMENT_NODE) return (Element) children.item(i);
    	throw new XBRLException("The data element of the fragment could not be found.");
    }
    
    /**
     * @see org.xbrlapi.Fragment#getMetadataRootElement()
     */
    public Element getMetadataRootElement() throws XBRLException {
    	if (builder != null) return builder.getMetadata();
    	return getResource();
    }

    /**
     * @see org.xbrlapi.Fragment#getDocumentNode()
     */
    public Document getDocumentNode() throws XBRLException {
    	if (builder != null) return builder.getData().getOwnerDocument();
    	return getResource().getOwnerDocument();
    }
    
    /**
     * @see org.xbrlapi.Fragment#isNewFragment()
     */
    public boolean isNewFragment() throws XBRLException {
    	if (getBuilder() == null) return false;
    	return getBuilder().isNewFragment();
    }

    /**
     * @see org.xbrlapi.Fragment#getFragmentIndex()
     */
    public String getFragmentIndex() {
    	return this.index;
    }
    
    /**
     * @see org.xbrlapi.Fragment#setFragmentIndex(String)
     */
    public void setFragmentIndex(String index) throws XBRLException {
    	if (index == null) throw new XBRLException("The index must not be null.");
		if (index.equals("")) throw new XBRLException("A fragment index must not be an empty string.");
		this.index = index;
		if (this.getResource() == null) {
			setBuilder(new BuilderImpl());
		}
		if (builder != null) builder.setMetaAttribute("index",index);
		// TODO Handle updating of the resource index
    }
 
    /**
     * @see org.xbrlapi.Fragment#getType()
     */
    public String getType() {
    	return this.getClass().getName();
    }

    /**
     * @see org.xbrlapi.Fragment#setMetaAttribute(String, String)
     */
    public void setMetaAttribute(String name, String value) throws XBRLException {

    	if (getBuilder() != null) {
    		getBuilder().setMetaAttribute(name,value);
    		return;
    	}
    	
		Element element = this.getMetadataRootElement();
		element.setAttribute(name,value);
		updateStore();
    }
    
	/**
	 * @see org.xbrlapi.Fragment#removeMetaAttribute(String)
	 */
    public void removeMetaAttribute(String name) throws XBRLException {
    	if (getBuilder() != null) {
    		getBuilder().removeMetaAttribute(name);
    		return;
    	}
    	
		Element element = this.getMetadataRootElement();
		if (element == null) throw new XBRLException("The metadata does not contain a root element.");		
		element.removeAttribute(name);
		updateStore();
    }
    
    /**
     * @see org.xbrlapi.Fragment#getMetaAttribute(String)
     */
    public String getMetaAttribute(String name) throws XBRLException {
    	if (builder != null) {
    		String value = getBuilder().getMetaAttribute(name);
        	if (value == null) return null;
        	if (value.equals("")) return null;
        	return value;
    	}
    	
		String value = getMetadataRootElement().getAttribute(name);
    	if (value.equals("")) return null;
    	return value;
    }

    /**
     * @see org.xbrlapi.Fragment#appendMetadataElement(String, HashMap)
     */
    public void appendMetadataElement(String eName, HashMap<String,String> attributes) throws XBRLException {
    	
    	if (eName == null) throw new XBRLException("An element name must be specified.");
    	
    	if (getBuilder() != null) {
        	getBuilder().appendMetadataElement(eName, attributes);
    		return;
    	}

    	Element root = getMetadataRootElement();
    	Document document = root.getOwnerDocument();
		Element child = document.createElementNS(Constants.XBRLAPINamespace,Constants.XBRLAPIPrefix + ":" + eName);

    	for (String aName: attributes.keySet()) {
			String aValue = attributes.get(aName);
			if (aName != null) {
				if (aValue == null) throw new XBRLException("A metadata element is being added but attribute, " + aName + ", has a null value.");
				child.setAttribute(aName,aValue); 
			} else throw new XBRLException("A metadata element is being added with an attribute with a null name.");
    	}
    	root.appendChild(child);
		updateStore();
    }
    
    /**
     * @see org.xbrlapi.Fragment#removeMetadataElement(String, HashMap)
     */

⌨️ 快捷键说明

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