📄 basislibrary.java
字号:
case org.w3c.dom.Node.ENTITY_NODE: // nothing ? break; case org.w3c.dom.Node.ENTITY_REFERENCE_NODE: newNode = doc.createEntityReference(nodeName); break; case org.w3c.dom.Node.NOTATION_NODE: // nothing ? break; case org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE: newNode = doc.createProcessingInstruction(nodeName, value); break; case org.w3c.dom.Node.TEXT_NODE: newNode = doc.createTextNode(value); break; } try { parent.appendChild(newNode); } catch (DOMException e) { runTimeError(RUN_TIME_INTERNAL_ERR, e.getMessage()); return; } } } /** * Utility function used to convert a w3c NodeList into a internal * DOM iterator. */ public static DTMAxisIterator nodeList2Iterator( org.w3c.dom.NodeList nodeList, Translet translet, DOM dom) { // w3c NodeList -> w3c DOM Document doc = null; try { doc = ((AbstractTranslet) translet).newDocument("", "__top__"); } catch (javax.xml.parsers.ParserConfigurationException e) { runTimeError(RUN_TIME_INTERNAL_ERR, e.getMessage()); return null; } // Copy all the nodes in the nodelist to be under the top element copyNodes(nodeList, doc, doc.getDocumentElement()); // w3cDOM -> DTM -> DOMImpl if (dom instanceof MultiDOM) { final MultiDOM multiDOM = (MultiDOM) dom; DTMDefaultBase dtm = (DTMDefaultBase)((DOMAdapter)multiDOM.getMain()).getDOMImpl(); DTMManager dtmManager = dtm.getManager(); DOM idom = (DOM)dtmManager.getDTM(new DOMSource(doc), false, null, true, false); // Create DOMAdapter and register with MultiDOM DOMAdapter domAdapter = new DOMAdapter(idom, translet.getNamesArray(), translet.getUrisArray(), translet.getTypesArray(), translet.getNamespaceArray()); multiDOM.addDOMAdapter(domAdapter); DTMAxisIterator iter1 = idom.getAxisIterator(Axis.CHILD); DTMAxisIterator iter2 = idom.getAxisIterator(Axis.CHILD); DTMAxisIterator iter = new AbsoluteIterator( new StepIterator(iter1, iter2)); iter.setStartNode(DTMDefaultBase.ROOTNODE); return iter; } else { runTimeError(RUN_TIME_INTERNAL_ERR, "nodeList2Iterator()"); return null; } } /** * Utility function used to convert references to DOMs. */ public static DOM referenceToResultTree(Object obj) { try { return ((DOM) obj); } catch (IllegalArgumentException e) { final String className = obj.getClass().getName(); runTimeError(DATA_CONVERSION_ERR, "reference", className); return null; } } /** * Utility function: used with nth position filters to convert a sequence * of nodes to just one single node (the one at position n). */ public static DTMAxisIterator getSingleNode(DTMAxisIterator iterator) { int node = iterator.next(); return(new SingletonIterator(node)); } /** * Utility function: used in xsl:copy. */ private static char[] _characterArray = new char[32]; public static void copy(Object obj, SerializationHandler handler, int node, DOM dom) { try { if (obj instanceof DTMAxisIterator) { DTMAxisIterator iter = (DTMAxisIterator) obj; dom.copy(iter.reset(), handler); } else if (obj instanceof Node) { dom.copy(((Node) obj).node, handler); } else if (obj instanceof DOM) { //((DOM)obj).copy(((com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase)((DOMAdapter)obj).getDOMImpl()).getDocument(), handler); DOM newDom = (DOM)obj; newDom.copy(newDom.getDocument(), handler); } else { String string = obj.toString(); // or call stringF() final int length = string.length(); if (length > _characterArray.length) _characterArray = new char[length]; string.getChars(0, length, _characterArray, 0); handler.characters(_characterArray, 0, length); } } catch (SAXException e) { runTimeError(RUN_TIME_COPY_ERR); } } /** * Utility function to check if xsl:attribute has a valid qname * This method should only be invoked if the name attribute is an AVT */ public static void checkAttribQName(String name) { final int firstOccur = name.indexOf(":"); final int lastOccur = name.lastIndexOf(":"); final String localName = name.substring(lastOccur + 1); if (firstOccur > 0) { final String newPrefix = name.substring(0, firstOccur); if (firstOccur != lastOccur) { final String oriPrefix = name.substring(firstOccur+1, lastOccur); if (!XML11Char.isXML11ValidNCName(oriPrefix)) { // even though the orignal prefix is ignored, it should still get checked for valid NCName runTimeError(INVALID_QNAME_ERR,oriPrefix+":"+localName); } } // prefix must be a valid NCName if (!XML11Char.isXML11ValidNCName(newPrefix)) { runTimeError(INVALID_QNAME_ERR,newPrefix+":"+localName); } } // local name must be a valid NCName and must not be XMLNS if ((!XML11Char.isXML11ValidNCName(localName))||(localName.equals(Constants.XMLNS_PREFIX))) { runTimeError(INVALID_QNAME_ERR,localName); } } /** * Utility function to check if a name is a valid ncname * This method should only be invoked if the attribute value is an AVT */ public static void checkNCName(String name) { if (!XML11Char.isXML11ValidNCName(name)) { runTimeError(INVALID_NCNAME_ERR,name); } } /** * Utility function to check if a name is a valid qname * This method should only be invoked if the attribute value is an AVT */ public static void checkQName(String name) { if (!XML11Char.isXML11ValidQName(name)) { runTimeError(INVALID_QNAME_ERR,name); } } /** * Utility function for the implementation of xsl:element. */ public static String startXslElement(String qname, String namespace, SerializationHandler handler, DOM dom, int node) { try { // Get prefix from qname String prefix; final int index = qname.indexOf(':'); if (index > 0) { prefix = qname.substring(0, index); // Handle case when prefix is not known at compile time if (namespace == null || namespace.length() == 0) { try { // not sure if this line of code ever works namespace = dom.lookupNamespace(node, prefix); } catch(RuntimeException e) { handler.flushPending(); // need to flush or else can't get namespacemappings NamespaceMappings nm = handler.getNamespaceMappings(); namespace = nm.lookupNamespace(prefix); if (namespace == null) { runTimeError(NAMESPACE_PREFIX_ERR,prefix); } } } handler.startElement(namespace, qname.substring(index+1), qname); handler.namespaceAfterStartElement(prefix, namespace); } else { // Need to generate a prefix? if (namespace != null && namespace.length() > 0) { prefix = generatePrefix(); qname = prefix + ':' + qname; handler.startElement(namespace, qname, qname); handler.namespaceAfterStartElement(prefix, namespace); } else { handler.startElement(null, null, qname); } } } catch (SAXException e) { throw new RuntimeException(e.getMessage()); } return qname; } /** * This function is used in the execution of xsl:element */ public static String getPrefix(String qname) { final int index = qname.indexOf(':'); return (index > 0) ? qname.substring(0, index) : null; } /** * This function is used in the execution of xsl:element */ private static int prefixIndex = 0; // not thread safe!! public static String generatePrefix() { return ("ns" + prefixIndex++); } public static final String RUN_TIME_INTERNAL_ERR = "RUN_TIME_INTERNAL_ERR"; public static final String RUN_TIME_COPY_ERR = "RUN_TIME_COPY_ERR"; public static final String DATA_CONVERSION_ERR = "DATA_CONVERSION_ERR"; public static final String EXTERNAL_FUNC_ERR = "EXTERNAL_FUNC_ERR"; public static final String EQUALITY_EXPR_ERR = "EQUALITY_EXPR_ERR"; public static final String INVALID_ARGUMENT_ERR = "INVALID_ARGUMENT_ERR"; public static final String FORMAT_NUMBER_ERR = "FORMAT_NUMBER_ERR"; public static final String ITERATOR_CLONE_ERR = "ITERATOR_CLONE_ERR"; public static final String AXIS_SUPPORT_ERR = "AXIS_SUPPORT_ERR"; public static final String TYPED_AXIS_SUPPORT_ERR = "TYPED_AXIS_SUPPORT_ERR"; public static final String STRAY_ATTRIBUTE_ERR = "STRAY_ATTRIBUTE_ERR"; public static final String STRAY_NAMESPACE_ERR = "STRAY_NAMESPACE_ERR"; public static final String NAMESPACE_PREFIX_ERR = "NAMESPACE_PREFIX_ERR"; public static final String DOM_ADAPTER_INIT_ERR = "DOM_ADAPTER_INIT_ERR"; public static final String PARSER_DTD_SUPPORT_ERR = "PARSER_DTD_SUPPORT_ERR"; public static final String NAMESPACES_SUPPORT_ERR = "NAMESPACES_SUPPORT_ERR"; public static final String CANT_RESOLVE_RELATIVE_URI_ERR = "CANT_RESOLVE_RELATIVE_URI_ERR"; public static final String UNSUPPORTED_XSL_ERR = "UNSUPPORTED_XSL_ERR"; public static final String UNSUPPORTED_EXT_ERR = "UNSUPPORTED_EXT_ERR"; public static final String UNKNOWN_TRANSLET_VERSION_ERR = "UNKNOWN_TRANSLET_VERSION_ERR"; public static final String INVALID_QNAME_ERR = "INVALID_QNAME_ERR"; public static final String INVALID_NCNAME_ERR = "INVALID_NCNAME_ERR"; public static final String UNALLOWED_EXTENSION_FUNCTION_ERR = "UNALLOWED_EXTENSION_FUNCTION_ERR"; public static final String UNALLOWED_EXTENSION_ELEMENT_ERR = "UNALLOWED_EXTENSION_ELEMENT_ERR"; // All error messages are localized and are stored in resource bundles. private static ResourceBundle m_bundle; public final static String ERROR_MESSAGES_KEY = "error-messages"; static { String resource = "com.sun.org.apache.xalan.internal.xsltc.runtime.ErrorMessages"; m_bundle = ResourceBundle.getBundle(resource); } /** * Print a run-time error message. */ public static void runTimeError(String code) { throw new RuntimeException(m_bundle.getString(code)); } public static void runTimeError(String code, Object[] args) { final String message = MessageFormat.format(m_bundle.getString(code), args); throw new RuntimeException(message); } public static void runTimeError(String code, Object arg0) { runTimeError(code, new Object[]{ arg0 } ); } public static void runTimeError(String code, Object arg0, Object arg1) { runTimeError(code, new Object[]{ arg0, arg1 } ); } public static void consoleOutput(String msg) { System.out.println(msg); } /** * Replace a certain character in a string with a new substring. */ public static String replace(String base, char ch, String str) { return (base.indexOf(ch) < 0) ? base : replace(base, String.valueOf(ch), new String[] { str }); } public static String replace(String base, String delim, String[] str) { final int len = base.length(); final StringBuffer result = new StringBuffer(); for (int i = 0; i < len; i++) { final char ch = base.charAt(i); final int k = delim.indexOf(ch); if (k >= 0) { result.append(str[k]); } else { result.append(ch); } } return result.toString(); } /** * Utility method to allow setting parameters of the form * {namespaceuri}localName * which get mapped to an instance variable in the class * Hence a parameter of the form "{http://foo.bar}xyz" * will be replaced with the corresponding values * by the BasisLibrary's utility method mapQNametoJavaName * and thus get mapped to legal java variable names */ public static String mapQNameToJavaName (String base ) { return replace(base, ".-:/{}?#%*", new String[] { "$dot$", "$dash$" ,"$colon$", "$slash$", "","$colon$","$ques$","$hash$","$per$", "$aster$"}); } //-- End utility functions}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -