KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > functions > Unicode


1 package net.sf.saxon.functions;
2 import net.sf.saxon.expr.XPathContext;
3 import net.sf.saxon.om.*;
4 import net.sf.saxon.trans.DynamicError;
5 import net.sf.saxon.trans.XPathException;
6 import net.sf.saxon.value.NumericValue;
7 import net.sf.saxon.value.StringValue;
8
9 /**
10  * This class supports the two functions string-to-codepoints() and codepoints-to-string()
11  */

12
13 public class Unicode extends SystemFunction {
14
15     public static final int TO_CODEPOINTS = 0;
16     public static final int FROM_CODEPOINTS = 1;
17
18     /**
19     * Evaluate
20     */

21
22     public Item evaluateItem(XPathContext c) throws XPathException {
23         switch (operation) {
24         case TO_CODEPOINTS:
25             throw new UnsupportedOperationException JavaDoc("Cannot call evaluateItem on a sequence");
26         case FROM_CODEPOINTS:
27             return StringValue.makeStringValue(unicodeToString(argument[0].iterate(c), c));
28         default:
29             throw new UnsupportedOperationException JavaDoc("Unknown Unicode operation");
30         }
31     }
32
33     public SequenceIterator iterate(XPathContext c) throws XPathException {
34         switch (operation) {
35         case TO_CODEPOINTS:
36             Item item = argument[0].evaluateItem(c);
37             if (item==null) {
38                 return EmptyIterator.getInstance();
39             }
40             return stringToUnicode(item.getStringValueCS());
41         case FROM_CODEPOINTS:
42             return SingletonIterator.makeIterator(evaluateItem(c));
43         default:
44             throw new UnsupportedOperationException JavaDoc("Unknown Unicode operation");
45         }
46     }
47
48     /**
49     * Return a sequence of integers representing the Unicode code values of the characters in a given
50     * string.
51     */

52
53     private static SequenceIterator stringToUnicode(CharSequence JavaDoc s) {
54         return StringValue.makeStringValue(s).iterateCharacters();
55     }
56
57     /**
58     * Return the Unicode string corresponding to a given sequence of Unicode code values
59     */

60
61     private static CharSequence JavaDoc unicodeToString(SequenceIterator chars, XPathContext context) throws XPathException {
62         FastStringBuffer sb = new FastStringBuffer(256);
63         while (true) {
64             NumericValue nextInt = (NumericValue)chars.next();
65             if (nextInt == null) {
66                 return sb.condense();
67             }
68             long next = nextInt.longValue();
69             if (next > Integer.MAX_VALUE || !XMLChar.isValid((int)next)) {
70                 DynamicError e = new DynamicError("Invalid XML character [decimal " + next + ']');
71                 e.setErrorCode("FOCH0001");
72                 e.setXPathContext(context);
73                 throw e;
74             }
75             if (next<65536) {
76                 sb.append((char)next);
77             } else { // output a surrogate pair
78
sb.append(XMLChar.highSurrogate((int)next));
79                 sb.append(XMLChar.lowSurrogate((int)next));
80             }
81         }
82     }
83
84
85 }
86
87
88
89 //
90
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
91
// you may not use this file except in compliance with the License. You may obtain a copy of the
92
// License at http://www.mozilla.org/MPL/
93
//
94
// Software distributed under the License is distributed on an "AS IS" basis,
95
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
96
// See the License for the specific language governing rights and limitations under the License.
97
//
98
// The Original Code is: all this file.
99
//
100
// The Initial Developer of the Original Code is Michael H. Kay.
101
//
102
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
103
//
104
// Contributor(s): none.
105
//
106
Popular Tags