1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
45   * <a href="WCUtil.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class WCUtil {
51  
52      public static List<WCEntry> getLarger() {
53          return _instance._getLarger();
54      }
55  
56      public static List<WCEntry> 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<WCEntry>();
91  
92          Element root = doc.getRootElement();
93  
94          Iterator<Element> itr1 = root.element("shorter").elements(
95              "entry").iterator();
96  
97          while (itr1.hasNext()) {
98              Element entry = itr1.next();
99  
100             List<String[]> proofs = new ArrayList<String[]>();
101 
102             Iterator<Element> itr2 = entry.element(
103                 "proofs").elements("scriptures").iterator();
104 
105             while (itr2.hasNext()) {
106                 Element scriptures = itr2.next();
107 
108                 proofs.add(StringUtil.split(
109                     scriptures.getText(), StringPool.SEMICOLON));
110             }
111 
112             _shorter.add(
113                 new WCEntry(
114                     entry.elementText("question"),
115                     entry.elementText("answer"),
116                     proofs.toArray(new String[0][0])));
117         }
118 
119         _shorter = Collections.unmodifiableList(_shorter);
120 
121         _larger = new ArrayList<WCEntry>();
122 
123         itr1 = root.element("larger").elements("entry").iterator();
124 
125         while (itr1.hasNext()) {
126             Element entry = itr1.next();
127 
128             List<String[]> proofs = new ArrayList<String[]>();
129 
130             Iterator<Element> itr2 = entry.element(
131                 "proofs").elements("scriptures").iterator();
132 
133             while (itr2.hasNext()) {
134                 Element scriptures = itr2.next();
135 
136                 proofs.add(StringUtil.split(
137                     scriptures.getText(), StringPool.SEMICOLON));
138             }
139 
140             _larger.add(
141                 new WCEntry(
142                     entry.elementText("question"),
143                     entry.elementText("answer"),
144                     proofs.toArray(new String[0][0])));
145         }
146 
147         _larger = Collections.unmodifiableList(_larger);
148     }
149 
150     private List<WCEntry> _getLarger() {
151         return _larger;
152     }
153 
154     private List<WCEntry> _getShorter() {
155         return _shorter;
156     }
157 
158     private static Log _log = LogFactory.getLog(WCUtil.class);
159 
160     private static WCUtil _instance = new WCUtil();
161 
162     private List<WCEntry> _larger;
163     private List<WCEntry> _shorter;
164 
165 }