Guest User

Untitled

a guest
Nov 20th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. package service;
  2.  
  3. import java.io.OutputStream;
  4. import java.text.SimpleDateFormat;
  5.  
  6. import javax.xml.parsers.DocumentBuilder;
  7. import javax.xml.parsers.DocumentBuilderFactory;
  8. import javax.xml.transform.TransformerFactory;
  9. import javax.xml.transform.dom.DOMSource;
  10. import javax.xml.transform.stream.StreamResult;
  11.  
  12. import model.Account;
  13. import model.AccountBook;
  14. import model.Category;
  15. import model.Entry;
  16.  
  17. import org.w3c.dom.Document;
  18. import org.w3c.dom.Element;
  19. import org.w3c.dom.Text;
  20.  
  21.  
  22.  
  23. /**
  24. * Schreibt das Haushaltsbuch gemass des XML-Schemas auf den Output Stream
  25. * @author Fabi
  26. *
  27. */
  28. public class ExportService {
  29.  
  30. public static void export(AccountBook accountBook,OutputStream out) throws ExportException{
  31.  
  32. if(accountBook == null){
  33. throw new NullPointerException();
  34. }
  35.  
  36. try{
  37. /**
  38. * Initialisierung
  39. */
  40. DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
  41. DocumentBuilder docbuilder =dbfac.newDocumentBuilder();
  42. Document doc = docbuilder.newDocument();
  43. /**Wurzel */
  44. Element accountBookRoot = doc.createElement("accountbook");
  45.  
  46. /**Accounts*/
  47.  
  48. Element accounts = doc.createElement("accounts");
  49. /**Der Wurzel wurde ein neues Kind hinzugefuegt*/
  50. accountBookRoot.appendChild(accounts);
  51.  
  52. for(Account ac : accountBook.getAccounts()){
  53.  
  54. Element account = doc.createElement("account");
  55. accounts.appendChild(account);
  56.  
  57. /**Name und Anfangskontostand werden erzeugt*/
  58. Element name =doc.createElement("name");
  59. account.appendChild(name);
  60.  
  61. Text text = doc.createTextNode(ac.getName());
  62. account.appendChild(text);
  63.  
  64. Element openingBalance = doc.createElement("openingBalance");
  65. account.appendChild(openingBalance);
  66. Text textopening = doc.createTextNode("" + ac.getOpeningBalance());
  67. account.appendChild(textopening);
  68.  
  69. }
  70.  
  71. /** Incomes erstellen */
  72. Element incomes = doc.createElement("incomes");
  73. accountBookRoot.appendChild(incomes);
  74.  
  75. for(Category i : accountBook.findAllIncomeCategories()){
  76. Element category = doc.createElement("category");
  77. /** Wenn es eine Oberkategorie hat */
  78. if(i.getParent() != null){
  79. category.setAttribute("parent", i.getParent().getUuid()
  80. .toString());
  81. }
  82. incomes.appendChild(category);
  83.  
  84. Element uuid = doc.createElement("uuid");
  85. category.appendChild(uuid);
  86. Text textuuid = doc.createTextNode(""+i.getUuid());
  87. category.appendChild(textuuid);
  88.  
  89. Element name = doc.createElement("name");
  90. category.appendChild(name);
  91. Text textname = doc.createTextNode(i.getName());
  92. category.appendChild(textname);
  93.  
  94. Element description = doc.createElement("description");
  95. category.appendChild(description);
  96. Text textdiscr = doc.createTextNode(i.getDiscription());
  97. category.appendChild(textdiscr);
  98. }
  99.  
  100.  
  101. /**Expenses erstellen */
  102.  
  103. Element expenses = doc.createElement("expenses");
  104. accountBookRoot.appendChild(expenses);
  105.  
  106. for(Category e : accountBook.findAllExpenseCategories()){
  107.  
  108. Element category = doc.createElement("category");
  109. if(e.getParent() != null){
  110. category.setAttribute("parent", e.getParent().getUuid()
  111. .toString());
  112. }
  113.  
  114. expenses.appendChild(category);
  115.  
  116. Element uuid = doc.createElement("uuid");
  117. category.appendChild(uuid);
  118. Text textcat = doc.createTextNode(""+e.getUuid());
  119. category.appendChild(textcat);
  120.  
  121. Element name = doc.createElement("name");
  122. category.appendChild(name);
  123. Text textname = doc.createTextNode(e.getName());
  124. category.appendChild(textname);
  125.  
  126. Element description = doc.createElement("description");
  127. category.appendChild(description);
  128. Text textdescr = doc.createTextNode(e.getDiscription());
  129. category.appendChild(textdescr);
  130. }
  131.  
  132.  
  133. /**Entries erstellen*/
  134. Element entries = doc.createElement("entries");
  135. accountBookRoot.appendChild(entries);
  136.  
  137. for(Entry entry : accountBook.getEntries()){
  138.  
  139. Element en = doc.createElement("entry");
  140. en.setAttribute("account", entry.getAccount().getName());
  141. en.setAttribute("category", ""+entry.getCategory().getUuid());
  142. entries.appendChild(en);
  143.  
  144. Element description = doc.createElement("description");
  145. en.appendChild(description);
  146. Text textdescription = doc.createTextNode(entry.getDescription());
  147. en.appendChild(textdescription);
  148.  
  149. Element value = doc.createElement("value");
  150. en.appendChild(value);
  151. Text textvalue = doc.createTextNode(""+entry.getValue());
  152. en.appendChild(textvalue);
  153.  
  154. Element timestamp = doc.createElement("timestamp");
  155. en.appendChild(timestamp);
  156.  
  157. SimpleDateFormat sdf = new SimpleDateFormat();
  158. sdf.applyPattern("dd.MM.yyyy");
  159. String date = sdf.format(entry.getTimestamp().getTime());
  160.  
  161. Text texttime = doc.createTextNode(date);
  162. en.appendChild(texttime);
  163.  
  164. }
  165.  
  166. TransformerFactory.newInstance().newTransformer()
  167. .transform(new DOMSource(doc), new StreamResult(out));
  168.  
  169. } catch(Exception e){
  170.  
  171. }
  172.  
  173.  
  174.  
  175. }
  176.  
  177. }
Add Comment
Please, Sign In to add comment