KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > FreeformEvaluator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.ant.freeform;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
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 JavaDoc;
40
41 /**
42  * Manages property evaluation for the freeform project.
43  * Refreshes properties if (1) project.xml changes; (2) some *.properties changes.
44  * @author Jesse Glick
45  */

46 final class FreeformEvaluator implements PropertyEvaluator, AntProjectListener, PropertyChangeListener JavaDoc {
47
48     private final FreeformProject project;
49     private PropertyEvaluator delegate;
50     private final List JavaDoc<PropertyChangeListener JavaDoc> listeners = new ArrayList JavaDoc<PropertyChangeListener JavaDoc>();
51     private final Set JavaDoc<PropertyEvaluator> intermediateEvaluators = new HashSet JavaDoc<PropertyEvaluator>();
52     
53     public FreeformEvaluator(FreeformProject project) throws IOException JavaDoc {
54         this.project = project;
55         init();
56         project.helper().addAntProjectListener(this);
57     }
58     
59     private void init() throws IOException JavaDoc {
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 JavaDoc {
71         // Stop listening to old intermediate evaluators.
72
Iterator JavaDoc<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 JavaDoc<PropertyProvider> defs = new ArrayList JavaDoc<PropertyProvider>();
79         Element JavaDoc genldata = project.getPrimaryConfigurationData();
80         Element JavaDoc properties = Util.findElement(genldata, "properties", FreeformProjectType.NS_GENERAL); // NOI18N
81
if (properties != null) {
82             for (Element JavaDoc e : Util.findSubElements(properties)) {
83                 if (e.getLocalName().equals("property")) { // NOI18N
84
String JavaDoc val = Util.findText(e);
85                     if (val == null) {
86                         val = "";
87                     }
88                     defs.add(PropertyUtils.fixedPropertyProvider(Collections.singletonMap(e.getAttribute("name"), val))); // NOI18N
89
} else {
90                     assert e.getLocalName().equals("property-file") : e;
91                     String JavaDoc fname = Util.findText(e);
92                     if (fname.contains("${")) { // NOI18N
93
// Tricky (#48230): need to listen to changes in the location of the file as well as its contents.
94
PropertyEvaluator intermediate = PropertyUtils.sequentialPropertyEvaluator(preprovider, defs.toArray(new PropertyProvider[defs.size()]));
95                         fname = intermediate.evaluate(fname);
96                         // Listen to changes in it, too.
97
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 JavaDoc getProperty(String JavaDoc prop) {
108         return delegate.getProperty(prop);
109     }
110     
111     public String JavaDoc evaluate(String JavaDoc text) {
112         return delegate.evaluate(text);
113     }
114     
115     public Map JavaDoc<String JavaDoc,String JavaDoc> getProperties() {
116         return delegate.getProperties();
117     }
118     
119     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
120         listeners.add(listener);
121     }
122     
123     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
124         listeners.remove(listener);
125     }
126     
127     private void fireChange(String JavaDoc prop) {
128         PropertyChangeListener JavaDoc[] _listeners;
129         synchronized (this) {
130             if (listeners.isEmpty()) {
131                 return;
132             }
133             _listeners = listeners.toArray(new PropertyChangeListener JavaDoc[listeners.size()]);
134         }
135         PropertyChangeEvent JavaDoc ev = new PropertyChangeEvent JavaDoc(this, prop, null, null);
136         for (PropertyChangeListener JavaDoc 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 JavaDoc ex) {
149             ErrorManager.getDefault().notify(ErrorManager.ERROR, ex);
150         } catch (RuntimeException JavaDoc ex) {
151             // Something else? E.g. IAE when parsing <properties> block.
152
ErrorManager.getDefault().notify(ErrorManager.ERROR, ex);
153         }
154         fireChange(null);
155     }
156     
157     public void propertiesChanged(AntProjectEvent ev) {
158         // ignore
159
}
160     
161     public void propertyChange(PropertyChangeEvent JavaDoc propertyChangeEvent) {
162         Object JavaDoc source = propertyChangeEvent.getSource();
163         assert source instanceof PropertyEvaluator : source;
164         if (intermediateEvaluators.contains(source)) {
165             // A <property-file> may have changed location. Generally need to rebuild the list of definers.
166
fireAnyChange();
167         } else {
168             // If a properties file changes on disk, we refire that from the delegate.
169
assert source == delegate : "Got change from " + source + " rather than current delegate " + delegate;
170             fireChange(propertyChangeEvent.getPropertyName());
171         }
172     }
173     
174 }
175
Popular Tags