507b503614093216b5b2a517d8120aaba84aec74
[debian/jabref.git] / src / java / net / sf / jabref / export / OOCalcDatabase.java
1 /*
2  * Created on Oct 23, 2004
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */
7 package net.sf.jabref.export;
8
9 import net.sf.jabref.export.layout.format.*;
10 import net.sf.jabref.*;
11 import java.util.*;
12 import javax.xml.parsers.*;
13 import org.w3c.dom.*;
14 import ca.odell.glazedlists.BasicEventList;
15 import ca.odell.glazedlists.SortedList;
16
17 /**
18  * @author Morten O. Alver.
19  * Based on net.sf.jabref.MODSDatabase by Michael Wrighton
20  *
21  * TODO To change the template for this generated type comment go to
22  * Window - Preferences - Java - Code Style - Code Templates
23  */
24 public class OOCalcDatabase {
25     protected Collection entries;
26
27     public OOCalcDatabase(BibtexDatabase bibtex, Set keySet) {
28         // Make a list of comparators for sorting the entries:
29         List comparators = new ArrayList();
30         comparators.add(new FieldComparator("author"));
31         comparators.add(new FieldComparator("year"));
32         comparators.add(new FieldComparator(BibtexFields.KEY_FIELD));
33         // Use glazed lists to get a sorted view of the entries:
34         BasicEventList entryList = new BasicEventList();
35         // Set up a list of all entries, if keySet==null, or the entries whose
36         // ids are in keySet, otherwise:
37         if (keySet == null)
38             entryList.addAll(bibtex.getEntries());
39         else {
40             for (Iterator i=keySet.iterator(); i.hasNext();)
41                 entryList.add(bibtex.getEntryById((String)i.next()));
42         }
43         
44         entries = new SortedList(entryList, new FieldComparatorStack(comparators));
45
46     }
47     public Document getDOMrepresentation() {
48         Document result = null;
49         try {
50             DocumentBuilder dbuild = DocumentBuilderFactory.newInstance().newDocumentBuilder();
51             result = dbuild.newDocument();
52             Element collection = result.createElement("office:document-content");
53             //collection.setAttribute("xmlns", "http://openoffice.org/2000/office");
54             collection.setAttribute("xmlns:office", "http://openoffice.org/2000/office");
55             collection.setAttribute("xmlns:style", "http://openoffice.org/2000/style");
56             collection.setAttribute("xmlns:text", "http://openoffice.org/2000/text");
57             collection.setAttribute("xmlns:table", "http://openoffice.org/2000/table");
58             collection.setAttribute("xmlns:office:class", "spreadsheet");
59             collection.setAttribute("xmlns:office:version", "1.0");
60             collection.setAttribute("xmlns:fo", "http://www.w3.org/1999/XSL/Format");
61             Element el = result.createElement("office:script");
62             collection.appendChild(el);
63
64             el = result.createElement("office:automatic-styles");
65             Element el2 = result.createElement("style:style");
66             el2.setAttribute("style:name", "ro1");
67             el2.setAttribute("style:family", "table-row");
68             Element el3 = result.createElement("style.properties");
69             el3.setAttribute("style:row-height", "0.1681inch");
70             el3.setAttribute("fo:break-before", "auto");
71             el3.setAttribute("style:use-optimal-row-height", "true");
72             el2.appendChild(el3);
73             el.appendChild(el2);
74             el2 = result.createElement("style:style");
75             el2.setAttribute("style:name", "ta1");
76             el2.setAttribute("style:family", "table");
77             el2.setAttribute("style:master-page-name", "Default");
78             el3 = result.createElement("style:properties");
79             el3.setAttribute("table:display", "true");
80             el2.appendChild(el3);
81             el.appendChild(el2);
82             collection.appendChild(el);
83
84             Element body = result.createElement("office:body"),
85                     table = result.createElement("table:table");
86             table.setAttribute("table:name", "biblio");
87             table.setAttribute("table.style-name", "ta1");
88
89             Element row = result.createElement("table:table-row");
90             row.setAttribute("table.style-name", "ro1");
91             addTableCell(result, row, "Type");
92             addTableCell(result, row, "ISBN");
93             addTableCell(result, row, "Identifier");
94             addTableCell(result, row, "Author");
95             addTableCell(result, row, "Title");
96             addTableCell(result, row, "Journal");
97             addTableCell(result, row, "Volume");
98             addTableCell(result, row, "Number");
99             addTableCell(result, row, "Month");
100             addTableCell(result, row, "Pages");
101             addTableCell(result, row, "Year");
102             addTableCell(result, row, "Address");
103             addTableCell(result, row, "Note");
104             addTableCell(result, row, "URL");
105             addTableCell(result, row, "Booktitle");
106             addTableCell(result, row, "Chapter");
107             addTableCell(result, row, "Edition");
108             addTableCell(result, row, "Series");
109             addTableCell(result, row, "Editor");
110             addTableCell(result, row, "Publisher");
111             addTableCell(result, row, "ReportType");
112             addTableCell(result, row, "Howpublished");
113             addTableCell(result, row, "Institution");
114             addTableCell(result, row, "Organization");
115             addTableCell(result, row, "School");
116             addTableCell(result, row, "Annote");
117             addTableCell(result, row, "Custom1");
118             addTableCell(result, row, "Custom2");
119             addTableCell(result, row, "Custom3");
120             addTableCell(result, row, "Custom4");
121             addTableCell(result, row, "Custom5");
122             table.appendChild(row);
123
124             for(Iterator iter = entries.iterator(); iter.hasNext(); ) {
125                 BibtexEntry e = (BibtexEntry)iter.next();
126                 row = result.createElement("table:table-row");
127                 addTableCell(result, row, new GetOpenOfficeType().format(e.getType().getName()));
128                 addTableCell(result, row, getField(e, "isbn"));
129                 addTableCell(result, row, getField(e, BibtexFields.KEY_FIELD));
130                 addTableCell(result, row, getField(e, "author"));//new AuthorLastFirst().format(getField(e, "author")));
131                 addTableCell(result, row, new RemoveWhitespace().format(new RemoveBrackets().format(getField(e, "title"))));
132                 addTableCell(result, row, getField(e, "journal"));
133                 addTableCell(result, row, getField(e, "volume"));
134                 addTableCell(result, row, getField(e, "number"));
135                 addTableCell(result, row, getField(e, "month"));
136                 addTableCell(result, row, getField(e, "pages"));
137                 addTableCell(result, row, getField(e, "year"));
138                 addTableCell(result, row, getField(e, "address"));
139                 addTableCell(result, row, getField(e, "note"));
140                 addTableCell(result, row, getField(e, "url"));
141                 addTableCell(result, row, getField(e, "booktitle"));
142                 addTableCell(result, row, getField(e, "chapter"));
143                 addTableCell(result, row, getField(e, "edition"));
144                 addTableCell(result, row, getField(e, "series"));
145                 addTableCell(result, row, getField(e, "editor"));//new AuthorLastFirst().format(getField(e, "editor")));
146                 addTableCell(result, row, getField(e, "publisher"));
147                 addTableCell(result, row, getField(e, "reporttype"));
148                 addTableCell(result, row, getField(e, "howpublished"));
149                 addTableCell(result, row, getField(e, "institution"));
150                 addTableCell(result, row, getField(e, "organization"));
151                 addTableCell(result, row, getField(e, "school"));
152                 addTableCell(result, row, getField(e, "annote"));
153                 addTableCell(result, row, "");
154                 addTableCell(result, row, "");
155                 addTableCell(result, row, "");
156                 addTableCell(result, row, "");
157                 addTableCell(result, row, "");
158                 table.appendChild(row);
159             }
160
161             body.appendChild(table);
162             collection.appendChild(body);
163
164             result.appendChild(collection);
165         } catch (Exception e) {
166             System.out.println("Exception caught..." + e);
167             e.printStackTrace();
168         }
169         return result;
170     }
171
172     protected String getField(BibtexEntry e, String field) {
173         Object o = e.getField(field);
174         return o == null ? "" : o.toString();
175     }
176
177     protected void addTableCell(Document doc, Element parent, String content) {
178         Element cell = doc.createElement("table:table-cell"),
179                 text = doc.createElement("text:p");
180     Text textNode = doc.createTextNode(content);
181     text.appendChild(textNode);
182         //text.setTextContent(content);
183         cell.appendChild(text);
184         parent.appendChild(cell);
185     }
186 }