1   /**
2    * Copyright (c) 2000-2007 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 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 }