001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.westminstercatechism.util;
022:
023: import com.liferay.portal.kernel.util.StringPool;
024: import com.liferay.portal.kernel.util.StringUtil;
025: import com.liferay.portlet.westminstercatechism.model.WCEntry;
026:
027: import java.net.URL;
028:
029: import java.util.ArrayList;
030: import java.util.Collections;
031: import java.util.Iterator;
032: import java.util.List;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036:
037: import org.dom4j.Document;
038: import org.dom4j.DocumentException;
039: import org.dom4j.Element;
040: import org.dom4j.io.SAXReader;
041:
042: /**
043: * <a href="WCUtil.java.html"><b><i>View Source</i></b></a>
044: *
045: * @author Brian Wing Shun Chan
046: *
047: */
048: public class WCUtil {
049:
050: public static List getLarger() {
051: return _instance._getLarger();
052: }
053:
054: public static List getShorter() {
055: return _instance._getShorter();
056: }
057:
058: public static String translate(String text) {
059: return StringUtil.replace(text, new String[] { " doth ",
060: " hath " }, new String[] { " does ", " has " });
061: }
062:
063: private WCUtil() {
064: Document doc = null;
065:
066: try {
067: SAXReader reader = new SAXReader();
068:
069: ClassLoader classLoader = getClass().getClassLoader();
070:
071: URL url = classLoader
072: .getResource("com/liferay/portlet/westminstercatechism/dependencies/"
073: + "westminster_catechmism.xml");
074:
075: doc = reader.read(url);
076: } catch (DocumentException de) {
077: _log.error(de);
078: }
079:
080: _shorter = new ArrayList();
081:
082: Element root = doc.getRootElement();
083:
084: Iterator itr1 = root.element("shorter").elements("entry")
085: .iterator();
086:
087: while (itr1.hasNext()) {
088: Element entry = (Element) itr1.next();
089:
090: List proofs = new ArrayList();
091:
092: Iterator itr2 = entry.element("proofs").elements(
093: "scriptures").iterator();
094:
095: while (itr2.hasNext()) {
096: Element scriptures = (Element) itr2.next();
097:
098: proofs.add(StringUtil.split(scriptures.getText(),
099: StringPool.SEMICOLON));
100: }
101:
102: _shorter.add(new WCEntry(entry.elementText("question"),
103: entry.elementText("answer"), (String[][]) proofs
104: .toArray(new String[0][0])));
105: }
106:
107: _shorter = Collections.unmodifiableList(_shorter);
108:
109: _larger = new ArrayList();
110:
111: itr1 = root.element("larger").elements("entry").iterator();
112:
113: while (itr1.hasNext()) {
114: Element entry = (Element) itr1.next();
115:
116: List proofs = new ArrayList();
117:
118: Iterator itr2 = entry.element("proofs").elements(
119: "scriptures").iterator();
120:
121: while (itr2.hasNext()) {
122: Element scriptures = (Element) itr2.next();
123:
124: proofs.add(StringUtil.split(scriptures.getText(),
125: StringPool.SEMICOLON));
126: }
127:
128: _larger.add(new WCEntry(entry.elementText("question"),
129: entry.elementText("answer"), (String[][]) proofs
130: .toArray(new String[0][0])));
131: }
132:
133: _larger = Collections.unmodifiableList(_larger);
134: }
135:
136: private List _getLarger() {
137: return _larger;
138: }
139:
140: private List _getShorter() {
141: return _shorter;
142: }
143:
144: private static Log _log = LogFactory.getLog(WCUtil.class);
145:
146: private static WCUtil _instance = new WCUtil();
147:
148: private List _larger;
149: private List _shorter;
150:
151: }
|