Changeset 5570


Ignore:
Timestamp:
Feb 16, 2011, 2:49:40 PM (11 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #1581: Show common character sets first in the list of character sets

This is now a configuration option that can be set in base.config[commonCharsets]. It is also possible to specifiy a regular expression to filter character sets that are not used. The default setting will filter all character sets starting with 'x-' or 'IBM'.

Location:
trunk
Files:
2 added
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/config/dist/base.config

    r5537 r5570  
    221221# locale.country =
    222222# locacle.variant =
     223
     224# The default character set that will be used to parse text files that
     225# hasn't specified a character set. The default value for this setting
     226# is ISO-8859-1.
     227#
     228# defaultCharset = ISO-8859-1
     229
     230# A comma-separated list of the most commonly used character sets for text
     231# files. This list is used to order selection lists so that the common values
     232# are listed at the top of the list.
     233#
     234# commonCharsets = UTF-8,US-ASCII,ISO-8859-1
     235
     236# A regular expression that used to filter character sets that are not used
     237# in order to keep the selection list shorter. If no value is specified all
     238# character sets are included. The standard setting will filter out all
     239# character sets starting with 'x-' or 'IBM'. Character sets that are specified
     240# in the commonCharsets setting are never filtered.
     241#
     242ignoreCharsets = ((x|X)\-|IBM).*
     243
    223244
    224245# ===============
  • trunk/src/core/net/sf/basedb/core/Config.java

    r5409 r5570  
    2323package net.sf.basedb.core;
    2424
     25import java.util.Arrays;
     26import java.util.Collections;
    2527import java.util.Locale;
    2628import java.util.Properties;
     29import java.util.SortedSet;
     30import java.util.TreeSet;
     31import java.util.regex.Pattern;
     32import java.util.regex.PatternSyntaxException;
    2733import java.io.FileNotFoundException;
    2834import java.io.InputStream;
    2935import java.net.URL;
    30 
     36import java.nio.charset.Charset;
     37
     38import org.apache.commons.collections.CollectionUtils;
     39import org.apache.commons.collections.Predicate;
     40
     41import net.sf.basedb.util.ComparableComparator;
    3142import net.sf.basedb.util.FileUtil;
     43import net.sf.basedb.util.PriorityComparator;
    3244
    3345/**
     
    6072  private static Locale locale = Locale.getDefault();
    6173 
     74  private static SortedSet<String> commonCharsets;
     75 
     76  private static SortedSet<String> allCharsets;
     77 
    6278  /**
    6379    Loads the settings from the configuration file.
     
    93109      if (is != null) FileUtil.close(is);
    94110    }
     111
    95112    isInitialised = true;
    96    
     113 
    97114    // Load locale
    98115    String language = getString("locale.language", "");
     
    107124      locale = Locale.getDefault();
    108125    }
     126   
     127    // Load character sets
     128    String charsets = getString("commonCharsets", "UTF-8,US-ASCII,ISO-8859-1").trim();
     129    commonCharsets = Collections.unmodifiableSortedSet(new TreeSet<String>(Arrays.asList(charsets.split("\\s*,\\s*"))));
     130
     131    SortedSet<String> tmp = new TreeSet<String>(
     132        new PriorityComparator<String>(commonCharsets,
     133        new ComparableComparator<String>()));
     134    tmp.addAll(Charset.availableCharsets().keySet());
     135    String filter = getString("ignoreCharsets", null);
     136    if (filter != null)
     137    {
     138      try
     139      {
     140        final Pattern p = Pattern.compile(filter);
     141        CollectionUtils.filter(tmp, new Predicate()
     142        {
     143          @Override
     144          public boolean evaluate(Object o)
     145          {
     146            return !p.matcher((String)o).matches() || commonCharsets.contains(o);
     147          }
     148        });
     149      }
     150      catch (PatternSyntaxException ex)
     151      {
     152        isInitialised = false;
     153        throw new ConfigurationException("Invalid pattern for 'ignoreCharsets' property in '/base.config'. ", ex);
     154      }
     155    }
     156    allCharsets = Collections.unmodifiableSortedSet(tmp);
    109157  }
    110158
     
    274322
    275323  /**
     324    Get a set containing the most common character sets used by text files.
     325    The set is sorted by name.
     326    @return A sorted set
     327    @since 2.17
     328  */
     329  public static SortedSet<String> getCommonCharsets()
     330  {
     331    return commonCharsets;
     332  }
     333 
     334  /**
     335    Get a set containing all character sets known to the current java virtual machine.
     336    The set is sorted by name with the most common character sets first.
     337    @return A sorted set
     338    @since 2.17
     339  */
     340  public static SortedSet<String> getAllCharsets()
     341  {
     342    return allCharsets;
     343  }
     344 
     345  /**
    276346    Get the default locale configured for the server.
    277347    @since 2.16
  • trunk/src/plugins/core/net/sf/basedb/plugins/util/Parameters.java

    r5430 r5570  
    2222package net.sf.basedb.plugins.util;
    2323
    24 import java.nio.charset.Charset;
    2524import java.text.SimpleDateFormat;
    2625import java.util.ArrayList;
     
    307306  private static final StringParameterType charsetType =
    308307    new StringParameterType(255, Config.getCharset(), false, 1, 0, 0,
    309     new ArrayList<String>(Charset.availableCharsets().keySet()));
     308    new ArrayList<String>(Config.getAllCharsets()));
    310309   
    311310 
  • trunk/www/common/import/select_file.jsp

    r5558 r5570  
    217217          <%
    218218          String defaultCharset = Config.getCharset();
    219           for (String charset : Charset.availableCharsets().keySet())
     219          int numCommon = Config.getCommonCharsets().size();
     220          int i = 0;
     221          for (String charset : Config.getAllCharsets())
    220222          {
    221223            %>
    222224            <option value="<%=charset%>"
    223               <%=defaultCharset.equalsIgnoreCase(charset) ? "selected" : ""%>><%=charset%>
     225              <%=defaultCharset.equalsIgnoreCase(charset) ? "selected" : ""%>
     226              <%=i==numCommon ? "style=\"border-top: 1px solid #666666;\"" : "" %>><%=charset%>
    224227            <%
     228            i++;
    225229          }
    226230          %>
  • trunk/www/common/plugin/test_with_file.jsp

    r5558 r5570  
    339339          <%
    340340          String defaultCharset = Config.getCharset();
    341           for (String charset : Charset.availableCharsets().keySet())
     341          int numCommon = Config.getCommonCharsets().size();
     342          int i = 0;
     343          for (String charset : Config.getAllCharsets())
    342344          {
    343345            %>
    344346            <option value="<%=charset%>"
    345               <%=defaultCharset.equalsIgnoreCase(charset) ? "selected" : ""%>><%=charset%>
     347              <%=defaultCharset.equalsIgnoreCase(charset) ? "selected" : ""%>
     348              <%=i==numCommon ? "style=\"border-top: 1px solid #666666;\"" : "" %>><%=charset%>
    346349            <%
     350            i++;
    347351          }
    348352          %>
  • trunk/www/filemanager/files/edit_file.jsp

    r5569 r5570  
    2929  import="net.sf.basedb.core.DbControl"
    3030  import="net.sf.basedb.core.Item"
     31  import="net.sf.basedb.core.Config"
    3132  import="net.sf.basedb.core.SystemItems"
    3233  import="net.sf.basedb.core.ItemContext"
     
    340341          String currentCharset = file == null ?
    341342            Values.getString(cc.getPropertyValue("characterSet")) : file.getCharacterSet();
     343          int numCommon = Config.getCommonCharsets().size();
     344          int i = 0;
    342345          boolean hasMatched = false;
    343           for (String charset : Charset.availableCharsets().keySet())
     346          for (String charset : Config.getAllCharsets())
    344347          {
    345             boolean selected = charset.equalsIgnoreCase(currentCharset);
    346             hasMatched |= selected;
     348            boolean selected = false;
     349            if (!hasMatched)
     350            {
     351              selected = charset.equalsIgnoreCase(currentCharset);
     352              hasMatched |= selected;
     353            }
    347354            %>
    348             <option value="<%=charset%>" <%=selected ? "selected" : ""%>><%=charset%>
     355            <option value="<%=charset%>" <%=selected ? "selected" : ""%>
     356              <%=i==numCommon ? "style=\"border-top: 1px solid #666666;\"" : "" %>><%=charset%>
    349357            <%
     358            i++;
    350359          }
    351360          if (!hasMatched && currentCharset != null)
  • trunk/www/filemanager/files/edit_multiple_file.jsp

    r5569 r5570  
    2929  import="net.sf.basedb.core.DbControl"
    3030  import="net.sf.basedb.core.Item"
     31  import="net.sf.basedb.core.Config"
    3132  import="net.sf.basedb.core.SystemItems"
    3233  import="net.sf.basedb.core.ItemContext"
     
    182183          <option value="">- n/a -
    183184          <%
    184           for (String charset : Charset.availableCharsets().keySet())
     185          int numCommon = Config.getCommonCharsets().size();
     186          int i = 0;
     187          for (String charset : Config.getAllCharsets())
    185188          {
    186189            %>
    187             <option value="<%=charset%>"><%=charset%>
     190            <option value="<%=charset%>" <%=i==numCommon ? "style=\"border-top: 1px solid #666666;\"" : "" %>><%=charset%>
    188191            <%
     192            i++;
    189193          }
    190194          %>
  • trunk/www/filemanager/upload/select.jsp

    r5554 r5570  
    3030  import="net.sf.basedb.core.SessionControl"
    3131  import="net.sf.basedb.core.DbControl"
     32  import="net.sf.basedb.core.Config"
    3233  import="net.sf.basedb.core.Item"
    3334  import="net.sf.basedb.core.SystemItems"
     
    486487          <%
    487488          String currentCharset = file == null ? null : file.getCharacterSet();
     489          int numCommon = Config.getCommonCharsets().size();
     490          int i = 0;
    488491          boolean hasSelected = false;
    489           for (String charset : Charset.availableCharsets().keySet())
     492          for (String charset : Config.getAllCharsets())
    490493          {
    491             boolean selected = charset.equalsIgnoreCase(currentCharset);
    492             hasSelected |= selected;
     494            boolean selected = false;
     495            if (!hasSelected)
     496            {
     497              selected = charset.equalsIgnoreCase(currentCharset);
     498              hasSelected |= selected;
     499            }
    493500            %>
    494             <option value="<%=charset%>" <%=selected ? "selected" : ""%>><%=charset%>
     501            <option value="<%=charset%>" <%=selected ? "selected" : ""%>
     502              <%=i==numCommon ? "style=\"border-top: 1px solid #666666;\"" : "" %>><%=charset%>
    495503            <%
     504            i++;
    496505          }
    497506          if (!hasSelected && currentCharset != null)
Note: See TracChangeset for help on using the changeset viewer.