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.randombibleverse.util;
022:
023: import com.liferay.portal.kernel.util.Randomizer;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.webcache.WebCacheItem;
026: import com.liferay.portal.kernel.webcache.WebCachePoolUtil;
027: import com.liferay.portlet.randombibleverse.model.Bible;
028: import com.liferay.portlet.randombibleverse.model.Verse;
029:
030: import java.net.URL;
031:
032: import java.util.ArrayList;
033: import java.util.Collections;
034: import java.util.Iterator;
035: import java.util.LinkedHashMap;
036: import java.util.List;
037: import java.util.Locale;
038: import java.util.Map;
039:
040: import javax.portlet.PortletPreferences;
041:
042: import org.apache.commons.logging.Log;
043: import org.apache.commons.logging.LogFactory;
044:
045: import org.dom4j.Document;
046: import org.dom4j.DocumentException;
047: import org.dom4j.Element;
048: import org.dom4j.io.SAXReader;
049:
050: /**
051: * <a href="RBVUtil.java.html"><b><i>View Source</i></b></a>
052: *
053: * @author Brian Wing Shun Chan
054: *
055: */
056: public class RBVUtil {
057:
058: public static Bible getBible(PortletPreferences prefs, Locale locale) {
059: return _instance._getBible(prefs, locale);
060: }
061:
062: public static Map getBibles() {
063: return _instance._bibles;
064: }
065:
066: public static Verse getVerse(PortletPreferences prefs, Locale locale) {
067: return _instance._getVerse(prefs, locale);
068: }
069:
070: private RBVUtil() {
071: Document doc = null;
072:
073: try {
074: SAXReader reader = new SAXReader();
075:
076: ClassLoader classLoader = getClass().getClassLoader();
077:
078: URL url = classLoader
079: .getResource("com/liferay/portlet/randombibleverse/dependencies/"
080: + "random_bible_verse.xml");
081:
082: doc = reader.read(url);
083: } catch (DocumentException de) {
084: _log.error(de);
085: }
086:
087: _bibles = new LinkedHashMap();
088: _verses = new ArrayList();
089:
090: Element root = doc.getRootElement();
091:
092: Iterator itr = root.element("bibles").elements("bible")
093: .iterator();
094:
095: while (itr.hasNext()) {
096: Element bible = (Element) itr.next();
097:
098: _bibles.put(bible.attributeValue("language"), new Bible(
099: bible.attributeValue("language"), bible
100: .attributeValue("language-name"), bible
101: .attributeValue("version-id")));
102: }
103:
104: _bibles = Collections.unmodifiableMap(_bibles);
105:
106: itr = root.element("verses").elements("verse").iterator();
107:
108: while (itr.hasNext()) {
109: Element verse = (Element) itr.next();
110:
111: _verses.add(verse.attributeValue("location"));
112: }
113:
114: _verses = Collections.unmodifiableList(_verses);
115: }
116:
117: private Bible _getBible(PortletPreferences prefs, Locale locale) {
118: Bible bible = (Bible) _bibles.get(prefs.getValue("language",
119: StringPool.BLANK));
120:
121: if (bible == null) {
122: bible = (Bible) _bibles.get(locale.getLanguage());
123: }
124:
125: if (bible == null) {
126: bible = (Bible) _bibles.get("en");
127: }
128:
129: return bible;
130: }
131:
132: private Verse _getVerse(PortletPreferences prefs, Locale locale) {
133: Bible bible = _getBible(prefs, locale);
134:
135: int i = Randomizer.getInstance().nextInt(_verses.size());
136:
137: return _getVerse((String) _verses.get(i), bible.getVersionId());
138: }
139:
140: private Verse _getVerse(String location, String versionId) {
141: WebCacheItem wci = new VerseWebCacheItem(location, versionId);
142:
143: return (Verse) WebCachePoolUtil.get(RBVUtil.class.getName()
144: + StringPool.PERIOD + location + StringPool.PERIOD
145: + versionId, wci);
146: }
147:
148: private static Log _log = LogFactory.getLog(RBVUtil.class);
149:
150: private static RBVUtil _instance = new RBVUtil();
151:
152: private Map _bibles;
153: private List _verses;
154:
155: }
|