1 19 package org.netbeans.modules.web.project.classpath; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.util.List ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.beans.PropertyChangeListener ; 26 import java.beans.PropertyChangeSupport ; 27 import java.net.MalformedURLException ; 28 import java.io.File ; 29 import java.net.URL ; 30 import org.netbeans.spi.java.classpath.ClassPathImplementation; 31 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 32 import org.netbeans.spi.project.support.ant.AntProjectHelper; 33 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 34 import org.netbeans.spi.project.support.ant.PropertyUtils; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.util.WeakListeners; 37 38 41 final class ProjectClassPathImplementation implements ClassPathImplementation, PropertyChangeListener { 42 43 private PropertyChangeSupport support = new PropertyChangeSupport (this); 44 private AntProjectHelper helper; 45 private String expression; 46 private boolean isProperty; 47 private String resolved; 48 private List resources; 49 private final PropertyEvaluator evaluator; 50 51 57 public ProjectClassPathImplementation(AntProjectHelper helper, String expression, PropertyEvaluator evaluator, boolean isProperty) { 58 assert helper != null && expression != null; 59 this.helper = helper; 60 this.evaluator = evaluator; 61 this.expression = expression; 62 this.isProperty = isProperty; 63 evaluator.addPropertyChangeListener(WeakListeners.propertyChange(this, evaluator)); 64 if (!isProperty) { 65 resolved = evaluator.evaluate (expression); 66 } 67 } 68 69 75 public ProjectClassPathImplementation(AntProjectHelper helper, String propertyName, PropertyEvaluator evaluator) { 76 this (helper, propertyName, evaluator, true); 77 } 78 79 public synchronized List getResources() { 80 if (this.resources == null) { 81 this.resources = this.getPath (); 82 } 83 return this.resources; 84 } 85 86 public void addPropertyChangeListener(PropertyChangeListener listener) { 87 support.addPropertyChangeListener (listener); 88 } 89 90 public void removePropertyChangeListener(PropertyChangeListener listener) { 91 support.removePropertyChangeListener (listener); 92 } 93 94 95 public void propertyChange(PropertyChangeEvent evt) { 96 if (isProperty && !evt.getPropertyName().equals(expression)) { 97 return; 99 } 100 if (!isProperty) { 101 String eval = evaluator.evaluate (expression); 102 if (eval.equals(resolved)) { 103 return; 104 } else { 105 resolved = eval; 106 } 107 } 108 109 List newRoots = getPath (); 110 boolean fire = false; 111 synchronized (this) { 112 if (this.resources != null && !this.resources.equals(newRoots)) { 113 this.resources = newRoots; 114 fire = true; 115 } 116 } 117 if (fire) { 118 support.firePropertyChange (PROP_RESOURCES,null,null); 119 } 120 } 121 122 private List getPath() { 123 List result = new ArrayList (); 124 String prop = isProperty ? evaluator.getProperty(expression) : resolved; 125 if (prop != null) { 126 String [] pieces = PropertyUtils.tokenizePath(prop); 127 for (int i = 0; i < pieces.length; i++) { 128 File f = helper.resolveFile(pieces[i]); 129 try { 130 URL entry = f.toURI().toURL(); 131 if (FileUtil.isArchiveFile(entry)) { 132 entry = FileUtil.getArchiveRoot(entry); 133 } else if (!f.exists()) { 134 assert !entry.toExternalForm().endsWith("/") : f; entry = new URL (entry.toExternalForm() + "/"); } 140 result.add(ClassPathSupport.createResource(entry)); 141 } catch (MalformedURLException mue) { 142 assert false : mue; 143 } 144 } 145 } 146 return Collections.unmodifiableList(result); 147 } 148 149 } 150 | Popular Tags |