Clover coverage report - Code Coverage for hivemind release 1.0-beta-1
Coverage timestamp: Sat Jul 3 2004 09:41:37 EDT
file stats: LOC: 169   Methods: 4
NCLOC: 87   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 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.internal.RegistryInfrastructure;
 29   
 import org.apache.hivemind.schema.SchemaProcessor;
 30   
 import org.apache.hivemind.schema.Translator;
 31   
 import org.apache.hivemind.util.PropertyUtils;
 32   
 
 33   
 /**
 34   
  * Static methods useful to {@link org.apache.hivemind.schema.Rule}s and
 35   
  * {@link org.apache.hivemind.schema.Translator}s.
 36   
  *
 37   
  * @author Howard Lewis Ship
 38   
  */
 39   
 public class RuleUtils
 40   
 {
 41   
     private static final Log LOG = LogFactory.getLog(RuleUtils.class);
 42   
 
 43   
     /**
 44   
      * Used to convert a {@link org.apache.hivemind.schema.Translator}
 45   
      * initializer string of the form:
 46   
      * <code><i>key</i>=<i>value</i>[,<i>key</i>=<i>value<i>]*</code>
 47   
      * into a Map of keys and values.  The keys and values are Strings.
 48   
      */
 49  25
     public static Map convertInitializer(String initializer)
 50   
     {
 51  25
         if (HiveMind.isBlank(initializer))
 52  2
             return Collections.EMPTY_MAP;
 53   
 
 54  23
         Map result = new HashMap();
 55   
 
 56  23
         int lastCommax = -1;
 57  23
         int inputLength = initializer.length();
 58   
 
 59  23
         while (lastCommax < inputLength)
 60   
         {
 61  38
             int nextCommax = initializer.indexOf(',', lastCommax + 1);
 62   
 
 63  38
             if (nextCommax < 0)
 64  23
                 nextCommax = inputLength;
 65   
 
 66  38
             String term = initializer.substring(lastCommax + 1, nextCommax);
 67   
 
 68  38
             int equalsx = term.indexOf('=');
 69   
 
 70  38
             if (equalsx <= 0)
 71  1
                 throw new ApplicationRuntimeException(
 72   
                     RulesMessages.invalidInitializer(initializer));
 73   
 
 74  37
             String key = term.substring(0, equalsx);
 75  37
             String value = term.substring(equalsx + 1);
 76   
 
 77  37
             result.put(key, value);
 78   
 
 79  37
             lastCommax = nextCommax;
 80   
         }
 81   
 
 82  22
         return result;
 83   
     }
 84   
 
 85   
     /**
 86   
      * Invoked to process text from an attribute or from an element's content.  Performs
 87   
      * two jobs:
 88   
      * <ul>
 89   
      * <li>Convert localized message references to localized strings
 90   
      * <li>Expand symbols using {@link Registry#expandSymbols(String, Location)}
 91   
      * </ul>
 92   
      * 
 93   
      * <p>
 94   
      * Note: if the input is a localized message then no symbol expansion takes place.
 95   
      * Localized message references are simply strings that begin with '%'.  The remainder
 96   
      * of the string is the message key.
 97   
      * 
 98   
      * <p>
 99   
      * A null input value passes through unchanged.
 100   
      */
 101  3235
     public static String processText(SchemaProcessor processor, Element element, String inputValue)
 102   
     {
 103  3235
         if (inputValue == null)
 104  1
             return null;
 105   
 
 106  3234
         Module contributingModule = processor.getContributingModule();
 107   
 
 108  3234
         if (inputValue.startsWith("%"))
 109   
         {
 110  1
             String key = inputValue.substring(1);
 111   
 
 112  1
             return contributingModule.getMessages().getMessage(key);
 113   
         }
 114   
 
 115  3233
         return contributingModule.expandSymbols(inputValue, element.getLocation());
 116   
     }
 117   
 
 118   
     /**
 119   
      * Sets a property of the target object to the given value.
 120   
      * Logs an error if there is a problem.
 121   
      */
 122  5
     public static void setProperty(
 123   
         SchemaProcessor processor,
 124   
         Element element,
 125   
         String propertyName,
 126   
         Object target,
 127   
         Object value)
 128   
     {
 129  5
         try
 130   
         {
 131  5
             PropertyUtils.write(target, propertyName, value);
 132   
         }
 133   
         catch (Exception ex)
 134   
         {
 135   
             // Have to decide if we need to display the location of the rule
 136   
             // or the element.
 137   
 
 138  0
             ErrorHandler errorHandler = processor.getContributingModule().getErrorHandler();
 139  0
             errorHandler.error(
 140   
                 LOG,
 141   
                 RulesMessages.unableToSetElementProperty(
 142   
                     propertyName,
 143   
                     target,
 144   
                     processor,
 145   
                     element,
 146   
                     ex),
 147   
                 element.getLocation(),
 148   
                 ex);
 149   
         }
 150   
     }
 151   
 
 152   
     /**
 153   
      * Convienience for invoking
 154   
      * {@link Module#getTranslator(String)}.
 155   
      * 
 156   
      * @param processor the processor for the schema being converted
 157   
      * @param translator the string identifying the translator to provide (may be null)
 158   
      * @return a translator obtained via the contributing module, or an
 159   
      * instance of {@link NullTranslator}
 160   
      */
 161  4
     public static Translator getTranslator(SchemaProcessor processor, String translator)
 162   
     {
 163  4
         if (translator == null)
 164  0
             return new NullTranslator();
 165   
 
 166  4
         return processor.getContributingModule().getTranslator(translator);
 167   
     }
 168   
 }
 169