getPersistentClass();
200 | }
201 |
--------------------------------------------------------------------------------
/hbnpojogen-persistence/src/main/java/com/github/wwadge/hbnpojogen/persistence/IPojoGenEntity.java:
--------------------------------------------------------------------------------
1 | package com.github.wwadge.hbnpojogen.persistence;
2 |
3 |
4 | import java.io.Serializable;
5 |
6 |
7 | /**
8 | * All generated model classes implement this interface.
9 | *
10 | * @author wallacew
11 | * @version $Revision: 3$
12 | */
13 | public interface IPojoGenEntity {
14 |
15 | /**
16 | * Return the type of this class. Useful for when dealing with proxies.
17 | *
18 | * @return Defining class.
19 | */
20 | Class> getClassType();
21 |
22 | /**
23 | * Return the id.
24 | *
25 | * @return the id.
26 | */
27 | Serializable getId();
28 |
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/hbnpojogen-persistence/src/main/java/com/github/wwadge/hbnpojogen/persistence/impl/CustomGeneratedId.java:
--------------------------------------------------------------------------------
1 | package com.github.wwadge.hbnpojogen.persistence.impl;
2 |
3 | import org.hibernate.dialect.Dialect;
4 | import org.hibernate.engine.SessionImplementor;
5 | import org.hibernate.id.AbstractUUIDGenerator;
6 | import org.hibernate.id.Configurable;
7 | import org.hibernate.type.Type;
8 |
9 | import java.io.Serializable;
10 | import java.lang.reflect.Method;
11 | import java.util.Properties;
12 |
13 | /**
14 | * uuid
15 | *
16 | * A custom UUID generator that returns a string from java.util.UUID,
17 | * This string will consist of only hex digits. Optionally,
18 | * the string may be generated with separators between each
19 | * component of the UUID.
20 | *
21 | * Mapping parameters supported: none
22 | *
23 | * @author wallacew
24 | */
25 |
26 | public class CustomGeneratedId extends AbstractUUIDGenerator implements Configurable {
27 |
28 | // private String sep = "";
29 |
30 | /**
31 | * Generate function.
32 | *
33 | * @param session Session handle
34 | * @param obj obj type
35 | * @return Serializable object
36 | */
37 | @SuppressWarnings("unused")
38 | public Serializable generate(SessionImplementor session, Object obj) {
39 | Serializable result = null;
40 |
41 | // if user has set the id manually, don't change it.
42 | try {
43 | Class> clazz = obj.getClass();
44 | Method idMethod = clazz.getMethod("getId");
45 | result = (Serializable) idMethod.invoke(obj);
46 | } catch (Exception e) {
47 | // do nothing
48 | }
49 |
50 | if (result == null) {
51 | result = java.util.UUID.randomUUID().toString();
52 | }
53 |
54 | return result;
55 | }
56 |
57 | /**
58 | * Currently unused.
59 | *
60 | * @param type hibernate config
61 | * @param params hibernate config
62 | * @param d dialect to use
63 | */
64 | @SuppressWarnings("unused")
65 | public void configure(Type type, Properties params, Dialect d) {
66 | // sep = PropertiesHelper.getString("separator", params, "");
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/hbnpojogen-persistence/src/main/java/com/github/wwadge/hbnpojogen/persistence/impl/HibernateUtils.java:
--------------------------------------------------------------------------------
1 | package com.github.wwadge.hbnpojogen.persistence.impl;
2 |
3 |
4 | import com.github.wwadge.hbnpojogen.persistence.IPojoGenEntity;
5 | import org.hibernate.SessionFactory;
6 | import org.hibernate.proxy.HibernateProxy;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.stereotype.Component;
9 |
10 | /**
11 | * Wrapper for narrowCast function.
12 | *
13 | * @author wallacew
14 | */
15 |
16 | @Component
17 | public class HibernateUtils {
18 |
19 | private static SessionFactory sessionFactory;
20 |
21 | @SuppressWarnings("unchecked")
22 | public static T narrowCast(T obj) {
23 | T result = obj;
24 |
25 | if (obj != null && obj instanceof HibernateProxy) {
26 | result = (T) ((HibernateProxy) obj).getHibernateLazyInitializer().getImplementation();
27 | }
28 |
29 | return result;
30 | }
31 |
32 |
33 | @Autowired
34 | public void setSessionFactory(SessionFactory sf) {
35 | HibernateUtils.sessionFactory = sf;
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/hbnpojogen-persistence/src/main/java/com/github/wwadge/hbnpojogen/persistence/impl/IDPresentAwareGenerator.java:
--------------------------------------------------------------------------------
1 | package com.github.wwadge.hbnpojogen.persistence.impl;
2 |
3 |
4 | import org.hibernate.engine.SessionImplementor;
5 | import org.hibernate.id.IdentityGenerator;
6 |
7 | import java.io.Serializable;
8 | import java.lang.reflect.Method;
9 |
10 |
11 | /**
12 | *
13 | * A custom generator that behaves as follows: If id is null (or zero to workaround a hibernate bug), behave as
14 | * if generator was set to AUTO (i.e. get the autoincrement value from the database. Otherwise use the provided ID.
15 | *
16 | * Mapping parameters supported: none
17 | *
18 | * @author wallacew
19 | */
20 | public class IDPresentAwareGenerator extends IdentityGenerator {
21 |
22 | @Override
23 | public Serializable generate(SessionImplementor session, Object obj) {
24 | Serializable result = null;
25 |
26 | // if user has set the id manually, don't change it.
27 | try {
28 | Class> clazz = obj.getClass();
29 | Method idMethod = clazz.getMethod("getId");
30 | result = (Serializable) idMethod.invoke(obj);
31 | } catch (Exception e) {
32 | // do nothing
33 | }
34 |
35 | // id not set (or set to 0 due to hibernate bug workaround), proceed
36 | // as if generator was set to AUTO
37 | if (result == null || (result.toString().equals("0"))) {
38 |
39 | return super.generate(session, obj);
40 | }
41 |
42 | return result;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/hbnpojogen-persistence/src/main/java/com/github/wwadge/hbnpojogen/persistence/impl/StringValuedEnum.java:
--------------------------------------------------------------------------------
1 | package com.github.wwadge.hbnpojogen.persistence.impl;
2 |
3 | /**
4 | * Utility class designed to allow dynamic fidding and manipulation of Enum
5 | * instances which hold a string value.
6 | *
7 | * @author unknown
8 | */
9 | public interface StringValuedEnum {
10 |
11 | /**
12 | * Current string value stored in the enum.
13 | *
14 | * @return string value.
15 | */
16 | String getValue();
17 |
18 | }
--------------------------------------------------------------------------------
/hbnpojogen-persistence/src/main/java/com/github/wwadge/hbnpojogen/persistence/impl/StringValuedEnumReflect.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Utility class designed to inspect StringValuedEnums.
3 | *
4 | * @author wallacew
5 | */
6 |
7 | package com.github.wwadge.hbnpojogen.persistence.impl;
8 |
9 |
10 | public class StringValuedEnumReflect {
11 |
12 | /**
13 | * Don't let anyone instantiate this class.
14 | * @throws UnsupportedOperationException Always.
15 | */
16 | private StringValuedEnumReflect() {
17 | throw new UnsupportedOperationException("This class must not be instantiated.");
18 | }
19 |
20 | /**
21 | * All Enum constants (instances) declared in the specified class.
22 | * @param enumClass Class to reflect
23 | * @param Type
24 | * @return Array of all declared EnumConstants (instances).
25 | */
26 | @SuppressWarnings("unchecked")
27 | private static T[]
28 | getValues(Class enumClass) {
29 | return enumClass.getEnumConstants();
30 | }
31 |
32 | /**
33 | * All possible string values of the string valued enum.
34 | * @param enumClass Class to reflect.
35 | * @param Type
36 | * @return Available string values.
37 | */
38 | @SuppressWarnings("unchecked")
39 | public static String[]
40 | getStringValues(Class enumClass) {
41 | T[] values = getValues(enumClass);
42 | String[] result = new String[values.length];
43 | for (int i = 0; i < values.length; i++) {
44 | result[i] = values[i].getValue();
45 | }
46 | return result;
47 | }
48 |
49 | /**
50 | * Name of the enum instance which hold the especified string value.
51 | * If value has duplicate enum instances than returns the first occurency.
52 | * @param enumClass Class to inspect.
53 | * @param value String.
54 | * @param Type
55 | * @return name of the enum instance.
56 | */
57 | @SuppressWarnings("unchecked")
58 | public static String
59 | getNameFromValue(Class enumClass, Object value) {
60 | T[] values = getValues(enumClass);
61 | for (int i = 0; i < values.length; i++) {
62 | if (values[i].getValue().toString().equals(value.toString())) {
63 | return values[i].name();
64 | }
65 | }
66 | return "";
67 | }
68 |
69 | }
--------------------------------------------------------------------------------
/hbnpojogen-persistence/src/main/java/com/github/wwadge/hbnpojogen/persistence/validator/Mandatory.java:
--------------------------------------------------------------------------------
1 | package com.github.wwadge.hbnpojogen.persistence.validator;
2 |
3 |
4 | import java.lang.annotation.Documented;
5 | import java.lang.annotation.Inherited;
6 | import java.lang.annotation.Retention;
7 | import java.lang.annotation.Target;
8 |
9 | import static java.lang.annotation.ElementType.FIELD;
10 | import static java.lang.annotation.ElementType.METHOD;
11 | import static java.lang.annotation.RetentionPolicy.RUNTIME;
12 |
13 |
14 | /**
15 | * Designates a property that is required to have a value. It cannot be null or
16 | * empty. The default message is "validator.required" which is assumed to exist
17 | * in the expected resource bundle ValidatorMessages.properties.
18 | *
19 | * @author alexiaa
20 | * @version $Revision: 1$
21 | */
22 | @Documented
23 | @Target({METHOD, FIELD})
24 | @Retention(RUNTIME)
25 | @Inherited
26 | @Deprecated
27 | public @interface Mandatory {
28 | /**
29 | * Default message.
30 | */
31 | String message() default "Property marked as mandatory and cannot be left blank.";
32 | }
33 |
--------------------------------------------------------------------------------
/hbnpojogen-persistence/src/main/java/com/github/wwadge/hbnpojogen/randomlib/data/CountryCodes.java:
--------------------------------------------------------------------------------
1 | package com.github.wwadge.hbnpojogen.randomlib.data;
2 |
3 | /**
4 | * Static country codes data.
5 | *
6 | * @author robertam
7 | */
8 | public class CountryCodes {
9 |
10 |
11 | /**
12 | * Static numeric Data for Country Codes.
13 | */
14 | private static final String[] countryCodesNum = {
15 | "004", "008", "010", "012", "016", "020", "024", "028", "031", "032", "036", "040", "044", "048", "050", "051",
16 | "052", "056", "060", "064", "068", "070", "072", "074", "076", "084", "086", "090", "092", "096", "100", "104",
17 | "108", "112", "116", "120", "124", "132", "136", "140", "144", "148", "152", "156", "158", "162", "166", "170",
18 | "174", "175", "178", "180", "184", "188", "191", "192", "196", "203", "204", "208", "212", "214", "218", "222",
19 | "226", "231", "232", "233", "234", "238", "239", "242", "246", "248", "250", "254", "258", "260", "262", "266",
20 | "268", "270", "275", "276", "288", "292", "296", "300", "304", "308", "312", "316", "320", "324", "328", "332",
21 | "334", "336", "340", "344", "348", "352", "356", "360", "364", "368", "372", "376", "380", "384", "388", "392",
22 | "398", "400", "404", "408", "410", "414", "417", "418", "422", "426", "428", "430", "434", "438", "440", "442",
23 | "446", "450", "454", "458", "462", "466", "470", "474", "478", "480", "484", "492", "496", "498", "500", "504",
24 | "508", "512", "516", "520", "524", "528", "530", "533", "540", "548", "554", "558", "562", "566", "570", "574",
25 | "578", "580", "581", "583", "584", "585", "586", "591", "598", "600", "604", "608", "612", "616", "620", "624",
26 | "626", "630", "634", "638", "642", "643", "646", "654", "659", "660", "662", "666", "670", "674", "678", "682",
27 | "686", "690", "694", "702", "703", "704", "705", "706", "710", "716", "724", "732", "736", "740", "744", "748",
28 | "752", "756", "760", "762", "764", "768", "772", "776", "780", "784", "788", "792", "795", "796", "798", "800",
29 | "804", "807", "818", "826", "830", "833", "834", "840", "850", "854", "858", "860", "862", "876", "882", "887",
30 | "891", "894"
31 | };
32 |
33 |
34 | /**
35 | * Static acronym Data for Country Codes.
36 | */
37 | private static final String[] countryCodesAc = {
38 | "GB", "US", "CA", "MX", "BM", "SE", "IT", "A2", "PR", "IN", "VI", "DE",
39 | "IR", "BO", "NG", "NL", "FR", "IL", "ES", "PA", "CL", "BS", "AR", "DM",
40 | "BE", "IE", "BZ", "BR", "CH", "ZA", "EG", "SC", "TZ", "KM", "RW", "DZ",
41 | "GH", "CI", "SZ", "CM", "MG", "KE", "AO", "NA", "MA", "SL", "GA", "MU",
42 | "TG", "LY", "SN", "SD", "UG", "ZW", "MZ", "MW", "ZM", "BW", "LR", "CD",
43 | "BJ", "TN", "JP", "EU", "AU", "TH", "CN", "MY", "PK", "NZ", "KR", "HK",
44 | "SG", "BD", "ID", "PH", "TW", "AF", "VN", "VU", "NC", "BN", "AP", "GR",
45 | "SA", "PL", "CZ", "RU", "CY", "NO", "AT", "UA", "TJ", "DK", "PT", "TR",
46 | "GE", "BY", "IQ", "AM", "LB", "MD", "BG", "FI", "OM", "LV", "KZ", "EE",
47 | "SK", "JO", "HU", "KW", "AL", "LT", "SM", "RO", "RS", "HR", "LU", "IS",
48 | "LI", "CR", "MK", "MT", "GM", "SI", "FK", "AZ", "MC", "HT", "GU", "JM",
49 | "FM", "EC", "CO", "PE", "KY", "GP", "HN", "YE", "VG", "LC", "SY", "NI",
50 | "DO", "AN", "GT", "VE", "BA", "HM", "UY", "SV", "AE", "TT", "LK", "BV",
51 | "MH", "BH", "CK", "GI", "PY", "AG", "LS", "KN", "WS", "PW", "QA", "KH",
52 | "AI", "AS", "TC", "MP", "UZ", "MO", "UM", "RE", "GY", "CU", "CG", "A1",
53 | "BB", "LA", "SR", "AW", "FJ", "MS", "GD", "VC", "NP", "NE", "KG", "ME",
54 | "TD", "FO", "SO", "ML", "PS", "BI", "GN", "ET", "MR", "MQ", "VA", "TM",
55 | "YT", "BF", "AD", "AQ", "GL", "WF", "PG", "MN", "PF", "MV", "GQ", "CF",
56 | "ER", "GW", "DJ", "CV", "ST", "GF", "SB", "TV", "KI", "TO", "IO", "NU",
57 | "TK", "NR", "BT", "NF", "MM", "KP"
58 | };
59 |
60 | /**
61 | * Return the i'th element of the country codes ac array.
62 | *
63 | * @param i the element to return
64 | * @return Return the i'th element of the country codes ac array
65 | */
66 | public static String getCountryCodesAc(int i) {
67 | return getCountryCode(countryCodesAc, i);
68 | }
69 |
70 | /**
71 | * Return the i'th element of the country codes num array.
72 | *
73 | * @param i the element to return
74 | * @return Return the i'th element of the country codes num array
75 | */
76 | public static String getCountryCodesNum(int i) {
77 | return getCountryCode(countryCodesNum, i);
78 | }
79 |
80 | /**
81 | * Return the length of the country codes num array.
82 | *
83 | * @return the length of the country codes num array
84 | */
85 | public static int getCountryCodesNumLength() {
86 | return getCountryCodeLength(countryCodesNum);
87 | }
88 |
89 | /**
90 | * Return the length of the country codes ac array.
91 | *
92 | * @return the length of the country codes ac array
93 | */
94 | public static int getCountryCodesAcLength() {
95 | return getCountryCodeLength(countryCodesAc);
96 | }
97 |
98 | /**
99 | * Return a country code.
100 | *
101 | * @param i index
102 | * @param ar array to use
103 | * @return a valid country code
104 | */
105 | public static String getCountryCode(String[] ar, int i) {
106 | if (i > ar.length) {
107 | throw new IndexOutOfBoundsException();
108 | }
109 | return ar[i];
110 | }
111 |
112 | /**
113 | * Return the length of the array.
114 | *
115 | * @param ar array to use
116 | * @return max no of country code array length
117 | */
118 | public static int getCountryCodeLength(String[] ar) {
119 | return ar.length;
120 | }
121 | }
122 |
--------------------------------------------------------------------------------
/hbnpojogen-persistence/src/main/java/com/github/wwadge/hbnpojogen/randomlib/data/dataGeneration/ComplexDataTypes.java:
--------------------------------------------------------------------------------
1 | package com.github.wwadge.hbnpojogen.randomlib.data.dataGeneration;
2 |
3 | import com.github.wwadge.hbnpojogen.randomlib.data.CountryCodes;
4 |
5 | import java.util.Locale;
6 | import java.util.Random;
7 |
8 |
9 | /**
10 | * A class providing methods for generating random data for complex data types.
11 | *
12 | * @author robertam
13 | */
14 | public class ComplexDataTypes {
15 |
16 | /**
17 | * Random Number Generator.
18 | */
19 | public final static Random generator = new Random();
20 |
21 | /**
22 | * Generates a random ID for Client or Provider.
23 | *
24 | * @return A random ID for Client or Provider
25 | */
26 | public static Long generateID() {
27 | long id = generator.nextLong();
28 |
29 | while (id <= 0) {
30 | id = generator.nextLong();
31 | }
32 |
33 | return id;
34 | }
35 |
36 | /**
37 | * Generates a random IP.
38 | *
39 | * @return an Ip Address
40 | */
41 | public static String generateRandomIp() {
42 | String ip = generator.nextInt(254) + "." + generator.nextInt(254) + "." + generator.nextInt(254) + "." + generator.nextInt(254);
43 | return ip;
44 | }
45 |
46 | /**
47 | * Generates a random URL.
48 | *
49 | * @return a random url
50 | */
51 | public static String generateRandomUrl() {
52 | StringBuffer sb = new StringBuffer();
53 | sb.append("http://www.");
54 | sb.append(BasicDataGenerator.generateRandomStringChar(10).toLowerCase(Locale.getDefault()));
55 | sb.append(".com");
56 | return sb.toString();
57 | }
58 |
59 | /**
60 | * Returns a random country code in the form of 3 digits.
61 | *
62 | * @return 3-digit country code
63 | */
64 | public static Integer generateNumericCountryCode() {
65 | int choice = generator.nextInt(CountryCodes.getCountryCodesNumLength());
66 | return Integer.parseInt(CountryCodes.getCountryCodesNum(choice));
67 | }
68 |
69 | /**
70 | * Returns a random country code in the form of 2 letters.
71 | *
72 | * @return 2-letter country code
73 | */
74 | public static String generateCountryCode() {
75 | int choice = generator.nextInt(CountryCodes.getCountryCodesAcLength());
76 | return CountryCodes.getCountryCodesAc(choice);
77 | }
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/inflector/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/inflector/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | inflector
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.openarchitectureware.xsd.plugin.xsdBuilder
15 |
16 |
17 |
18 |
19 | org.maven.ide.eclipse.maven2Builder
20 |
21 |
22 |
23 |
24 |
25 | org.maven.ide.eclipse.maven2Nature
26 | org.eclipse.jdt.core.javanature
27 |
28 |
29 |
--------------------------------------------------------------------------------
/inflector/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | #Thu Jun 18 11:31:09 CEST 2009
2 | eclipse.preferences.version=1
3 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
5 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6 | org.eclipse.jdt.core.compiler.compliance=1.5
7 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
9 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12 | org.eclipse.jdt.core.compiler.source=1.5
13 |
--------------------------------------------------------------------------------
/inflector/.settings/org.maven.ide.eclipse.prefs:
--------------------------------------------------------------------------------
1 | #Thu Jun 18 11:27:55 CEST 2009
2 | activeProfiles=
3 | eclipse.preferences.version=1
4 | fullBuildGoals=process-test-resources
5 | includeModules=false
6 | resolveWorkspaceProjects=true
7 | resourceFilterGoals=process-resources resources\:testResources
8 | skipCompilerPlugin=true
9 | version=1
10 |
--------------------------------------------------------------------------------
/inflector/bin/org/jvnet/inflector/package.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Classes for inflecting natural language words, especially for forming plurals.
4 |
5 |
--------------------------------------------------------------------------------
/inflector/bin/org/jvnet/inflector/rule/package.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Classes for specifying inflections using rules.
4 |
5 |
--------------------------------------------------------------------------------
/inflector/bin/overview.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Inflector - User Guide
4 |
5 |
6 |
7 |
8 |
9 |
10 | Inflector provides an API for forming the plurals of nouns. Here's a simple example:
11 |
12 |
13 | System.out.println(Noun.pluralOf("loaf"));
14 |
15 |
16 | This will print
17 |
18 |
19 | loaves
20 |
21 |
22 | to the console.
23 |
24 |
25 |
26 | It's handy to use Java 5 static imports to make this more readable,
27 | particularly if you are forming lots of plurals.
28 |
29 |
30 | import static org.jvnet.inflector.Noun.pluralOf;
31 | ...
32 | System.out.println(pluralOf("loaf"));
33 |
34 |
35 | Nouns
36 |
37 |
38 | It is often useful to use a {@link java.util.Formatter}. For example,
39 |
40 |
41 | System.out.printf("I bought 10 %s.", pluralOf("loaf"));
42 |
43 |
44 |
45 | prints
46 |
47 |
48 | I bought 10 loaves.
49 |
50 |
51 | Of course, you can parameterize the number, as follows.
52 |
53 |
54 | int n = 10;
55 | System.out.printf("I bought %d %s.", n, pluralOf("loaf", n));
56 |
57 |
58 |
59 | Notice that you pass the number into the pluralOf
method to make sure the noun agrees.
60 | For example, if n = 1
in the previous example, it would correctly print
61 |
62 |
63 | I bought 1 loaf.
64 |
65 | Internationalization
66 |
67 |
68 | The examples so far use the default locale to determine the pluralization algorithm to use. You can explictly set
69 | the locale to use by passing another argument:
70 |
71 |
72 | System.out.println(pluralOf("pagnotta", Locale.ITALIAN));
73 |
74 |
75 |
76 | prints
77 |
78 |
79 | pagnotte
80 |
81 | Custom Pluralization
82 |
83 |
84 | The pluralization algorithms that come with Inflector may not meet your needs. Perhaps there is no algorithm for your language,
85 | or perhaps the rules don't fit the usage that is appropriate for your application. The English
86 | pluralizer, for example, is fairly modern: it does not support many classical rules that are rare nowadays.
87 |
88 |
89 |
90 | In this case it is straightforward to write your own pluralizer, or simply extend another one to override some of its rules.
91 | The follow code overrides the plural for box to boxen by using a {@link org.jvnet.inflector.rule.RegexReplacementRule}:
92 |
93 |
94 | List<Rule> customRules = new ArrayList<Rule>();
95 | customRules.add(new RegexReplacementRule("(?i)(.*)box", "$1boxen"));
96 | Pluralizer customPluralizer = new RuleBasedPluralizer(customRules, Locale.ENGLISH, Noun.pluralizer(Locale.ENGLISH));
97 | System.out.println(pluralOf("box"));
98 | System.out.println(pluralOf("box", customPluralizer));
99 | System.out.println(pluralOf("chatterbox", customPluralizer));
100 |
101 |
102 |
103 | This prints
104 |
105 |
106 | boxes
107 | boxen
108 | chatterboxen
109 |
110 |
111 | If you are interested in writing more complex rules, there are many other implementations of
112 | {@link org.jvnet.inflector.Rule} in the {@link org.jvnet.inflector.rule} package.
113 |
114 |
115 |
116 |
117 |
118 |
--------------------------------------------------------------------------------
/inflector/build.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 | ================================= WARNING ================================
105 | Junit isn't present in your ${ANT_HOME}/lib directory. Tests not executed.
106 | ==========================================================================
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
--------------------------------------------------------------------------------
/inflector/maven.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/inflector/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | Inflector
5 | jar
6 | net.java.dev.inflector
7 | inflector
8 | 0.7.0-patched
9 |
10 |
11 | Tom White
12 | http://www.tiling.org/
13 |
14 |
15 | 2006
16 |
17 |
18 | Inflector is a Java API for forming plurals of
19 | words. The goal of the project is to provide a high-quality,
20 | easy-to-use API that is localizable to support any language.
21 |
22 |
23 |
24 |
25 |
26 | org.easymock
27 | easymock
28 | 2.4
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 | maven-compiler-plugin
37 | 2.0.1
38 |
39 | 1.6
40 | 1.6
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/inflector/project.properties:
--------------------------------------------------------------------------------
1 | maven.javanet.project=inflector
2 |
3 | maven.changelog.factory=org.apache.maven.svnlib.SvnChangeLogFactory
4 |
5 | maven.javadoc.excludepackagenames=org.jvnet.inflector.lang.*
6 | maven.javadoc.links=http://java.sun.com/j2se/1.5.0/docs/api/
7 | maven.javadoc.overview=src/main/java/overview.html
--------------------------------------------------------------------------------
/inflector/src/main/java/org/jvnet/inflector/Noun.java:
--------------------------------------------------------------------------------
1 | package org.jvnet.inflector;
2 | import java.io.Serializable;
3 | import java.util.Locale;
4 |
5 | /**
6 | *
7 | * A Noun
represents the grammatical part of speech that refers to
8 | * a person, place, thing, event, substance, quality or idea.
9 | *
10 | *
11 | * This class contains a number of static convenience methods ({@link #pluralOf(String)} for forming plurals.
12 | *
13 | * @author Tom White
14 | */
15 | public class Noun implements Serializable{
16 |
17 | /**
18 | *
19 | * Creates a new {@link Pluralizer} instance for the default locale.
20 | *
21 | * @return a pluralizer instance for the default locale
22 | */
23 | public static Pluralizer pluralizer() {
24 | return pluralizer(Locale.getDefault());
25 | }
26 |
27 | /**
28 | *
29 | * Creates a new {@link Pluralizer} instance for the specified locale.
30 | *
31 | * @param locale the locale specifying the language of the pluralizer
32 | * @return a pluralizer instance for the specified locale, or null
if there is none for this locale
33 | */
34 | public static Pluralizer pluralizer(Locale locale) {
35 | String className = "org.jvnet.inflector.lang." + locale.getLanguage() + ".NounPluralizer";
36 | try {
37 | Class> c = Class.forName(className);
38 | return (Pluralizer) c.newInstance();
39 | } catch (ClassNotFoundException e) {
40 | return null;
41 | } catch (InstantiationException e) {
42 | throw new RuntimeException("Problem instantiating " + className, e);
43 | } catch (IllegalAccessException e) {
44 | throw new RuntimeException("Problem instantiating " + className, e);
45 | }
46 | }
47 |
48 | /**
49 | *
50 | * Converts a noun to its plural form using the {@link Pluralizer} for the default locale.
51 | *
52 | *
53 | * The return value is not defined if this method is passed a plural form.
54 | *
55 | * @param word a singular form
56 | * @return the plural form
57 | */
58 | public static String pluralOf(String word) {
59 | return pluralOf(word, pluralizer());
60 | }
61 |
62 | /**
63 | *
64 | * Converts a noun to its plural form for the given number of instances
65 | * using the {@link Pluralizer} for the default locale.
66 | *
67 | *
68 | * The return value is not defined if this method is passed a plural form.
69 | *
70 | * @param word a singular form
71 | * @param number the number of objects being referred to in the plural
72 | * @return the plural form
73 | */
74 | public static String pluralOf(String word, int number) {
75 | return pluralOf(word, number, pluralizer());
76 | }
77 |
78 | /**
79 | *
80 | * Converts a noun to its plural form using the {@link Pluralizer} for the given locale.
81 | *
82 | *
83 | * The return value is not defined if this method is passed a plural form.
84 | *
85 | * @param word a singular form
86 | * @param locale the locale specifying the language of the pluralizer
87 | * @return the plural form
88 | */
89 | public static String pluralOf(String word, Locale locale) {
90 | return pluralOf(word, pluralizer(locale));
91 | }
92 |
93 | /**
94 | *
95 | * Converts a noun to its plural form for the given number of instances
96 | * using the {@link Pluralizer} for the given locale.
97 | *
98 | *
99 | * The return value is not defined if this method is passed a plural form.
100 | *
101 | * @param word a singular form
102 | * @param number the number of objects being referred to in the plural
103 | * @param locale the locale specifying the language of the pluralizer
104 | * @return the plural form
105 | */
106 | public static String pluralOf(String word, int number, Locale locale) {
107 | return pluralOf(word, number, pluralizer(locale));
108 | }
109 |
110 | /**
111 | *
112 | * Converts a noun to its plural form using the given {@link Pluralizer}.
113 | *
114 | *
115 | * The return value is not defined if this method is passed a plural form.
116 | *
117 | * @param word a singular form
118 | * @param pluralizer a noun pluralizer
119 | * @return the plural form
120 | */
121 | public static String pluralOf(String word, Pluralizer pluralizer) {
122 | return pluralizer == null ? null : pluralizer.pluralize(word);
123 | }
124 |
125 | /**
126 | *
127 | * Converts a noun to its plural form for the given number of instances
128 | * using the given {@link Pluralizer}.
129 | *
130 | *
131 | * The return value is not defined if this method is passed a plural form.
132 | *
133 | * @param word a singular form
134 | * @param number the number of objects being referred to in the plural
135 | * @param pluralizer a noun pluralizer
136 | * @return the plural form
137 | */
138 | public static String pluralOf(String word, int number, Pluralizer pluralizer) {
139 | return pluralizer == null ? null : pluralizer.pluralize(word, number);
140 | }
141 |
142 | }
143 |
--------------------------------------------------------------------------------
/inflector/src/main/java/org/jvnet/inflector/Pluralizer.java:
--------------------------------------------------------------------------------
1 | package org.jvnet.inflector;
2 |
3 | /**
4 | *
5 | * Pluralizer
converts singular word forms to their plural forms.
6 | * Methods that are passed null
must throw
7 | * a {@link java.lang.NullPointerException}.
8 | *
9 | *
10 | * Implementations of this interface must be safe for use by multiple concurrent threads.
11 | *
12 | * @author Tom White
13 | */
14 | public interface Pluralizer {
15 |
16 | /**
17 | *
18 | * Converts a word to its plural form.
19 | *
20 | *
21 | * The return value is not defined if this method is passed a plural form.
22 | *
23 | * @param word a singular form
24 | * @return the plural form
25 | */
26 | public String pluralize(String word);
27 |
28 | /**
29 | *
30 | * Converts a word to its plural form for the given number of instances.
31 | * Some languages (such as Polish)
32 | * have different plural forms depending on the number
33 | * of things being referred to.
34 | *
35 | *
36 | * The return value is not defined if this method is passed a plural form.
37 | *
38 | * @param word a singular form
39 | * @param number the number of objects being referred to in the plural
40 | * @return the plural form
41 | */
42 | public String pluralize(String word, int number);
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/inflector/src/main/java/org/jvnet/inflector/Rule.java:
--------------------------------------------------------------------------------
1 | package org.jvnet.inflector;
2 |
3 | /**
4 | *
5 | * A Rule
represents how a word changes from one form to another.
6 | *
7 | *
8 | * Implementations of this interface must be safe for use by multiple concurrent threads in order to
9 | * satisfy the contract of {@link Pluralizer}.
10 | *
11 | * @author Tom White
12 | */
13 | public interface Rule {
14 | /**
15 | *
16 | * Tests to see if this rule applies for the given word.
17 | *
18 | * @param word the word that is being tested
19 | * @return true
if this rule should be applied, false
otherwise
20 | */
21 | public boolean applies(String word);
22 |
23 | /**
24 | *
25 | * Applies this rule to the word, and transforming it into a new form.
26 | *
27 | * @param word the word to apply this rule to
28 | * @return the transformed word
29 | */
30 | public String apply(String word);
31 | }
32 |
--------------------------------------------------------------------------------
/inflector/src/main/java/org/jvnet/inflector/RuleBasedPluralizer.java:
--------------------------------------------------------------------------------
1 | package org.jvnet.inflector;
2 |
3 | import java.io.Serializable;
4 | import java.util.Collections;
5 | import java.util.List;
6 | import java.util.Locale;
7 | import java.util.regex.Matcher;
8 | import java.util.regex.Pattern;
9 |
10 | /**
11 | *
12 | * An implementation of {@link Pluralizer} that is implemented using an ordered list of {@link Rule}s.
13 | * It is possible to specify a fallback {@link Pluralizer} that is invoked if none of the rules match.
14 | * This makes it easy to override some rules of another {@link Pluralizer}.
15 | *
16 | *
17 | * This class also preserves leading and trailing whitespace, so individual rules don't need to
18 | * explicitly handle it.
19 | * Case is also preserved; that is, the output of all uppercase input is automatically uppercased, and
20 | * the output of titlecase input is automatically titlecased.
21 | * This means rules can act in a case-insensitive manner.
22 | *
23 | *
24 | * Instances of this class are safe for multiple concurrent threads.
25 | *
26 | * @author Tom White
27 | */
28 | public class RuleBasedPluralizer implements Pluralizer, Serializable{
29 |
30 | /** */
31 | private static final long serialVersionUID = 7466676432551660465L;
32 |
33 | static class IdentityPluralizer implements Pluralizer, Serializable {
34 | /** */
35 | private static final long serialVersionUID = -794888679795568207L;
36 | public String pluralize(String word) {
37 | return word;
38 | }
39 | public String pluralize(String word, int number) {
40 | return word;
41 | }
42 | }
43 |
44 | private static final Pluralizer IDENTITY_PLURALIZER = new IdentityPluralizer();
45 |
46 | private List rules;
47 | private Locale locale;
48 | private Pluralizer fallbackPluralizer;
49 |
50 | /**
51 | *
52 | * Constructs a pluralizer with an empty list of rules.
53 | * Use the setters to configure.
54 | *
55 | */
56 | @SuppressWarnings("unchecked")
57 | public RuleBasedPluralizer() {
58 | this(Collections.EMPTY_LIST, Locale.getDefault());
59 | }
60 |
61 | /**
62 | *
63 | * Constructs a pluralizer that uses a list of rules then an identity {@link Pluralizer}
64 | * if none of the rules match.
65 | * This is useful to build your own {@link Pluralizer} from scratch.
66 | *
67 | * @param rules the rules to apply in order
68 | * @param locale the locale specifying the language of the pluralizer
69 | */
70 | public RuleBasedPluralizer(List rules, Locale locale) {
71 | this(rules, locale, IDENTITY_PLURALIZER);
72 | }
73 |
74 | /**
75 | *
76 | * Constructs a pluralizer that uses first a list of rules then a fallback {@link Pluralizer}.
77 | * This is useful to override the behaviour of an existing {@link Pluralizer}.
78 | *
79 | * @param rules the rules to apply in order
80 | * @param locale the locale specifying the language of the pluralizer
81 | * @param fallbackPluralizer the pluralizer to use if no rules match
82 | */
83 | public RuleBasedPluralizer(List rules, Locale locale, Pluralizer fallbackPluralizer) {
84 | this.rules = rules;
85 | this.locale = locale;
86 | this.fallbackPluralizer = fallbackPluralizer;
87 | }
88 |
89 | public Pluralizer getFallbackPluralizer() {
90 | return fallbackPluralizer;
91 | }
92 |
93 | public void setFallbackPluralizer(Pluralizer fallbackPluralizer) {
94 | this.fallbackPluralizer = fallbackPluralizer;
95 | }
96 |
97 | public Locale getLocale() {
98 | return locale;
99 | }
100 |
101 | public void setLocale(Locale locale) {
102 | this.locale = locale;
103 | }
104 |
105 | public List getRules() {
106 | return rules;
107 | }
108 |
109 | public void setRules(List rules) {
110 | this.rules = rules;
111 | }
112 |
113 | /**
114 | *
115 | * Converts a noun or pronoun to its plural form.
116 | *
117 | *
118 | * This method is equivalent to calling pluralize(word, 2)
.
119 | *
120 | *
121 | * The return value is not defined if this method is passed a plural form.
122 | *
123 | * @param word a singular noun
124 | * @return the plural form of the noun
125 | */
126 | public String pluralize(String word) {
127 | return pluralize(word, 2);
128 | }
129 |
130 | /**
131 | *
132 | * Converts a noun or pronoun to its plural form for the given number of instances.
133 | * If number
is 1, word
is returned unchanged.
134 | *
135 | *
136 | * The return value is not defined if this method is passed a plural form.
137 | *
138 | * @param word a singular noun
139 | * @param number the number of objects being referred to in the plural
140 | * @return the plural form of the noun
141 | */
142 | public String pluralize(String word, int number) {
143 | if (number == 1) {
144 | return word;
145 | }
146 |
147 | Pattern pattern = Pattern.compile("\\A(\\s*)(.+?)(\\s*)\\Z");
148 | Matcher matcher = pattern.matcher(word);
149 | if (matcher.matches()) {
150 | String pre = matcher.group(1);
151 | String trimmedWord = matcher.group(2);
152 | String post = matcher.group(3);
153 | String plural = pluralizeInternal(trimmedWord);
154 | if (plural == null) {
155 | return fallbackPluralizer.pluralize(word, number);
156 | }
157 | return pre + postProcess(trimmedWord, plural) + post;
158 | }
159 | return word;
160 |
161 | }
162 |
163 | /**
164 | *
165 | * Goes through the rules in turn until a match is found at which point the rule is applied
166 | * and the result returned.
167 | * If no rule matches, returns null
.
168 | *
169 | * @param word a singular noun
170 | * @return the plural form of the noun, or null
if no rule matches
171 | */
172 | protected String pluralizeInternal(String word) {
173 | for (Rule rule : rules) {
174 | if (rule.applies(word)) {
175 | return rule.apply(word);
176 | }
177 | }
178 | return null;
179 | }
180 |
181 | /**
182 | *
183 | * Apply processing to pluralizedWord
. This implementation ensures
184 | * the case of the plural is consistent with the case of the input word.
185 | *
186 | *
187 | * If trimmedWord
is all uppercase, then pluralizedWord
188 | * is uppercased.
189 | * If trimmedWord
is titlecase, then pluralizedWord
190 | * is titlecased.
191 | *
192 | * @param trimmedWord the input word, with leading and trailing whitespace removed
193 | * @param pluralizedWord the pluralized word
194 | * @return the pluralizedWord
after processing
195 | */
196 | protected String postProcess(String trimmedWord, String pluralizedWord) {
197 | if (trimmedWord.matches("^\\p{Lu}+$")) {
198 | return pluralizedWord.toUpperCase(locale);
199 | } else if (trimmedWord.matches("^\\p{Lu}.*")) {
200 | return pluralizedWord.substring(0, 1).toUpperCase(locale) + pluralizedWord.substring(1);
201 | }
202 | return pluralizedWord;
203 | }
204 |
205 | }
206 |
--------------------------------------------------------------------------------
/inflector/src/main/java/org/jvnet/inflector/lang/it/NounPluralizer.java:
--------------------------------------------------------------------------------
1 | package org.jvnet.inflector.lang.it;
2 |
3 | import static org.jvnet.inflector.rule.AbstractRegexReplacementRule.disjunction;
4 | import static org.jvnet.inflector.rule.IrregularMappingRule.toMap;
5 |
6 | import java.io.Serializable;
7 | import java.util.Arrays;
8 | import java.util.List;
9 | import java.util.Locale;
10 | import java.util.Map;
11 |
12 | import org.jvnet.inflector.Pluralizer;
13 | import org.jvnet.inflector.Rule;
14 | import org.jvnet.inflector.RuleBasedPluralizer;
15 | import org.jvnet.inflector.rule.CategoryInflectionRule;
16 | import org.jvnet.inflector.rule.IrregularMappingRule;
17 | import org.jvnet.inflector.rule.RegexReplacementRule;
18 | import org.jvnet.inflector.rule.SuffixInflectionRule;
19 |
20 | /**
21 | *
22 | * A {@link Pluralizer} for Italian nouns.
23 | *
24 | *
25 | * Instances of this class are safe for multiple concurrent threads.
26 | *
27 | *
28 | * This code is based on the rules for plurals described in
29 | * BBC Italian Grammar,
30 | * by Alwena Lamping.
31 | *
32 | *
33 | * This is not a full implementation since most exceptional categories (e.g. irregular nouns,
34 | * words ending -co which change to -ci, etc.) are very incomplete.
35 | * Also, pronouns are not supported.
36 | *
37 | * @author Tom White
38 | */
39 | public class NounPluralizer extends RuleBasedPluralizer implements Serializable {
40 |
41 | /** */
42 | private static final long serialVersionUID = 4260156287305105361L;
43 |
44 | private static final Map IRREGULAR_NOUNS = toMap(new String[][]{
45 | { "moglie", "mogli" },
46 | { "uovo", "uova" },
47 | { "lenzuolo", "lenzuola" },
48 | { "paio", "paia" },
49 | { "braccio", "braccia" },
50 | { "dito", "dita" },
51 | { "centinaio", "centinaia" },
52 | { "uomo", "uomini" },
53 | { "dio", "dei" },
54 | { "collega", "colleghi" }, // assume male form
55 | { "atleta", "atleti" }, // assume male form
56 | });
57 |
58 | private static final String[] CATEGORY_UNINFLECTED_NOUNS = {
59 | "radio", "foto", "moto", // feminine word ending in -o
60 | "computer", "chef", "hostess", // loan words
61 | };
62 |
63 | private static final String[] CATEGORY_MA_MA_RULE = {
64 | // words ending -ma which are uninflected
65 | "cinema", "clima"
66 | };
67 |
68 | private static final String[] CATEGORY_MA_ME_RULE = {
69 | // words ending -ma which change to -me (not sure this is a category)
70 | "vittima"
71 | };
72 |
73 | private static final String[] CATEGORY_IO_II_RULE = {
74 | // words ending -io where the i is stressed
75 | "zio",
76 | };
77 |
78 | private static final String[] CATEGORY_CO_CI_RULE = {
79 | // words ending -co preceded by a vowel
80 | "amico",
81 | };
82 |
83 | private static final String[] CATEGORY_GO_GI_RULE = {
84 | // words ending -go preceded by a vowel
85 | "asparago",
86 | };
87 |
88 | private static final String[] CATEGORY_CIA_CIE_RULE = {
89 | // words ending -cia preceded by a vowel or where the i is stressed
90 | "farmacia",
91 | };
92 |
93 | private static final String[] CATEGORY_GIA_GIE_RULE = {
94 | // words ending -gia preceded by a vowel or where the i is stressed
95 | "valigia",
96 | };
97 |
98 | private final List rules = Arrays.asList(new Rule[] {
99 |
100 | // Blank word
101 | new RegexReplacementRule("^(\\s)$", "$1"),
102 |
103 | // Irregular nouns
104 | new IrregularMappingRule(IRREGULAR_NOUNS, "(?i)" + disjunction(IRREGULAR_NOUNS.keySet()) + "$"),
105 |
106 | // Nouns ending in -ista (referring to people)
107 | new SuffixInflectionRule("-ista", "-isti"), // assume male form
108 |
109 | // Irregular nouns that do not inflect in the plural
110 | new CategoryInflectionRule(CATEGORY_UNINFLECTED_NOUNS, "-", "-"),
111 | new SuffixInflectionRule("-[\u00e0|\u00e8|\u00ec|\u00f9]", "-", "-"),
112 | new SuffixInflectionRule("-ie?", "-", "-"),
113 |
114 | // Irregular masculine nouns ending in -ma
115 | new CategoryInflectionRule(CATEGORY_MA_MA_RULE, "-ma", "-ma"),
116 | new CategoryInflectionRule(CATEGORY_MA_ME_RULE, "-ma", "-me"),
117 | new SuffixInflectionRule("-ma", "-mi"),
118 |
119 | // Regular nouns ending in -o
120 |
121 | new CategoryInflectionRule(CATEGORY_IO_II_RULE, "-io", "-ii"),
122 | new SuffixInflectionRule("-io", "-o", "-"),
123 |
124 | new CategoryInflectionRule(CATEGORY_CO_CI_RULE, "-co", "-ci"),
125 | new SuffixInflectionRule("-co", "-chi"),
126 |
127 | new CategoryInflectionRule(CATEGORY_GO_GI_RULE, "-go", "-gi"),
128 | new SuffixInflectionRule("-go", "-ghi"),
129 |
130 | new SuffixInflectionRule("-o", "-i"),
131 |
132 | // Regular nouns ending in -a
133 |
134 | new SuffixInflectionRule("-ca", "-che"),
135 |
136 | new SuffixInflectionRule("-ga", "-ghe"),
137 |
138 | new CategoryInflectionRule(CATEGORY_CIA_CIE_RULE, "-cia", "-cie"),
139 | new SuffixInflectionRule("-cia", "-ce"),
140 |
141 | new CategoryInflectionRule(CATEGORY_GIA_GIE_RULE, "-gia", "-gie"),
142 | new SuffixInflectionRule("-gia", "-ge"),
143 |
144 | new SuffixInflectionRule("-a", "-e"),
145 |
146 | // Regular nouns ending in -e
147 |
148 | new SuffixInflectionRule("-e", "-i"),
149 |
150 | // Don't inflect by default
151 | new SuffixInflectionRule("-", "-"),
152 | });
153 |
154 | /**
155 | * Default constructor.
156 | */
157 | public NounPluralizer() {
158 | setRules(rules);
159 | setLocale(Locale.ITALIAN);
160 | }
161 |
162 |
163 | }
164 |
--------------------------------------------------------------------------------
/inflector/src/main/java/org/jvnet/inflector/package.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Classes for inflecting natural language words, especially for forming plurals.
4 |
5 |
--------------------------------------------------------------------------------
/inflector/src/main/java/org/jvnet/inflector/rule/AbstractRegexReplacementRule.java:
--------------------------------------------------------------------------------
1 | package org.jvnet.inflector.rule;
2 |
3 | import java.io.Serializable;
4 | import java.util.Set;
5 | import java.util.regex.Matcher;
6 | import java.util.regex.Pattern;
7 |
8 | import org.jvnet.inflector.Rule;
9 |
10 | /**
11 | *
12 | * An abstract rule specified using a regular expression and replacement. Subclasses
13 | * must implement {@link #replace} to perform the actual replacement.
14 | *
15 | * @author Tom White
16 | */
17 | public abstract class AbstractRegexReplacementRule implements Rule, Serializable {
18 |
19 | /** */
20 | private static final long serialVersionUID = -3287025981231837237L;
21 | private final Pattern pattern;
22 |
23 | /**
24 | *
25 | * Construct a rule using the given regular expression.
26 | *
27 | * @param regex the regular expression used to match words. Match information
28 | * is available to subclasses in the {@link #replace} method.
29 | */
30 | public AbstractRegexReplacementRule(String regex) {
31 | this.pattern = Pattern.compile(regex);
32 | }
33 |
34 | public boolean applies(String word) {
35 | return pattern.matcher(word).matches();
36 | }
37 |
38 | public String apply(String word) {
39 | Matcher matcher = pattern.matcher(word);
40 | if (!matcher.matches()) {
41 | throw new IllegalArgumentException("Word '" + word + "' does not match regex: " + pattern.pattern());
42 | }
43 | return replace(matcher);
44 | }
45 |
46 | /**
47 | *
48 | * Use the state in the given {@link Matcher} to perform a replacement.
49 | *
50 | * @param matcher the matcher used to match the word
51 | * @return the transformed word
52 | */
53 | public abstract String replace(Matcher matcher);
54 |
55 | /**
56 | *
57 | * Form the disjunction of the given regular expression patterns.
58 | * For example if patterns contains "a" and "b" then the disjunction is "(a|b)",
59 | * that is, "a or b".
60 | *
61 | * @param patterns an array of regular expression patterns
62 | * @return a pattern that matches if any of the input patterns match
63 | */
64 | public static String disjunction(String[] patterns) {
65 | String regex = "";
66 | for (int i = 0; i < patterns.length; i++) {
67 | regex += patterns[i];
68 | if (i < patterns.length - 1) {
69 | regex += "|";
70 | }
71 | }
72 | return "(?:" + regex + ")";
73 | }
74 |
75 | /**
76 | *
77 | * Form the disjunction of the given regular expression patterns.
78 | * For example if patterns contains "a" and "b" then the disjunction is "(a|b)",
79 | * that is, "a or b".
80 | *
81 | * @param patterns a set of regular expression patterns
82 | * @return a pattern that matches if any of the input patterns match
83 | */
84 | public static String disjunction(Set patterns) {
85 | return disjunction(patterns.toArray(new String[0]));
86 | }
87 |
88 | }
89 |
--------------------------------------------------------------------------------
/inflector/src/main/java/org/jvnet/inflector/rule/CategoryInflectionRule.java:
--------------------------------------------------------------------------------
1 | package org.jvnet.inflector.rule;
2 |
3 | import java.io.Serializable;
4 |
5 | /**
6 | *
7 | * A rule for specifying an inflection using suffixes that only applies to a
8 | * subset of words with those suffixes (a category).
9 | *
10 | * @author Tom White
11 | */
12 | public class CategoryInflectionRule extends SuffixInflectionRule implements Serializable {
13 |
14 | /** */
15 | private static final long serialVersionUID = 8202369008967739022L;
16 | private final String regex;
17 |
18 | /**
19 | *
20 | * Construct a rule for words
with suffix singularSuffix
which
21 | * becomes pluralSuffix
in the plural.
22 | *
23 | * @param words the set of words that define this category
24 | * @param singularSuffix the singular suffix, starting with a "-" character
25 | * @param pluralSuffix the plural suffix, starting with a "-" character
26 | */
27 | public CategoryInflectionRule(String[] words, String singularSuffix, String pluralSuffix) {
28 | super(singularSuffix, pluralSuffix);
29 | this.regex = "(?i)" + AbstractRegexReplacementRule.disjunction(words);
30 | }
31 |
32 | @Override
33 | public boolean applies(String word) {
34 | return word.matches(regex);
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/inflector/src/main/java/org/jvnet/inflector/rule/IrregularMappingRule.java:
--------------------------------------------------------------------------------
1 | package org.jvnet.inflector.rule;
2 | import java.io.Serializable;
3 | import java.util.HashMap;
4 | import java.util.Map;
5 | import java.util.regex.Matcher;
6 |
7 | /**
8 | *
9 | * A rule for specifying an irregular inflection using a combination of a map of
10 | * singular to plural forms and a regular expression replacement. Subclasses
11 | * cn implement {@link #replace} to perform the actual replacement, which by default
12 | * uses the map to substitute a plural form for the corresponding singular form found in group 0
13 | * of the regular expression match.
14 | *
15 | * @author Tom White
16 | */
17 | public class IrregularMappingRule extends AbstractRegexReplacementRule implements Serializable{
18 |
19 | /** */
20 | private static final long serialVersionUID = 9069582525363046599L;
21 | protected final Map mappings;
22 |
23 | /**
24 | *
25 | * Construct a rule using the given regular expression and irregular forms map.
26 | *
27 | * @param wordMappings the map of singular to plural forms
28 | * @param regex the regular expression used to match words. Match information
29 | * is available to subclasses in the {@link #replace} method.
30 | */
31 | public IrregularMappingRule(Map wordMappings, String regex) {
32 | super(regex);
33 | this.mappings = wordMappings;
34 | }
35 |
36 | @Override
37 | public String replace(Matcher m) {
38 | return mappings.get(m.group(0).toLowerCase());
39 | }
40 |
41 | /**
42 | *
43 | * Turn the array of String array mapping pairs into a map.
44 | *
45 | * @param wordMappings
46 | * @return a map of singular to plural forms
47 | */
48 | public static Map toMap(String[][] wordMappings) {
49 | Map mappings = new HashMap();
50 | for (int i = 0; i < wordMappings.length; i++) {
51 | String singular = wordMappings[i][0];
52 | String plural = wordMappings[i][1];
53 | mappings.put(singular, plural);
54 | }
55 | return mappings;
56 | }
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/inflector/src/main/java/org/jvnet/inflector/rule/RegexReplacementRule.java:
--------------------------------------------------------------------------------
1 | package org.jvnet.inflector.rule;
2 | import java.io.Serializable;
3 | import java.util.regex.Matcher;
4 |
5 | /**
6 | *
7 | * A rule specified using a regular expression and a replacement string.
8 | *
9 | * @author Tom White
10 | */
11 | public class RegexReplacementRule extends AbstractRegexReplacementRule implements Serializable {
12 |
13 | /** */
14 | private static final long serialVersionUID = -2153299891328152681L;
15 | private final String replacement;
16 |
17 | /**
18 | * Construct a rule using the given regular expression and replacement string.
19 | * @param regex the regular expression used to match words
20 | * @param replacement the string to use during replacement.
21 | * The replacement string may contain references to subsequences captured matching.
22 | * See {@link Matcher#appendReplacement}.
23 | */
24 | public RegexReplacementRule(String regex, String replacement) {
25 | super(regex);
26 | this.replacement = replacement;
27 | }
28 |
29 | @Override
30 | public String replace(Matcher matcher) {
31 | return matcher.replaceFirst(replacement);
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/inflector/src/main/java/org/jvnet/inflector/rule/SuffixInflectionRule.java:
--------------------------------------------------------------------------------
1 | package org.jvnet.inflector.rule;
2 | import java.io.Serializable;
3 |
4 | import org.jvnet.inflector.Rule;
5 |
6 | /**
7 | *
8 | * A rule for specifying an inflection using suffixes. For example, the English nouns
9 | * which have the suffix -y, generally change the suffix to -ies in the plural. Such a rule would
10 | * be expressed as new SuffixInflectionRule("-y", "-ies")
.
11 | *
12 | * @author Tom White
13 | */
14 | public class SuffixInflectionRule implements Rule, Serializable{
15 |
16 | /** */
17 | private static final long serialVersionUID = -6412698059095031731L;
18 | private final String regex;
19 | private final String singularSuffix;
20 | private final String pluralSuffix;
21 |
22 | /**
23 | *
24 | * Construct a rule for words with suffix singularSuffix
which
25 | * becomes pluralSuffix
in the plural.
26 | *
27 | * @param singularSuffix the singular suffix, starting with a "-" character
28 | * @param pluralSuffix the plural suffix, starting with a "-" character
29 | */
30 | public SuffixInflectionRule(String singularSuffix, String pluralSuffix) {
31 | this(singularSuffix, singularSuffix, pluralSuffix);
32 | }
33 |
34 | /**
35 | *
36 | * Construct a rule for words with suffix suffix
, where
37 | * singularSuffix
38 | * becomes pluralSuffix
in the plural.
39 | * @param suffix the suffix, starting with a "-" character, which the end of the word must match.
40 | * Note that regular expression patterns may be used.
41 | * @param singularSuffix the singular suffix, starting with a "-" character.
42 | * Note that it must be true that suffix
ends with singularSuffix
.
43 | * @param pluralSuffix the plural suffix, starting with a "-" character
44 | *
45 | */
46 | public SuffixInflectionRule(String suffix, String singularSuffix, String pluralSuffix) {
47 | // TODO: check suffix ends with singularSuffix?
48 | this.regex = "(?i).*" + suffix.substring(1) + "$";
49 | this.singularSuffix = singularSuffix;
50 | this.pluralSuffix = pluralSuffix;
51 | }
52 |
53 | public boolean applies(String word) {
54 | return word.matches(regex);
55 | }
56 |
57 | public String apply(String word) {
58 | int i = word.toUpperCase().lastIndexOf(singularSuffix.substring(1).toUpperCase());
59 | //TODO: check i
60 | // TODO: make case insensitive
61 | return word.substring(0, i) + pluralSuffix.substring(1);
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/inflector/src/main/java/org/jvnet/inflector/rule/package.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Classes for specifying inflections using rules.
4 |
5 |
--------------------------------------------------------------------------------
/inflector/src/main/java/overview.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Inflector - User Guide
4 |
5 |
6 |
7 |
8 |
9 |
10 | Inflector provides an API for forming the plurals of nouns. Here's a simple example:
11 |
12 |
13 | System.out.println(Noun.pluralOf("loaf"));
14 |
15 |
16 | This will print
17 |
18 |
19 | loaves
20 |
21 |
22 | to the console.
23 |
24 |
25 |
26 | It's handy to use Java 5 static imports to make this more readable,
27 | particularly if you are forming lots of plurals.
28 |
29 |
30 | import static org.jvnet.inflector.Noun.pluralOf;
31 | ...
32 | System.out.println(pluralOf("loaf"));
33 |
34 |
35 | Nouns
36 |
37 |
38 | It is often useful to use a {@link java.util.Formatter}. For example,
39 |
40 |
41 | System.out.printf("I bought 10 %s.", pluralOf("loaf"));
42 |
43 |
44 |
45 | prints
46 |
47 |
48 | I bought 10 loaves.
49 |
50 |
51 | Of course, you can parameterize the number, as follows.
52 |
53 |
54 | int n = 10;
55 | System.out.printf("I bought %d %s.", n, pluralOf("loaf", n));
56 |
57 |
58 |
59 | Notice that you pass the number into the pluralOf
method to make sure the noun agrees.
60 | For example, if n = 1
in the previous example, it would correctly print
61 |
62 |
63 | I bought 1 loaf.
64 |
65 | Internationalization
66 |
67 |
68 | The examples so far use the default locale to determine the pluralization algorithm to use. You can explictly set
69 | the locale to use by passing another argument:
70 |
71 |
72 | System.out.println(pluralOf("pagnotta", Locale.ITALIAN));
73 |
74 |
75 |
76 | prints
77 |
78 |
79 | pagnotte
80 |
81 | Custom Pluralization
82 |
83 |
84 | The pluralization algorithms that come with Inflector may not meet your needs. Perhaps there is no algorithm for your language,
85 | or perhaps the rules don't fit the usage that is appropriate for your application. The English
86 | pluralizer, for example, is fairly modern: it does not support many classical rules that are rare nowadays.
87 |
88 |
89 |
90 | In this case it is straightforward to write your own pluralizer, or simply extend another one to override some of its rules.
91 | The follow code overrides the plural for box to boxen by using a {@link org.jvnet.inflector.rule.RegexReplacementRule}:
92 |
93 |
94 | List<Rule> customRules = new ArrayList<Rule>();
95 | customRules.add(new RegexReplacementRule("(?i)(.*)box", "$1boxen"));
96 | Pluralizer customPluralizer = new RuleBasedPluralizer(customRules, Locale.ENGLISH, Noun.pluralizer(Locale.ENGLISH));
97 | System.out.println(pluralOf("box"));
98 | System.out.println(pluralOf("box", customPluralizer));
99 | System.out.println(pluralOf("chatterbox", customPluralizer));
100 |
101 |
102 |
103 | This prints
104 |
105 |
106 | boxes
107 | boxen
108 | chatterboxen
109 |
110 |
111 | If you are interested in writing more complex rules, there are many other implementations of
112 | {@link org.jvnet.inflector.Rule} in the {@link org.jvnet.inflector.rule} package.
113 |
114 |
115 |
116 |
117 |
118 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 | 4.0.0
5 |
6 | com.github.wwadge
7 | hbnpojogen
8 | 1.5.13
9 | pom
10 |
11 | Hibernate POJO Generator
12 | The parent project of Hibernate POJO Generator artifacts.
13 |
14 | https://github.com/wwadge/hbnpojogen
15 |
16 |
17 |
18 | The Apache License, Version 2.0
19 | http://www.apache.org/licenses/LICENSE-2.0.txt
20 |
21 |
22 |
23 |
24 |
25 | Wallace Wadge
26 | wwadge@gmail.com
27 | Wallace Wadge
28 | https://github.com/wwadge/hbnpojogen
29 |
30 |
31 |
32 |
33 | scm:git:git://github.com/wwadge/hbnpojogen.git
34 | scm:git:ssh://github.com:wwadge/hbnpojogen.git
35 | https://github.com/wwadge/hbnpojogen
36 |
37 |
38 |
39 | hbnpojogen-core
40 | hbnpojogen-persistence
41 | hbnpojogen-maven
42 |
43 |
44 |
45 | 3.5.2-Final
46 | 2.5.6
47 | true
48 |
49 |
50 |
51 |
52 |
53 | com.github.wwadge
54 | hbnpojogen-core
55 | ${project.version}
56 |
57 |
58 |
59 | com.google.guava
60 | guava
61 | 18.0
62 |
63 |
64 |
65 | commons-configuration
66 | commons-configuration
67 | 1.5
68 |
69 |
70 | commons-io
71 | commons-io
72 | 1.4
73 |
74 |
75 |
76 | commons-lang
77 | commons-lang
78 | 2.4
79 |
80 |
81 |
82 | commons-collections
83 | commons-collections
84 | 3.2.1
85 |
86 |
87 |
88 | org.hibernate
89 | hibernate-core
90 | ${hibernate.version}
91 |
92 |
93 |
94 | org.hibernate
95 | hibernate-validator
96 | 4.1.0.Final
97 |
98 |
99 |
100 | net.java.dev.inflector
101 | inflector
102 | 0.7.0
103 |
104 |
105 |
106 | mysql
107 | mysql-connector-java
108 | 8.0.17
109 |
110 |
111 |
112 | org.postgresql
113 | postgresql
114 | 42.2.5
115 | runtime
116 | true
117 |
118 |
119 |
120 | org.apache.velocity
121 | velocity
122 | 1.5
123 |
124 |
125 |
126 | net.sourceforge.jtds
127 | jtds
128 | 1.2.4
129 | runtime
130 |
131 |
132 |
133 | org.springframework
134 | spring-beans
135 | ${spring.version}
136 |
137 |
138 |
139 | org.springframework
140 | spring-context
141 | ${spring.version}
142 |
143 |
144 |
145 | log4j
146 | log4j
147 | 1.2.16
148 | compile
149 |
150 |
151 |
152 | org.apache.maven
153 | maven-plugin-api
154 | 3.3.3
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 | maven-source-plugin
163 | 2.1.2
164 |
165 |
166 | package
167 |
168 | jar
169 |
170 |
171 |
172 |
173 |
174 |
175 | org.apache.maven.plugins
176 | maven-plugin-plugin
177 | 3.4
178 |
179 |
180 |
181 | maven-compiler-plugin
182 | 2.0.1
183 |
184 | 1.8
185 | 1.8
186 |
187 |
188 |
189 | org.sonatype.plugins
190 | nexus-staging-maven-plugin
191 | 1.6.7
192 | true
193 |
194 | ossrh
195 | https://oss.sonatype.org/
196 | true
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 | ossrh
209 | https://oss.sonatype.org/content/repositories/snapshots
210 |
211 |
212 |
213 |
214 |
--------------------------------------------------------------------------------