1
/*******************************************************************************
2
* Copyright (C) 2003-2008, 2013, Guillaume Brocker
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
10
* Guillaume Brocker - Initial API and implementation
12
******************************************************************************/
14
package eclox.core.doxygen;
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;
22
import org.eclipse.core.resources.IFile;
23
import org.eclipse.core.runtime.IPath;
25
import eclox.core.IPreferences;
26
import eclox.core.Plugin;
29
* Implements the abstract doxygen frontend. Sub-classes provides concret
34
public abstract class Doxygen {
37
* a string containing defining the default doxygen command to use
39
protected final static String COMMAND_NAME = "doxygen";
43
* Retrieves the default doxygen instance to use.
45
public static Doxygen getDefault() {
46
final String identifier = Plugin.getDefault().getPluginPreferences().getString( IPreferences.DEFAULT_DOXYGEN );
47
Doxygen doxygen = null;
49
// Try the creation of a default doxygen instance.
50
doxygen = DefaultDoxygen.createFromIdentifier(identifier);
51
if( doxygen != null ) {
55
// Try the creation of a custom doxygen instance.
56
doxygen = CustomDoxygen.createFromIdentifier(identifier);
57
if( doxygen != null ) {
61
// Try the creation of a bundled doxygen instance.
62
doxygen = BundledDoxygen.createFromIdentifier(identifier);
63
if( doxygen != null ) {
67
// No doxygen could be created.
73
* Retrieves the version string of wrapped doxygen.
75
* @return a string containing the doxygen version string
77
public String getVersion() {
79
// Builds the command.
80
String[] command = new String[2];
82
command[0] = getCommand();
83
command[1] = "--help";
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()) );
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() );
94
if( matcher.matches() ) {
95
return matcher.group( 1 );
98
String errorMessage = new String();
100
while( (line = error.readLine()) != null ) {
101
errorMessage = errorMessage.concat(line);
104
throw new RuntimeException( "Unable to get doxygen version. " + errorMessage );
107
catch( Throwable t ) {
114
* Launch a documentation build.
116
* @param file the file representing the doxygen configuration to use for the build
118
* @return The process that run the build.
120
public Process build( IFile file ) throws InvokeException, RunException {
121
if( file.exists() == false ) {
122
throw new RunException("Missing or bad doxyfile");
126
String[] command = new String[3];
128
command[0] = getCommand();
130
command[2] = file.getLocation().makeAbsolute().toOSString();
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();
138
return Runtime.getRuntime().exec( command, null, getDir(file).toFile() );
140
catch(IOException ioException) {
141
throw new InvokeException(ioException);
146
* Generate an empty configuration file.
148
* @param file the configuration file to generate.
150
public void generate( IFile file ) throws InvokeException, RunException {
154
String[] command = new String[3];
156
// Build the command.
157
command[0] = getCommand();
159
command[2] = file.getLocation().toOSString();
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();
168
for(line=reader.readLine(); line != null; line=reader.readLine()) {
169
errorMsg = errorMsg.concat(line);
171
throw new RunException( errorMsg );
174
// Force some refresh to display the file.
175
file.refreshLocal( 0, null );
177
catch( RunException runException ) {
180
catch( SecurityException securityException ) {
181
throw new InvokeException(securityException);
183
catch( IOException ioException ) {
184
throw new InvokeException(ioException);
186
catch( Throwable throwable ) {
187
Plugin.log(throwable);
193
* Retrieves the string containing the command line to the doxygen binary.
194
* Sub-classes must implement this method.
196
* @return a string containing the path to the doxygen binary file
198
public abstract String getCommand();
202
* Retrieves the description string of the doxygen wrapper instance.
204
* @return a string containing the description of the dowygen wrapper
206
public abstract String getDescription();
210
* Retrieves the identifier of the doxygen wrapper.
212
* @return a string containing the doxygen wrapper identifier
214
public abstract String getIdentifier();
218
* Retrieve the diretory of the specified file.
220
* @param file The file for which the directory must be retrieved.
222
* @return The path of the containing directory.
224
private static IPath getDir( IFile file ) {
225
return file.getLocation().makeAbsolute().removeLastSegments( 1 );