5 import java
.io
.FileWriter
;
6 import java
.io
.IOException
;
9 import com
.topcoder
.client
.contestant
.ProblemComponentModel
;
10 import com
.topcoder
.shared
.language
.Language
;
11 import com
.topcoder
.shared
.problem
.DataType
;
12 import com
.topcoder
.shared
.problem
.Renderer
;
13 import com
.topcoder
.shared
.problem
.TestCase
;
16 * @author Charles McGarvey
17 * The TopCoder Arena editor plug-in providing support for Vim.
19 * Distributable under the terms and conditions of the 2-clause BSD license;
20 * see the file COPYING for a complete text of the license.
25 * The problem ID number.
30 * The name of the class.
35 * The path of the current source file.
37 private File sourceFile
;
40 * The path of the problem directory.
42 private File directory
;
46 * Map languages names to file extensions.
48 private static final Map
<String
,String
> languageExtension
= new HashMap
<String
,String
>();
51 languageExtension
.put("Java", "java");
52 languageExtension
.put("C++", "cc");
53 languageExtension
.put("C#", "cs");
54 languageExtension
.put("VB", "vb");
55 languageExtension
.put("Python", "py");
60 * Construct an editor with the problem objects given us by the Arena.
61 * @param component A container for the particulars of the problem.
62 * @param language The currently selected language.
63 * @param renderer A helper object to help format the problem statement.
64 * @throws Exception If the editor could not set itself up.
66 public Editor(ProblemComponentModel component
,
68 Renderer renderer
) throws Exception
70 this.id
= String
.valueOf(component
.getProblem().getProblemID());
71 this.name
= component
.getClassName();
73 // Make sure the top-level vimcoder directory exists.
74 File topDir
= VimCoder
.getStorageDirectory();
75 if (!topDir
.isDirectory())
77 if (!topDir
.mkdirs()) throw new IOException(topDir
.getPath());
80 // Make sure the problem directory exists.
81 this.directory
= new File(topDir
, id
);
82 if (!directory
.isDirectory())
84 if (!directory
.mkdirs()) throw new IOException(directory
.getPath());
87 String lang
= language
.getName();
88 String ext
= languageExtension
.get(lang
);
90 // Set up the terms used for the template expansion.
91 HashMap
<String
,String
> terms
= new HashMap
<String
,String
>();
92 terms
.put("RETURNTYPE", component
.getReturnType().getDescriptor(language
).replaceAll("\\s+", ""));
93 terms
.put("CLASSNAME", name
);
94 terms
.put("METHODNAME", component
.getMethodName());
95 terms
.put("METHODPARAMS", getMethodParams(component
.getParamTypes(),
96 component
.getParamNames(),
98 terms
.put("METHODPARAMNAMES", Util
.join(component
.getParamNames(), ", "));
99 terms
.put("METHODPARAMSTREAMIN", Util
.join(component
.getParamNames(), " >> "));
100 terms
.put("METHODPARAMSTREAMOUT", Util
.join(component
.getParamNames(), " << \", \" << "));
101 terms
.put("METHODPARAMDECLARES", getMethodParamDeclarations(component
.getParamTypes(),
102 component
.getParamNames(),
105 // Write the problem statement as an HTML file in the problem directory.
106 File problemFile
= new File(directory
, "Problem.html");
107 if (!problemFile
.canRead())
109 FileWriter writer
= new FileWriter(problemFile
);
112 writer
.write(renderer
.toHTML(language
));
120 // Expand the template for the main class and write it to the current
122 sourceFile
= new File(directory
, name
+ "." + ext
);
123 if (!sourceFile
.canRead())
125 String text
= Util
.expandTemplate(readTemplate(lang
+ "Template"),
127 FileWriter writer
= new FileWriter(sourceFile
);
132 // Expand the driver template and write it to a source file.
133 File driverFile
= new File(directory
, "driver." + ext
);
134 if (!driverFile
.canRead())
136 String text
= Util
.expandTemplate(readTemplate(lang
+ "Driver"),
138 FileWriter writer
= new FileWriter(driverFile
);
143 // Write the test cases to a text file. The driver code can read this
144 // file and perform the tests based on what it reads.
145 File testcaseFile
= new File(directory
, "testcases.txt");
146 if (!testcaseFile
.canRead())
148 StringBuilder text
= new StringBuilder();
149 if (component
.hasTestCases())
151 for (TestCase testCase
: component
.getTestCases())
153 text
.append(testCase
.getOutput() + System
.getProperty("line.separator"));
154 for (String input
: testCase
.getInput())
156 text
.append(input
+ System
.getProperty("line.separator"));
160 FileWriter writer
= new FileWriter(testcaseFile
);
161 writer
.write(text
.toString());
165 // Finally, expand the Makefile template and write it.
166 File makeFile
= new File(directory
, "Makefile");
168 String text
= Util
.expandTemplate(readTemplate(lang
+ "Makefile"),
170 FileWriter writer
= new FileWriter(makeFile
);
177 * Save the source code provided by the server, and tell the Vim server to
178 * edit the current source file.
179 * @param source The source code.
180 * @throws Exception If the source couldn't be written or the Vim server
183 public void setSource(String source
) throws Exception
185 FileWriter writer
= new FileWriter(new File(directory
, name
));
186 writer
.write(source
);
188 sendVimCommand("--remote-tab-silent", sourceFile
.getPath());
192 * Read the source code from the current source file.
193 * @return The source code.
194 * @throws IOException If the source file could not be read.
196 public String
getSource() throws IOException
198 return Util
.readFile(sourceFile
) + "\n// Edited by " +
199 VimCoder
.version
+ "\n// " + VimCoder
.website
+ "\n\n";
204 * Send a command to the Vim server.
205 * If the server isn't running, it will be started with the name
206 * VIMCODER#### where #### is the problem ID.
207 * @param command The command to send to the server.
208 * @param argument A single argument for the remote command.
209 * @throws Exception If the command could not be sent.
211 private void sendVimCommand(String command
,
212 String argument
) throws Exception
214 String
[] arguments
= {argument
};
215 sendVimCommand(command
, arguments
);
219 * Send a command to the Vim server.
220 * If the server isn't running, it will be started with the name
221 * VIMCODER#### where #### is the problem ID.
222 * @param command The command to send to the server.
223 * @param argument Arguments for the remote command.
224 * @throws Exception If the command could not be sent.
226 private void sendVimCommand(String command
,
227 String
[] arguments
) throws Exception
229 String
[] vimCommand
= VimCoder
.getVimCommand().split("\\s");
230 String
[] flags
= {"--servername", "VimCoder" + id
, command
};
231 vimCommand
= Util
.concat(vimCommand
, flags
);
232 vimCommand
= Util
.concat(vimCommand
, arguments
);
233 Process child
= Runtime
.getRuntime().exec(vimCommand
, null, directory
);
235 /* FIXME: This is a pretty bad hack. The problem is that the Vim
236 * process doesn't fork to the background on some systems, so we
237 * can't wait on the child. At the same time, calling this method
238 * before the previous child could finish initializing the server
239 * may result in multiple editor windows popping up. We'd also
240 * like to be able to get the return code from the child if we can.
241 * The workaround here is to stall the thread for a little while or
242 * until we see that the child exits. If the child never exits
243 * before the timeout, we will assume it is not backgrounding and
244 * that everything worked. This works as long as the Vim server is
245 * able to start within the stall period. */
246 long expire
= System
.currentTimeMillis() + 250;
247 while (System
.currentTimeMillis() < expire
)
252 int exitCode
= child
.exitValue();
253 if (exitCode
!= 0) throw new Exception("Vim process returned exit code " + exitCode
+ ".");
256 catch (IllegalThreadStateException exception
)
258 // The child has not exited; intentionally ignoring exception.
266 * We first look in the storage directory. If we can't find one, we
267 * look among the resources.
268 * @param tName The name of the template.
269 * @return The contents of the template file, or an empty string.
271 private String
readTemplate(String tName
)
273 File templateFile
= new File(VimCoder
.getStorageDirectory(), tName
);
276 if (templateFile
.canRead()) return Util
.readFile(templateFile
);
277 return Util
.readResource(tName
);
279 catch (IOException exception
)
287 * Convert an array of data types to an array of strings according to a
289 * @param types The data types.
290 * @param language The language to use in the conversion.
291 * @return The array of string representations of the data types.
293 private String
[] getStringTypes(DataType
[] types
, Language language
)
295 String
[] strings
= new String
[types
.length
];
296 for (int i
= 0; i
< types
.length
; ++i
)
298 strings
[i
] = types
[i
].getDescriptor(language
).replaceAll("\\s+", "");
304 * Combine the data types and parameter names into a comma-separated list of
305 * the method parameters.
306 * The result could be used inside the parentheses of a method
308 * @param types The data types of the parameters.
309 * @param names The names of the parameters.
310 * @param language The language used for representing the data types.
311 * @return The list of parameters.
313 private String
getMethodParams(DataType
[] types
,
317 String
[] typeStrings
= getStringTypes(types
, language
);
318 return Util
.join(Util
.combine(typeStrings
, names
, " "), ", ");
322 * Combine the data types and parameter names into a group of variable
324 * Each declaration is separated by a new line and terminated with a
326 * @param types The data types of the parameters.
327 * @param names The names of the parameters.
328 * @param language The language used for representing the data types.
329 * @return The parameters as a block of declarations.
331 private String
getMethodParamDeclarations(DataType
[] types
,
335 final String end
= ";" + System
.getProperty("line.separator");
336 String
[] typeStrings
= getStringTypes(types
, language
);
337 return Util
.join(Util
.combine(typeStrings
, names
, "\t"), end
) + end
;