001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.binding.http.strategy;
019:
020: import java.util.ArrayList;
021: import java.util.Arrays;
022: import java.util.HashMap;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.regex.Matcher;
026: import java.util.regex.Pattern;
027:
028: public class Inflector {
029:
030: private List<String> uncountable = new ArrayList<String>();
031: private List<Rule> singular = new ArrayList<Rule>();
032: private List<Rule> plural = new ArrayList<Rule>();
033: private Map<String, String> irregular = new HashMap<String, String>();
034:
035: public String singularlize(String orig) {
036: if (uncountable.contains(orig)) {
037: return orig;
038: }
039:
040: for (Map.Entry<String, String> entry : irregular.entrySet()) {
041: if (entry.getValue().equals(orig)) {
042: return entry.getKey();
043: }
044: }
045:
046: for (Rule r : singular) {
047: Matcher m = r.getRegex().matcher(orig);
048: if (m.find()) {
049: return m.replaceAll(r.getReplacement());
050: }
051: }
052:
053: return orig;
054: }
055:
056: public String pluralize(String orig) {
057: if (uncountable.contains(orig)) {
058: return orig;
059: }
060:
061: String irr = irregular.get(orig);
062: if (irr != null) {
063: return irr;
064: }
065:
066: for (Rule r : plural) {
067: Matcher m = r.getRegex().matcher(orig);
068: //System.out.println(m.pattern().pattern());
069: if (m.find()) {
070: //System.out.println("!!!found match!!!");
071: return m.replaceAll(r.getReplacement());
072: }
073: }
074:
075: return orig;
076: }
077:
078: public void addPlural(String regex, String replacement) {
079: plural.add(0, new Rule(regex, replacement));
080: }
081:
082: public void addSingular(String regex, String replacement) {
083: singular.add(0, new Rule(regex, replacement));
084: }
085:
086: public void addIrregular(String orig, String replacement) {
087: irregular.put(orig, replacement);
088: }
089:
090: public void addUncountable(String[] words) {
091: uncountable.addAll(Arrays.asList(words));
092: }
093:
094: public void addUncountable(String word) {
095: uncountable.add(word);
096: }
097:
098: static class Rule {
099: private Pattern regex;
100: private String replacement;
101:
102: public Rule(String regex, String replacement) {
103: this .regex = Pattern.compile(regex,
104: Pattern.CASE_INSENSITIVE);
105: this .replacement = replacement;
106: }
107:
108: public Pattern getRegex() {
109: return regex;
110: }
111:
112: public String getReplacement() {
113: return replacement;
114: }
115: }
116: }
|