~ubuntu-branches/ubuntu/utopic/eclipse-eclox/utopic

« back to all changes in this revision

Viewing changes to eclox.core/src/eclox/core/doxygen/Doxygen.java

  • Committer: Package Import Robot
  • Author(s): Graham Inggs
  • Date: 2013-07-07 20:33:10 UTC
  • Revision ID: package-import@ubuntu.com-20130707203310-a44yw80gqtc2s9ob
Tags: upstream-0.10.0
ImportĀ upstreamĀ versionĀ 0.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (C) 2003-2008, 2013, Guillaume Brocker
 
3
 * 
 
4
 * All rights reserved. This program and the accompanying materials
 
5
 * are made available under the terms of the Eclipse Public License v1.0
 
6
 * which accompanies this distribution, and is available at
 
7
 * http://www.eclipse.org/legal/epl-v10.html
 
8
 *
 
9
 * Contributors:
 
10
 *     Guillaume Brocker - Initial API and implementation
 
11
 *
 
12
 ******************************************************************************/ 
 
13
 
 
14
package eclox.core.doxygen;
 
15
 
 
16
import java.io.BufferedReader;
 
17
import java.io.IOException;
 
18
import java.io.InputStreamReader;
 
19
import java.util.regex.Matcher;
 
20
import java.util.regex.Pattern;
 
21
 
 
22
import org.eclipse.core.resources.IFile;
 
23
import org.eclipse.core.runtime.IPath;
 
24
 
 
25
import eclox.core.IPreferences;
 
26
import eclox.core.Plugin;
 
27
 
 
28
/**
 
29
 * Implements the abstract doxygen frontend. Sub-classes provides concret
 
30
 * doxygen access.
 
31
 * 
 
32
 * @author gbrocker
 
33
 */
 
