1
20 package org.crosswire.jsword.book.sword;
21
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import org.crosswire.jsword.JSOtherMsg;
27 import org.crosswire.jsword.book.BookException;
28 import org.crosswire.jsword.book.OSISUtil;
29 import org.crosswire.jsword.book.basic.AbstractBook;
30 import org.crosswire.jsword.book.filter.SourceFilter;
31 import org.crosswire.jsword.book.sword.processing.RawTextToXmlProcessor;
32 import org.crosswire.jsword.passage.DefaultKeyList;
33 import org.crosswire.jsword.passage.DefaultLeafKeyList;
34 import org.crosswire.jsword.passage.Key;
35 import org.crosswire.jsword.passage.NoSuchKeyException;
36 import org.jdom2.Content;
37 import org.jdom2.Element;
38
39
45 public class SwordDictionary extends AbstractBook {
46
52 protected SwordDictionary(SwordBookMetaData sbmd, Backend backend) {
53 super(sbmd, backend);
54
55 if (!(backend instanceof AbstractKeyBackend)) {
56 throw new IllegalArgumentException("AbstractBackend must be an AbstractKeyBackened");
57 }
58
59 this.filter = sbmd.getFilter();
60 }
61
62
65 public Iterator<Content> getOsisIterator(final Key key, boolean allowEmpty, final boolean allowGenTitles) throws BookException {
66 assert key != null;
67
68 List<Content> content = new ArrayList<Content>();
69 Element title = OSISUtil.factory().createGeneratedTitle();
70 title.addContent(key.getName());
71 content.add(title);
72
73 String txt = getBackend().getRawText(key);
74 List<Content> osisContent = filter.toOSIS(this, key, txt);
75 content.addAll(osisContent);
76
77 return content.iterator();
78 }
79
80
83 public String getRawText(Key key) throws BookException {
84 return getBackend().getRawText(key);
85 }
86
87
90 public boolean contains(Key key) {
91 Backend backend = getBackend();
92 return backend != null && backend.contains(key);
93 }
94
95
98 @Override
99 public List<Content> getOsis(Key key, RawTextToXmlProcessor processor) throws BookException {
100 assert key != null;
101
102 return getBackend().readToOsis(key, processor);
103 }
104
105
108 public boolean isWritable() {
109 return getBackend().isWritable();
110 }
111
112
115 public void setRawText(Key key, String rawData) throws BookException {
116 throw new BookException(JSOtherMsg.lookupText("This Book is read-only."));
117 }
118
119
122 public void setAliasKey(Key alias, Key source) throws BookException {
123 throw new BookException(JSOtherMsg.lookupText("This Book is read-only."));
124 }
125
126
129 public Key getGlobalKeyList() {
130 return (AbstractKeyBackend) getBackend();
131 }
132
133
136 public Key getValidKey(String name) {
137 try {
138 return getKey(name);
139 } catch (NoSuchKeyException e) {
140 return createEmptyKeyList();
141 }
142 }
143
144
147 public Key getKey(String text) throws NoSuchKeyException {
148 AbstractKeyBackend keyBackend = (AbstractKeyBackend) getBackend();
149
150 int pos = keyBackend.indexOf(new DefaultLeafKeyList(text));
151 if (pos < 0) {
152 if (keyBackend.getCardinality() > -pos - 1) {
153 return keyBackend.get(-pos - 1);
154 }
155 return keyBackend.get(keyBackend.getCardinality() - 1);
156 }
157 return keyBackend.get(pos);
158 }
159
160
163 public Key createEmptyKeyList() {
164 return new DefaultKeyList();
165 }
166
167
170 private SourceFilter filter;
171
172 }
173