summaryrefslogtreecommitdiff
path: root/core/source/org/openoffice/ide/eclipse/core/builders/TypesBuilder.java
blob: d1036c2c7400b1c57e706565b2bc136f1b221141 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*************************************************************************
 *
 * $RCSfile: TypesBuilder.java,v $
 *
 * $Revision: 1.15 $
 *
 * last change: $Author: cedricbosdo $ $Date: 2008/12/13 13:42:50 $
 *
 * The Contents of this file are made available subject to the terms of
 * the GNU Lesser General Public License Version 2.1
 *
 * Sun Microsystems Inc., October, 2000
 *
 *
 * GNU Lesser General Public License Version 2.1
 * =============================================
 * Copyright 2000 by Sun Microsystems, Inc.
 * 901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 * 
 * The Initial Developer of the Original Code is: Sun Microsystems, Inc..
 *
 * Copyright: 2002 by Sun Microsystems, Inc.
 *
 * All Rights Reserved.
 *
 * Contributor(s): Cedric Bosdonnat
 *
 *
 ************************************************************************/
package org.openoffice.ide.eclipse.core.builders;

import java.io.File;
import java.util.Map;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.openoffice.ide.eclipse.core.OOEclipsePlugin;
import org.openoffice.ide.eclipse.core.PluginLogger;
import org.openoffice.ide.eclipse.core.model.IUnoidlProject;
import org.openoffice.ide.eclipse.core.model.ProjectsManager;
import org.openoffice.ide.eclipse.core.model.config.ISdk;
import org.openoffice.ide.eclipse.core.model.language.ILanguageBuilder;

/**
 * <p>Main builder of the UNO-IDL projects, it computes the language specific
 * type files and types registry from the <code>idl</code> files. In order
 * to split the work, the different tasks have been split into several builders:
 *   <ul>
 *     <li>{@link RegmergeBuilder} merging the urd files into the types 
 *            registry</li>
 *     <li>{@link ILanguageBuilder#generateFromTypes(
 *          ISdk, org.openoffice.ide.eclipse.core.preferences.IOOo, IProject, File, File, String, IProgressMonitor)}
 *            generating the language specific type files</li>
 *   </ul>
 * </p>
 * 
 * @author Cedric Bosdonnat
 *
 */
public class TypesBuilder extends IncrementalProjectBuilder {

    /**
     * The builder ID as set in the <code>plugin.xml</code> file.
     */
    public static final String BUILDER_ID = OOEclipsePlugin.OOECLIPSE_PLUGIN_ID + ".types"; //$NON-NLS-1$
    
    public static final int IDLC_STATE = 1;

    public static final int REGMERGE_STATE = 2;

    public static final int GENERATE_TYPES_STATE = 3;

    public static final int COMPLETED_STATE = 4;

    public static final int NOT_STARTED_STATE = -1;
    
    static int sBuildState = NOT_STARTED_STATE;
    
    private boolean mChangedIdl = false;
    
    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    protected IProject[] build(int pKind, Map pArgs, IProgressMonitor pMonitor) throws CoreException {
        
        mChangedIdl = false;
        
        if (sBuildState < 0) {
            IResourceDelta delta = getDelta(getProject());
            if (delta != null) {
                delta.accept(new IResourceDeltaVisitor() {
                    public boolean visit(IResourceDelta pDelta) throws CoreException {

                        boolean visitChildren = false;

                        IProject prj = getProject();
                        IUnoidlProject unoprj = ProjectsManager.getProject(prj.getName());
                        IPath idlPath = unoprj.getIdlPath();
                        IPath resPath = pDelta.getResource().getProjectRelativePath();

                        if (pDelta.getResource() instanceof IContainer && 
                                resPath.segmentCount() < idlPath.segmentCount()) {
                            visitChildren = true;
                        } else if (pDelta.getResource() instanceof IContainer && 
                                resPath.toString().startsWith(idlPath.toString())) {
                            visitChildren = true;
                        } else {
                            if ((pDelta.getResource() instanceof IFile) && 
                                    resPath.getFileExtension().equals("idl")) { //$NON-NLS-1$
                                visitChildren = false;
                                mChangedIdl = true;
                            } else if ((pDelta.getResource() instanceof IFile) && 
                                    resPath.toString().endsWith(unoprj.getTypesPath().toString())) {
                                sBuildState = COMPLETED_STATE;
                            }
                        }
                        return visitChildren;
                    }
                });
            } else {
                mChangedIdl = true;
            }

            if (mChangedIdl && sBuildState < 0) {
                try {
                    build(getProject(), pMonitor);
                } catch (Exception e) {
                    sBuildState = NOT_STARTED_STATE;
                    CoreException thrown = new CoreException(new Status(
                            Status.ERROR, OOEclipsePlugin.OOECLIPSE_PLUGIN_ID, 
                            Messages.getString("TypesBuilder.BuildError0"), e)); //$NON-NLS-1$
                    if (!(e instanceof CoreException)) {
                        thrown = (CoreException)e;
                    }
                    throw thrown;
                }
                sBuildState = NOT_STARTED_STATE;
            } else if (sBuildState == COMPLETED_STATE) {
                sBuildState = NOT_STARTED_STATE;
            }
        }
        
        return null;
    }
    
    /**
     * Build the types of a project.
     * 
     * @param pPrj the project to build
     * @param pMonitor a monitor to report the build progress
     * 
     * @throws Exception if anything wrong happens during the build
     */
    public static void build(IProject pPrj, IProgressMonitor pMonitor) throws Exception {

        IUnoidlProject unoprj = ProjectsManager.getProject(
                pPrj.getName());
        
        // Clears the registries before beginning
        sBuildState = IDLC_STATE;
        removeAllRegistries(pPrj);
        buildIdl(unoprj, pMonitor);
        
        sBuildState = REGMERGE_STATE;
        RegmergeBuilder.build(unoprj, pMonitor);
        
        sBuildState = GENERATE_TYPES_STATE;
        File types = pPrj.getLocation().append(unoprj.getTypesPath()).toFile();
        File build = pPrj.getLocation().append(unoprj.getBuildPath()).toFile();
        
        ILanguageBuilder languageBuilder = unoprj.getLanguage().getLanguageBuidler();
        languageBuilder.generateFromTypes(
                unoprj.getSdk(),
                unoprj.getOOo(),
                pPrj,
                types, build,
                unoprj.getRootModule(), pMonitor);
        
        pPrj.refreshLocal(IResource.DEPTH_INFINITE, pMonitor);
        sBuildState = NOT_STARTED_STATE;
    }
    
    /**
     * Removes all the registries, ie <code>.urd</code> and 
     * <code>types.rdb</code> files.
     * 
     * @param pPrj the project from which to remove the registries
     */
    private static void removeAllRegistries(IProject pPrj) {
        
        IUnoidlProject unoprj = ProjectsManager.getProject(
                pPrj.getName());
        
        try {
            IPath rdbPath = unoprj.getTypesPath();
            IFile rdbFile = pPrj.getFile(rdbPath);
            if (rdbFile.exists()) {
                rdbFile.delete(true, null);
            }
            
            IPath urdPath = unoprj.getUrdPath();
            IFolder urdFolder = pPrj.getFolder(urdPath);
            IResource[] members = urdFolder.members();
            
            for (int i = 0, length = members.length; i < length; i++) {
                IResource resi = members[i];
                if (resi.exists()) {
                    resi.delete(true, null);
                }
            }
            
        } catch (CoreException e) {
            PluginLogger.debug(e.getMessage());
        }
    }
    
    /**
     * Runs the idl files compilation.
     * 
     * @param pProject the uno project to build
     * @param pMonitor a monitor to watch the progress
     * @throws Exception if anything wrong happened
     */
    public static void buildIdl(IUnoidlProject pProject, IProgressMonitor pMonitor) throws Exception {
        
        // compile each idl file
        IFolder idlFolder = pProject.getFolder(
                pProject.getRootModulePath());
        idlFolder.accept(new IdlcBuildVisitor(pMonitor));
    }
    
    /**
     * Convenience method to execute the <code>idlc</code> tool on a given
     * file.
     * 
     * @param pFile the file to run <code>idlc</code> on.
     * @param pMonitor a progress monitor
     */
    static void runIdlcOnFile(IFile pFile, IProgressMonitor pMonitor) {
        
        IUnoidlProject project = ProjectsManager.getProject(pFile.getProject().getName());
        
        ISdk sdk = project.getSdk();
        
        if (null != sdk) {
            
            // Get local references to the SDK used members
            String sdkHome = sdk.getHome();
            
            Path sdkPath = new Path(sdkHome);
            int segmentCount = project.getIdlPath().segmentCount();
            
            IPath outputLocation = project.getUrdPath().append(
                    pFile.getProjectRelativePath().removeLastSegments(1).
                    removeFirstSegments(segmentCount));
            
            String command = "idlc -O \"" + outputLocation.toOSString() + "\"" + //$NON-NLS-1$ //$NON-NLS-2$
                " -I \"" + sdkPath.append("idl").toOSString() + "\"" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                " -I \"" + project.getIdlPath().toOSString() + "\"" +  //$NON-NLS-1$ //$NON-NLS-2$
                " " + pFile.getProjectRelativePath().toOSString();  //$NON-NLS-1$
            
            Process process = project.getSdk().runTool(project, command, pMonitor);
            
            IdlcErrorReader errorReader = new IdlcErrorReader(
                    process.getErrorStream(), pFile);
            errorReader.readErrors();
        }
    }
}