1
22
23 package com.liferay.portlet.westminstercatechism.util;
24
25 import com.liferay.portal.kernel.util.StringPool;
26 import com.liferay.portal.kernel.util.StringUtil;
27 import com.liferay.portlet.westminstercatechism.model.WCEntry;
28
29 import java.net.URL;
30
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.Iterator;
34 import java.util.List;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 import org.dom4j.Document;
40 import org.dom4j.DocumentException;
41 import org.dom4j.Element;
42 import org.dom4j.io.SAXReader;
43
44
50 public class WCUtil {
51
52 public static List getLarger() {
53 return _instance._getLarger();
54 }
55
56 public static List getShorter() {
57 return _instance._getShorter();
58 }
59
60 public static String translate(String text) {
61 return StringUtil.replace(
62 text,
63 new String[] {
64 " doth ", " hath "
65 },
66 new String[] {
67 " does ", " has "
68 }
69 );
70 }
71
72 private WCUtil() {
73 Document doc = null;
74
75 try {
76 SAXReader reader = new SAXReader();
77
78 ClassLoader classLoader = getClass().getClassLoader();
79
80 URL url = classLoader.getResource(
81 "com/liferay/portlet/westminstercatechism/dependencies/" +
82 "westminster_catechmism.xml");
83
84 doc = reader.read(url);
85 }
86 catch (DocumentException de) {
87 _log.error(de);
88 }
89
90 _shorter = new ArrayList();
91
92 Element root = doc.getRootElement();
93
94 Iterator itr1 = root.element("shorter").elements("entry").iterator();
95
96 while (itr1.hasNext()) {
97 Element entry = (Element)itr1.next();
98
99 List proofs = new ArrayList();
100
101 Iterator itr2 = entry.element(
102 "proofs").elements("scriptures").iterator();
103
104 while (itr2.hasNext()) {
105 Element scriptures = (Element)itr2.next();
106
107 proofs.add(StringUtil.split(
108 scriptures.getText(), StringPool.SEMICOLON));
109 }
110
111 _shorter.add(
112 new WCEntry(
113 entry.elementText("question"),
114 entry.elementText("answer"),
115 (String[][])proofs.toArray(new String[0][0])));
116 }
117
118 _shorter = Collections.unmodifiableList(_shorter);
119
120 _larger = new ArrayList();
121
122 itr1 = root.element("larger").elements("entry").iterator();
123
124 while (itr1.hasNext()) {
125 Element entry = (Element)itr1.next();
126
127 List proofs = new ArrayList();
128
129 Iterator itr2 = entry.element(
130 "proofs").elements("scriptures").iterator();
131
132 while (itr2.hasNext()) {
133 Element scriptures = (Element)itr2.next();
134
135 proofs.add(StringUtil.split(
136 scriptures.getText(), StringPool.SEMICOLON));
137 }
138
139 _larger.add(
140 new WCEntry(
141 entry.elementText("question"),
142 entry.elementText("answer"),
143 (String[][])proofs.toArray(new String[0][0])));
144 }
145
146 _larger = Collections.unmodifiableList(_larger);
147 }
148
149 private List _getLarger() {
150 return _larger;
151 }
152
153 private List _getShorter() {
154 return _shorter;
155 }
156
157 private static Log _log = LogFactory.getLog(WCUtil.class);
158
159 private static WCUtil _instance = new WCUtil();
160
161 private List _larger;
162 private List _shorter;
163
164 }