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