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