KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.functions;
2 import net.sf.saxon.codenorm.Normalizer;
3 import net.sf.saxon.expr.XPathContext;
4 import net.sf.saxon.om.Item;
5 import net.sf.saxon.trans.DynamicError;
6 import net.sf.saxon.trans.XPathException;
7 import net.sf.saxon.value.StringValue;
8
9 /**
10  * Implement the XPath normalize-unicode() function
11  */

12
13 public class NormalizeUnicode extends SystemFunction {
14
15     /**
16     * Evaluate in a general context
17     */

18
19     public Item evaluateItem(XPathContext c) throws XPathException {
20         StringValue sv = (StringValue)argument[0].evaluateItem(c);
21         if (sv==null) {
22             return StringValue.EMPTY_STRING;
23         }
24         byte fb = Normalizer.C;
25         if (argument.length == 2) {
26             String JavaDoc form = argument[1].evaluateAsString(c);
27             if (form.equals("NFC")) {
28                 fb = Normalizer.C;
29             } else if (form.equals("NFD")) {
30                 fb = Normalizer.D;
31             } else if (form.equals("NFKC")) {
32                 fb = Normalizer.KC;
33             } else if (form.equals("NFKD")) {
34                 fb = Normalizer.KD;
35             } else {
36                 String JavaDoc msg = "Normalization form " + form + " is not supported";
37                 DynamicError err = new DynamicError(msg);
38                 err.setErrorCode("FOCH0003");
39                 err.setXPathContext(c);
40                 throw err;
41             }
42         }
43         Normalizer norm = new Normalizer(fb);
44         String JavaDoc result = norm.normalize(sv.getStringValue());
45         return StringValue.makeStringValue(result);
46     }
47
48 }
49
50
51
52
53 //
54
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
55
// you may not use this file except in compliance with the License. You may obtain a copy of the
56
// License at http://www.mozilla.org/MPL/
57
//
58
// Software distributed under the License is distributed on an "AS IS" basis,
59
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
60
// See the License for the specific language governing rights and limitations under the License.
61
//
62
// The Original Code is: all this file.
63
//
64
// The Initial Developer of the Original Code is Michael H. Kay.
65
//
66
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
67
//
68
// Contributor(s): none.
69
//
70
Popular Tags