001: package org.andromda.core.translation;
002:
003: import org.andromda.core.common.ExceptionUtils;
004: import org.andromda.core.common.Introspector;
005: import org.apache.commons.lang.ObjectUtils;
006: import org.apache.commons.lang.StringUtils;
007:
008: /**
009: * Contains translation utilities.
010: *
011: * @author Chad Brandon
012: */
013: public class TranslationUtils {
014: /**
015: * <p/>
016: * <code>TranslationUtils</code> instances should NOT be constructed in standard programming. Instead, the class
017: * should be used as <code>TranslationUtils.replacePattern(" some pattern ");</code>. </p>
018: * <p/>
019: * This constructor is public to permit tools that require a JavaBean instance to operate. </p>
020: */
021: public TranslationUtils() {
022: // Here for documentation purposes
023: }
024:
025: /**
026: * Searches for and replaces the specified pattern with braces around it, like so --> "{pattern}" every time it
027: * occurs in the string.
028: *
029: * @param string the string to to perform replacement on.
030: * @param pattern the pattern to find
031: * @param replaceWith the pattern to place the existing one with.
032: * @return String the string will all replacements
033: */
034: public static String replacePattern(String string,
035: final String pattern, final String replaceWith) {
036: if (string != null) {
037: ExceptionUtils.checkNull("pattern", pattern);
038: ExceptionUtils.checkNull("replaceWith", replaceWith);
039: string = StringUtils.replace(string, "{" + pattern + "}",
040: replaceWith);
041: }
042: return string;
043: }
044:
045: /**
046: * Searches for and replaces the specified pattern with braces around it, like so --> "{pattern}" the first time it
047: * occurs in the string
048: *
049: * @param string the string to to perform replacement on.
050: * @param pattern the pattern to find
051: * @param replaceWith the pattern to place the existing one with.
052: * @return String the string will all replacements
053: */
054: public static String replaceFirstPattern(String string,
055: final String pattern, final String replaceWith) {
056: if (string != null) {
057: ExceptionUtils.checkNull("pattern", pattern);
058: ExceptionUtils.checkNull("replaceWith", replaceWith);
059: string = StringUtils.replaceOnce(string, "{" + pattern
060: + "}", replaceWith);
061: }
062: return string;
063: }
064:
065: /**
066: * Returns true if the specified pattern with braces around it, like so --> "{pattern}" exists in the string.
067: *
068: * @param string the string to to perform replacement on.
069: * @param pattern the pattern to find
070: * @return boolean true if the string contains the pattern, false otherwise
071: */
072: public static boolean containsPattern(final String string,
073: final String pattern) {
074: boolean containsPattern = string != null && pattern != null;
075: if (containsPattern) {
076: containsPattern = StringUtils.contains(string, "{"
077: + pattern + "}");
078: }
079: return containsPattern;
080: }
081:
082: /**
083: * Calls the object's toString method and trims the value. Returns and empty string if null is given.
084: *
085: * @param object the object to use.
086: * @return String
087: */
088: public static String trimToEmpty(final Object object) {
089: return StringUtils.trimToEmpty(ObjectUtils.toString(object));
090: }
091:
092: /**
093: * Calls the object's toString method and deletes any whitespace from the value. Returns and empty string if null is
094: * given.
095: *
096: * @param object the object to deleteWhite space from.
097: * @return String
098: */
099: public static String deleteWhitespace(final Object object) {
100: return StringUtils.deleteWhitespace(ObjectUtils
101: .toString(object));
102: }
103:
104: /**
105: * Retrieves the "starting" property name from one that is nested, for example, will return ' <name1>' from the
106: * string --> <name1>. <name2>. <name3>. If the property isn't nested, then just return the name that is passed in.
107: *
108: * @param property the property.
109: * @return String
110: */
111: public static String getStartingProperty(String property) {
112: StringUtils.trimToEmpty(property);
113: int dotIndex = property.indexOf('.');
114: if (dotIndex != -1) {
115: property = property.substring(0, dotIndex);
116: }
117: return property;
118: }
119:
120: /**
121: * Removes any extra whitepace --> does not remove the spaces between the words. Only removes tabs and newline
122: * characters. This is to allow everything to be on one line while keeping the spaces between words.
123: *
124: * @param string
125: * @return String the string with the removed extra spaces.
126: */
127: public static String removeExtraWhitespace(final String string) {
128: return string == null ? "" : string.replaceAll("[$\\s]+", " ")
129: .trim();
130: }
131:
132: /**
133: * Just retriieves properties from a bean, but gives a more informational error when the property can't be
134: * retrieved, it also cleans the resulting property from any excess white space
135: *
136: * @param bean the bean from which to retrieve the property
137: * @param property the property name
138: * @return Object the value of the property
139: */
140: public static Object getProperty(final Object bean,
141: final String property) {
142: final String methodName = "TranslationUtils.getProperty";
143: try {
144: return Introspector.instance().getProperty(bean, property);
145: } catch (final Exception exception) {
146: throw new TranslatorException("Error performing "
147: + methodName + " with bean '" + bean
148: + "' and property '" + property + "'", exception);
149: }
150: }
151:
152: /**
153: * Just retriieves properties from a bean, but gives a more informational error when the property can't be
154: * retrieved, it also cleans the resulting property from any excess white space
155: *
156: * @param bean the bean from which to retrieve the property
157: * @param property the property name
158: * @return Object the value of the property
159: */
160: public static String getPropertyAsString(final Object bean,
161: final String property) {
162: return TranslationUtils.trimToEmpty(TranslationUtils
163: .getProperty(bean, property));
164: }
165: }
|