1 9 package com.vladium.emma.ant; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Properties ; 17 18 import org.apache.tools.ant.BuildException; 19 import org.apache.tools.ant.Task; 20 21 import com.vladium.emma.EMMAProperties; 22 import com.vladium.util.IProperties; 23 import com.vladium.util.Property; 24 25 35 public 36 class GenericCfg 37 { 38 40 41 public GenericCfg (final Task task) 42 { 43 if (task == null) throw new IllegalArgumentException ("null input: task"); 44 45 m_task = task; 46 m_genericPropertyElements = new ArrayList (); 47 } 48 49 50 52 public void setProperties (final File file) 53 { 54 m_settingsFile = file; } 56 57 59 public PropertyElement createProperty () 60 { 61 m_genericSettings = null; 62 63 final PropertyElement property = new PropertyElement (); 64 m_genericPropertyElements.add (property); 65 66 return property; 67 } 68 69 71 public IProperties getFileSettings () 72 { 73 IProperties fileSettings = m_fileSettings; 74 if ((fileSettings == null) && (m_settingsFile != null)) 75 { 76 try 77 { 78 fileSettings = EMMAProperties.wrap (Property.getPropertiesFromFile (m_settingsFile)); 79 } 80 catch (IOException ioe) 81 { 82 throw (BuildException) SuppressableTask.newBuildException (m_task.getTaskName () 83 + ": property file [" + m_settingsFile.getAbsolutePath () + "] could not be read" , ioe, m_task.getLocation ()).fillInStackTrace (); 84 } 85 86 m_fileSettings = fileSettings; 87 88 return fileSettings; 89 } 90 91 return fileSettings; 92 } 93 94 public IProperties getGenericSettings () 95 { 96 IProperties genericSettings = m_genericSettings; 97 if (genericSettings == null) 98 { 99 genericSettings = EMMAProperties.wrap (new Properties ()); 100 101 for (Iterator i = m_genericPropertyElements.iterator (); i.hasNext (); ) 102 { 103 final PropertyElement property = (PropertyElement) i.next (); 104 105 final String name = property.getName (); 106 String value = property.getValue (); 107 if (value == null) value = ""; 108 109 if (name != null) 110 { 111 113 final String currentValue = genericSettings.getProperty (name); 114 if ((currentValue != null) && ! value.equals (currentValue)) 115 { 116 throw (BuildException) SuppressableTask.newBuildException (m_task.getTaskName () 117 + ": conflicting settings for property [" + name + "]: [" + value + "]" , m_task.getLocation ()).fillInStackTrace (); 118 } 119 else 120 { 121 genericSettings.setProperty (name, value); 122 } 123 } 124 } 125 126 m_genericSettings = genericSettings; 127 128 return genericSettings; 129 } 130 131 return genericSettings; 132 } 133 134 136 138 140 141 private final Task m_task; 142 143 private final List m_genericPropertyElements; private File m_settingsFile; 146 private transient IProperties m_fileSettings; private transient IProperties m_genericSettings; 149 } | Popular Tags |