1 15 package org.apache.hivemind.schema.rules; 16 17 import java.util.Collections ; 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.apache.hivemind.ApplicationRuntimeException; 24 import org.apache.hivemind.Element; 25 import org.apache.hivemind.ErrorHandler; 26 import org.apache.hivemind.HiveMind; 27 import org.apache.hivemind.internal.Module; 28 import org.apache.hivemind.schema.SchemaProcessor; 29 import org.apache.hivemind.schema.Translator; 30 import org.apache.hivemind.util.PropertyUtils; 31 32 38 public class RuleUtils 39 { 40 private static final Log LOG = LogFactory.getLog(RuleUtils.class); 41 42 47 public static Map convertInitializer(String initializer) 48 { 49 if (HiveMind.isBlank(initializer)) 50 return Collections.EMPTY_MAP; 51 52 Map result = new HashMap (); 53 54 int lastCommax = -1; 55 int inputLength = initializer.length(); 56 57 while (lastCommax < inputLength) 58 { 59 int nextCommax = initializer.indexOf(',', lastCommax + 1); 60 61 if (nextCommax < 0) 62 nextCommax = inputLength; 63 64 String term = initializer.substring(lastCommax + 1, nextCommax); 65 66 int equalsx = term.indexOf('='); 67 68 if (equalsx <= 0) 69 throw new ApplicationRuntimeException(RulesMessages.invalidInitializer(initializer)); 70 71 String key = term.substring(0, equalsx); 72 String value = term.substring(equalsx + 1); 73 74 result.put(key, value); 75 76 lastCommax = nextCommax; 77 } 78 79 return result; 80 } 81 82 96 public static String processText(SchemaProcessor processor, Element element, String inputValue) 97 { 98 if (inputValue == null) 99 return null; 100 101 Module contributingModule = processor.getContributingModule(); 102 103 if (inputValue.startsWith("%")) 104 { 105 String key = inputValue.substring(1); 106 107 return contributingModule.getMessages().getMessage(key); 108 } 109 110 return processor.getSymbolExpander().expandSymbols(inputValue, element.getLocation()); 111 } 112 113 116 public static void setProperty(SchemaProcessor processor, Element element, String propertyName, 117 Object target, Object value) 118 { 119 try 120 { 121 PropertyUtils.write(target, propertyName, value); 122 } 123 catch (Exception ex) 124 { 125 128 ErrorHandler errorHandler = processor.getContributingModule().getErrorHandler(); 129 errorHandler.error(LOG, RulesMessages.unableToSetElementProperty( 130 propertyName, 131 target, 132 processor, 133 element, 134 ex), element.getLocation(), ex); 135 } 136 } 137 138 148 public static Translator getTranslator(SchemaProcessor processor, String translator) 149 { 150 if (translator == null) 151 return new NullTranslator(); 152 153 return processor.getTranslator(translator); 154 } 155 } | Popular Tags |