KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > style > SaxonCollation


1 package net.sf.saxon.style;
2 import net.sf.saxon.Configuration;
3 import net.sf.saxon.expr.Expression;
4 import net.sf.saxon.instruct.Executable;
5 import net.sf.saxon.om.AttributeCollection;
6 import net.sf.saxon.trans.XPathException;
7
8 import java.text.Collator JavaDoc;
9 import java.text.ParseException JavaDoc;
10 import java.text.RuleBasedCollator JavaDoc;
11 import java.util.Comparator JavaDoc;
12 import java.util.Locale JavaDoc;
13
14 /**
15 * A saxon:collation element in the style sheet: this is a top-level
16 * element that defines details of a named collation. The attributes of the
17 * element provide different ways of instantiating an instance of java.util.Comparator
18 */

19
20 public class SaxonCollation extends StyleElement {
21
22     private String JavaDoc collationName;
23
24     private Comparator JavaDoc collator; // it's best to supply a java.text.Collator,
25
// but we can cope with any java.util.Comparator
26
private boolean isDefault = false;
27
28     public void prepareAttributes() throws XPathException {
29
30         AttributeCollection atts = getAttributeList();
31
32         String JavaDoc nameAtt = null; // collation name for use in expressions
33
String JavaDoc classAtt = null; // Java class name of Collator
34
String JavaDoc strengthAtt = null; // primary, secondary, tertiary, or identical
35
String JavaDoc decompositionAtt = null; // canonical, full, or none
36
String JavaDoc langAtt = null; // identifies a locale: country+language
37
String JavaDoc rulesAtt = null; // collation rules as used in RuleBasedCollator
38
String JavaDoc defaultAtt = null;
39
40         for (int a=0; a<atts.getLength(); a++) {
41             int nc = atts.getNameCode(a);
42             String JavaDoc f = getNamePool().getClarkName(nc);
43             if (f==StandardNames.NAME) {
44                 nameAtt = atts.getValue(a).trim();
45             } else if (f==StandardNames.CLASS) {
46                 classAtt = atts.getValue(a).trim();
47             } else if (f==StandardNames.STRENGTH) {
48                 strengthAtt = atts.getValue(a).trim();
49             } else if (f==StandardNames.DECOMPOSITION) {
50                 decompositionAtt = atts.getValue(a).trim();
51             } else if (f==StandardNames.LANG) {
52                 langAtt = atts.getValue(a).trim();
53             } else if (f==StandardNames.RULES) {
54                 rulesAtt = atts.getValue(a).trim();
55             } else if (f==StandardNames.DEFAULT) {
56                 defaultAtt = atts.getValue(a).trim();
57             } else {
58                 checkUnknownAttribute(nc);
59             }
60         }
61
62         if (nameAtt!=null) {
63             collationName = nameAtt.trim();
64         }
65
66         if (classAtt!=null) {
67             if (rulesAtt != null || langAtt != null || strengthAtt != null || decompositionAtt != null) {
68                 compileError("The class attribute cannot be combined with rules, lang, strength, or decomposition");
69             }
70             try {
71                 collator = getConfiguration().makeCollator(classAtt);
72                 return;
73             } catch (XPathException err) {
74                 collator = Collator.getInstance(); // so that error paths work
75
throw err;
76             }
77         }
78
79         if (rulesAtt!=null) {
80             if (langAtt != null || strengthAtt != null || decompositionAtt != null) {
81                 compileError("The rules attribute cannot be combined with lang, strength, or decomposition");
82             }
83             try {
84                 collator = new RuleBasedCollator JavaDoc(rulesAtt);
85             } catch (ParseException JavaDoc e) {
86                 collator = Collator.getInstance(); // so that error paths work
87
compileError("Invalid collation rules: " + e.getMessage());
88             }
89         }
90
91         // Start with the lang attribute
92

93         if (langAtt!=null) {
94             collator = Collator.getInstance(Configuration.getLocale(langAtt));
95         } else if (collator == null) {
96             collator = Collator.getInstance(); // use default locale
97
}
98
99         if (strengthAtt!=null && collator instanceof Collator JavaDoc) {
100             if (strengthAtt.equals("primary")) {
101                 ((Collator JavaDoc)collator).setStrength(Collator.PRIMARY);
102             } else if (strengthAtt.equals("secondary")) {
103                 ((Collator JavaDoc)collator).setStrength(Collator.SECONDARY);
104             } else if (strengthAtt.equals("tertiary")) {
105                 ((Collator JavaDoc)collator).setStrength(Collator.TERTIARY);
106             } else if (strengthAtt.equals("identical")) {
107                 ((Collator JavaDoc)collator).setStrength(Collator.IDENTICAL);
108             } else {
109                 compileError("strength must be primary, secondary, tertiary, or identical");
110             }
111         }
112
113         if (decompositionAtt!=null && collator instanceof Collator JavaDoc) {
114             if (decompositionAtt.equals("none")) {
115                 ((Collator JavaDoc)collator).setDecomposition(Collator.NO_DECOMPOSITION);
116             } else if (decompositionAtt.equals("standard")) {
117                 ((Collator JavaDoc)collator).setDecomposition(Collator.CANONICAL_DECOMPOSITION);
118             } else if (decompositionAtt.equals("full")) {
119                 ((Collator JavaDoc)collator).setDecomposition(Collator.FULL_DECOMPOSITION);
120             } else {
121                 compileError("decomposition must be none, standard, or full");
122             }
123         }
124
125         if (defaultAtt != null) {
126             if (defaultAtt.equals("yes")) {
127                 isDefault = true;
128             } else if (defaultAtt.equals("no")) {
129                 // ignore it
130
} else {
131                 compileError("default attribute must be yes or no");
132             }
133         }
134
135         // register the collation early, so it's available when optimizing XPath expressions
136
getPrincipalStylesheet().setCollation(collationName, collator, isDefault);
137     }
138
139     public void validate() throws XPathException {
140         checkTopLevel(null);
141         checkEmpty();
142     }
143
144     public Expression compile(Executable exec) throws XPathException {
145         getPrincipalStylesheet().setCollation(collationName, collator, isDefault);
146         exec.setReasonUnableToCompile("Cannot compile a stylesheet that uses saxon:collation (because the Java class " +
147                 "java.text.RuleBasedCollator is not serializable)");
148         return null;
149     }
150
151     public String JavaDoc getCollationName() {
152         return collationName;
153     }
154
155     public boolean isDefaultCollation() {
156         return isDefault;
157     }
158
159     public Comparator JavaDoc getCollator() {
160         return collator;
161     }
162
163     /**
164     * Utility method to print details of the locales for which a collator
165     * is available. (The results depend on the Java VM)
166     */

167
168     public static void main(String JavaDoc[] args) {
169         System.err.println("The following locales have collations available:");
170         Locale JavaDoc[] loc = Collator.getAvailableLocales();
171         for (int i=0; i<loc.length; i++) {
172             Locale JavaDoc l=loc[i];
173             System.err.println("Locale:" +
174               ("".equals(l.getCountry()) ? "" : " country='" + l.getCountry() + "' (" + l.getDisplayCountry() + ")" ) +
175               ("".equals(l.getLanguage()) ? "" : " language='" + l.getLanguage() + "' (" + l.getDisplayLanguage() + ")" ) +
176               ("".equals(l.getVariant()) ? "" : " variant='" + l.getVariant() + "' (" + l.getDisplayVariant() + ")" ));
177         }
178     }
179
180 }
181
182 //
183
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
184
// you may not use this file except in compliance with the License. You may obtain a copy of the
185
// License at http://www.mozilla.org/MPL/
186
//
187
// Software distributed under the License is distributed on an "AS IS" basis,
188
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
189
// See the License for the specific language governing rights and limitations under the License.
190
//
191
// The Original Code is: all this file.
192
//
193
// The Initial Developer of the Original Code is Michael H. Kay of International Computers Limited (mhkay@iclway.co.uk).
194
//
195
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
196
//
197
// Contributor(s): none.
198
//
199
Popular Tags