1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
package org.crosswire.jsword.examples; |
21 | |
|
22 | |
import java.io.FileOutputStream; |
23 | |
import java.io.IOException; |
24 | |
import java.io.OutputStreamWriter; |
25 | |
import java.io.Writer; |
26 | |
import java.text.FieldPosition; |
27 | |
import java.text.MessageFormat; |
28 | |
|
29 | |
import org.crosswire.common.xml.SAXEventProvider; |
30 | |
import org.crosswire.common.xml.XMLProcess; |
31 | |
import org.crosswire.common.xml.XMLUtil; |
32 | |
import org.crosswire.jsword.book.Book; |
33 | |
import org.crosswire.jsword.book.BookData; |
34 | |
import org.crosswire.jsword.book.BookException; |
35 | |
import org.crosswire.jsword.book.BookMetaData; |
36 | |
import org.crosswire.jsword.book.Books; |
37 | |
import org.crosswire.jsword.passage.Key; |
38 | |
import org.xml.sax.SAXException; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 0 | public class DictToOsis { |
47 | |
|
48 | |
|
49 | |
|
50 | |
private static final String BOOK_NAME = "WebstersDict"; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public static void main(String[] args) throws BookException, IOException { |
58 | 0 | new DictToOsis().dump(BOOK_NAME); |
59 | 0 | } |
60 | |
|
61 | |
public void dump(String name) throws BookException, IOException { |
62 | 0 | Books books = Books.installed(); |
63 | 0 | Book book = books.getBook(name); |
64 | 0 | BookMetaData bmd = book.getBookMetaData(); |
65 | 0 | StringBuffer buf = new StringBuffer(); |
66 | |
|
67 | 0 | Key keys = book.getGlobalKeyList(); |
68 | |
|
69 | 0 | buildDocumentOpen(buf, bmd); |
70 | |
|
71 | |
|
72 | 0 | for (Key key : keys) { |
73 | 0 | BookData bdata = new BookData(book, key); |
74 | 0 | SAXEventProvider osissep = bdata.getSAXEventProvider(); |
75 | |
try { |
76 | 0 | buildEntryOpen(buf, key.getName(), XMLUtil.writeToString(osissep)); |
77 | 0 | } catch (SAXException e) { |
78 | 0 | e.printStackTrace(System.err); |
79 | 0 | } |
80 | 0 | } |
81 | |
|
82 | 0 | buildDocumentClose(buf); |
83 | |
|
84 | 0 | Writer writer = null; |
85 | |
try { |
86 | 0 | writer = new OutputStreamWriter(new FileOutputStream(bmd.getInitials() + ".xml"), "UTF-8"); |
87 | 0 | writer.write(buf.toString()); |
88 | |
} finally { |
89 | 0 | if (writer != null) { |
90 | 0 | writer.close(); |
91 | |
} |
92 | |
} |
93 | 0 | XMLProcess parser = new XMLProcess(); |
94 | |
|
95 | 0 | parser.parse(bmd.getInitials() + ".xml"); |
96 | 0 | } |
97 | |
|
98 | |
private void buildDocumentOpen(StringBuffer buf, BookMetaData bmd) { |
99 | 0 | MessageFormat msgFormat = new MessageFormat( |
100 | |
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<osis\n xmlns=\"http://www.bibletechnologies.net/2003/OSIS/namespace\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xsi:schemaLocation=\"http://www.bibletechnologies.net/2003/OSIS/namespace osisCore.2.1.xsd\">\n<osisText osisIDWork=\"{0}\" osisRefWork=\"defaultReferenceScheme\" xml:lang=\"en\">\n <header>\n <work osisWork=\"{0}\">\n <title>{1}</title>\n <identifier type=\"OSIS\">Dict.{0}</identifier>\n <refSystem>Dict.{0}</refSystem>\n </work>\n <work osisWork=\"defaultReferenceScheme\">\n <refSystem>Dict.{0}</refSystem>\n </work>\n </header>\n<div>\n"); |
101 | 0 | msgFormat.format(new Object[] { |
102 | |
bmd.getInitials(), bmd.getName() |
103 | |
}, buf, pos); |
104 | 0 | } |
105 | |
|
106 | |
private void buildDocumentClose(StringBuffer buf) { |
107 | 0 | buf.append("</div>\n</osisText>\n</osis>\n"); |
108 | 0 | } |
109 | |
|
110 | |
private void buildEntryOpen(StringBuffer buf, String entryName, String entryDef) { |
111 | 0 | String tmp = entryName; |
112 | 0 | if (tmp.indexOf(' ') != -1) { |
113 | 0 | tmp = "x"; |
114 | |
} |
115 | 0 | MessageFormat msgFormat = new MessageFormat( |
116 | |
"<div type=\"entry\" osisID=\"{0}\" canonical=\"true\"><seg type=\"x-form\"><seg type=\"x-orth\">{0}</seg></seg><seg type=\"x-def\">{1}</seg></div>\n"); |
117 | 0 | msgFormat.format(new Object[] { |
118 | |
tmp, entryDef |
119 | |
}, buf, pos); |
120 | 0 | } |
121 | |
|
122 | 0 | private static FieldPosition pos = new FieldPosition(0); |
123 | |
} |