1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
package org.crosswire.jsword.book.filter.thml; |
21 | |
|
22 | |
import java.util.List; |
23 | |
|
24 | |
import org.crosswire.jsword.book.Book; |
25 | |
import org.crosswire.jsword.book.DataPolice; |
26 | |
import org.crosswire.jsword.book.OSISUtil; |
27 | |
import org.crosswire.jsword.passage.Key; |
28 | |
import org.jdom2.Content; |
29 | |
import org.jdom2.Element; |
30 | |
import org.jdom2.Text; |
31 | |
import org.xml.sax.Attributes; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public class SyncTag extends AbstractTag { |
46 | |
|
47 | |
|
48 | |
|
49 | |
public String getTagName() { |
50 | 0 | return "sync"; |
51 | |
} |
52 | |
|
53 | |
@Override |
54 | |
public Element processTag(Book book, Key key, Element ele, Attributes attrs) { |
55 | |
|
56 | 0 | String type = attrs.getValue("type"); |
57 | 0 | String value = attrs.getValue("value"); |
58 | |
|
59 | 0 | if ("Strongs".equals(type)) { |
60 | 0 | List<Content> siblings = ele.getContent(); |
61 | 0 | int size = siblings.size(); |
62 | 0 | if (size == 0) { |
63 | 0 | return null; |
64 | |
} |
65 | 0 | Content lastEle = siblings.get(size - 1); |
66 | 0 | if (lastEle instanceof Text) { |
67 | 0 | Element w = OSISUtil.factory().createW(); |
68 | 0 | w.setAttribute(OSISUtil.ATTRIBUTE_W_LEMMA, OSISUtil.LEMMA_STRONGS + value); |
69 | 0 | siblings.set(size - 1, w); |
70 | 0 | w.addContent(lastEle); |
71 | 0 | } else if (lastEle instanceof Element) { |
72 | 0 | Element wEle = (Element) lastEle; |
73 | 0 | if (wEle.getName().equals(OSISUtil.OSIS_ELEMENT_W)) { |
74 | 0 | StringBuilder buf = new StringBuilder(); |
75 | 0 | String strongsAttr = wEle.getAttributeValue(OSISUtil.ATTRIBUTE_W_LEMMA); |
76 | 0 | if (strongsAttr != null) { |
77 | 0 | buf.append(strongsAttr); |
78 | 0 | buf.append(' '); |
79 | |
} |
80 | 0 | buf.append(OSISUtil.LEMMA_STRONGS); |
81 | 0 | buf.append(value); |
82 | 0 | wEle.setAttribute(OSISUtil.ATTRIBUTE_W_LEMMA, buf.toString()); |
83 | |
} |
84 | |
} |
85 | 0 | return null; |
86 | |
} |
87 | |
|
88 | 0 | if ("morph".equals(type)) { |
89 | 0 | List<Content> siblings = ele.getContent(); |
90 | 0 | int size = siblings.size(); |
91 | 0 | if (size == 0) { |
92 | 0 | return null; |
93 | |
} |
94 | 0 | Content lastEle = siblings.get(size - 1); |
95 | 0 | if (lastEle instanceof Text) { |
96 | 0 | Element w = OSISUtil.factory().createW(); |
97 | 0 | w.setAttribute(OSISUtil.ATTRIBUTE_W_MORPH, OSISUtil.MORPH_ROBINSONS + value); |
98 | 0 | siblings.set(size - 1, w); |
99 | 0 | w.addContent(lastEle); |
100 | 0 | } else if (lastEle instanceof Element) { |
101 | 0 | Element wEle = (Element) lastEle; |
102 | 0 | if (wEle.getName().equals(OSISUtil.OSIS_ELEMENT_W)) { |
103 | 0 | StringBuilder buf = new StringBuilder(); |
104 | 0 | String strongsAttr = wEle.getAttributeValue(OSISUtil.ATTRIBUTE_W_MORPH); |
105 | 0 | if (strongsAttr != null) { |
106 | 0 | buf.append(strongsAttr); |
107 | 0 | buf.append(' '); |
108 | |
} |
109 | 0 | buf.append(OSISUtil.MORPH_ROBINSONS); |
110 | 0 | buf.append(value); |
111 | 0 | wEle.setAttribute(OSISUtil.ATTRIBUTE_W_MORPH, buf.toString()); |
112 | |
} |
113 | |
} |
114 | 0 | return null; |
115 | |
} |
116 | |
|
117 | 0 | if ("lemma".equals(type)) { |
118 | 0 | List<Content> siblings = ele.getContent(); |
119 | 0 | int size = siblings.size(); |
120 | 0 | if (size == 0) { |
121 | 0 | return null; |
122 | |
} |
123 | 0 | Content lastEle = siblings.get(size - 1); |
124 | 0 | if (lastEle instanceof Text) { |
125 | 0 | Element w = OSISUtil.factory().createW(); |
126 | 0 | w.setAttribute(OSISUtil.ATTRIBUTE_W_LEMMA, OSISUtil.LEMMA_MISC + value); |
127 | 0 | siblings.set(size - 1, w); |
128 | 0 | w.addContent(lastEle); |
129 | 0 | } else if (lastEle instanceof Element) { |
130 | 0 | Element wEle = (Element) lastEle; |
131 | 0 | if (wEle.getName().equals(OSISUtil.OSIS_ELEMENT_W)) { |
132 | 0 | StringBuilder buf = new StringBuilder(); |
133 | 0 | String lemmaAttr = wEle.getAttributeValue(OSISUtil.ATTRIBUTE_W_LEMMA); |
134 | 0 | if (lemmaAttr != null) { |
135 | 0 | buf.append(lemmaAttr); |
136 | 0 | buf.append(' '); |
137 | |
} |
138 | 0 | buf.append(OSISUtil.LEMMA_MISC); |
139 | 0 | buf.append(value); |
140 | 0 | wEle.setAttribute(OSISUtil.ATTRIBUTE_W_LEMMA, buf.toString()); |
141 | |
} |
142 | |
} |
143 | 0 | return null; |
144 | |
} |
145 | |
|
146 | 0 | if ("Dict".equals(type)) { |
147 | 0 | Element div = OSISUtil.factory().createDiv(); |
148 | 0 | div.setAttribute(OSISUtil.OSIS_ATTR_OSISID, "dict://" + value); |
149 | |
|
150 | 0 | if (ele != null) { |
151 | 0 | ele.addContent(div); |
152 | |
} |
153 | |
|
154 | 0 | return div; |
155 | |
} |
156 | |
|
157 | 0 | DataPolice.report(book, key, "sync tag has type=" + type + " when value=" + value); |
158 | 0 | return null; |
159 | |
} |
160 | |
} |