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.themegallery.util;
022:
023: import com.liferay.portal.kernel.util.ReleaseInfo;
024: import com.liferay.portal.kernel.util.StringUtil;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.portlet.themegallery.model.ThemeEntry;
027:
028: import java.util.ArrayList;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Map;
033:
034: import org.dom4j.Document;
035: import org.dom4j.Element;
036: import org.dom4j.io.SAXReader;
037:
038: /**
039: * <a href="ThemeGalleryUtil.java.html"><b><i>View Source</i></b></a>
040: *
041: * @author Brian Wing Shun Chan
042: *
043: */
044: public class ThemeGalleryUtil {
045:
046: public static String[] getCategories() {
047: return _instance._categories;
048: }
049:
050: public static List getEntries(String categoryId) {
051: List entries = null;
052:
053: if (Validator.isNotNull(categoryId)) {
054: entries = (List) _instance._entriesByCategory
055: .get(categoryId);
056: }
057:
058: if (entries == null) {
059: entries = (List) _instance._entriesByCategory.get("ALL");
060: }
061:
062: return entries;
063: }
064:
065: public static List getEntries(String categoryId, int begin, int end) {
066: List entries = getEntries(categoryId);
067:
068: if (end > entries.size()) {
069: end = entries.size();
070: }
071:
072: return entries.subList(begin, end);
073: }
074:
075: public static int getEntriesSize(String categoryId) {
076: List entries = getEntries(categoryId);
077:
078: return entries.size();
079: }
080:
081: private ThemeGalleryUtil() {
082: try {
083: ClassLoader classLoader = getClass().getClassLoader();
084:
085: SAXReader reader = new SAXReader();
086:
087: Document doc = reader
088: .read(classLoader
089: .getResourceAsStream("com/liferay/portlet/themegallery/dependencies/gallery.xml"));
090:
091: Element root = doc.getRootElement();
092:
093: _categories = StringUtil.split(root
094: .elementText("available-categories"));
095:
096: for (int i = 0; i < _categories.length; i++) {
097: _entriesByCategory.put(_categories[i], new ArrayList());
098: }
099:
100: List allEntries = new ArrayList();
101:
102: _entriesByCategory.put("ALL", allEntries);
103:
104: Iterator itr = root.elements("entry").iterator();
105:
106: while (itr.hasNext()) {
107: Element entryEl = (Element) itr.next();
108:
109: String download = entryEl.elementText("download");
110:
111: String version = ReleaseInfo.getVersion();
112:
113: version = StringUtil.replace(version, " ", "");
114: version = version.toLowerCase();
115:
116: download = StringUtil.replace(download, ".war", "-"
117: + version + ".war");
118:
119: ThemeEntry entry = new ThemeEntry(entryEl
120: .attributeValue("name"), entryEl
121: .elementText("description"), download, entryEl
122: .elementText("image-large"), entryEl
123: .elementText("image-medium"), entryEl
124: .elementText("image-small"));
125:
126: String[] categories = StringUtil.split(entryEl
127: .elementText("categories"));
128:
129: for (int i = 0; i < categories.length; i++) {
130: List entries = (List) _entriesByCategory
131: .get(categories[i]);
132:
133: entries.add(entry);
134: }
135:
136: allEntries.add(entry);
137: }
138: } catch (Exception e) {
139: e.printStackTrace();
140: }
141: }
142:
143: private static ThemeGalleryUtil _instance = new ThemeGalleryUtil();
144:
145: private String[] _categories = null;
146: private Map _entriesByCategory = new HashMap();
147:
148: }
|