/*
 * Created on Sep 29, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.isogen.i18nsupport.compare;

import java.util.Comparator;
import java.util.Locale;

import com.ibm.icu.text.Collator;
import com.ibm.icu.text.RuleBasedCollator;

/**
 * Constructs ICU collators.
 */
public class IcuComparatorFactoryImpl implements ComparatorFactory {

	/* Correction comment-2(5)
	 * May 23, 2006 Antenna House, Inc.
	 */
	public Comparator getComparatorForLocale(
		Locale locale, String collationRuleSpec, int replace_rules) throws ComparatorException {
		// TODO: Use comparator/index configuration to get any local collation rules.
		Collator col = null;
		if (collationRuleSpec != null) {
			try {
				if (replace_rules == 0) {
					String baseRules = ((RuleBasedCollator)com.ibm.icu.text.Collator.getInstance(locale)).getRules();
					String addRules = collationRuleSpec;
					collationRuleSpec = baseRules + addRules;
				}
				col = new RuleBasedCollator(collationRuleSpec);
			} catch (Exception e) {
				// e.printStackTrace();
				String msg = this.getClass().getName() + ".getComparatorForLocale(): Exception constructing a RuleBasedCollator";
				throw new ComparatorException(msg, e);
			}
		} else {
			col = Collator.getInstance(locale);			
		}
		return (Comparator)col;
	}

}
