forked from Tamarik/CSCI4100_Spring12
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLatexFile.java
110 lines (100 loc) · 4.29 KB
/
LatexFile.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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*this class depends on RetrieveXML class
* generates latex test from database of questions
* @author derek
*/
import java.io.*;
import java.util.Formatter;
import java.awt.List;
public class LatexFile {
private Formatter latex_file_io;
private File latex_file;
private int qcount;
private RetrieveXML XMLretriever;
protected List LatexQuestions;
/**
* Constructor to create latex file for test
* @param path the path of the filename for the test's latex file
* @param dpath the path to the database file
* @throws IOException
*/
public LatexFile(String path, String dpath) throws IOException{
XMLretriever = new RetrieveXML(dpath);
latex_file = new File(path);
if(!latex_file.exists()){
latex_file.createNewFile();
}
latex_file_io = new Formatter(latex_file.getAbsolutePath());
qcount=1;
}
/**
* Sets up the header for the test's latex file
* @param testname the name of the test
*/
public void WriteLatexHead(String testname){
latex_file_io.format("\\documentclass[11pt,a4paper]{article}\n\\usepackage{amsmath,amsthm}\n\n\\newcommand{\\fn}[1]{{\\tt #1}}\n\\newcommand{\\cn}[1]{{\\tt \\char\"5C #1}}\n\n\\title{");
latex_file_io.format(testname);
latex_file_io.format("}\n\n\\begin{document}\n\n\\maketitle\n\n");
}
/**
* add instructions for problems on the test
* @param instructions instructions for problems
*/
public void WriteLatexInstructions(String instructions){
latex_file_io.format("\n\\section{" + instructions + "}\n");
}
/**
* Will add questions to the test from a section that are of certain difficulty
* and will add no more than questionQuantity number of questions
* @param subject course subject title
* @param section section of course material
* @param difficulty difficulty of questions
* @param questionQuantity number of questions
*/
public void WriteLatexQuestions(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();
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(d));
}
}
if(questionQuantity<=LatexQuestions.getItemCount()){
for(int c=0;c<questionQuantity;c++){
latex_file_io.format(qcount + ") " + XMLretriever.returnTestData(LatexQuestions.getItem(c), "latex_instructions"));
latex_file_io.format("\n\n$" + XMLretriever.returnTestData(LatexQuestions.getItem(c), "latex_q") + "$\n\n");
latex_file_io.format("\\vfill");
qcount++;
}
}
else{
System.out.println("Not enough questions in database. Adding " + LatexQuestions.getItemCount());
for(int c=0;c<LatexQuestions.getItemCount();c++){
latex_file_io.format(qcount + ") " + XMLretriever.returnTestData(LatexQuestions.getItem(c), "latex_instructions"));
latex_file_io.format("\n\n$" + XMLretriever.returnTestData(LatexQuestions.getItem(c), "latex_q") + "$\n\n");
qcount++;
}
}
}
/**
* writes foot of latex test file and closes the formatter
*/
public void WriteLatexFoot(){
latex_file_io.format("\n\n\\end{document}");
latex_file_io.close();
}
}