KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > schema > rules > RuleUtils


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 JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
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     public static Map JavaDoc convertInitializer(String JavaDoc initializer)
48     {
49         if (HiveMind.isBlank(initializer))
50             return Collections.EMPTY_MAP;
51
52         Map JavaDoc result = new HashMap JavaDoc();
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 JavaDoc 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 JavaDoc key = term.substring(0, equalsx);
72             String JavaDoc value = term.substring(equalsx + 1);
73
74             result.put(key, value);
75
76             lastCommax = nextCommax;
77         }
78
79         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.SymbolExpander#expandSymbols(String, org.apache.hivemind.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     public static String JavaDoc processText(SchemaProcessor processor, Element element, String JavaDoc inputValue)
97     {
98         if (inputValue == null)
99             return null;
100
101         Module contributingModule = processor.getContributingModule();
102
103         if (inputValue.startsWith("%"))
104         {
105             String JavaDoc key = inputValue.substring(1);
106
107             return contributingModule.getMessages().getMessage(key);
108         }
109
110         return processor.getSymbolExpander().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     public static void setProperty(SchemaProcessor processor, Element element, String JavaDoc propertyName,
117             Object JavaDoc target, Object JavaDoc value)
118     {
119         try
120         {
121             PropertyUtils.write(target, propertyName, value);
122         }
123         catch (Exception JavaDoc ex)
124         {
125             // Have to decide if we need to display the location of the rule
126
// or the element.
127

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     /**
139      * Convienience for invoking {@link org.apache.hivemind.TranslatorManager#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     public static Translator getTranslator(SchemaProcessor processor, String JavaDoc translator)
149     {
150         if (translator == null)
151             return new NullTranslator();
152         
153         return processor.getTranslator(translator);
154     }
155 }
Popular Tags