Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package service;
- import java.io.OutputStream;
- import java.text.SimpleDateFormat;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.dom.DOMSource;
- import javax.xml.transform.stream.StreamResult;
- import model.Account;
- import model.AccountBook;
- import model.Category;
- import model.Entry;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Text;
- /**
- * Schreibt das Haushaltsbuch gemass des XML-Schemas auf den Output Stream
- * @author Fabi
- *
- */
- public class ExportService {
- public static void export(AccountBook accountBook,OutputStream out) throws ExportException{
- if(accountBook == null){
- throw new NullPointerException();
- }
- try{
- /**
- * Initialisierung
- */
- DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
- DocumentBuilder docbuilder =dbfac.newDocumentBuilder();
- Document doc = docbuilder.newDocument();
- /**Wurzel */
- Element accountBookRoot = doc.createElement("accountbook");
- /**Accounts*/
- Element accounts = doc.createElement("accounts");
- /**Der Wurzel wurde ein neues Kind hinzugefuegt*/
- accountBookRoot.appendChild(accounts);
- for(Account ac : accountBook.getAccounts()){
- Element account = doc.createElement("account");
- accounts.appendChild(account);
- /**Name und Anfangskontostand werden erzeugt*/
- Element name =doc.createElement("name");
- account.appendChild(name);
- Text text = doc.createTextNode(ac.getName());
- account.appendChild(text);
- Element openingBalance = doc.createElement("openingBalance");
- account.appendChild(openingBalance);
- Text textopening = doc.createTextNode("" + ac.getOpeningBalance());
- account.appendChild(textopening);
- }
- /** Incomes erstellen */
- Element incomes = doc.createElement("incomes");
- accountBookRoot.appendChild(incomes);
- for(Category i : accountBook.findAllIncomeCategories()){
- Element category = doc.createElement("category");
- /** Wenn es eine Oberkategorie hat */
- if(i.getParent() != null){
- category.setAttribute("parent", i.getParent().getUuid()
- .toString());
- }
- incomes.appendChild(category);
- Element uuid = doc.createElement("uuid");
- category.appendChild(uuid);
- Text textuuid = doc.createTextNode(""+i.getUuid());
- category.appendChild(textuuid);
- Element name = doc.createElement("name");
- category.appendChild(name);
- Text textname = doc.createTextNode(i.getName());
- category.appendChild(textname);
- Element description = doc.createElement("description");
- category.appendChild(description);
- Text textdiscr = doc.createTextNode(i.getDiscription());
- category.appendChild(textdiscr);
- }
- /**Expenses erstellen */
- Element expenses = doc.createElement("expenses");
- accountBookRoot.appendChild(expenses);
- for(Category e : accountBook.findAllExpenseCategories()){
- Element category = doc.createElement("category");
- if(e.getParent() != null){
- category.setAttribute("parent", e.getParent().getUuid()
- .toString());
- }
- expenses.appendChild(category);
- Element uuid = doc.createElement("uuid");
- category.appendChild(uuid);
- Text textcat = doc.createTextNode(""+e.getUuid());
- category.appendChild(textcat);
- Element name = doc.createElement("name");
- category.appendChild(name);
- Text textname = doc.createTextNode(e.getName());
- category.appendChild(textname);
- Element description = doc.createElement("description");
- category.appendChild(description);
- Text textdescr = doc.createTextNode(e.getDiscription());
- category.appendChild(textdescr);
- }
- /**Entries erstellen*/
- Element entries = doc.createElement("entries");
- accountBookRoot.appendChild(entries);
- for(Entry entry : accountBook.getEntries()){
- Element en = doc.createElement("entry");
- en.setAttribute("account", entry.getAccount().getName());
- en.setAttribute("category", ""+entry.getCategory().getUuid());
- entries.appendChild(en);
- Element description = doc.createElement("description");
- en.appendChild(description);
- Text textdescription = doc.createTextNode(entry.getDescription());
- en.appendChild(textdescription);
- Element value = doc.createElement("value");
- en.appendChild(value);
- Text textvalue = doc.createTextNode(""+entry.getValue());
- en.appendChild(textvalue);
- Element timestamp = doc.createElement("timestamp");
- en.appendChild(timestamp);
- SimpleDateFormat sdf = new SimpleDateFormat();
- sdf.applyPattern("dd.MM.yyyy");
- String date = sdf.format(entry.getTimestamp().getTime());
- Text texttime = doc.createTextNode(date);
- en.appendChild(texttime);
- }
- TransformerFactory.newInstance().newTransformer()
- .transform(new DOMSource(doc), new StreamResult(out));
- } catch(Exception e){
- }
- }
- }
Add Comment
Please, Sign In to add comment