代码之家  ›  专栏  ›  技术社区  ›  lisak

在春季从ResourceBundleMessageSource按模式获取属性键

  •  3
  • lisak  · 技术社区  · 14 年前

    我有差不多一百个这样的财产

        NotEmpty.order.languageFrom=Field Language can't be empty
        NotEmpty.order.languageTo=Field Language can't be empty
        NotEmpty.order.description=Description field can't be empty
        NotEmpty.order.formType=FormType field can't be empty
        NotEmpty.cart.formType=FormType field can't be empty
        NotEmpty.cart.formType=FormType field can't be empty
    

    我希望能够在不了解键的情况下获得这些属性(键/值),比如 getPropertyPair(regexp .*.order.[a-z]*=)

    有人知道Spring或JDK是否为此提供了帮助吗?我想我必须得到resourcebundle并获得所有的键并重新导出它们…

    3 回复  |  直到 8 年前
        1
  •  6
  •   Gary    14 年前

    我不认为你能在春天完成它,但这里有一些代码可以帮助:

    public class Main {
      public static void main(String[] args) {
        ResourceBundle labels = ResourceBundle.getBundle("spring-regex/regex-resources", Locale.UK);
        Enumeration<String> labelKeys = labels.getKeys();
    
        // Build up a buffer of label keys
        StringBuffer sb = new StringBuffer();
        while (labelKeys.hasMoreElements()) {
          String key = labelKeys.nextElement();
          sb.append(key + "|");
        }
    
        // Choose the pattern for matching
        Pattern pattern = Pattern.compile(".*.order.[a-z]*\\|");
        Matcher matcher = pattern.matcher(sb);
    
        // Attempt to find all matching keys
        List<String> matchingLabelKeys = new ArrayList<String>();
        while (matcher.find()) {
          String key=matcher.group();
          matchingLabelKeys.add(key.substring(0,key.length()-1));
        }
    
        // Show results
        for (String value: matchingLabelKeys) {
          System.out.format("Key=%s Resource=%s",value,labels.getString(value));
        }
    
      }
    
    }
    

    它有点粗糙,但我相信你能把它整理成更有用的东西。

        2
  •  3
  •   BalusC    14 年前

    有人知道Spring或JDK是否为此提供了帮助吗?

    不。

    我想我必须得到resourcebundle并获得所有的键并重新导出它们…

    对。

        3
  •  1
  •   Justin Killen    8 年前

    谈话有点晚了,但我偶然发现了谷歌的这个答案…

    有一个纯JDK实现 here 在matchingsubset()函数中,通过迭代propertyname()返回的属性来查找匹配项。如果您真的需要使用regex,它可以很容易地进行调整。

    如果链接出错,请在下面发布代码段:

    /**
     *  Copyright 2007 University Of Southern California
     *
     *  Licensed under the Apache License, Version 2.0 (the "License");
     *  you may not use this file except in compliance with the License.
     *  You may obtain a copy of the License at
     *
     *  http://www.apache.org/licenses/LICENSE-2.0
     *
     *  Unless required by applicable law or agreed to in writing,
     *  software distributed under the License is distributed on an "AS IS" BASIS,
     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     *  See the License for the specific language governing permissions and
     *  limitations under the License.
     */
    
    public Properties matchingSubset(String prefix, boolean keepPrefix) {
    Properties result = new Properties();
    
    // sanity check
    if (prefix == null || prefix.length() == 0) {
      return result;
    }
    
    String prefixMatch; // match prefix strings with this
    String prefixSelf; // match self with this
    if (prefix.charAt(prefix.length() - 1) != '.') {
      // prefix does not end in a dot
      prefixSelf = prefix;
      prefixMatch = prefix + '.';
    } else {
      // prefix does end in one dot, remove for exact matches
      prefixSelf = prefix.substring(0, prefix.length() - 1);
      prefixMatch = prefix;
    }
    // POSTCONDITION: prefixMatch and prefixSelf are initialized!
    
    // now add all matches into the resulting properties.
    // Remark 1: #propertyNames() will contain the System properties!
    // Remark 2: We need to give priority to System properties. This is done
    // automatically by calling this class's getProperty method.
    String key;
    for (Enumeration e = propertyNames(); e.hasMoreElements(); ) {
      key = (String) e.nextElement();
    
      if (keepPrefix) {
        // keep full prefix in result, also copy direct matches
        if (key.startsWith(prefixMatch) || key.equals(prefixSelf)) {
          result.setProperty(key,
                             getProperty(key));
        }
      } else {
        // remove full prefix in result, dont copy direct matches
        if (key.startsWith(prefixMatch)) {
          result.setProperty(key.substring(prefixMatch.length()),
                             getProperty(key));
        }
      }
    }
    
    // done
    return result;
    

    }