34
public abstract class Doxygen {
 
35
        
 
36
        /**
 
37
         * a string containing defining the default doxygen command to use
 
38
         */
 
39
        protected final static String COMMAND_NAME = "doxygen";
 
40
                
 
41
        
 
42
        /**
 
43
         * Retrieves the default doxygen instance to use.
 
44
         */
 
45
        public static Doxygen getDefault() {
 
46
                final String    identifier      = Plugin.getDefault().getPluginPreferences().getString( IPreferences.DEFAULT_DOXYGEN );
 
47
                Doxygen                 doxygen         = null;
 
48
                
 
49
                // Try the creation of a default doxygen instance.
 
50
                doxygen = DefaultDoxygen.createFromIdentifier(identifier);
 
51
                if( doxygen != null ) {
 
52
                        return doxygen;
 
53
                }
 
54
 
 
55
                // Try the creation of a custom doxygen instance.
 
56
                doxygen = CustomDoxygen.createFromIdentifier(identifier);
 
57
                if( doxygen != null ) {
 
58
                        return doxygen;
 
59
                }
 
60
                
 
61
                // Try the creation of a bundled doxygen instance.
 
62
                doxygen = BundledDoxygen.createFromIdentifier(identifier);
 
63
                if( doxygen != null ) {
 
64
                        return doxygen;
 
65
                }
 
66
                
 
67
                // No doxygen could be created.
 
68
                return null;    
 
69
        }
 
70
        
 
71
        
 
72
        /**
 
73
         * Retrieves the version string of wrapped doxygen.
 
74
         * 
 
75
         * @return      a string containing the doxygen version string
 
76
         */
 
77
        public String getVersion() {
 
78
                try {
 
79
                        // Builds the command.
 
80
                        String[]        command = new String[2];
 
81
                        
 
82
                        command[0] = getCommand();
 
83
                        command[1] = "--help";
 
84
                        
 
85
                        // Runs the command and retrieves the version string.
 
86
                        Process                         doxygen = Runtime.getRuntime().exec( command );
 
87
                        BufferedReader          input   = new BufferedReader( new InputStreamReader(doxygen.getInputStream()) );
 
88
                        BufferedReader          error   = new BufferedReader( new InputStreamReader(doxygen.getErrorStream()) );
 
89
                        
 
90
                        // Matches the doxygen welcome message.
 
91
                        Pattern pattern = Pattern.compile( "^doxygen\\s+version\\s+([\\d\\.]+).*", Pattern.CASE_INSENSITIVE|Pattern.DOTALL );
 
92
                        Matcher matcher = pattern.matcher( input.readLine() );
 
93
                        
 
94
                        if( matcher.matches() ) {
 
95
                                return matcher.group( 1 ); 
 
96
                        }
 
97
                        else {
 
98
                                String  errorMessage = new String();
 
99
                                String  line;
 
100
                                while( (line = error.readLine()) != null ) {
 
101
                                        errorMessage = errorMessage.concat(line);
 
102
                                }
 
103
                                
 
104
                                throw new RuntimeException( "Unable to get doxygen version. " + errorMessage );
 
105
                        }
 
106
                }
 
107
                catch( Throwable t ) {
 
108
                        Plugin.log( t );
 
109
                        return null;
 
110
                }
 
111
        }
 
112
        
 
113
        /**
 
114
         * Launch a documentation build.
 
115
         * 
 
116
         * @param       file    the file representing the doxygen configuration to use for the build
 
117
         * 
 
118
         * @return      The process that run the build.
 
119
         */
 
120
        public Process build( IFile file ) throws InvokeException, RunException {
 
121
                if( file.exists() == false ) {
 
122
                        throw new RunException("Missing or bad doxyfile");
 
123
                }
 
124
                
 
125
                try {
 
126
                        String[]        command = new String[3];
 
127
                        
 
128
                        command[0] = getCommand();
 
129
                        command[1] = "-b";
 
130
                        command[2] = file.getLocation().makeAbsolute().toOSString();
 
131
 
 
132
// TODO This is code only supported by jre >= 1.5
 
133
//                      ProcessBuilder  processBuilder = new ProcessBuilder( command );
 
134
//                      processBuilder.directory( getDir(file).toFile() );
 
135
//                      processBuilder.redirectErrorStream( true );
 
136
//                      return processBuilder.start();
 
137
                        
 
138
                        return Runtime.getRuntime().exec( command, null, getDir(file).toFile() );
 
139
                }
 
140
                catch(IOException ioException) {
 
141
                        throw new InvokeException(ioException);
 
142
                }
 
143
        }
 
144
        
 
145
        /**
 
146
         * Generate an empty configuration file.
 
147
         * 
 
148
         * @param       file    the configuration file to generate.
 
149
         */     
 
150
        public void generate( IFile file ) throws InvokeException, RunException {
 
151
                try
 
152
                {
 
153
                        Process         process;
 
154
                        String[]        command = new String[3];
 
155
                        
 
156
                        // Build the command. 
 
157
                        command[0] = getCommand();
 
158
                        command[1] =  "-g";
 
159
                        command[2] = file.getLocation().toOSString();
 
160
                        
 
161
                        // Run the command and check for errors.
 
162
                        process = Runtime.getRuntime().exec( command, null );
 
163
                        if(process.waitFor() != 0) {
 
164
                                BufferedReader  reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
 
165
                                String                  errorMsg = new String();
 
166
                                String                  line;
 
167
                                
 
168
                                for(line=reader.readLine(); line != null; line=reader.readLine()) {
 
169
                                        errorMsg = errorMsg.concat(line);
 
170
                                }
 
171
                                throw new RunException( errorMsg );
 
172
                        }
 
173
                        
 
174
                        // Force some refresh to display the file.
 
175
                        file.refreshLocal( 0, null );
 
176
                }
 
177
                catch( RunException runException ) {
 
178
                        throw runException;
 
179
                }
 
180
                catch( SecurityException securityException ) {
 
181
                        throw new InvokeException(securityException);
 
182
                }
 
183
                catch( IOException ioException ) {
 
184
                        throw new InvokeException(ioException);
 
185
                }
 
186
                catch( Throwable throwable ) {
 
187
                        Plugin.log(throwable);
 
188
                }
 
189
        }
 
190
        
 
191
        
 
192
        /**
 
193
         * Retrieves the string containing the command line to the doxygen binary.
 
194
         * Sub-classes must implement this method.
 
195
         * 
 
196
         * @return      a string containing the path to the doxygen binary file
 
197
         */
 
198
        public abstract String getCommand();
 
199
        
 
200
        
 
201
        /**
 
202
         * Retrieves the description string of the doxygen wrapper instance.
 
203
         * 
 
204
         * @return      a string containing the description of the dowygen wrapper
 
205
         */
 
206
        public abstract String getDescription();
 
207
        
 
208
        
 
209
        /**
 
210
         * Retrieves the identifier of the doxygen wrapper.
 
211
         * 
 
212
         * @return      a string containing the doxygen wrapper identifier
 
213
         */
 
214
        public abstract String getIdentifier();
 
215
        
 
216
        
 
217
        /**
 
218
         * Retrieve the diretory of the specified file.
 
219
         * 
 
220
         * @param       file    The file for which the directory must be retrieved.
 
221
         * 
 
222
         * @return      The path of the containing directory.
 
223
         */
 
224
        private static IPath getDir( IFile file ) {
 
225
                return file.getLocation().makeAbsolute().removeLastSegments( 1 );
 
226
        }
 
227
                
 
228
}