org.mentawai.list.BaseListData Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mentawai Show documentation
Show all versions of mentawai Show documentation
A Java full-stack web framework with programmatic configuration instead of XML and Annotations.
/*
* Mentawai Web Framework http://mentawai.lohis.com.br/
* Copyright (C) 2005 Sergio Oliveira Jr. ([email protected])
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.mentawai.list;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import org.mentawai.core.ApplicationManager;
import org.mentawai.i18n.I18N;
import org.mentawai.i18n.I18NMap;
import org.mentawai.i18n.LocaleManager;
import org.mentawai.util.StringComparator;
/**
* A base implementation for a ListData that will load files and sort.
*
* The files should be i18n files inside a given directory (or the default).
*
* Sorting is supported and can be by id, by value or by the order they appear in the file.
*
* @author Sergio Oliveira
*/
public class BaseListData implements ListData {
private static final String FULLDIR = ApplicationManager.getRealPath();
private static final String SEP = File.separator;
/** Sort the list according to the id (key) values, which can be integers */
public static final int ORDER_BY_ID = 1;
/** Sort the list according to the id (key) values, which can be strings */
public static final int ORDER_BY_STRING_ID = 4;
/** Sort the list according to item values, which are strings */
public static final int ORDER_BY_VALUE = 2;
/** Sort the list according to the order they appear inside the filename (don't sort) */
public static final int ORDER_BY_FILE = 3;
private String listname;
private String listDir;
private Map> map = new HashMap>();
private Map> values = new HashMap>();
private int size = 0;
/**
* Creates a new BaseListData
*
* @param listname The name of the list
* @param ordertype How this list should be sorted
* @param listDir The directory where to look for list files.
*
* @throws IOException
*/
public BaseListData(String listname, int ordertype, String listDir) throws IOException {
this.listname = listname;
this.listDir = listDir;
load(ordertype);
}
/**
* Creates a new BaseListData, looking for files inside the default directory "/lists".
*
* @param listname The name of the list
* @param ordertype How this list should be sorted.
* @throws IOException
*/
public BaseListData(String listname, int ordertype) throws IOException {
this(listname, ordertype, ListManager.LIST_DIR);
}
/**
* Creates a new BaseListData, looking for files inside the default directory "/lists",
* and using the default sort order ORDER_BY_VALUE.
*
* @param listname The name of the list
* @throws IOException
*/
public BaseListData(String listname) throws IOException {
this(listname, ORDER_BY_VALUE, ListManager.LIST_DIR);
}
/**
* The name of this list, for example "genders"
*
* @return The name of the list.
*/
public String getName() {
return listname;
}
/**
* The size of the list, for example a "genders" list should have 2 items.
*
* @return The size of the list
*/
public int size() {
return size;
}
/**
* Return a list of ListItem objects so you can print
*
* the whole list for this locale.
* @param loc The locale of the list
* @return a list of ListItem objects
*/
public List getValues(Locale loc) {
if (map.containsKey(loc)) {
return map.get(loc);
} else if (!map.isEmpty()) {
return map.values().iterator().next();
} else {
return new ArrayList(0);
}
}
public List getValues() {
return getValues(LocaleManager.getDefaultLocale());
}
/**
* Given an id and a locale return the list item value.
*
* @param id The id of the list item
* @param loc The locale of the list from where to get the value.
* @return The value of the list item
*/
public String getValue(String id, Locale loc) {
Map m = null;
if (values.containsKey(loc)) {
m = values.get(loc);
} else if (!values.isEmpty()) {
m = values.values().iterator().next();
} else {
return "!" + id + "!";
}
ListItem item = m.get(id);
if (item == null) {
return "!!" + id + "!!";
}
return item.getValue();
}
public String getValue(String id) {
String value = getValue(id, LocaleManager.getDefaultLocale());
if (value == null) {
// return from the first locale available...
Iterator