2010-11-21 2 views
2

Ich habe fast hundert Eigenschaften wie dieseErhalten Eigenschaftsschlüssel durch Muster von ResourceBundleMessageSource im Frühjahr

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 

Und ich möchte diese Eigenschaften in der Lage bekommen (beide Schlüssel/Werte) ohne Vorkenntnisse der Schlüssel .. .etwas wie getPropertyPair(regexp .*.order.[a-z]*=)

Weiß jemand, ob Frühling oder JDK etwas dafür anbietet? Ich nehme an, ich werde das ResourceBundle bekommen und alle Schlüssel bekommen und sie regulieren müssen ...

Antwort

6

Das glaube ich nicht, dass Sie es im Frühjahr tun, aber hier ist ein Code, die helfen können:

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)); 
    } 

    } 

} 

Es ist ein bisschen hacky, aber ich bin sicher, dass Sie es bis zu etwas mehr nützlich ordentlich können.

+0

danke Gary, diese Frühjahr Fehlercodes, Resolver, Prozessoren, Ressourcen-Bundles ... es ist so chaotisch, das war total verwirrt ... Wenn die Feder Validierung nicht im Frühjahr Weg und die Form Taglib-Stil getan wird, ist es hart – lisak

+0

@lisak Kein Problem - froh zu helfen. Der Frühling ist großartig, aber manchmal ist es am besten, es einfach zu halten. –

3

Weiß jemand, ob Frühling oder JDK etwas dafür anbietet?

Nr

ich werde ich nehme an haben die Resource zu bekommen und die Schlüssel und regexp alle sie bekommen ...

Ja.

+0

Sie wissen, bekommen Resource nicht ganz einfach ist, ResourceBundleMessageSource.getResouceBundle geschützt ist und diese Klasse erstreckt, ist übertrieben. Resolving ResourceBundle basierend auf Locales ist nicht einfach. Du solltest nicht so kurz antworten müssen. Die Leute antworten normalerweise nicht genau so, wie sie gefragt wurden, aber sie spüren, was man sagen möchte. Nein/Ja – lisak

+0

Ich meine, ich wusste Nein/Ja, aber was sollte folgen – lisak

+0

Verkomplizieren Sie das nicht? Verwenden Sie ['ResourceBundle # getBundle()'] (http://download.oracle.com/javase/6/docs/api/java/util/ResourceBundle.html#getBundle%28java.lang.String,%20java.util. Gebietsschema% 29), um das gewünschte Bündel zu erhalten und ['ResourceBundle # keySet()'] zu verwenden (http://download.oracle.com/javase/6/docs/api/java/util/ResourceBundle.html#keySet%28 % 29), um alle Schlüssel zu erhalten. Entschuldigung, es war nicht offensichtlich aus der Frage, dass Sie Schwierigkeiten haben, das Bundle und die Schlüssel manuell zu bekommen. – BalusC

1

Ein wenig, um das Gespräch zu spät, aber ich über diese Antwort von Google gestolpert ...

Es gibt eine reine JDK-Implementierung ist here in der matchingSubset() Funktion, die durch propertyzurück Matches durch Iterieren über die Eigenschaften findet(). Wenn Sie wirklich eine Regex verwenden müssten, könnte sie ziemlich einfach angepasst werden.

Code-Snippet unten, falls gebucht der Link geht schlecht:

/** 
* 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; 

}

+0

Vielen Dank. Dies ist ein großartiger Artikel. –