1
/*******************************************************************************
2
* Copyright (c) 2007, 2009 Symbian Software Systems and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Andrew Ferguson (Symbian) - Initial implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.core.index.export;
14
import java.util.ArrayList;
15
import java.util.Arrays;
16
import java.util.Calendar;
17
import java.util.Date;
18
import java.util.HashMap;
19
import java.util.List;
22
import com.ibm.icu.text.DateFormat;
23
import com.ibm.icu.text.MessageFormat;
25
import org.eclipse.cdt.core.CCorePlugin;
26
import org.eclipse.cdt.core.dom.IPDOMManager;
27
import org.eclipse.cdt.core.index.IIndexLocationConverter;
28
import org.eclipse.cdt.core.index.ResourceContainerRelativeLocationConverter;
29
import org.eclipse.cdt.core.model.CoreModel;
30
import org.eclipse.cdt.core.model.ICProject;
31
import org.eclipse.cdt.core.model.IPathEntry;
32
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
33
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
34
import org.eclipse.cdt.core.settings.model.extension.impl.CDefaultConfigurationData;
35
import org.eclipse.cdt.internal.core.index.IIndexFragment;
36
import org.eclipse.cdt.internal.core.pdom.indexer.IndexerPreferences;
37
import org.eclipse.core.resources.IFolder;
38
import org.eclipse.core.resources.IProject;
39
import org.eclipse.core.resources.IProjectDescription;
40
import org.eclipse.core.resources.IResource;
41
import org.eclipse.core.resources.IWorkspace;
42
import org.eclipse.core.resources.IWorkspaceRunnable;
43
import org.eclipse.core.resources.ResourcesPlugin;
44
import org.eclipse.core.runtime.CoreException;
45
import org.eclipse.core.runtime.IProgressMonitor;
46
import org.eclipse.core.runtime.NullProgressMonitor;
47
import org.eclipse.core.runtime.Path;
50
* An IExportProjectProvider suitable for indexing an external folder. The arguments understood by this provider
53
* <li>-source what will become the root of the indexed content
54
* <li>-include any preinclude files to configure the parser with
55
* <li>-id the id to write to the produce fragment
58
public class ExternalExportProjectProvider extends AbstractExportProjectProvider {
59
private static final String PREBUILT_PROJECT_OWNER = "org.eclipse.cdt.core.index.export.prebuiltOwner"; //$NON-NLS-1$
60
private static final String ORG_ECLIPSE_CDT_CORE_INDEX_EXPORT_DATESTAMP = "org.eclipse.cdt.core.index.export.datestamp"; //$NON-NLS-1$
61
private static final String CONTENT = "content"; //$NON-NLS-1$
62
public static final String OPT_SOURCE = "-source"; //$NON-NLS-1$
63
public static final String OPT_INCLUDE = "-include"; //$NON-NLS-1$
64
public static final String OPT_FRAGMENT_ID = "-id"; //$NON-NLS-1$
66
private IFolder content;
67
private String fragmentId;
69
public ExternalExportProjectProvider() {
75
* @see org.eclipse.cdt.core.index.export.IProjectForExportManager#createProject(java.util.Map)
77
public ICProject createProject() throws CoreException {
79
File source= new File(getSingleString(OPT_SOURCE));
80
if(!source.exists()) {
81
fail(MessageFormat.format(Messages.ExternalContentPEM_LocationToIndexNonExistent, new Object[] {source}));
85
List<String> includeFiles= new ArrayList<String>();
86
if(isPresent(OPT_INCLUDE)) {
87
includeFiles.addAll(getParameters(OPT_INCLUDE));
91
fragmentId= getSingleString(OPT_FRAGMENT_ID);
93
return createCCProject("__" + System.currentTimeMillis(), source, includeFiles); //$NON-NLS-1$
97
* Returns the project folder the external content is stored in
98
* @return the project folder the external content is stored in
100
protected IFolder getContentFolder() {
105
* Convenience method for creating a cproject
106
* @param projectName the name for the new project
107
* @param location the absolute path of some external content
108
* @param includeFiles a list of include paths to add to the project scanner
109
* @return a new project
110
* @throws CoreException
112
private ICProject createCCProject(final String projectName, final File location,
113
final List<String> includeFiles) throws CoreException {
114
final IWorkspace ws = ResourcesPlugin.getWorkspace();
115
final ICProject newProject[] = new ICProject[1];
117
ws.run(new IWorkspaceRunnable() {
118
public void run(IProgressMonitor monitor) throws CoreException {
119
IWorkspace workspace= ResourcesPlugin.getWorkspace();
120
IProject project= workspace.getRoot().getProject("__prebuilt_index_temp__" + System.currentTimeMillis()); //$NON-NLS-1$
121
IProjectDescription description = workspace.newProjectDescription(project.getName());
122
CCorePlugin.getDefault().createCProject(description, project, NPM, PREBUILT_PROJECT_OWNER);
123
CCorePlugin.getDefault().convertProjectFromCtoCC(project, NPM);
124
ICProjectDescription pd= CCorePlugin.getDefault().getProjectDescription(project, true);
125
newCfg(pd, project.getName(), "config"); //$NON-NLS-1$
127
CoreModel.getDefault().setProjectDescription(project, pd, true, new NullProgressMonitor());
129
ICProject cproject= CCorePlugin.getDefault().getCoreModel().create(project);
131
// External content appears under a linked folder
132
content= cproject.getProject().getFolder(CONTENT);
133
content.createLink(new Path(location.getAbsolutePath()), IResource.NONE, null);
135
// Setup path entries
136
List<IPathEntry> entries= new ArrayList<IPathEntry>(Arrays.asList(CoreModel.getRawPathEntries(cproject)));
139
for(String path : includeFiles) {
140
entries.add(CoreModel.newIncludeFileEntry(project.getFullPath(), new Path(path)));
143
// content directory is a source root
144
entries.add(CoreModel.newSourceEntry(content.getProjectRelativePath()));
146
// any additional entries
147
entries.addAll(getAdditionalRawEntries());
149
cproject.setRawPathEntries(entries.toArray(new IPathEntry[entries.size()]),
150
new NullProgressMonitor()
153
newProject[0]= cproject;
155
IndexerPreferences.set(newProject[0].getProject(), IndexerPreferences.KEY_INDEXER_ID, IPDOMManager.ID_NO_INDEXER);
156
IndexerPreferences.set(newProject[0].getProject(), IndexerPreferences.KEY_INDEX_ALL_FILES, Boolean.TRUE.toString());
157
IndexerPreferences.set(newProject[0].getProject(), IndexerPreferences.KEY_INDEX_UNUSED_HEADERS_WITH_DEFAULT_LANG, Boolean.TRUE.toString());
161
return newProject[0];
165
* Get additional raw entries (above those added as part of the ExternalExportProjectProvider functionality)
166
* @return a list of additional entries to add to the project
168
protected List<IPathEntry> getAdditionalRawEntries() {
169
List<IPathEntry> entries= new ArrayList<IPathEntry>();
170
entries.add(CoreModel.newIncludeEntry(content.getProjectRelativePath(), null, content.getLocation(), true));
174
private ICConfigurationDescription newCfg(ICProjectDescription des, String project, String config) throws CoreException {
175
CDefaultConfigurationData data= new CDefaultConfigurationData(project + "." + config, //$NON-NLS-1$
176
project + " " + config + " name", null); //$NON-NLS-1$ //$NON-NLS-2$
177
data.initEmptyData();
178
return des.createConfiguration(CCorePlugin.DEFAULT_PROVIDER_ID, data);
182
* @see org.eclipse.cdt.core.index.export.IExportProjectProvider#getLocationConverter(org.eclipse.cdt.core.model.ICProject)
184
public IIndexLocationConverter getLocationConverter(final ICProject cproject) {
185
return new ResourceContainerRelativeLocationConverter(content);
189
* @see org.eclipse.cdt.core.index.export.IExportProjectProvider#getExportProperties()
191
public Map<String, String> getExportProperties() {
192
Map<String, String> properties= new HashMap<String, String>();
193
Date now= Calendar.getInstance().getTime();
194
properties.put(ORG_ECLIPSE_CDT_CORE_INDEX_EXPORT_DATESTAMP,
195
DateFormat.getDateInstance().format(now)
196
+ " " + DateFormat.getTimeInstance().format(now)); //$NON-NLS-1$
197
properties.put(IIndexFragment.PROPERTY_FRAGMENT_ID, fragmentId);