📄 builderimpl.java
字号:
package org.xbrlapi.builder;import java.util.HashMap;import java.util.Iterator;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.loader.Loader;import org.xbrlapi.utilities.Constants;import org.xbrlapi.utilities.XBRLException;import org.xbrlapi.utilities.XMLDOMBuilder;import org.xml.sax.Attributes;/** * Class defining the SAX event handlers that * enable a fragment to be built up during * DTS discovery. * This should only be instantiated during * the creation of a fragment subclass. * The builder is responsible for construction of the XML * constituting the fragment itself and the XML that contains * the metadata about the fragment and its relationship to other * fragments. * @author Geoffrey Shuetrim (geoff@galexy.net) */public class BuilderImpl implements Builder { protected static Logger logger = Logger.getLogger(Loader.class); /** * The XML DOM used to build up fragments. */ private static Document dom = null; /** * The data root element. */ private Element data = null; /** * The metadata root element. */ private Element metadata = null; /** * The element to append new content during construction. */ private Element insertionPoint = null; /** * Flag to indicate that a fragment has yet to have any data inserted into it. */ private boolean isNewFragment = true; /** * Create the builder making sure that the static DOM * is instantiated and creating the metadata root element. * @param dom The DOM to use in the builder. * @throws XBRLException if the DOM is not the same as an existing * DOM being used by the builder. */ public BuilderImpl(Document dom) throws XBRLException { if (BuilderImpl.dom != null) { if (! BuilderImpl.dom.equals(dom)) throw new XBRLException("All builders used in a load must use the same DOM."); } BuilderImpl.dom = dom; setupBuilder(); } /** * Set up the data and metadata elements. * The element that will contain the data itself is a child of the metadata element. * The metadata element becomes the root of the XML fragment that is stored in the data store. */ private void setupBuilder() { metadata = dom.createElementNS(Constants.XBRLAPINamespace,Constants.XBRLAPIPrefix + ":" + Constants.FragmentRootElementName); Element container = dom.createElementNS(Constants.XBRLAPINamespace,Constants.XBRLAPIPrefix + ":" + Constants.FragmentDataContainerElementName); setInsertionPoint(container); metadata.appendChild(container); // Declare the xml namespace to keep Xindice happy. metadata.setAttribute("xmlns:" + Constants.XMLPrefix, Constants.XMLNamespace); } /** * Create the builder making sure that the static DOM * is instantiated and creating the metadata root element. */ public BuilderImpl() { if (dom == null) { dom = XMLDOMBuilder.newDocument(); } setupBuilder(); } /** * Restores the builder to its pre-use state. */ public void close() { BuilderImpl.dom = null; } /** * Get the XML DOM node. * @return the DOM node. */ private Document getDOM() { return dom; } /** * Get the root element of the data structure. * @return the data XML structure. */ public Element getData() { return data; } /** * Get the metadata DOM document. * @return the metadata XML structure. */ public Element getMetadata() { return metadata; } /** * Returns true iff the builder has not yet added an element to the fragment. * @return true iff the builder has not yet added an element to the fragment. */ public boolean isNewFragment() { return isNewFragment; } /** * Get the insertion point for new data content. * @return the insertion point for new data nodes. */ public Element getInsertionPoint() { return insertionPoint; } /** * Set the insertion point for new fragment material * @param e The element that is the current insertion point for data. */ private void setInsertionPoint(Element e) { insertionPoint = e; } /** * Append a node to the data. * @param child The node to be appended. * @throws XBRLException if the first node to be appended * is not an Element node. */ private void appendChild(Node child) throws XBRLException { if (isNewFragment()) { if (child.getNodeType() != Element.ELEMENT_NODE) throw new XBRLException("The first child to be inserted must be an element node"); getInsertionPoint().appendChild(child); data = (Element) child; isNewFragment = false; } else { getInsertionPoint().appendChild(child); } if (child.getNodeType() == Element.ELEMENT_NODE) setInsertionPoint((Element) child); } /** * Append a text node. * @param text The node to be appended. * @throws XBRLException if the node cannot be appended. */ public void appendText(String text) throws XBRLException { appendChild(getDOM().createTextNode(text)); } /** * Append a processing instruction node * @param target The processing target application identifier. * @param data The data defining what is to be done. * @throws XBRLException if the node cannot be appended. */ public void appendProcessingInstruction(String target, String data) throws XBRLException { appendChild(getDOM().createProcessingInstruction(target,data)); } /** * Append a comment node. * @param text The data constituting the content of the comment. * @throws XBRLException if the node cannot be appended. */ public void appendComment(String text) throws XBRLException { appendChild(getDOM().createComment(text)); } /** * Append an element node. * @param namespaceURI The namespace of the element found by the SAX parser. * @param lName The local name of the element found by the SAX parser. * @param qName The QName of the element found by the SAX parser. * @param attrs The set of attributes found by the SAX parser. * @throws XBRLException if the node cannot be appended. */ public void appendElement( String namespaceURI, String lName, String qName, Attributes attrs) throws XBRLException { Element newElement = createElement(namespaceURI, lName, qName, attrs); if (newElement == null) { throw new XBRLException("Could not create element: " + namespaceURI + " " + lName); } appendChild(newElement); } /** * Create an element node. * @param namespaceURI The namespace of the element found by the SAX parser. * @param lName The local name of the element found by the SAX parser. * @param qName The QName of the element found by the SAX parser. * @param attrs The set of attributes found by the SAX parser. */ private Element createElement( String namespaceURI, String lName, String qName, Attributes attrs) { // Keep track of namespaces used by element or its attributes HashMap<String,String> namespaces = new HashMap<String,String>(); namespaces.put(namespaceURI,""); Element newElement = getDOM().createElementNS(namespaceURI, qName); // Handle elements created with a null attrs array (not created from SAX parsing input) if (attrs == null) return newElement; // Insert all attributes with namespaces for (int i = 0; i < attrs.getLength(); i++) { if (attrs.getURI(i).equals(Constants.XMLNamespace)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -