4 import java
.io
.BufferedReader
;
6 import java
.io
.FileReader
;
7 import java
.io
.FileWriter
;
8 import java
.io
.IOException
;
9 import java
.io
.InputStream
;
12 import com
.topcoder
.client
.contestant
.ProblemComponentModel
;
13 import com
.topcoder
.shared
.language
.Language
;
14 import com
.topcoder
.shared
.problem
.DataType
;
15 import com
.topcoder
.shared
.problem
.Renderer
;
16 import com
.topcoder
.shared
.problem
.TestCase
;
19 * @author Charles McGarvey
26 private File sourceFile
;
27 private File directory
;
30 private static final Map
<String
,String
> languageExtension
= new HashMap
<String
,String
>();
33 languageExtension
.put("Java", "java");
34 languageExtension
.put("C++", "cc");
35 languageExtension
.put("C#", "cs");
36 languageExtension
.put("VB", "vb");
37 languageExtension
.put("Python", "py");
41 public Editor(ProblemComponentModel component
,
43 Renderer renderer
) throws IOException
45 this.id
= String
.valueOf(component
.getProblem().getProblemID());
46 this.name
= component
.getClassName();
48 File topDir
= new File(System
.getProperty("user.home"), ".vimcoder");
49 if (!topDir
.isDirectory())
51 if (!topDir
.mkdirs()) throw new IOException(topDir
.getPath());
54 this.directory
= new File(topDir
, id
);
55 if (!directory
.isDirectory())
57 if (!directory
.mkdirs()) throw new IOException(directory
.getPath());
60 String lang
= language
.getName();
61 String ext
= languageExtension
.get(lang
);
63 HashMap
<String
,String
> terms
= new HashMap
<String
,String
>();
64 terms
.put("RETURNTYPE", component
.getReturnType().getDescriptor(language
).replaceAll("\\s+", ""));
65 terms
.put("CLASSNAME", name
);
66 terms
.put("METHODNAME", component
.getMethodName());
67 terms
.put("METHODPARAMS", getMethodParams(component
.getParamTypes(),
68 component
.getParamNames(),
70 terms
.put("METHODPARAMNAMES", Utility
.join(component
.getParamNames(), ", "));
71 terms
.put("METHODPARAMSTREAMIN", Utility
.join(component
.getParamNames(), " >> "));
72 terms
.put("METHODPARAMSTREAMOUT", Utility
.join(component
.getParamNames(), " << "));
73 terms
.put("METHODPARAMDECLARES", getMethodParamDeclarations(component
.getParamTypes(),
74 component
.getParamNames(),
77 File problemFile
= new File(directory
, "Problem.html");
78 if (!problemFile
.canRead())
80 FileWriter writer
= new FileWriter(problemFile
);
83 writer
.write(renderer
.toHTML(language
));
85 catch (Exception exception
)
91 sourceFile
= new File(directory
, name
+ "." + ext
);
92 if (!sourceFile
.canRead())
94 String text
= Utility
.expandTemplate(Utility
.readResource(lang
+ "Template"),
96 FileWriter writer
= new FileWriter(sourceFile
);
101 File testcaseFile
= new File(directory
, "testcases.txt");
102 if (!testcaseFile
.canRead())
104 StringBuilder text
= new StringBuilder();
105 if (component
.hasTestCases())
107 for (TestCase testCase
: component
.getTestCases())
109 text
.append(testCase
.getOutput() + System
.getProperty("line.separator"));
110 for (String input
: testCase
.getInput())
112 text
.append(input
+ System
.getProperty("line.separator"));
116 FileWriter writer
= new FileWriter(testcaseFile
);
117 writer
.write(text
.toString());
121 File driverFile
= new File(directory
, "driver." + ext
);
122 if (!driverFile
.canRead())
124 String text
= Utility
.expandTemplate(Utility
.readResource(lang
+ "Driver"),
126 FileWriter writer
= new FileWriter(driverFile
);
131 File makeFile
= new File(directory
, "Makefile");
133 String text
= Utility
.expandTemplate(Utility
.readResource(lang
+ "Makefile"),
135 FileWriter writer
= new FileWriter(makeFile
);
141 public void setSource(String source
) throws Exception
143 FileWriter writer
= new FileWriter(new File(directory
, name
));
144 writer
.write(source
);
146 doVimCommand("--remote-tab-silent", sourceFile
.getPath());
149 public String
getSource() throws IOException
151 return Utility
.readFile(sourceFile
) + "\n// Edited by " + VimCoder
.version
+ "\n// " + VimCoder
.website
+ "\n\n";
155 private void doVimCommand(String command
, String argument
) throws Exception
157 String
[] arguments
= {argument
};
158 doVimCommand(command
, arguments
);
161 private void doVimCommand(String command
, String
[] arguments
) throws Exception
163 String
[] exec
= {"gvim", "--servername", "VimCoder" + id
,
165 exec
= Utility
.concat(exec
, arguments
);
166 Process child
= Runtime
.getRuntime().exec(exec
, null, directory
);
168 long expire
= System
.currentTimeMillis() + 500;
169 while (System
.currentTimeMillis() < expire
)
173 int exitCode
= child
.exitValue();
174 if (exitCode
!= 0) throw new Exception("Vim process returned exit code " + exitCode
+ ".");
177 catch (IllegalThreadStateException exception
)
184 private String
getMethodParams(DataType
[] types
,
188 StringBuilder text
= new StringBuilder();
190 text
.append(types
[0].getDescriptor(language
).replaceAll("\\s+", "") + " " + names
[0]);
191 for (int i
= 1; i
< names
.length
; ++i
)
193 text
.append(", " + types
[i
].getDescriptor(language
).replaceAll("\\s+", "") + " " + names
[i
]);
196 return text
.toString();
199 private String
getMethodParamDeclarations (DataType
[] types
,
203 StringBuilder text
= new StringBuilder();
205 for (int i
= 0; i
< names
.length
; ++i
)
207 text
.append(types
[i
].getDescriptor(language
).replaceAll("\\s+", "") + "\t" + names
[i
] + ";" + System
.getProperty("line.separator"));
210 return text
.toString();