Clover coverage report - Code Coverage for hivemind release 1.1-beta-1
Coverage timestamp: Thu Apr 28 2005 19:53:41 EDT
file stats: LOC: 155   Methods: 4
NCLOC: 76   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 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  140
     public static Map convertInitializer(String initializer)
 48   
     {
 49  140
         if (HiveMind.isBlank(initializer))
 50  2
             return Collections.EMPTY_MAP;
 51   
 
 52  138
         Map result = new HashMap();
 53   
 
 54  138
         int lastCommax = -1;
 55  138
         int inputLength = initializer.length();
 56   
 
 57  138
         while (lastCommax < inputLength)
 58   
         {
 59  153
             int nextCommax = initializer.indexOf(',', lastCommax + 1);
 60   
 
 61  153
             if (nextCommax < 0)
 62  138
                 nextCommax = inputLength;
 63   
 
 64  153
             String term = initializer.substring(lastCommax + 1, nextCommax);
 65   
 
 66  153
             int equalsx = term.indexOf('=');
 67   
 
 68  153
             if (equalsx <= 0)
 69  1
                 throw new ApplicationRuntimeException(RulesMessages.invalidInitializer(initializer));
 70   
 
 71  152
             String key = term.substring(0, equalsx);
 72  152
             String value = term.substring(equalsx + 1);
 73   
 
 74  152
             result.put(key, value);
 75   
 
 76  152
             lastCommax = nextCommax;
 77   
         }
 78   
 
 79  137
         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  6399
     public static String processText(SchemaProcessor processor, Element element, String inputValue)
 97   
     {
 98  6399
         if (inputValue == null)
 99  449
             return null;
 100   
 
 101  5950
         Module contributingModule = processor.getContributingModule();
 102   
 
 103  5950
         if (inputValue.startsWith("%"))
 104   
         {
 105  1
             String key = inputValue.substring(1);
 106   
 
 107  1
             return contributingModule.getMessages().getMessage(key);
 108   
         }
 109   
 
 110  5949
         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  146
     public static Translator getTranslator(SchemaProcessor processor, String translator)
 149   
     {
 150  146
         if (translator == null)
 151  0
             return new NullTranslator();
 152   
 
 153  146
         return processor.getContributingModule().getTranslator(translator);
 154   
     }
 155   
 }