KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > sort > StandardCollationURIResolver


1 package net.sf.saxon.sort;
2 import net.sf.saxon.Configuration;
3 import net.sf.saxon.trans.XPathException;
4
5 import java.net.URI JavaDoc;
6 import java.net.URISyntaxException JavaDoc;
7 import java.text.Collator JavaDoc;
8 import java.util.Comparator JavaDoc;
9 import java.util.Locale JavaDoc;
10 import java.util.StringTokenizer JavaDoc;
11
12 /**
13  * StandardCollationURIResolver allows a Collation to be created given
14  * a URI starting with "http://saxon.sf.net/collation?" followed by a set of query parameters.
15 */

16
17 public class StandardCollationURIResolver implements CollationURIResolver {
18
19     private static final StandardCollationURIResolver theInstance = new StandardCollationURIResolver();
20
21     /**
22      * The class is a singleton
23      */

24     private StandardCollationURIResolver() {
25     }
26
27     /**
28      * Return the singleton instance of this class
29      */

30
31     public static final StandardCollationURIResolver getInstance() {
32         return theInstance;
33     }
34
35     /**
36      * Make a collator with given properties
37      * @param langAtt the language
38      * @param strengthAtt the collation strength: primary, secondary, tertiary, or identical
39      * @param decompositionAtt whether strings are normalized into Unicode decomposed normal form:
40      * none, standard, or full
41      * @return the collator
42      */

43
44     private static Collator JavaDoc makeUsingProperties(
45             String JavaDoc langAtt,
46             String JavaDoc strengthAtt,
47             String JavaDoc decompositionAtt )
48     {
49
50         Collator JavaDoc collator;
51         // Start with the lang attribute
52

53         if (langAtt!=null) {
54             collator = Collator.getInstance(getLocale(langAtt));
55         } else {
56             collator = Collator.getInstance(); // use default locale
57
}
58
59         if (strengthAtt!=null && collator instanceof Collator JavaDoc) {
60             if (strengthAtt.equals("primary")) {
61                 collator.setStrength(Collator.PRIMARY);
62             } else if (strengthAtt.equals("secondary")) {
63                 collator.setStrength(Collator.SECONDARY);
64             } else if (strengthAtt.equals("tertiary")) {
65                 collator.setStrength(Collator.TERTIARY);
66             } else if (strengthAtt.equals("identical")) {
67                 collator.setStrength(Collator.IDENTICAL);
68             } else {
69                 //throw new XPathException.Dynamic("Collation strength must be primary, secondary, tertiary, or identical");
70
}
71         }
72
73         if (decompositionAtt!=null && collator instanceof Collator JavaDoc) {
74             if (decompositionAtt.equals("none")) {
75                 collator.setDecomposition(Collator.NO_DECOMPOSITION);
76             } else if (decompositionAtt.equals("standard")) {
77                 collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
78             } else if (decompositionAtt.equals("full")) {
79                 collator.setDecomposition(Collator.FULL_DECOMPOSITION);
80             } else {
81                 //throw new XPathException.Dynamic("Collation decomposition must be none, standard, or full");
82
}
83         }
84         return collator;
85     }
86
87     /**
88     * Get a locale given a language code in XML format
89     */

90
91     private static Locale JavaDoc getLocale(String JavaDoc lang) {
92         int hyphen = lang.indexOf("-");
93         String JavaDoc language, country;
94         if (hyphen < 1) {
95             language = lang;
96             country = "";
97         } else {
98             language = lang.substring(1, hyphen);
99             country = lang.substring(hyphen+1);
100         }
101         return new Locale JavaDoc(language, country);
102     }
103
104     /**
105      * Create a collator from a parameterized URI
106      * @return null if the collation URI is not suitable
107      */

108
109     public Comparator JavaDoc resolve(String JavaDoc uri, String JavaDoc base, Configuration config) {
110         if (uri.equals("http://saxon.sf.net/collation")) {
111             return makeUsingProperties(null, null, null);
112         } else if (uri.startsWith("http://saxon.sf.net/collation?")) {
113             URI JavaDoc uuri;
114             try {
115                 uuri = new URI JavaDoc(uri);
116             } catch (URISyntaxException JavaDoc err) {
117                 return null;
118             }
119             String JavaDoc query = uuri.getQuery();
120             String JavaDoc lang = null;
121             String JavaDoc strength = null;
122             String JavaDoc decomposition = null;
123             String JavaDoc classname = null;
124             StringTokenizer JavaDoc queryTokenizer = new StringTokenizer JavaDoc(query, ";&");
125             while (queryTokenizer.hasMoreElements()) {
126                 String JavaDoc param = queryTokenizer.nextToken();
127                 int eq = param.indexOf('=');
128                 if (eq > 0 && eq < param.length()-1) {
129                     String JavaDoc kw = param.substring(0, eq);
130                     String JavaDoc val = param.substring(eq + 1);
131                     if (kw.equals("lang")) {
132                         lang = val;
133                     } else if (kw.equals("strength")) {
134                         strength = val;
135                     } else if (kw.equals("decomposition")) {
136                         decomposition = val;
137                     } else if (kw.equals("class")) {
138                         classname = val;
139                     }
140                 }
141             }
142             if (classname != null) {
143                 try {
144                     return config.makeCollator(classname);
145                 } catch (XPathException err) {
146                     return null;
147                 }
148             }
149             return makeUsingProperties(lang, strength, decomposition);
150         } else {
151             return null;
152         }
153     }
154
155
156 }
157
158 //
159
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
160
// you may not use this file except in compliance with the License. You may obtain a copy of the
161
// License at http://www.mozilla.org/MPL/
162
//
163
// Software distributed under the License is distributed on an "AS IS" basis,
164
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
165
// See the License for the specific language governing rights and limitations under the License.
166
//
167
// The Original Code is: all this file.
168
//
169
// The Initial Developer of the Original Code is Michael Kay
170
//
171
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
172
//
173
// Contributor(s): none.
174
//
175
Popular Tags