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 ; 9 import java.text.ParseException ; 10 import java.text.RuleBasedCollator ; 11 import java.util.Comparator ; 12 import java.util.Locale ; 13 14 19 20 public class SaxonCollation extends StyleElement { 21 22 private String collationName; 23 24 private Comparator collator; private boolean isDefault = false; 27 28 public void prepareAttributes() throws XPathException { 29 30 AttributeCollection atts = getAttributeList(); 31 32 String nameAtt = null; String classAtt = null; String strengthAtt = null; String decompositionAtt = null; String langAtt = null; String rulesAtt = null; String defaultAtt = null; 39 40 for (int a=0; a<atts.getLength(); a++) { 41 int nc = atts.getNameCode(a); 42 String 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(); 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 (rulesAtt); 85 } catch (ParseException e) { 86 collator = Collator.getInstance(); compileError("Invalid collation rules: " + e.getMessage()); 88 } 89 } 90 91 93 if (langAtt!=null) { 94 collator = Collator.getInstance(Configuration.getLocale(langAtt)); 95 } else if (collator == null) { 96 collator = Collator.getInstance(); } 98 99 if (strengthAtt!=null && collator instanceof Collator ) { 100 if (strengthAtt.equals("primary")) { 101 ((Collator )collator).setStrength(Collator.PRIMARY); 102 } else if (strengthAtt.equals("secondary")) { 103 ((Collator )collator).setStrength(Collator.SECONDARY); 104 } else if (strengthAtt.equals("tertiary")) { 105 ((Collator )collator).setStrength(Collator.TERTIARY); 106 } else if (strengthAtt.equals("identical")) { 107 ((Collator )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 ) { 114 if (decompositionAtt.equals("none")) { 115 ((Collator )collator).setDecomposition(Collator.NO_DECOMPOSITION); 116 } else if (decompositionAtt.equals("standard")) { 117 ((Collator )collator).setDecomposition(Collator.CANONICAL_DECOMPOSITION); 118 } else if (decompositionAtt.equals("full")) { 119 ((Collator )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 } else { 131 compileError("default attribute must be yes or no"); 132 } 133 } 134 135 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 getCollationName() { 152 return collationName; 153 } 154 155 public boolean isDefaultCollation() { 156 return isDefault; 157 } 158 159 public Comparator getCollator() { 160 return collator; 161 } 162 163 167 168 public static void main(String [] args) { 169 System.err.println("The following locales have collations available:"); 170 Locale [] loc = Collator.getAvailableLocales(); 171 for (int i=0; i<loc.length; i++) { 172 Locale 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 | Popular Tags |