From: <sha...@us...> - 2011-08-28 05:23:25
|
Revision: 3146 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3146&view=rev Author: shadowtm Date: 2011-08-28 05:23:17 +0000 (Sun, 28 Aug 2011) Log Message: ----------- Updated IConfiguration to get the configured Object versus the String - that way we don't have to write a ton of new PropertyEditors for the different collection types. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/configuration/IConfigurationProperty.java trunk/interfaces/src/main/java/org/dllearner/cli/ConfFileOption2.java trunk/interfaces/src/main/java/org/dllearner/configuration/spring/ConfigurationBasedBeanDefinitionRegistryPostProcessor.java trunk/interfaces/src/main/java/org/dllearner/configuration/spring/CustomPropertyEditorRegistrar.java trunk/interfaces/src/main/java/org/dllearner/confparser3/ConfParser.java trunk/interfaces/src/main/java/org/dllearner/confparser3/ConfParserConfiguration.java trunk/interfaces/src/main/java/org/dllearner/confparser3/PostProcessor.java trunk/interfaces/src/main/java/org/dllearner/confparser3/conf3.jj trunk/interfaces/src/test/java/org/dllearner/configuration/spring/ConfigurationBasedPropertyOverrideConfigurerTest.java trunk/interfaces/src/test/java/org/dllearner/confparser3/ParseTest.java trunk/interfaces/src/test/resources/org/dllearner/configuration/spring/configurationBasedPropertyOverrideConfigurer.conf Modified: trunk/components-core/src/main/java/org/dllearner/configuration/IConfigurationProperty.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/configuration/IConfigurationProperty.java 2011-08-28 00:59:13 UTC (rev 3145) +++ trunk/components-core/src/main/java/org/dllearner/configuration/IConfigurationProperty.java 2011-08-28 05:23:17 UTC (rev 3146) @@ -23,7 +23,7 @@ * * @return The String representation of the value of this property. */ - public String getValue(); + public Object getValue(); /** * Get the type of this value. Modified: trunk/interfaces/src/main/java/org/dllearner/cli/ConfFileOption2.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/cli/ConfFileOption2.java 2011-08-28 00:59:13 UTC (rev 3145) +++ trunk/interfaces/src/main/java/org/dllearner/cli/ConfFileOption2.java 2011-08-28 05:23:17 UTC (rev 3146) @@ -46,10 +46,6 @@ private Class<?> propertyType; - // TODO: Do we want to store the actual value as object here or leave it up to - // the corresponding PropertyEditor to create it? - // WARNING: This feature does not work in conjunction with prefix post-processing yet! - @Deprecated private Object valueObject; public ConfFileOption2() { @@ -112,8 +108,8 @@ } @Override - public String getValue() { - return getPropertyValue(); + public Object getValue() { + return getValueObject(); } @Override Modified: trunk/interfaces/src/main/java/org/dllearner/configuration/spring/ConfigurationBasedBeanDefinitionRegistryPostProcessor.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/configuration/spring/ConfigurationBasedBeanDefinitionRegistryPostProcessor.java 2011-08-28 00:59:13 UTC (rev 3145) +++ trunk/interfaces/src/main/java/org/dllearner/configuration/spring/ConfigurationBasedBeanDefinitionRegistryPostProcessor.java 2011-08-28 05:23:17 UTC (rev 3146) @@ -73,17 +73,16 @@ Object value = property.getValue(); //Process Single Bean References if (property.isBeanReference()) { - BeanDefinition referencedBean = beanFactory.getBeanDefinition(property.getValue()); + BeanDefinition referencedBean = beanFactory.getBeanDefinition((String)property.getValue()); value = referencedBean; } //Process collections of bean references if(property.isBeanReferenceCollection()){ - StringTokenizer tokenizer = new StringTokenizer(property.getValue(),"{,} ",false); Collection<RuntimeBeanReference> beanReferences = new ManagedSet<RuntimeBeanReference>(); - while(tokenizer.hasMoreTokens()){ - String referencedBeanName = tokenizer.nextToken(); - beanReferences.add(new RuntimeBeanReference(referencedBeanName)); + Collection<String> referencedBeanNames = (Collection<String>)property.getValue(); + for (String referencedBeanName : referencedBeanNames) { + beanReferences.add(new RuntimeBeanReference(referencedBeanName)); } value = beanReferences; } Modified: trunk/interfaces/src/main/java/org/dllearner/configuration/spring/CustomPropertyEditorRegistrar.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/configuration/spring/CustomPropertyEditorRegistrar.java 2011-08-28 00:59:13 UTC (rev 3145) +++ trunk/interfaces/src/main/java/org/dllearner/configuration/spring/CustomPropertyEditorRegistrar.java 2011-08-28 05:23:17 UTC (rev 3146) @@ -1,17 +1,8 @@ package org.dllearner.configuration.spring; -import org.dllearner.confparser3.IndividualCollectionEditor; -import org.dllearner.confparser3.MapEditor; -import org.dllearner.confparser3.SetEditor; -import org.dllearner.learningproblems.PosNegLP; -import org.springframework.beans.BeanWrapper; import org.springframework.beans.PropertyEditorRegistrar; import org.springframework.beans.PropertyEditorRegistry; -import java.util.Collection; -import java.util.Map; -import java.util.Set; - /** * Created by IntelliJ IDEA. * User: Chris @@ -25,19 +16,7 @@ @Override public void registerCustomEditors(PropertyEditorRegistry registry) { + //Register any custom editors here. - if (registry instanceof BeanWrapper) { - Object wrappedInstance = ((BeanWrapper) registry).getWrappedInstance(); - if (wrappedInstance instanceof PosNegLP) { - registry.registerCustomEditor(Collection.class, "positiveExamples", new IndividualCollectionEditor()); - registry.registerCustomEditor(Collection.class, "negativeExamples", new IndividualCollectionEditor()); - } - } - - //Wrappers for all beans - registry.registerCustomEditor(Map.class,new MapEditor()); - registry.registerCustomEditor(Set.class,new SetEditor()); - - } } Modified: trunk/interfaces/src/main/java/org/dllearner/confparser3/ConfParser.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/confparser3/ConfParser.java 2011-08-28 00:59:13 UTC (rev 3145) +++ trunk/interfaces/src/main/java/org/dllearner/confparser3/ConfParser.java 2011-08-28 05:23:17 UTC (rev 3146) @@ -92,7 +92,7 @@ boolean containsSubOption=false; String value="", value1="", value2="", tmp="", tmp2=""; Set<String> values = new HashSet<String>(); - List<StringTuple> tuples = new LinkedList<StringTuple>(); + Map<String,String> tuples = new HashMap<String,String>(); ConfFileOption2 option = new ConfFileOption2(); boolean isBeanRef = false; @@ -207,7 +207,7 @@ jj_consume_token(16); tmp2 = String(); jj_consume_token(21); - tuples.add(new StringTuple(tmp,tmp2)); propertyValue += "(\u005c""+ tmp + "\u005c",\u005c"" + tmp2 + "\u005c"), "; + tuples.put(tmp,tmp2); propertyValue += "(\u005c""+ tmp + "\u005c",\u005c"" + tmp2 + "\u005c"), "; jj_consume_token(16); } jj_consume_token(20); @@ -215,7 +215,7 @@ jj_consume_token(16); tmp2 = String(); jj_consume_token(21); - tuples.add(new StringTuple(tmp,tmp2)); propertyValue += "(\u005c""+ tmp + "\u005c",\u005c"" + tmp2 + "\u005c")"; + tuples.put(tmp,tmp2); propertyValue += "(\u005c""+ tmp + "\u005c",\u005c"" + tmp2 + "\u005c")"; jj_consume_token(19); val = tuples; propertyType = List.class; propertyValue = "["+ propertyValue + "]"; break; Modified: trunk/interfaces/src/main/java/org/dllearner/confparser3/ConfParserConfiguration.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/confparser3/ConfParserConfiguration.java 2011-08-28 00:59:13 UTC (rev 3145) +++ trunk/interfaces/src/main/java/org/dllearner/confparser3/ConfParserConfiguration.java 2011-08-28 05:23:17 UTC (rev 3146) @@ -63,7 +63,7 @@ Class<?> result = null; - String value = (String) option.getPropertyValue(); + String value = (String) option.getValue(); // first option: use long name of @ComponentAnn annotation Class<? extends Component> classFromName = AnnComponentManager.getInstance().getComponentsNamed().getKey(value); if(classFromName != null) { @@ -79,7 +79,7 @@ result = Class.forName(value); } catch (ClassNotFoundException e) { // if all methods fail, throw an exception - throw new RuntimeException("Problem getting class type for bean: " + beanName + " - trying to instantiate class: " + option.getPropertyValue()); + throw new RuntimeException("Problem getting class type for bean: " + beanName + " - trying to instantiate class: " + value); } return result; } Modified: trunk/interfaces/src/main/java/org/dllearner/confparser3/PostProcessor.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/confparser3/PostProcessor.java 2011-08-28 00:59:13 UTC (rev 3145) +++ trunk/interfaces/src/main/java/org/dllearner/confparser3/PostProcessor.java 2011-08-28 05:23:17 UTC (rev 3146) @@ -19,13 +19,9 @@ */ package org.dllearner.confparser3; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; +import java.util.*; import org.dllearner.cli.ConfFileOption2; -import org.dllearner.utilities.Helper; -import org.dllearner.utilities.datastructures.StringTuple; /** * Performs post processing of conf files based on special parsing directives. @@ -50,17 +46,47 @@ // apply prefix directive ConfFileOption2 prefixOption = directives.get("prefixes"); if(prefixOption != null) { - List<StringTuple> prefixes = (List<StringTuple>) prefixOption.getValueObject(); + Map<String,String> prefixes = (Map<String,String>) prefixOption.getValueObject(); // loop through all options and replaces prefixes for(ConfFileOption2 option : confOptions) { - String value = option.getPropertyValue(); - for(StringTuple prefix : prefixes) { - // we only replace the prefix if it occurs directly after a quote - value = value.replaceAll("\"" + prefix.a + ":", "\"" + prefix.b); - } - option.setPropertyValue(value); + Object valueObject = option.getValue(); + + if(valueObject instanceof String){ + for (String prefix : prefixes.keySet()) { + // we only replace the prefix if it occurs directly after a quote + valueObject = ((String) valueObject).replaceAll(prefix + ":", prefixes.get(prefix)); + } + + }else{ + // Check for collections of string + if (valueObject instanceof Collection) { + processStringCollection(prefixes, (Collection) valueObject); + } + } + + option.setValueObject(valueObject); } } } + + private void processStringCollection(Map<String,String> prefixes, Collection valueObject) { + Map<String, String> oldNewStringValues = new HashMap<String, String>(); + Iterator itr = valueObject.iterator(); + while (itr.hasNext()) { + for (String prefix : prefixes.keySet()) { + Object nextObject = itr.next(); + if (nextObject instanceof String) { + String oldValue = (String) nextObject; + String newValue = oldValue.replaceAll( prefix + ":", prefixes.get(prefix)); + oldNewStringValues.put(oldValue, newValue); + } + } + } + + Collection<String> oldValues = oldNewStringValues.keySet(); + Collection<String> newValues = oldNewStringValues.values(); + valueObject.removeAll(oldValues); + valueObject.addAll(newValues); + } } Modified: trunk/interfaces/src/main/java/org/dllearner/confparser3/conf3.jj =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/confparser3/conf3.jj 2011-08-28 00:59:13 UTC (rev 3145) +++ trunk/interfaces/src/main/java/org/dllearner/confparser3/conf3.jj 2011-08-28 05:23:17 UTC (rev 3146) @@ -140,7 +140,7 @@ boolean containsSubOption=false; String value="", value1="", value2="", tmp="", tmp2=""; Set<String> values = new HashSet<String>(); - List<StringTuple> tuples = new LinkedList<StringTuple>(); + Map<String,String> tuples = new HashMap<String,String>(); ConfFileOption2 option = new ConfFileOption2(); boolean isBeanRef = false; @@ -184,9 +184,9 @@ // e.g. [("a","b"),("c","d")] | "[" ( LOOKAHEAD(6) "(" tmp=String() "," tmp2=String() ")" - {tuples.add(new StringTuple(tmp,tmp2)); propertyValue += "(\""+ tmp + "\",\"" + tmp2 + "\"), "; } "," )* + { tuples.put(tmp,tmp2); propertyValue += "(\""+ tmp + "\",\"" + tmp2 + "\"), "; } "," )* "(" tmp=String() "," tmp2=String() ")" - {tuples.add(new StringTuple(tmp,tmp2)); propertyValue += "(\""+ tmp + "\",\"" + tmp2 + "\")";} + { tuples.put(tmp,tmp2); propertyValue += "(\""+ tmp + "\",\"" + tmp2 + "\")";} "]" { val = tuples; propertyType = List.class; propertyValue = "["+ propertyValue + "]";} ) // <CONF_END> Modified: trunk/interfaces/src/test/java/org/dllearner/configuration/spring/ConfigurationBasedPropertyOverrideConfigurerTest.java =================================================================== --- trunk/interfaces/src/test/java/org/dllearner/configuration/spring/ConfigurationBasedPropertyOverrideConfigurerTest.java 2011-08-28 00:59:13 UTC (rev 3145) +++ trunk/interfaces/src/test/java/org/dllearner/configuration/spring/ConfigurationBasedPropertyOverrideConfigurerTest.java 2011-08-28 05:23:17 UTC (rev 3146) @@ -66,8 +66,13 @@ validateSecondBean(secondBean); validateThirdBean(secondBean.getComponent()); validateFourthBean(context.getBean("fourthBean", TestBean.class)); + validateFifthBean(context.getBean("fifthBean", TestBean.class)); } + private void validateFifthBean(TestBean fifthBean) { + Assert.assertTrue(fifthBean.getSimpleValue().equals("http://localhost/foo#test")); + } + private void validateThirdBean(TestBean thirdBean) { Assert.assertEquals(thirdBean.getIntValue(), (Integer) 3); TestBean fourthBean = thirdBean.getComponent(); Modified: trunk/interfaces/src/test/java/org/dllearner/confparser3/ParseTest.java =================================================================== --- trunk/interfaces/src/test/java/org/dllearner/confparser3/ParseTest.java 2011-08-28 00:59:13 UTC (rev 3145) +++ trunk/interfaces/src/test/java/org/dllearner/confparser3/ParseTest.java 2011-08-28 05:23:17 UTC (rev 3146) @@ -37,7 +37,7 @@ public void test() throws FileNotFoundException, ParseException { ConfParser parser = ConfParser.parseFile(new File("../examples/family/father.conf")); for(ConfFileOption2 option : parser.getConfOptions()) { - System.out.print(option.getBeanName() + "." + option.getPropertyName() + " = " + option.getPropertyValue()); + System.out.print(option.getBeanName() + "." + option.getPropertyName() + " = " + option.getValue().toString()); if(option.isBeanRef()) { System.out.println(" (bean reference)"); } else { Modified: trunk/interfaces/src/test/resources/org/dllearner/configuration/spring/configurationBasedPropertyOverrideConfigurer.conf =================================================================== --- trunk/interfaces/src/test/resources/org/dllearner/configuration/spring/configurationBasedPropertyOverrideConfigurer.conf 2011-08-28 00:59:13 UTC (rev 3145) +++ trunk/interfaces/src/test/resources/org/dllearner/configuration/spring/configurationBasedPropertyOverrideConfigurer.conf 2011-08-28 05:23:17 UTC (rev 3146) @@ -2,6 +2,8 @@ * A multi-line comment, which is ignored. */ +prefixes = [ ("kb","http://localhost/foo#") ] + // a single line comment, which is ignored testBean.simpleValue="simple value example" // simple value @@ -31,3 +33,6 @@ fourthBean.simpleValue="Fourth Bean - not specified in xml" fourthBean.type="org.dllearner.configuration.spring.TestBean" +fifthBean.type="org.dllearner.configuration.spring.TestBean" +fifthBean.simpleValue="kb:test" + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |