📄 pointerresolverimpl.java
字号:
package org.xbrlapi.xpointer.resolver;import java.net.URL;import java.util.HashMap;import java.util.Vector;import org.apache.log4j.Logger;import org.xbrlapi.Fragment;import org.xbrlapi.FragmentList;import org.xbrlapi.data.Store;import org.xbrlapi.utilities.Constants;import org.xbrlapi.utilities.XBRLException;import org.xbrlapi.xpointer.ParseException;import org.xbrlapi.xpointer.PointerGrammar;import org.xbrlapi.xpointer.PointerPart;/** * Implementation of the XPointer resolver. * * TODO Eliminate PointerResolverImpl class. * Only the pointer parser is actually used and that * parser is only used to resolve xlink:href values. * * @author Geoffrey Shuetrim (geoff@galexy.net) */public class PointerResolverImpl implements PointerResolver { // Create the logger static Logger logger = Logger.getLogger(PointerResolver.class); private Store store; private URL url; /** * XPointer resolver constructor */ public PointerResolverImpl(Store store) { super(); this.store = store; } /** * Set the URL of the document within which the XPointer is being resolved. * @param url The URL of the document. */ public void setDocumentURL(URL url) { this.url = url; } /** * Get the URL of the document within which the XPointer is being resolved. * @return the url of the document within which the XPointer is being resolved. */ public URL getDocumentURL() { return url; } /** * Set the data store to be used by the loader * TODO Decide if the loader setStore method should be private or public */ private void setStore(Store store) { this.store = store; } /** * Get the data store used by a loader * TODO should the loader getStore method be public or private */ public Store getStore() { return store; } /** * Resolver the XPointer expression to a fragment in the data store. * @param pointer The XPointer expression * @return The fragment identified by the XPointer expression or null if none * is found in the data store. * @throws XBRLException */ @SuppressWarnings("unchecked") public Fragment resolveXPointer(String pointer) throws XBRLException { java.io.StringReader stringReader = new java.io.StringReader(pointer); java.io.Reader reader = new java.io.BufferedReader(stringReader); PointerGrammar grammar = new PointerGrammar(reader); try { Vector pointerParts = grammar.Pointer(); // TODO Iterate pointer parts until X pointer is resolved to a specific fragment. HashMap<String,String> namespaces = new HashMap<String,String>(); for (int i=0; i<pointerParts.size(); i++) { PointerPart part = (PointerPart) pointerParts.get(i); String data = part.getUnescapedSchemeData(); // Handle xmlns scheme pointer parts if (part.getSchemeNamespace().equals(PointerPart.DefaultPointerNamespace) && part.getSchemeLocalName().equals("xmlns")) { String[] declaration = data.split("\\s*=\\s*"); if (declaration.length != 2) { throw new XBRLException("The xmlns scheme: " + data + " failed to parse correctly."); } String prefix = declaration[0]; String namespace = declaration[1]; namespaces.put(prefix,namespace); // Handle element scheme pointer parts } else if (part.getSchemeNamespace().toString().equals(PointerPart.DefaultPointerNamespace) && part.getSchemeLocalName().equals("element")) { String xpath = "/" + Constants.XBRLAPIPrefix + ":" + "fragment[@url='" + getDocumentURL() + "' and " + Constants.XBRLAPIPrefix + ":" + "xptr/@value='" + data + "']"; FragmentList<Fragment> fl = getStore().query(xpath); if (fl.getLength() != 0) { if (fl.getLength() > 1) { throw new XBRLException("A locator locates more than one fragment in the DTS."); } return fl.getFragment(0); } } } } catch (ParseException e) { throw new XBRLException("The xpointer parser failed.", e); } return null; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -