summaryrefslogtreecommitdiff
path: root/core/source/org/openoffice/ide/eclipse/core/builders/RegmergeBuilder.java
blob: e9435cf794ff4c7c48b01215973cda71b9053da2 (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
/*************************************************************************
 *
 * $RCSfile: RegmergeBuilder.java,v $
 *
 * $Revision: 1.11 $
 *
 * last change: $Author: cedricbosdo $ $Date: 2007/11/25 20:32:27 $
 *
 * 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 org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.openoffice.ide.eclipse.core.model.IUnoidlProject;
import org.openoffice.plugin.core.utils.FileHelper;

/**
 * Builder for the URD files generating the <code>types.rdb</code> registry.
 * 
 * <p>This builder should not be associated directly
 * to a UNO project: the right builder for this is {@link TypesBuilder}. 
 * This builder doesn't make any difference between full and incremental 
 * builds.</p>
 * 
 * @author cedricbosdo
 *
 */
public class RegmergeBuilder {
        
    /**
     * Root of the generated types, used by regmerge and javamaker. UCR 
     * is chosen for OpenOffice.org compatibility 
     */
    public static final String TYPE_ROOT_KEY = "/UCR"; //$NON-NLS-1$
    
    /**
     * Computes the full build of all the <code>urd</code> files into a single
     * <code>types.rdb</code> file. This resulting file is given by 
     * {@link IUnoidlProject#getTypesPath()}. This methods simply launches the
     * {@link RegmergeBuildVisitor} on the urd folder.
     * 
     * @param pUnoprj the project to build
     * @param pMonitor a monitor to watch the build progress
     * @throws Exception is thrown is anything wrong happens
     */
    public static void build(IUnoidlProject pUnoprj, IProgressMonitor pMonitor) throws Exception {
        
        IProject prj = ResourcesPlugin.getWorkspace().getRoot().getProject(pUnoprj.getName());

        IFile typesFile = pUnoprj.getFile(pUnoprj.getTypesPath());
        File mergeFile = prj.getLocation().append(typesFile.getProjectRelativePath()).toFile();
        if (mergeFile != null && mergeFile.exists()) {
            FileHelper.remove(mergeFile);
        }

        // merge each urd file
        IFolder urdFolder = pUnoprj.getFolder(pUnoprj.getUrdPath());
        IPath urdPath = prj.getLocation().append(urdFolder.getProjectRelativePath());
        File urdFile = urdPath.toFile();
        VisitableFile visitableUrd = new VisitableFile(urdFile);
        visitableUrd.accept(new RegmergeBuildVisitor(pUnoprj, pMonitor));
    }

    /**
     * Convenience method to execute the <code>regmerge</code> tool 
     * on a given file.
     * 
     * @param pFile the file to run <code>regmerge</code> on.
     * @param pUnoprj the UNO project on which to run the <code>regmerge</code> tool
     * @param pMonitor a progress monitor
     */
    static void runRegmergeOnFile(File pFile, IUnoidlProject pUnoprj, IProgressMonitor pMonitor) {
        
        // The registry file is placed in the root of the project as announced 
        // to the api-dev mailing-list
        IFile mergeFile = pUnoprj.getFile(pUnoprj.getTypesPath());
        
        String existingReg = ""; //$NON-NLS-1$
        if (mergeFile.exists()) {
            existingReg = mergeFile.getProjectRelativePath().toOSString() + " "; //$NON-NLS-1$
        }
        
        String command = "regmerge types.rdb " + TYPE_ROOT_KEY + " " + //$NON-NLS-1$ //$NON-NLS-2$
                           existingReg + "\"" + pFile.getAbsolutePath() + "\""; //$NON-NLS-1$ //$NON-NLS-2$
        
        // Process creation. Need to set the PATH value using OOo path: due to some tools changes in 3.1
        String[] sPaths = pUnoprj.getOOo().getBinPath();
        String sPathValue = "PATH="; //$NON-NLS-1$
        for (String sPath : sPaths) {
            if ( !sPathValue.endsWith("=")) { //$NON-NLS-1$
                sPathValue += System.getProperty("path.separator"); //$NON-NLS-1$
            }
            sPathValue += sPath;
        }
        
        IProject prj = ResourcesPlugin.getWorkspace().getRoot().getProject( pUnoprj.getName() );
        Process process = pUnoprj.getSdk().runToolWithEnv( prj, 
                pUnoprj.getOOo(), command, new String[]{ sPathValue }, pMonitor);
        
        // Just wait for the process to end before destroying it
        try {
            process.waitFor();
        } catch (InterruptedException e) {
            // Process has been interrupted by the user
        }
    }
}