1 19 20 package org.netbeans.modules.ant.freeform; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.Collections ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.Set ; 32 import org.netbeans.modules.ant.freeform.spi.support.Util; 33 import org.netbeans.spi.project.support.ant.AntProjectEvent; 34 import org.netbeans.spi.project.support.ant.AntProjectListener; 35 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 36 import org.netbeans.spi.project.support.ant.PropertyProvider; 37 import org.netbeans.spi.project.support.ant.PropertyUtils; 38 import org.openide.ErrorManager; 39 import org.w3c.dom.Element ; 40 41 46 final class FreeformEvaluator implements PropertyEvaluator, AntProjectListener, PropertyChangeListener { 47 48 private final FreeformProject project; 49 private PropertyEvaluator delegate; 50 private final List <PropertyChangeListener > listeners = new ArrayList <PropertyChangeListener >(); 51 private final Set <PropertyEvaluator> intermediateEvaluators = new HashSet <PropertyEvaluator>(); 52 53 public FreeformEvaluator(FreeformProject project) throws IOException { 54 this.project = project; 55 init(); 56 project.helper().addAntProjectListener(this); 57 } 58 59 private void init() throws IOException { 60 if (delegate != null) { 61 delegate.removePropertyChangeListener(this); 62 } 63 delegate = initEval(); 64 delegate.addPropertyChangeListener(this); 65 if (org.netbeans.modules.ant.freeform.Util.err.isLoggable(ErrorManager.INFORMATIONAL)) { 66 org.netbeans.modules.ant.freeform.Util.err.log("properties for " + project.getProjectDirectory() + ": " + delegate.getProperties()); 67 } 68 } 69 70 private PropertyEvaluator initEval() throws IOException { 71 Iterator <PropertyEvaluator> ieIt = intermediateEvaluators.iterator(); 73 while (ieIt.hasNext()) { 74 ieIt.next().removePropertyChangeListener(this); 75 ieIt.remove(); 76 } 77 PropertyProvider preprovider = project.helper().getStockPropertyPreprovider(); 78 List <PropertyProvider> defs = new ArrayList <PropertyProvider>(); 79 Element genldata = project.getPrimaryConfigurationData(); 80 Element properties = Util.findElement(genldata, "properties", FreeformProjectType.NS_GENERAL); if (properties != null) { 82 for (Element e : Util.findSubElements(properties)) { 83 if (e.getLocalName().equals("property")) { String val = Util.findText(e); 85 if (val == null) { 86 val = ""; 87 } 88 defs.add(PropertyUtils.fixedPropertyProvider(Collections.singletonMap(e.getAttribute("name"), val))); } else { 90 assert e.getLocalName().equals("property-file") : e; 91 String fname = Util.findText(e); 92 if (fname.contains("${")) { PropertyEvaluator intermediate = PropertyUtils.sequentialPropertyEvaluator(preprovider, defs.toArray(new PropertyProvider[defs.size()])); 95 fname = intermediate.evaluate(fname); 96 intermediate.addPropertyChangeListener(this); 98 intermediateEvaluators.add(intermediate); 99 } 100 defs.add(PropertyUtils.propertiesFilePropertyProvider(project.helper().resolveFile(fname))); 101 } 102 } 103 } 104 return PropertyUtils.sequentialPropertyEvaluator(preprovider, defs.toArray(new PropertyProvider[defs.size()])); 105 } 106 107 public String getProperty(String prop) { 108 return delegate.getProperty(prop); 109 } 110 111 public String evaluate(String text) { 112 return delegate.evaluate(text); 113 } 114 115 public Map <String ,String > getProperties() { 116 return delegate.getProperties(); 117 } 118 119 public void addPropertyChangeListener(PropertyChangeListener listener) { 120 listeners.add(listener); 121 } 122 123 public void removePropertyChangeListener(PropertyChangeListener listener) { 124 listeners.remove(listener); 125 } 126 127 private void fireChange(String prop) { 128 PropertyChangeListener [] _listeners; 129 synchronized (this) { 130 if (listeners.isEmpty()) { 131 return; 132 } 133 _listeners = listeners.toArray(new PropertyChangeListener [listeners.size()]); 134 } 135 PropertyChangeEvent ev = new PropertyChangeEvent (this, prop, null, null); 136 for (PropertyChangeListener l : _listeners) { 137 l.propertyChange(ev); 138 } 139 } 140 141 public void configurationXmlChanged(AntProjectEvent ev) { 142 fireAnyChange(); 143 } 144 145 private void fireAnyChange() { 146 try { 147 init(); 148 } catch (IOException ex) { 149 ErrorManager.getDefault().notify(ErrorManager.ERROR, ex); 150 } catch (RuntimeException ex) { 151 ErrorManager.getDefault().notify(ErrorManager.ERROR, ex); 153 } 154 fireChange(null); 155 } 156 157 public void propertiesChanged(AntProjectEvent ev) { 158 } 160 161 public void propertyChange(PropertyChangeEvent propertyChangeEvent) { 162 Object source = propertyChangeEvent.getSource(); 163 assert source instanceof PropertyEvaluator : source; 164 if (intermediateEvaluators.contains(source)) { 165 fireAnyChange(); 167 } else { 168 assert source == delegate : "Got change from " + source + " rather than current delegate " + delegate; 170 fireChange(propertyChangeEvent.getPropertyName()); 171 } 172 } 173 174 } 175 | Popular Tags |