📄 basislibrary.java
字号:
/* * Copyright 2001-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * $Id: BasisLibrary.java,v 1.6 2006/06/20 21:51:58 spericas Exp $ */package com.sun.org.apache.xalan.internal.xsltc.runtime;import java.text.DecimalFormat;import java.text.DecimalFormatSymbols;import java.text.FieldPosition;import java.text.MessageFormat;import java.text.NumberFormat;import java.util.Locale;import java.util.ResourceBundle;import javax.xml.transform.dom.DOMSource;import com.sun.org.apache.xalan.internal.xsltc.DOM;import com.sun.org.apache.xalan.internal.xsltc.Translet;import com.sun.org.apache.xalan.internal.xsltc.dom.AbsoluteIterator;import com.sun.org.apache.xml.internal.dtm.Axis;import com.sun.org.apache.xalan.internal.xsltc.dom.DOMAdapter;import com.sun.org.apache.xalan.internal.xsltc.dom.MultiDOM;import com.sun.org.apache.xalan.internal.xsltc.dom.SingletonIterator;import com.sun.org.apache.xalan.internal.xsltc.dom.StepIterator;import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;import com.sun.org.apache.xml.internal.dtm.DTMManager;import com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase;import org.w3c.dom.DOMException;import org.w3c.dom.Document;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;import com.sun.org.apache.xml.internal.serializer.NamespaceMappings;import com.sun.org.apache.xml.internal.serializer.SerializationHandler;import com.sun.org.apache.xml.internal.utils.XML11Char;/** * Standard XSLT functions. All standard functions expect the current node * and the DOM as their last two arguments. */public final class BasisLibrary { private final static String EMPTYSTRING = ""; /** * Standard function count(node-set) */ public static int countF(DTMAxisIterator iterator) { return(iterator.getLast()); } /** * Standard function position() * @deprecated This method exists only for backwards compatibility with old * translets. New code should not reference it. */ public static int positionF(DTMAxisIterator iterator) { return iterator.isReverse() ? iterator.getLast() - iterator.getPosition() + 1 : iterator.getPosition(); } /** * XSLT Standard function sum(node-set). * stringToDouble is inlined */ public static double sumF(DTMAxisIterator iterator, DOM dom) { try { double result = 0.0; int node; while ((node = iterator.next()) != DTMAxisIterator.END) { result += Double.parseDouble(dom.getStringValueX(node)); } return result; } catch (NumberFormatException e) { return Double.NaN; } } /** * XSLT Standard function string() */ public static String stringF(int node, DOM dom) { return dom.getStringValueX(node); } /** * XSLT Standard function string(value) */ public static String stringF(Object obj, DOM dom) { 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 { return obj.toString(); } } /** * XSLT Standard function string(value) */ public static String stringF(Object obj, int node, DOM dom) { 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) { // When the first argument is a DOM we want the whole fecking // DOM and not just a single node - that would not make sense. //return ((DOM)obj).getStringValueX(node); return ((DOM)obj).getStringValue(); } else if (obj instanceof Double) { Double d = (Double)obj; final String result = d.toString(); final int length = result.length(); if ((result.charAt(length-2)=='.') && (result.charAt(length-1) == '0')) return result.substring(0, length-2); else return result; } else { return obj != null ? obj.toString() : ""; } } /** * XSLT Standard function number() */ public static double numberF(int node, DOM dom) { return stringToReal(dom.getStringValueX(node)); } /** * XSLT Standard function number(value) */ public static double numberF(Object obj, DOM dom) { if (obj instanceof Double) { return ((Double) obj).doubleValue(); } else if (obj instanceof Integer) { return ((Integer) obj).doubleValue(); } else if (obj instanceof Boolean) { return ((Boolean) obj).booleanValue() ? 1.0 : 0.0; } else if (obj instanceof String) { return stringToReal((String) obj); } else if (obj instanceof DTMAxisIterator) { DTMAxisIterator iter = (DTMAxisIterator) obj; return stringToReal(dom.getStringValueX(iter.reset().next())); } else if (obj instanceof Node) { return stringToReal(dom.getStringValueX(((Node) obj).node)); } else if (obj instanceof DOM) { return stringToReal(((DOM) obj).getStringValue()); } else { final String className = obj.getClass().getName(); runTimeError(INVALID_ARGUMENT_ERR, className, "number()"); return 0.0; } } /** * XSLT Standard function round() */ public static double roundF(double d) { return (d<-0.5 || d>0.0)?Math.floor(d+0.5):((d==0.0)? d:(Double.isNaN(d)?Double.NaN:-0.0)); } /** * XSLT Standard function boolean() */ public static boolean booleanF(Object obj) { if (obj instanceof Double) { final double temp = ((Double) obj).doubleValue(); return temp != 0.0 && !Double.isNaN(temp); } else if (obj instanceof Integer) { return ((Integer) obj).doubleValue() != 0; } else if (obj instanceof Boolean) { return ((Boolean) obj).booleanValue(); } else if (obj instanceof String) { return !((String) obj).equals(EMPTYSTRING); } else if (obj instanceof DTMAxisIterator) { DTMAxisIterator iter = (DTMAxisIterator) obj; return iter.reset().next() != DTMAxisIterator.END; } else if (obj instanceof Node) { return true; } else if (obj instanceof DOM) { String temp = ((DOM) obj).getStringValue(); return !temp.equals(EMPTYSTRING); } else { final String className = obj.getClass().getName(); runTimeError(INVALID_ARGUMENT_ERR, className, "boolean()"); } return false; } /** * XSLT Standard function substring(). Must take a double because of * conversions resulting into NaNs and rounding. */ public static String substringF(String value, double start) { try { final int strlen = value.length(); int istart = (int)Math.round(start) - 1; if (Double.isNaN(start)) return(EMPTYSTRING); if (istart > strlen) return(EMPTYSTRING); if (istart < 1) istart = 0; return value.substring(istart); } catch (IndexOutOfBoundsException e) { runTimeError(RUN_TIME_INTERNAL_ERR, "substring()"); return null; } } /** * XSLT Standard function substring(). Must take a double because of * conversions resulting into NaNs and rounding. */ public static String substringF(String value, double start, double length) { try { final int strlen = value.length(); int istart = (int)Math.round(start) - 1; int isum = istart + (int)Math.round(length); if (Double.isInfinite(length)) isum = Integer.MAX_VALUE; if (Double.isNaN(start) || Double.isNaN(length)) return(EMPTYSTRING); if (Double.isInfinite(start)) return(EMPTYSTRING); if (istart > strlen) return(EMPTYSTRING); if (isum < 0) return(EMPTYSTRING); if (istart < 0) istart = 0; if (isum > strlen) return value.substring(istart); else return value.substring(istart, isum); } catch (IndexOutOfBoundsException e) { runTimeError(RUN_TIME_INTERNAL_ERR, "substring()"); return null; } } /** * XSLT Standard function substring-after(). */ public static String substring_afterF(String value, String substring) { final int index = value.indexOf(substring); if (index >= 0) return value.substring(index + substring.length()); else return EMPTYSTRING; } /** * XSLT Standard function substring-before(). */ public static String substring_beforeF(String value, String substring) { final int index = value.indexOf(substring); if (index >= 0) return value.substring(0, index); else return EMPTYSTRING; } /** * XSLT Standard function translate(). */ public static String translateF(String value, String from, String to) { final int tol = to.length(); final int froml = from.length(); final int valuel = value.length(); final StringBuffer result = new StringBuffer(); for (int j, i = 0; i < valuel; i++) { final char ch = value.charAt(i); for (j = 0; j < froml; j++) { if (ch == from.charAt(j)) { if (j < tol) result.append(to.charAt(j)); break; } } if (j == froml) result.append(ch); } return result.toString(); } /** * XSLT Standard function normalize-space(). */ public static String normalize_spaceF(int node, DOM dom) { return normalize_spaceF(dom.getStringValueX(node)); } /** * XSLT Standard function normalize-space(string). */ public static String normalize_spaceF(String value) { int i = 0, n = value.length(); StringBuffer result = new StringBuffer(); while (i < n && isWhiteSpace(value.charAt(i))) i++; while (true) { while (i < n && !isWhiteSpace(value.charAt(i))) { result.append(value.charAt(i++)); } if (i == n) break; while (i < n && isWhiteSpace(value.charAt(i))) { i++; } if (i < n) result.append(' '); } return result.toString(); } /** * XSLT Standard function generate-id(). */ public static String generate_idF(int node) { if (node > 0) // Only generate ID if node exists return "N" + node; else // Otherwise return an empty string return EMPTYSTRING; } /** * utility function for calls to local-name(). */ public static String getLocalName(String value) { int idx = value.lastIndexOf(':'); if (idx >= 0) value = value.substring(idx + 1); idx = value.lastIndexOf('@'); if (idx >= 0) value = value.substring(idx + 1); return(value); } /** * External functions that cannot be resolved are replaced with a call * to this method. This method will generate a runtime errors. A good * stylesheet checks whether the function exists using conditional
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -