Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. /**
  2. * Default constructor for the QuestionSaver
  3. */
  4. public QuestionSaver(){
  5.  
  6. }
  7.  
  8. public void saveQuestion(Question question) throws ParserConfigurationException {
  9. DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
  10. DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  11.  
  12. // create the quiz root element
  13. Document doc = docBuilder.newDocument();
  14. Element rootElement = doc.createElement("question");
  15. doc.appendChild(rootElement);
  16.  
  17. save(rootElement, doc, question);
  18. }
  19.  
  20. /**
  21. * General method for saving the question to the xml file
  22. */
  23. private void save(Element rootElement, Document doc, Question question) {
  24. /* first, save the question type independent properties */
  25. SaveIndependentProperties(rootElement, doc, question);
  26.  
  27. /* Save the generated Document to an XML file */
  28. try {
  29. SaveToFile(doc);
  30. } catch (TransformerConfigurationException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. }
  35.  
  36. /**
  37. * Saves the created document to an XML file
  38. *
  39. * @throws TransformerConfigurationException
  40. */
  41. private void SaveToFile(Document doc) throws TransformerConfigurationException {
  42. try {
  43. TransformerFactory transformerFactory = TransformerFactory.newInstance();
  44. Transformer transformer = transformerFactory.newTransformer();
  45. DOMSource source = new DOMSource(doc);
  46. // StreamResult result = new StreamResult(new File("C:\file.xml"));
  47.  
  48. // Output to console for testing
  49. StreamResult result = new StreamResult(System.out);
  50.  
  51. transformer.transform(source, result);
  52. } catch (TransformerException e) {
  53. System.out.println("TransformerException got thrown!");
  54. }
  55.  
  56. System.out.println("File saved!");
  57.  
  58. }
  59.  
  60. /**
  61. * Saves the properties of a question, which are independent from the questiontype
  62. *
  63. * @param rootElement
  64. */
  65. private void SaveIndependentProperties(Element rootElement, Document doc, Question question) {
  66. /* add the actual question */
  67. Element actualquestion = doc.createElement("actualquestion");
  68. actualquestion.appendChild(doc.createTextNode(question.getQuestion()));
  69. rootElement.appendChild(actualquestion);
  70.  
  71. /* add the score */
  72. Element score = doc.createElement("score");
  73. score.appendChild(doc.createTextNode(Integer.toString(question.getScore())));
  74. rootElement.appendChild(score);
  75.  
  76. /* add the type */
  77. QuestionType questionType = question.getType();
  78. Element type = doc.createElement("type");
  79. type.appendChild(doc.createTextNode(getQuestionTypeString(questionType)));
  80. rootElement.appendChild(type);
  81.  
  82. /* add the source */
  83. Element source = doc.createElement("source");
  84. source.appendChild(doc.createTextNode(question.getSource()));
  85. rootElement.appendChild(source);
  86.  
  87. /* add the extra information */
  88. Element extraInformation = doc.createElement("extrainformation");
  89. extraInformation.appendChild(doc.createTextNode(question.getExtraInformation()));
  90. rootElement.appendChild(extraInformation);
  91.  
  92. }
  93.  
  94. /**
  95. * Converts a QuestionType to a string representing the QuestionType
  96. *
  97. * @param questionType the QuestionType
  98. * @return
  99. */
  100. private String getQuestionTypeString(QuestionType questionType) {
  101. if (questionType == QuestionType.MULTIPLEANSWER)
  102. return "multipleanswer";
  103. else if (questionType == QuestionType.MULTIPLECHOICE)
  104. return "multiplechoice";
  105. else
  106. return "speed";
  107. }
  108.  
  109. static void main(String args[]) {
  110. try {
  111. new QuestionSaver().saveQuestion(question);
  112.  
  113. } catch (Exception e) {
  114. e.printStackTrace();
  115. }
  116.  
  117. public class QuestionSaver {
  118.  
  119. /**
  120. * Public constructor for the QuestionSaver
  121. *
  122. * @param question the question to be saved
  123. * @throws ParserConfigurationException
  124. */
  125. public QuestionSaver(){
  126.  
  127. }
  128.  
  129. public void saveQuestion(Question question) throws ParserConfigurationException {
  130. DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
  131. DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  132.  
  133. // create the quiz root element
  134. Document doc = docBuilder.newDocument();
  135. Element rootElement = doc.createElement("question");
  136. doc.appendChild(rootElement);
  137.  
  138. save(rootElement, doc, question);
  139. }
  140.  
  141. /**
  142. * General method for saving the question to the xml file
  143. */
  144. private void save(Element rootElement, Document doc, Question question) {
  145. /* first, save the question type independent properties */
  146. SaveIndependentProperties(rootElement, doc, question);
  147.  
  148. /* Save the generated Document to an XML file */
  149. try {
  150. SaveToFile(doc);
  151. } catch (TransformerConfigurationException e) {
  152. // TODO Auto-generated catch block
  153. e.printStackTrace();
  154. }
  155. }
  156.  
  157. /**
  158. * Saves the created document to an XML file
  159. *
  160. * @throws TransformerConfigurationException
  161. */
  162. private void SaveToFile(Document doc) throws TransformerConfigurationException {
  163. try {
  164. TransformerFactory transformerFactory = TransformerFactory.newInstance();
  165. Transformer transformer = transformerFactory.newTransformer();
  166. DOMSource source = new DOMSource(doc);
  167. // StreamResult result = new StreamResult(new File("C:\file.xml"));
  168.  
  169. // Output to console for testing
  170. StreamResult result = new StreamResult(System.out);
  171.  
  172. transformer.transform(source, result);
  173. } catch (TransformerException e) {
  174. System.out.println("Fout met transformen!");
  175. }
  176.  
  177. System.out.println("File saved!");
  178.  
  179. }
  180.  
  181. /**
  182. * Saves the properties of a question, which are independent from the questiontype
  183. *
  184. * @param rootElement
  185. */
  186. private void SaveIndependentProperties(Element rootElement, Document doc, Question question) {
  187. /* add the actual question */
  188. Element actualquestion = doc.createElement("actualquestion");
  189. actualquestion.appendChild(doc.createTextNode(question.getQuestion()));
  190. rootElement.appendChild(actualquestion);
  191.  
  192. /* add the score */
  193. Element score = doc.createElement("score");
  194. score.appendChild(doc.createTextNode(Integer.toString(question.getScore())));
  195. rootElement.appendChild(score);
  196.  
  197. /* add the type */
  198. QuestionType questionType = question.getType();
  199. Element type = doc.createElement("type");
  200. type.appendChild(doc.createTextNode(getQuestionTypeString(questionType)));
  201. rootElement.appendChild(type);
  202.  
  203. /* add the source */
  204. Element source = doc.createElement("source");
  205. source.appendChild(doc.createTextNode(question.getSource()));
  206. rootElement.appendChild(source);
  207.  
  208. /* add the extra information */
  209. Element extraInformation = doc.createElement("extrainformation");
  210. extraInformation.appendChild(doc.createTextNode(question.getExtraInformation()));
  211. rootElement.appendChild(extraInformation);
  212.  
  213. }
  214.  
  215. /**
  216. * Converts a QuestionType to a string representing the QuestionType
  217. *
  218. * @param questionType the QuestionType
  219. * @return
  220. */
  221. private String getQuestionTypeString(QuestionType questionType) {
  222. if (questionType == QuestionType.MULTIPLEANSWER)
  223. return "multipleanswer";
  224. else if (questionType == QuestionType.MULTIPLECHOICE)
  225. return "multiplechoice";
  226. else
  227. return "speed";
  228. }
  229.  
  230. public static void main(String[] args) {
  231. try {
  232. new QuestionSaver().saveQuestion(new Question());
  233. } catch (Exception e) {
  234. e.printStackTrace();
  235. }
  236. }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement