forked from Tamarik/CSCI4100_Spring12
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhtmlFile.java
119 lines (113 loc) · 4.89 KB
/
htmlFile.java
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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*this class uses the program latex2html, LatexFile class, and RetrieveXML class
* generates a temporary latex file to convert latex math mode to png files
* generates test directory that contains index.html this is the html test
* @author derek
*/
import java.io.*;
import java.util.Formatter;
import java.awt.List;
public class htmlFile {
private int qcount;
private RetrieveXML XMLretriever;
private LatexFile tex_file;
private List html_questions;
private String test_name;
/**
* constructor
* @throws IOException
*/
public htmlFile(String dpath) throws IOException{
XMLretriever = new RetrieveXML(dpath);
html_questions = new List();
tex_file = new LatexFile("temp.tex",dpath);
tex_file.WriteLatexHead("temp");
}
/**
* creates list of questions to add
* adds problems to temp.tex
* @param subject course subject title
* @param section lesson section
* @param difficulty difficulty of problems
* @param questionQuantity number of problems to add
*/
public void WritehtmlQuestions(String subject, double section, int difficulty, int questionQuantity){
List questionsbysubject = XMLretriever.returnByTopic(subject);
List questionsbysection = XMLretriever.returnBySection(section);
List questionsbydifficulty = XMLretriever.returnByDifficulty(difficulty);
List temp_list = new List();
List LatexQuestions = new List();
for(int c=0;c<questionsbysubject.getItemCount();c++){
for(int d=0;d<questionsbysection.getItemCount();d++){
if(questionsbysubject.getItem(c).compareTo(questionsbysection.getItem(d))==0)
temp_list.add(questionsbysection.getItem(d));
}
}
for(int c=0;c<temp_list.getItemCount();c++){
for(int d=0;d<questionsbydifficulty.getItemCount();d++){
if(temp_list.getItem(c).compareTo(questionsbydifficulty.getItem(d))==0)
LatexQuestions.add(questionsbydifficulty.getItem(c));
}
}
tex_file.WriteLatexQuestions(subject, section, difficulty, questionQuantity);
if(questionQuantity<=LatexQuestions.getItemCount()){
for(int c=0;c<questionQuantity;c++){
html_questions.add(LatexQuestions.getItem(c));
}
}
else{
System.out.println("Not enough questions in database. Adding " + LatexQuestions.getItemCount() + ".");
for(int c=0;c<LatexQuestions.getItemCount();c++)
html_questions.add(LatexQuestions.getItem(c));
}
}
/**
* depends on latex2html. latex2html generates png files from math mode
* sections in the temp.tex file. the directory created by latex2html
* is moved to a directory named after String testname. generates index.html
* and moves it to test directory
* @param testname name of html test to be generated
*/
public void GeneratehtmlTest(String testname){
String test_dir = testname;
test_dir = test_dir.replaceAll(" ", "_");
try{
tex_file.WriteLatexFoot();
Process p;
p = Runtime.getRuntime().exec("mkdir " + test_dir);
p.waitFor();
p=Runtime.getRuntime().exec("latex2html temp.tex");
p.waitFor();
p=Runtime.getRuntime().exec("mv temp " + test_dir);
p.waitFor();
p=Runtime.getRuntime().exec("rm temp.tex");
p.waitFor();
File html_test = new File("index.html");
if(!html_test.exists())
html_test.createNewFile();
Formatter html_test_io = new Formatter(html_test.getAbsolutePath());
html_test_io.format("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n");
html_test_io.format("<HTML>\n<HEAD>\n<TITLE>" + testname + "</TITLE>\n</HEAD>\n<BODY>\n\n");
html_test_io.format("<center><b>" + testname + "</b></center><br><br>");
for(int c=0;c<html_questions.getItemCount();c++){
html_test_io.format((c+1) + ") " + XMLretriever.returnTestData(html_questions.getItem(c), "latex_instructions") + "\n<br>\n");
html_test_io.format("<IMG SRC=\"temp/img" + (c+1) + ".png\">\n<br><br><br>\n");
}
html_test_io.format("\n\n</BODY>\n\n</HTML>");
html_test_io.close();
p=Runtime.getRuntime().exec("mv index.html " + test_dir);
p.waitFor();
}
catch(IOException e){
System.out.println("IOexception");
e.printStackTrace();
}
catch(InterruptedException d){
System.out.println("InterruptedException");
}
}
}