View Javadoc

1   /*
2    * Copyright 2004-2005 Dan Washusen
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.reactive.beans.factory.config.jfig;
18  
19  import org.reactive.beans.factory.config.PlaceholderEvaluator;
20  import org.reactive.beans.factory.config.Placeholder;
21  import org.reactive.beans.factory.config.InvalidPlaceholderException;
22  import org.reactive.beans.factory.config.AbstractPlaceholderEvaluator;
23  import org.reactive.beans.factory.config.commons.CommonsPlaceholderEvaluator;
24  import org.apache.commons.configuration.Configuration;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.igfay.jfig.JFigIF;
28  
29  import java.util.*;
30  
31  /***
32   * Used to evaluate a placeholder using the JFig configuration agent.
33   *
34   * @author Dan Washusen
35   * @version $Id: JFigPlaceholderEvaluator.java,v 1.1 2004/12/30 07:35:28 dan_washusen Exp $
36   * @since 30/12/2004
37   * @see JFigConfigurationFactory
38   */
39  public class JFigPlaceholderEvaluator
40          extends AbstractPlaceholderEvaluator {
41      private static final Log log = LogFactory.getLog(JFigPlaceholderEvaluator.class);
42  
43      protected JFigIF configuration;
44  
45      public JFigIF getConfiguration() {
46          return configuration;
47      }
48  
49      public void setConfiguration(JFigIF configuration) {
50          this.configuration = configuration;
51      }
52  
53      protected String evaluateString(Placeholder placeholder) {
54          // convert the placeholder key to something usable by JFig
55          SectionKey sectionKey = new SectionKey(placeholder.getKey());
56          return getConfiguration().getValue(sectionKey.getSection(), sectionKey.getKey(), null);
57      }
58  
59      protected String[] evaluateArray(Placeholder placeholder) {
60          // convert the placeholder key to something usable by JFig
61          SectionKey sectionKey = new SectionKey(placeholder.getKey());
62          return getConfiguration().getArrayValue(sectionKey.getSection(), sectionKey.getKey(), null);
63      }
64  
65      protected List evaluateList(Placeholder placeholder) {
66          String[] strings = evaluateArray(placeholder);
67          List list = null;
68  
69          if (strings != null) {
70              list = new ArrayList();
71              for (int i = 0; i < strings.length; i++) {
72                  String string = strings[i];
73                  list.add(string);
74              }
75          }
76  
77          return list;
78      }
79  
80      protected Set evaluateSet(Placeholder placeholder) {
81          String[] strings = evaluateArray(placeholder);
82          Set set = null;
83  
84          if (strings != null) {
85              set = new HashSet();
86              for (int i = 0; i < strings.length; i++) {
87                  String string = strings[i];
88                  set.add(string);
89              }
90          }
91  
92          return set;
93      }
94  
95      protected Map evaluateMap(Placeholder placeholder) {
96          throw new InvalidPlaceholderException("Map evaluation is not supported for JFig");
97      }
98  
99      protected Properties evaluateProperties(Placeholder placeholder) {
100         throw new InvalidPlaceholderException("Properties evaluation is not supported for JFig");
101     }
102 
103     protected Object evaluateObject(Placeholder placeholder) {
104         return evaluateString(placeholder);
105     }
106 
107     /***
108      * Wraps a key because JFig uses the concept of a section.  The section is presumed to by the text in the key proceeding
109      * the section seperator ":" string.
110      * @see org.reactive.beans.factory.config.Placeholder#getKey()
111      */
112     protected class SectionKey {
113         public static final String SECTION_SEPERATOR = ":";
114 
115         protected String section;
116         protected String key;
117 
118         /***
119          * Constructs a new SectionKey object from the original placeholder key.
120          * @param originalKey The original {@link org.reactive.beans.factory.config.Placeholder#getKey() placeholder key}
121          */
122         public SectionKey(String originalKey) {
123             int seperatorIndex = originalKey.indexOf(SECTION_SEPERATOR);
124 
125             // if no section was provided then all is lost
126             if (seperatorIndex == -1)
127                 throw new InvalidPlaceholderException("The key '" + originalKey + "' did not contain any section information.");
128 
129             this.section = originalKey.substring(0, seperatorIndex);
130             this.key = originalKey.substring(seperatorIndex + 1, originalKey.length());
131         }
132 
133         /***
134          * Returns the new key value (the text trailing the section seperator).
135          * @return the new key value
136          */
137         public String getKey() {
138             return key;
139         }
140 
141         /***
142          * Returns the section name value (the text proceeding the section seperator).
143          * @return
144          */
145         public String getSection() {
146             return section;
147         }
148     }
149 }