2009/04/15 - Apache HiveMind has been retired.

For more information, please explore the Attic.

Clover coverage report - Code Coverage for hivemind release 1.2.1
Coverage timestamp: Fri Feb 10 2006 16:33:43 PST
file stats: LOC: 155   Methods: 4
NCLOC: 76   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
RuleUtils.java 92.9% 90.6% 100% 92%
coverage coverage
 1    // Copyright 2004, 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // You may obtain a copy of the License at
 6    //
 7    // http://www.apache.org/licenses/LICENSE-2.0
 8    //
 9    // Unless required by applicable law or agreed to in writing, software
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 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    /**
 33    * Static methods useful to {@link org.apache.hivemind.schema.Rule}s and
 34    * {@link org.apache.hivemind.schema.Translator}s.
 35    *
 36    * @author Howard Lewis Ship
 37    */
 38    public class RuleUtils
 39    {
 40    private static final Log LOG = LogFactory.getLog(RuleUtils.class);
 41   
 42    /**
 43    * Used to convert a {@link org.apache.hivemind.schema.Translator} initializer string of the
 44    * form: <code><i>key</i>=<i>value</i>[,<i>key</i>=<i>value<i>]*</code> into a Map of
 45    * keys and values. The keys and values are Strings.
 46    */
 47  147 public static Map convertInitializer(String initializer)
 48    {
 49  147 if (HiveMind.isBlank(initializer))
 50  2 return Collections.EMPTY_MAP;
 51   
 52  145 Map result = new HashMap();
 53   
 54  145 int lastCommax = -1;
 55  145 int inputLength = initializer.length();
 56   
 57  145 while (lastCommax < inputLength)
 58    {
 59  160 int nextCommax = initializer.indexOf(',', lastCommax + 1);
 60   
 61  160 if (nextCommax < 0)
 62  145 nextCommax = inputLength;
 63   
 64  160 String term = initializer.substring(lastCommax + 1, nextCommax);
 65   
 66  160 int equalsx = term.indexOf('=');
 67   
 68  160 if (equalsx <= 0)
 69  1 throw new ApplicationRuntimeException(RulesMessages.invalidInitializer(initializer));
 70   
 71  159 String key = term.substring(0, equalsx);
 72  159 String value = term.substring(equalsx + 1);
 73   
 74  159 result.put(key, value);
 75   
 76  159 lastCommax = nextCommax;
 77    }
 78   
 79  144 return result;
 80    }
 81   
 82    /**
 83    * Invoked to process text from an attribute or from an element's content. Performs two jobs:
 84    * <ul>
 85    * <li>Convert localized message references to localized strings
 86    * <li>Expand symbols using
 87    * {@link org.apache.hivemind.Registry#expandSymbols(String, Location)}
 88    * </ul>
 89    * <p>
 90    * Note: if the input is a localized message then no symbol expansion takes place. Localized
 91    * message references are simply strings that begin with '%'. The remainder of the string is the
 92    * message key.
 93    * <p>
 94    * A null input value passes through unchanged.
 95    */
 96  7023 public static String processText(SchemaProcessor processor, Element element, String inputValue)
 97    {
 98  7023 if (inputValue == null)
 99  480 return null;
 100   
 101  6543 Module contributingModule = processor.getContributingModule();
 102   
 103  6543 if (inputValue.startsWith("%"))
 104    {
 105  1 String key = inputValue.substring(1);
 106   
 107  1 return contributingModule.getMessages().getMessage(key);
 108    }
 109   
 110  6542 return contributingModule.expandSymbols(inputValue, element.getLocation());
 111    }
 112   
 113    /**
 114    * Sets a property of the target object to the given value. Logs an error if there is a problem.
 115    */
 116  5 public static void setProperty(SchemaProcessor processor, Element element, String propertyName,
 117    Object target, Object value)
 118    {
 119  5 try
 120    {
 121  5 PropertyUtils.write(target, propertyName, value);
 122    }
 123    catch (Exception ex)
 124    {
 125    // Have to decide if we need to display the location of the rule
 126    // or the element.
 127   
 128  0 ErrorHandler errorHandler = processor.getContributingModule().getErrorHandler();
 129  0 errorHandler.error(LOG, RulesMessages.unableToSetElementProperty(
 130    propertyName,
 131    target,
 132    processor,
 133    element,
 134    ex), element.getLocation(), ex);
 135    }
 136    }
 137   
 138    /**
 139    * Convienience for invoking {@link Module#getTranslator(String)}.
 140    *
 141    * @param processor
 142    * the processor for the schema being converted
 143    * @param translator
 144    * the string identifying the translator to provide (may be null)
 145    * @return a translator obtained via the contributing module, or an instance of
 146    * {@link NullTranslator}
 147    */
 148  158 public static Translator getTranslator(SchemaProcessor processor, String translator)
 149    {
 150  158 if (translator == null)
 151  0 return new NullTranslator();
 152   
 153  158 return processor.getContributingModule().getTranslator(translator);
 154    }
 155    }