📄 basislibrary.java
字号:
result = !result; } return result; } // Next, node-set/t for t in {real, string, node-set, result-tree} DTMAxisIterator iter = ((DTMAxisIterator)left).reset(); if (right instanceof DTMAxisIterator) { result = compare(iter, (DTMAxisIterator)right, op, dom); } else if (right instanceof String) { result = compare(iter, (String)right, op, dom); } else if (right instanceof Number) { final double temp = ((Number)right).doubleValue(); result = compare(iter, temp, op, dom); } else if (right instanceof Boolean) { boolean temp = ((Boolean)right).booleanValue(); result = (iter.reset().next() != DTMAxisIterator.END) == temp; } else if (right instanceof DOM) { result = compare(iter, ((DOM)right).getStringValue(), op, dom); } else if (right == null) { return(false); } else { final String className = right.getClass().getName(); runTimeError(INVALID_ARGUMENT_ERR, className, "compare()"); } } return result; } /** * Utility function: used to test context node's language */ public static boolean testLanguage(String testLang, DOM dom, int node) { // language for context node (if any) String nodeLang = dom.getLanguage(node); if (nodeLang == null) return(false); else nodeLang = nodeLang.toLowerCase(); // compare context node's language agains test language testLang = testLang.toLowerCase(); if (testLang.length() == 2) { return(nodeLang.startsWith(testLang)); } else { return(nodeLang.equals(testLang)); } } private static boolean hasSimpleType(Object obj) { return obj instanceof Boolean || obj instanceof Double || obj instanceof Integer || obj instanceof String || obj instanceof Node || obj instanceof DOM; } /** * Utility function: used in StringType to convert a string to a real. */ public static double stringToReal(String s) { try { return Double.valueOf(s).doubleValue(); } catch (NumberFormatException e) { return Double.NaN; } } /** * Utility function: used in StringType to convert a string to an int. */ public static int stringToInt(String s) { try { return Integer.parseInt(s); } catch (NumberFormatException e) { return(-1); // ??? } } private static final int DOUBLE_FRACTION_DIGITS = 340; private static final double lowerBounds = 0.001; private static final double upperBounds = 10000000; private static DecimalFormat defaultFormatter, xpathFormatter; private static String defaultPattern = ""; static { NumberFormat f = NumberFormat.getInstance(Locale.getDefault()); defaultFormatter = (f instanceof DecimalFormat) ? (DecimalFormat) f : new DecimalFormat(); // Set max fraction digits so that truncation does not occur. Setting // the max to Integer.MAX_VALUE may cause problems with some JDK's. defaultFormatter.setMaximumFractionDigits(DOUBLE_FRACTION_DIGITS); defaultFormatter.setMinimumFractionDigits(0); defaultFormatter.setMinimumIntegerDigits(1); defaultFormatter.setGroupingUsed(false); // This formatter is used to convert numbers according to the XPath // 1.0 syntax which ignores locales (http://www.w3.org/TR/xpath#NT-Number) xpathFormatter = new DecimalFormat("", new DecimalFormatSymbols(Locale.US)); xpathFormatter.setMaximumFractionDigits(DOUBLE_FRACTION_DIGITS); xpathFormatter.setMinimumFractionDigits(0); xpathFormatter.setMinimumIntegerDigits(1); xpathFormatter.setGroupingUsed(false); } /** * Utility function: used in RealType to convert a real to a string. * Removes the decimal if null. Uses a specialized formatter object * for very large and very small numbers that ignores locales, thus * using always using "." as a decimal separator. */ public static String realToString(double d) { final double m = Math.abs(d); if ((m >= lowerBounds) && (m < upperBounds)) { final String result = Double.toString(d); final int length = result.length(); // Remove leading zeros. if ((result.charAt(length-2) == '.') && (result.charAt(length-1) == '0')) return result.substring(0, length-2); else return result; } else { if (Double.isNaN(d) || Double.isInfinite(d)) return(Double.toString(d)); // Use the XPath formatter to ignore locales StringBuffer result = new StringBuffer(); xpathFormatter.format(d, result, _fieldPosition); return result.toString(); } } /** * Utility function: used in RealType to convert a real to an integer */ public static int realToInt(double d) { return (int)d; } /** * Utility function: used to format/adjust a double to a string. The * DecimalFormat object comes from the 'formatSymbols' hashtable in * AbstractTranslet. */ private static FieldPosition _fieldPosition = new FieldPosition(0); public static String formatNumber(double number, String pattern, DecimalFormat formatter) { // bugzilla fix 12813 if (formatter == null) { formatter = defaultFormatter; } try { StringBuffer result = new StringBuffer(); if (pattern != defaultPattern) { formatter.applyLocalizedPattern(pattern); } formatter.format(number, result, _fieldPosition); return result.toString(); } catch (IllegalArgumentException e) { runTimeError(FORMAT_NUMBER_ERR, Double.toString(number), pattern); return(EMPTYSTRING); } } /** * Utility function: used to convert references to node-sets. If the * obj is an instanceof Node then create a singleton iterator. */ public static DTMAxisIterator referenceToNodeSet(Object obj) { // Convert var/param -> node if (obj instanceof Node) { return(new SingletonIterator(((Node)obj).node)); } // Convert var/param -> node-set else if (obj instanceof DTMAxisIterator) { return(((DTMAxisIterator)obj).cloneIterator()); } else { final String className = obj.getClass().getName(); runTimeError(DATA_CONVERSION_ERR, className, "node-set"); return null; } } /** * Utility function: used to convert reference to org.w3c.dom.NodeList. */ public static NodeList referenceToNodeList(Object obj, DOM dom) { if (obj instanceof Node || obj instanceof DTMAxisIterator) { DTMAxisIterator iter = referenceToNodeSet(obj); return dom.makeNodeList(iter); } else if (obj instanceof DOM) { dom = (DOM)obj; return dom.makeNodeList(DTMDefaultBase.ROOTNODE); } else { final String className = obj.getClass().getName(); runTimeError(DATA_CONVERSION_ERR, className, "org.w3c.dom.NodeList"); return null; } } /** * Utility function: used to convert reference to org.w3c.dom.Node. */ public static org.w3c.dom.Node referenceToNode(Object obj, DOM dom) { if (obj instanceof Node || obj instanceof DTMAxisIterator) { DTMAxisIterator iter = referenceToNodeSet(obj); return dom.makeNode(iter); } else if (obj instanceof DOM) { dom = (DOM)obj; DTMAxisIterator iter = dom.getChildren(DTMDefaultBase.ROOTNODE); return dom.makeNode(iter); } else { final String className = obj.getClass().getName(); runTimeError(DATA_CONVERSION_ERR, className, "org.w3c.dom.Node"); return null; } } /** * Utility function: used to convert reference to long. */ public static long referenceToLong(Object obj) { if (obj instanceof Number) { return ((Number) obj).longValue(); // handles Integer and Double } else { final String className = obj.getClass().getName(); runTimeError(DATA_CONVERSION_ERR, className, Long.TYPE); return 0; } } /** * Utility function: used to convert reference to double. */ public static double referenceToDouble(Object obj) { if (obj instanceof Number) { return ((Number) obj).doubleValue(); // handles Integer and Double } else { final String className = obj.getClass().getName(); runTimeError(DATA_CONVERSION_ERR, className, Double.TYPE); return 0; } } /** * Utility function: used to convert reference to boolean. */ public static boolean referenceToBoolean(Object obj) { if (obj instanceof Boolean) { return ((Boolean) obj).booleanValue(); } else { final String className = obj.getClass().getName(); runTimeError(DATA_CONVERSION_ERR, className, Boolean.TYPE); return false; } } /** * Utility function: used to convert reference to String. */ public static String referenceToString(Object obj, DOM dom) { if (obj instanceof String) { return (String) obj; } else if (obj instanceof DTMAxisIterator) { return dom.getStringValueX(((DTMAxisIterator)obj).reset().next()); } else if (obj instanceof Node) { return dom.getStringValueX(((Node)obj).node); } else if (obj instanceof DOM) { return ((DOM) obj).getStringValue(); } else { final String className = obj.getClass().getName(); runTimeError(DATA_CONVERSION_ERR, className, String.class); return null; } } /** * Utility function used to convert a w3c Node into an internal DOM iterator. */ public static DTMAxisIterator node2Iterator(org.w3c.dom.Node node, Translet translet, DOM dom) { final org.w3c.dom.Node inNode = node; // Create a dummy NodeList which only contains the given node to make // use of the nodeList2Iterator() interface. org.w3c.dom.NodeList nodelist = new org.w3c.dom.NodeList() { public int getLength() { return 1; } public org.w3c.dom.Node item(int index) { if (index == 0) return inNode; else return null; } }; return nodeList2Iterator(nodelist, translet, dom); } /** * Utility function used to copy a node list to be under a parent node. */ private static void copyNodes(org.w3c.dom.NodeList nodeList, org.w3c.dom.Document doc, org.w3c.dom.Node parent) { final int size = nodeList.getLength(); // copy Nodes from NodeList into new w3c DOM for (int i = 0; i < size; i++) { org.w3c.dom.Node curr = nodeList.item(i); int nodeType = curr.getNodeType(); String value = null; try { value = curr.getNodeValue(); } catch (DOMException ex) { runTimeError(RUN_TIME_INTERNAL_ERR, ex.getMessage()); return; } String nodeName = curr.getNodeName(); org.w3c.dom.Node newNode = null; switch (nodeType){ case org.w3c.dom.Node.ATTRIBUTE_NODE: newNode = doc.createAttributeNS(curr.getNamespaceURI(), nodeName); break; case org.w3c.dom.Node.CDATA_SECTION_NODE: newNode = doc.createCDATASection(value); break; case org.w3c.dom.Node.COMMENT_NODE: newNode = doc.createComment(value); break; case org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE: newNode = doc.createDocumentFragment(); break; case org.w3c.dom.Node.DOCUMENT_NODE: newNode = doc.createElementNS(null, "__document__"); copyNodes(curr.getChildNodes(), doc, newNode); break; case org.w3c.dom.Node.DOCUMENT_TYPE_NODE: // nothing? break; case org.w3c.dom.Node.ELEMENT_NODE: // For Element node, also copy the children and the // attributes. org.w3c.dom.Element element = doc.createElementNS( curr.getNamespaceURI(), nodeName); if (curr.hasAttributes()) { org.w3c.dom.NamedNodeMap attributes = curr.getAttributes(); for (int k = 0; k < attributes.getLength(); k++) { org.w3c.dom.Node attr = attributes.item(k); element.setAttributeNS(attr.getNamespaceURI(), attr.getNodeName(), attr.getNodeValue()); } } copyNodes(curr.getChildNodes(), doc, element); newNode = element; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -