001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.modeldiscovery.provider;
043:
044: import java.io.File;
045: import java.util.ArrayList;
046: import java.util.HashMap;
047: import java.util.HashSet;
048: import java.util.Iterator;
049: import java.util.List;
050: import java.util.Map;
051: import org.netbeans.api.project.Project;
052: import org.netbeans.modules.cnd.api.model.CsmFile;
053: import org.netbeans.modules.cnd.api.model.CsmModelAccessor;
054: import org.netbeans.modules.cnd.api.model.CsmProject;
055: import org.netbeans.modules.cnd.api.project.NativeFileItem.Language;
056: import org.netbeans.modules.cnd.discovery.api.Configuration;
057: import org.netbeans.modules.cnd.discovery.api.DiscoveryProvider;
058: import org.netbeans.modules.cnd.discovery.api.ItemProperties;
059: import org.netbeans.modules.cnd.discovery.api.ProjectProperties;
060: import org.netbeans.modules.cnd.discovery.api.ProjectProxy;
061: import org.netbeans.modules.cnd.discovery.api.ProjectUtil;
062: import org.netbeans.modules.cnd.discovery.api.ProviderProperty;
063: import org.netbeans.modules.cnd.discovery.api.SourceFileProperties;
064: import org.netbeans.modules.cnd.makeproject.api.configurations.BooleanConfiguration;
065: import org.netbeans.modules.cnd.makeproject.api.configurations.ConfigurationDescriptorProvider;
066: import org.netbeans.modules.cnd.makeproject.api.configurations.Item;
067: import org.netbeans.modules.cnd.makeproject.api.configurations.ItemConfiguration;
068: import org.netbeans.modules.cnd.makeproject.api.configurations.MakeConfiguration;
069: import org.netbeans.modules.cnd.makeproject.api.configurations.MakeConfigurationDescriptor;
070: import org.openide.filesystems.FileUtil;
071: import org.openide.util.NbBundle;
072: import org.openide.util.Utilities;
073:
074: /**
075: *
076: * @author Alexander Simon
077: */
078: public class AnalyzeModel implements DiscoveryProvider {
079: private Map<String, ProviderProperty> myProperties = new HashMap<String, ProviderProperty>();
080: public static final String MODEL_FOLDER_KEY = "folder"; // NOI18N
081: protected boolean isStoped = false;
082:
083: public AnalyzeModel() {
084: clean();
085: }
086:
087: public void clean() {
088: myProperties.clear();
089: myProperties.put(MODEL_FOLDER_KEY, new ProviderProperty() {
090: private String myPath;
091:
092: public String getName() {
093: return i18n("Model_Files_Name"); // NOI18N
094: }
095:
096: public String getDescription() {
097: return i18n("Model_Files_Description"); // NOI18N
098: }
099:
100: public Object getValue() {
101: return myPath;
102: }
103:
104: public void setValue(Object value) {
105: if (value instanceof String) {
106: myPath = (String) value;
107: }
108: }
109:
110: public ProviderProperty.PropertyKind getKind() {
111: return ProviderProperty.PropertyKind.Folder;
112: }
113: });
114: }
115:
116: public String getID() {
117: return "model-folder"; // NOI18N
118: }
119:
120: public String getName() {
121: return i18n("Model_Provider_Name"); // NOI18N
122: }
123:
124: public String getDescription() {
125: return i18n("Model_Provider_Description"); // NOI18N
126: }
127:
128: public List<String> getPropertyKeys() {
129: return new ArrayList<String>(myProperties.keySet());
130: }
131:
132: public ProviderProperty getProperty(String key) {
133: return myProperties.get(key);
134: }
135:
136: public void stop() {
137: isStoped = true;
138: }
139:
140: public List<Configuration> analyze(ProjectProxy project) {
141: isStoped = false;
142: MyConfiguration conf = new MyConfiguration(project);
143: List<Configuration> confs = new ArrayList<Configuration>();
144: confs.add(conf);
145: return confs;
146: }
147:
148: private Map<String, List<String>> search(String root) {
149: HashSet<String> set = new HashSet<String>();
150: ArrayList<String> list = new ArrayList<String>();
151: list.add(root);
152: for (Iterator<String> it = list.iterator(); it.hasNext();) {
153: if (isStoped) {
154: break;
155: }
156: File f = new File(it.next());
157: gatherSubFolders(f, set);
158: }
159: HashMap<String, List<String>> map = new HashMap<String, List<String>>();
160: for (Iterator it = set.iterator(); it.hasNext();) {
161: if (isStoped) {
162: break;
163: }
164: File d = new File((String) it.next());
165: if (d.isDirectory()) {
166: File[] ff = d.listFiles();
167: for (int i = 0; i < ff.length; i++) {
168: if (ff[i].isFile()) {
169: List<String> l = map.get(ff[i].getName());
170: if (l == null) {
171: l = new ArrayList<String>();
172: map.put(ff[i].getName(), l);
173: }
174: String path = ff[i].getAbsolutePath();
175: if (Utilities.isWindows()) {
176: path = path.replace('\\', '/');
177: }
178: l.add(path);
179: }
180: }
181: }
182: }
183: return map;
184: }
185:
186: private void gatherSubFolders(File d, HashSet<String> set) {
187: if (isStoped) {
188: return;
189: }
190: if (d.isDirectory()) {
191: if (ProjectUtil.ignoreFolder(d)) {
192: return;
193: }
194: String path = d.getAbsolutePath();
195: if (Utilities.isWindows()) {
196: path = path.replace('\\', '/');
197: }
198: if (!set.contains(path)) {
199: set.add(path);
200: File[] ff = d.listFiles();
201: for (int i = 0; i < ff.length; i++) {
202: gatherSubFolders(ff[i], set);
203: }
204: }
205: }
206: }
207:
208: private static String i18n(String id) {
209: return NbBundle.getMessage(AnalyzeModel.class, id);
210: }
211:
212: public boolean isApplicable(ProjectProxy project) {
213: if (project.getProject() != null) {
214: Project makeProject = project.getProject();
215: CsmProject langProject = CsmModelAccessor.getModel()
216: .getProject(makeProject);
217: if (langProject != null/* && langProject.isStable(null)*/) {
218: return true;
219: }
220: }
221: return false;
222: }
223:
224: public int canAnalyze(ProjectProxy project) {
225: return 40;
226: }
227:
228: private class MyConfiguration implements Configuration {
229: private List<SourceFileProperties> myFileProperties;
230: private List<String> myIncludedFiles;
231: private MakeConfigurationDescriptor makeConfigurationDescriptor;
232: private CsmProject langProject;
233:
234: private MyConfiguration(ProjectProxy project) {
235: Project makeProject = project.getProject();
236: langProject = CsmModelAccessor.getModel().getProject(
237: makeProject);
238: ConfigurationDescriptorProvider pdp = makeProject
239: .getLookup().lookup(
240: ConfigurationDescriptorProvider.class);
241: makeConfigurationDescriptor = (MakeConfigurationDescriptor) pdp
242: .getConfigurationDescriptor();
243: }
244:
245: public List<ProjectProperties> getProjectConfiguration() {
246: return divideByLanguage(getSourcesConfiguration());
247: }
248:
249: protected List<ProjectProperties> divideByLanguage(
250: List<SourceFileProperties> sources) {
251: ModelProject cProp = null;
252: ModelProject cppProp = null;
253: for (SourceFileProperties source : sources) {
254: ItemProperties.LanguageKind lang = source
255: .getLanguageKind();
256: ModelProject current = null;
257: if (lang == ItemProperties.LanguageKind.C) {
258: if (cProp == null) {
259: cProp = new ModelProject(lang);
260: }
261: current = cProp;
262: } else {
263: if (cppProp == null) {
264: cppProp = new ModelProject(lang);
265: }
266: current = cppProp;
267: }
268: current.update(source);
269: }
270: List<ProjectProperties> languages = new ArrayList<ProjectProperties>();
271: if (cProp != null) {
272: languages.add(cProp);
273: }
274: if (cppProp != null) {
275: languages.add(cppProp);
276: }
277: return languages;
278: }
279:
280: public List<Configuration> getDependencies() {
281: return null;
282: }
283:
284: public boolean isExcluded(Item item) {
285: MakeConfiguration makeConfiguration = (MakeConfiguration) item
286: .getFolder().getConfigurationDescriptor()
287: .getConfs().getActive();
288: ItemConfiguration itemConfiguration = item
289: .getItemConfiguration(makeConfiguration); //ItemConfiguration)makeConfiguration.getAuxObject(ItemConfiguration.getId(item.getPath()));
290: if (itemConfiguration == null) {
291: return true;
292: }
293: BooleanConfiguration excl = itemConfiguration.getExcluded();
294: return excl.getValue();
295: }
296:
297: private List<SourceFileProperties> getSourceFileProperties(
298: String root) {
299: List<SourceFileProperties> res = new ArrayList<SourceFileProperties>();
300: Map<String, List<String>> searchBase = search(root);
301: Item[] items = makeConfigurationDescriptor
302: .getProjectItems();
303: for (int i = 0; i < items.length; i++) {
304: if (isStoped) {
305: break;
306: }
307: Item item = items[i];
308: if (isExcluded(item)) {
309: continue;
310: }
311: Language lang = item.getLanguage();
312: if (lang == Language.C || lang == Language.CPP) {
313: String path = item.getFile().getAbsolutePath();
314: CsmFile langFile = langProject.findFile(path);
315: SourceFileProperties source = new ModelSource(item,
316: langFile, searchBase);
317: res.add(source);
318: }
319: }
320: return res;
321: }
322:
323: public List<SourceFileProperties> getSourcesConfiguration() {
324: if (myFileProperties == null) {
325: myFileProperties = getSourceFileProperties((String) getProperty(
326: MODEL_FOLDER_KEY).getValue());
327: }
328: return myFileProperties;
329: }
330:
331: public List<String> getIncludedFiles() {
332: if (myIncludedFiles == null) {
333: HashSet<String> unique = new HashSet<String>();
334: Item[] items = makeConfigurationDescriptor
335: .getProjectItems();
336: for (int i = 0; i < items.length; i++) {
337: if (isStoped) {
338: break;
339: }
340: Item item = items[i];
341: if (isExcluded(item)) {
342: continue;
343: }
344: String path = item.getAbsPath();
345: File file = new File(path);
346: if (file.exists()) {
347: unique.add(FileUtil.normalizeFile(file)
348: .getAbsolutePath());
349: }
350: }
351: HashSet<String> unUnique = new HashSet<String>();
352: for (SourceFileProperties source : getSourcesConfiguration()) {
353: if (source instanceof ModelSource) {
354: unUnique.addAll(((ModelSource) source)
355: .getIncludedFiles());
356: }
357: }
358: for (String path : unUnique) {
359: File file = new File(path);
360: if (file.exists()) {
361: unique.add(FileUtil.normalizeFile(file)
362: .getAbsolutePath());
363: }
364: }
365: myIncludedFiles = new ArrayList<String>(unique);
366: }
367: return myIncludedFiles;
368: }
369: }
370: }
|