Index: lucene/src/java/org/apache/lucene/messages/Message.java =================================================================== --- lucene/src/java/org/apache/lucene/messages/Message.java (revision 1215327) +++ lucene/src/java/org/apache/lucene/messages/Message.java (working copy) @@ -1,36 +0,0 @@ -package org.apache.lucene.messages; - -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ - -import java.util.Locale; - -/** - * Message Interface for a lazy loading. - * For Native Language Support (NLS), system of software internationalization. - */ -public interface Message { - - public String getKey(); - - public Object[] getArguments(); - - public String getLocalizedMessage(); - - public String getLocalizedMessage(Locale locale); - -} Index: lucene/src/java/org/apache/lucene/messages/MessageImpl.java =================================================================== --- lucene/src/java/org/apache/lucene/messages/MessageImpl.java (revision 1215327) +++ lucene/src/java/org/apache/lucene/messages/MessageImpl.java (working copy) @@ -1,70 +0,0 @@ -package org.apache.lucene.messages; - -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ - -import java.util.Locale; - -/** - * Default implementation of Message interface. - * For Native Language Support (NLS), system of software internationalization. - */ -public class MessageImpl implements Message { - - private String key; - - private Object[] arguments = new Object[0]; - - public MessageImpl(String key) { - this.key = key; - - } - - public MessageImpl(String key, Object... args) { - this(key); - this.arguments = args; - } - - public Object[] getArguments() { - return this.arguments; - } - - public String getKey() { - return this.key; - } - - public String getLocalizedMessage() { - return getLocalizedMessage(Locale.getDefault()); - } - - public String getLocalizedMessage(Locale locale) { - return NLS.getLocalizedMessage(getKey(), locale, getArguments()); - } - - @Override - public String toString() { - Object[] args = getArguments(); - StringBuilder sb = new StringBuilder(getKey()); - if (args != null) { - for (int i = 0; i < args.length; i++) { - sb.append(i == 0 ? " " : ", ").append(args[i]); - } - } - return sb.toString(); - } - -} Index: lucene/src/java/org/apache/lucene/messages/NLSException.java =================================================================== --- lucene/src/java/org/apache/lucene/messages/NLSException.java (revision 1215327) +++ lucene/src/java/org/apache/lucene/messages/NLSException.java (working copy) @@ -1,34 +0,0 @@ -package org.apache.lucene.messages; - -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ - -/** - * Interface that exceptions should implement to support lazy loading of messages. - * - * For Native Language Support (NLS), system of software internationalization. - * - * This Interface should be implemented by all exceptions that require - * translation - * - */ -public interface NLSException { - /** - * @return a instance of a class that implements the Message interface - */ - public Message getMessageObject(); -} Index: lucene/src/java/org/apache/lucene/messages/NLS.java =================================================================== --- lucene/src/java/org/apache/lucene/messages/NLS.java (revision 1215327) +++ lucene/src/java/org/apache/lucene/messages/NLS.java (working copy) @@ -1,206 +0,0 @@ -package org.apache.lucene.messages; - -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ - -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.text.MessageFormat; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Locale; -import java.util.Map; -import java.util.MissingResourceException; -import java.util.ResourceBundle; - -/** - * MessageBundles classes extend this class, to implement a bundle. - * - * For Native Language Support (NLS), system of software internationalization. - * - * This interface is similar to the NLS class in eclipse.osgi.util.NLS class - - * initializeMessages() method resets the values of all static strings, should - * only be called by classes that extend from NLS (see TestMessages.java for - * reference) - performs validation of all message in a bundle, at class load - * time - performs per message validation at runtime - see NLSTest.java for - * usage reference - * - * MessageBundle classes may subclass this type. - */ -public class NLS { - - private static Map> bundles = - new HashMap>(0); - - protected NLS() { - // Do not instantiate - } - - public static String getLocalizedMessage(String key) { - return getLocalizedMessage(key, Locale.getDefault()); - } - - public static String getLocalizedMessage(String key, Locale locale) { - Object message = getResourceBundleObject(key, locale); - if (message == null) { - return "Message with key:" + key + " and locale: " + locale - + " not found."; - } - return message.toString(); - } - - public static String getLocalizedMessage(String key, Locale locale, - Object... args) { - String str = getLocalizedMessage(key, locale); - - if (args.length > 0) { - str = MessageFormat.format(str, args); - } - - return str; - } - - public static String getLocalizedMessage(String key, Object... args) { - return getLocalizedMessage(key, Locale.getDefault(), args); - } - - /** - * Initialize a given class with the message bundle Keys Should be called from - * a class that extends NLS in a static block at class load time. - * - * @param bundleName - * Property file with that contains the message bundle - * @param clazz - * where constants will reside - */ - protected static void initializeMessages(String bundleName, Class clazz) { - try { - load(clazz); - if (!bundles.containsKey(bundleName)) - bundles.put(bundleName, clazz); - } catch (Throwable e) { - // ignore all errors and exceptions - // because this function is supposed to be called at class load time. - } - } - - private static Object getResourceBundleObject(String messageKey, Locale locale) { - - // slow resource checking - // need to loop thru all registered resource bundles - for (Iterator it = bundles.keySet().iterator(); it.hasNext();) { - Class clazz = bundles.get(it.next()); - ResourceBundle resourceBundle = ResourceBundle.getBundle(clazz.getName(), - locale); - if (resourceBundle != null) { - try { - Object obj = resourceBundle.getObject(messageKey); - if (obj != null) - return obj; - } catch (MissingResourceException e) { - // just continue it might be on the next resource bundle - } - } - } - // if resource is not found - return null; - } - - /** - * @param clazz - */ - private static void load(Class clazz) { - final Field[] fieldArray = clazz.getDeclaredFields(); - - boolean isFieldAccessible = (clazz.getModifiers() & Modifier.PUBLIC) != 0; - - // build a map of field names to Field objects - final int len = fieldArray.length; - Map fields = new HashMap(len * 2); - for (int i = 0; i < len; i++) { - fields.put(fieldArray[i].getName(), fieldArray[i]); - loadfieldValue(fieldArray[i], isFieldAccessible, clazz); - } - } - - /** - * @param field - * @param isFieldAccessible - */ - private static void loadfieldValue(Field field, boolean isFieldAccessible, - Class clazz) { - int MOD_EXPECTED = Modifier.PUBLIC | Modifier.STATIC; - int MOD_MASK = MOD_EXPECTED | Modifier.FINAL; - if ((field.getModifiers() & MOD_MASK) != MOD_EXPECTED) - return; - - // Set a value for this empty field. - if (!isFieldAccessible) - makeAccessible(field); - try { - field.set(null, field.getName()); - validateMessage(field.getName(), clazz); - } catch (IllegalArgumentException e) { - // should not happen - } catch (IllegalAccessException e) { - // should not happen - } - } - - /** - * @param key - * - Message Key - */ - private static void validateMessage(String key, Class clazz) { - // Test if the message is present in the resource bundle - try { - ResourceBundle resourceBundle = ResourceBundle.getBundle(clazz.getName(), - Locale.getDefault()); - if (resourceBundle != null) { - Object obj = resourceBundle.getObject(key); - if (obj == null) - System.err.println("WARN: Message with key:" + key + " and locale: " - + Locale.getDefault() + " not found."); - } - } catch (MissingResourceException e) { - System.err.println("WARN: Message with key:" + key + " and locale: " - + Locale.getDefault() + " not found."); - } catch (Throwable e) { - // ignore all other errors and exceptions - // since this code is just a test to see if the message is present on the - // system - } - } - - /* - * Make a class field accessible - */ - private static void makeAccessible(final Field field) { - if (System.getSecurityManager() == null) { - field.setAccessible(true); - } else { - AccessController.doPrivileged(new PrivilegedAction() { - public Void run() { - field.setAccessible(true); - return null; - } - }); - } - } -} Index: lucene/src/java/org/apache/lucene/messages/package.html =================================================================== --- lucene/src/java/org/apache/lucene/messages/package.html (revision 1215327) +++ lucene/src/java/org/apache/lucene/messages/package.html (working copy) @@ -1,99 +0,0 @@ - - - - - - - - -For Native Language Support (NLS), system of software internationalization. - -

NLS message API

-

-This utility API, adds support for NLS messages in the apache code. -It is currently used by the lucene "New Flexible Query PArser". -

-

-Features: -

    -
  1. Message reference in the code, using static Strings
  2. -
  3. Message resource validation at class load time, for easier debugging
  4. -
  5. Allows for message IDs to be re-factored using eclipse or other code re-factor tools
  6. -
  7. Allows for reference count on messages, just like code
  8. -
  9. Lazy loading of Message Strings
  10. -
  11. Normal loading Message Strings
  12. -
-

- -
-
-

-Lazy loading of Message Strings - -

-	public class MessagesTestBundle extends NLS {
-	
-	  private static final String BUNDLE_NAME = MessagesTestBundle.class.getName();
-	
-	  private MessagesTestBundle() {
-	    // should never be instantiated
-	  }
-	
-	  static {
-	    // register all string ids with NLS class and initialize static string
-	    // values
-	    NLS.initializeMessages(BUNDLE_NAME, MessagesTestBundle.class);
-	  }
-	
-	  // static string must match the strings in the property files.
-	  public static String Q0001E_INVALID_SYNTAX;
-	  public static String Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION;
-	
-	  // this message is missing from the properties file
-	  public static String Q0005E_MESSAGE_NOT_IN_BUNDLE;
-	}
-
-    // Create a message reference
-    Message invalidSyntax = new MessageImpl(MessagesTestBundle.Q0001E_INVALID_SYNTAX, "XXX");
-    
-    // Do other stuff in the code...
-    // when is time to display the message to the user or log the message on a file
-    // the message is loaded from the correct bundle
-    
-    String message1 = invalidSyntax.getLocalizedMessage();
-    String message2 = invalidSyntax.getLocalizedMessage(Locale.JAPANESE);
-
-

- -
-
-

-Normal loading of Message Strings - -

-	String message1 = NLS.getLocalizedMessage(MessagesTestBundle.Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION);
-	String message2 = NLS.getLocalizedMessage(MessagesTestBundle.Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION, Locale.JAPANESE);
-
-

- -

-The org.apache.lucene.messages.TestNLS junit contains several other examples. -The TestNLS java code is available from the Apache Lucene code repository. -

- - Index: lucene/src/java/org/apache/lucene/messages/Message.java =================================================================== --- lucene/src/java/org/apache/lucene/messages/Message.java (revision 1215327) +++ lucene/src/java/org/apache/lucene/messages/Message.java (working copy) @@ -1,36 +0,0 @@ -package org.apache.lucene.messages; - -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ - -import java.util.Locale; - -/** - * Message Interface for a lazy loading. - * For Native Language Support (NLS), system of software internationalization. - */ -public interface Message { - - public String getKey(); - - public Object[] getArguments(); - - public String getLocalizedMessage(); - - public String getLocalizedMessage(Locale locale); - -} Index: lucene/src/java/org/apache/lucene/messages/MessageImpl.java =================================================================== --- lucene/src/java/org/apache/lucene/messages/MessageImpl.java (revision 1215327) +++ lucene/src/java/org/apache/lucene/messages/MessageImpl.java (working copy) @@ -1,70 +0,0 @@ -package org.apache.lucene.messages; - -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ - -import java.util.Locale; - -/** - * Default implementation of Message interface. - * For Native Language Support (NLS), system of software internationalization. - */ -public class MessageImpl implements Message { - - private String key; - - private Object[] arguments = new Object[0]; - - public MessageImpl(String key) { - this.key = key; - - } - - public MessageImpl(String key, Object... args) { - this(key); - this.arguments = args; - } - - public Object[] getArguments() { - return this.arguments; - } - - public String getKey() { - return this.key; - } - - public String getLocalizedMessage() { - return getLocalizedMessage(Locale.getDefault()); - } - - public String getLocalizedMessage(Locale locale) { - return NLS.getLocalizedMessage(getKey(), locale, getArguments()); - } - - @Override - public String toString() { - Object[] args = getArguments(); - StringBuilder sb = new StringBuilder(getKey()); - if (args != null) { - for (int i = 0; i < args.length; i++) { - sb.append(i == 0 ? " " : ", ").append(args[i]); - } - } - return sb.toString(); - } - -} Index: lucene/src/java/org/apache/lucene/messages/NLS.java =================================================================== --- lucene/src/java/org/apache/lucene/messages/NLS.java (revision 1215327) +++ lucene/src/java/org/apache/lucene/messages/NLS.java (working copy) @@ -1,206 +0,0 @@ -package org.apache.lucene.messages; - -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ - -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.text.MessageFormat; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Locale; -import java.util.Map; -import java.util.MissingResourceException; -import java.util.ResourceBundle; - -/** - * MessageBundles classes extend this class, to implement a bundle. - * - * For Native Language Support (NLS), system of software internationalization. - * - * This interface is similar to the NLS class in eclipse.osgi.util.NLS class - - * initializeMessages() method resets the values of all static strings, should - * only be called by classes that extend from NLS (see TestMessages.java for - * reference) - performs validation of all message in a bundle, at class load - * time - performs per message validation at runtime - see NLSTest.java for - * usage reference - * - * MessageBundle classes may subclass this type. - */ -public class NLS { - - private static Map> bundles = - new HashMap>(0); - - protected NLS() { - // Do not instantiate - } - - public static String getLocalizedMessage(String key) { - return getLocalizedMessage(key, Locale.getDefault()); - } - - public static String getLocalizedMessage(String key, Locale locale) { - Object message = getResourceBundleObject(key, locale); - if (message == null) { - return "Message with key:" + key + " and locale: " + locale - + " not found."; - } - return message.toString(); - } - - public static String getLocalizedMessage(String key, Locale locale, - Object... args) { - String str = getLocalizedMessage(key, locale); - - if (args.length > 0) { - str = MessageFormat.format(str, args); - } - - return str; - } - - public static String getLocalizedMessage(String key, Object... args) { - return getLocalizedMessage(key, Locale.getDefault(), args); - } - - /** - * Initialize a given class with the message bundle Keys Should be called from - * a class that extends NLS in a static block at class load time. - * - * @param bundleName - * Property file with that contains the message bundle - * @param clazz - * where constants will reside - */ - protected static void initializeMessages(String bundleName, Class clazz) { - try { - load(clazz); - if (!bundles.containsKey(bundleName)) - bundles.put(bundleName, clazz); - } catch (Throwable e) { - // ignore all errors and exceptions - // because this function is supposed to be called at class load time. - } - } - - private static Object getResourceBundleObject(String messageKey, Locale locale) { - - // slow resource checking - // need to loop thru all registered resource bundles - for (Iterator it = bundles.keySet().iterator(); it.hasNext();) { - Class clazz = bundles.get(it.next()); - ResourceBundle resourceBundle = ResourceBundle.getBundle(clazz.getName(), - locale); - if (resourceBundle != null) { - try { - Object obj = resourceBundle.getObject(messageKey); - if (obj != null) - return obj; - } catch (MissingResourceException e) { - // just continue it might be on the next resource bundle - } - } - } - // if resource is not found - return null; - } - - /** - * @param clazz - */ - private static void load(Class clazz) { - final Field[] fieldArray = clazz.getDeclaredFields(); - - boolean isFieldAccessible = (clazz.getModifiers() & Modifier.PUBLIC) != 0; - - // build a map of field names to Field objects - final int len = fieldArray.length; - Map fields = new HashMap(len * 2); - for (int i = 0; i < len; i++) { - fields.put(fieldArray[i].getName(), fieldArray[i]); - loadfieldValue(fieldArray[i], isFieldAccessible, clazz); - } - } - - /** - * @param field - * @param isFieldAccessible - */ - private static void loadfieldValue(Field field, boolean isFieldAccessible, - Class clazz) { - int MOD_EXPECTED = Modifier.PUBLIC | Modifier.STATIC; - int MOD_MASK = MOD_EXPECTED | Modifier.FINAL; - if ((field.getModifiers() & MOD_MASK) != MOD_EXPECTED) - return; - - // Set a value for this empty field. - if (!isFieldAccessible) - makeAccessible(field); - try { - field.set(null, field.getName()); - validateMessage(field.getName(), clazz); - } catch (IllegalArgumentException e) { - // should not happen - } catch (IllegalAccessException e) { - // should not happen - } - } - - /** - * @param key - * - Message Key - */ - private static void validateMessage(String key, Class clazz) { - // Test if the message is present in the resource bundle - try { - ResourceBundle resourceBundle = ResourceBundle.getBundle(clazz.getName(), - Locale.getDefault()); - if (resourceBundle != null) { - Object obj = resourceBundle.getObject(key); - if (obj == null) - System.err.println("WARN: Message with key:" + key + " and locale: " - + Locale.getDefault() + " not found."); - } - } catch (MissingResourceException e) { - System.err.println("WARN: Message with key:" + key + " and locale: " - + Locale.getDefault() + " not found."); - } catch (Throwable e) { - // ignore all other errors and exceptions - // since this code is just a test to see if the message is present on the - // system - } - } - - /* - * Make a class field accessible - */ - private static void makeAccessible(final Field field) { - if (System.getSecurityManager() == null) { - field.setAccessible(true); - } else { - AccessController.doPrivileged(new PrivilegedAction() { - public Void run() { - field.setAccessible(true); - return null; - } - }); - } - } -} Index: lucene/src/java/org/apache/lucene/messages/NLSException.java =================================================================== --- lucene/src/java/org/apache/lucene/messages/NLSException.java (revision 1215327) +++ lucene/src/java/org/apache/lucene/messages/NLSException.java (working copy) @@ -1,34 +0,0 @@ -package org.apache.lucene.messages; - -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ - -/** - * Interface that exceptions should implement to support lazy loading of messages. - * - * For Native Language Support (NLS), system of software internationalization. - * - * This Interface should be implemented by all exceptions that require - * translation - * - */ -public interface NLSException { - /** - * @return a instance of a class that implements the Message interface - */ - public Message getMessageObject(); -} Index: lucene/src/java/org/apache/lucene/messages/package.html =================================================================== --- lucene/src/java/org/apache/lucene/messages/package.html (revision 1215327) +++ lucene/src/java/org/apache/lucene/messages/package.html (working copy) @@ -1,99 +0,0 @@ - - - - - - - - -For Native Language Support (NLS), system of software internationalization. - -

NLS message API

-

-This utility API, adds support for NLS messages in the apache code. -It is currently used by the lucene "New Flexible Query PArser". -

-

-Features: -

    -
  1. Message reference in the code, using static Strings
  2. -
  3. Message resource validation at class load time, for easier debugging
  4. -
  5. Allows for message IDs to be re-factored using eclipse or other code re-factor tools
  6. -
  7. Allows for reference count on messages, just like code
  8. -
  9. Lazy loading of Message Strings
  10. -
  11. Normal loading Message Strings
  12. -
-

- -
-
-

-Lazy loading of Message Strings - -

-	public class MessagesTestBundle extends NLS {
-	
-	  private static final String BUNDLE_NAME = MessagesTestBundle.class.getName();
-	
-	  private MessagesTestBundle() {
-	    // should never be instantiated
-	  }
-	
-	  static {
-	    // register all string ids with NLS class and initialize static string
-	    // values
-	    NLS.initializeMessages(BUNDLE_NAME, MessagesTestBundle.class);
-	  }
-	
-	  // static string must match the strings in the property files.
-	  public static String Q0001E_INVALID_SYNTAX;
-	  public static String Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION;
-	
-	  // this message is missing from the properties file
-	  public static String Q0005E_MESSAGE_NOT_IN_BUNDLE;
-	}
-
-    // Create a message reference
-    Message invalidSyntax = new MessageImpl(MessagesTestBundle.Q0001E_INVALID_SYNTAX, "XXX");
-    
-    // Do other stuff in the code...
-    // when is time to display the message to the user or log the message on a file
-    // the message is loaded from the correct bundle
-    
-    String message1 = invalidSyntax.getLocalizedMessage();
-    String message2 = invalidSyntax.getLocalizedMessage(Locale.JAPANESE);
-
-

- -
-
-

-Normal loading of Message Strings - -

-	String message1 = NLS.getLocalizedMessage(MessagesTestBundle.Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION);
-	String message2 = NLS.getLocalizedMessage(MessagesTestBundle.Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION, Locale.JAPANESE);
-
-

- -

-The org.apache.lucene.messages.TestNLS junit contains several other examples. -The TestNLS java code is available from the Apache Lucene code repository. -

- - Index: lucene/src/test/org/apache/lucene/messages/MessagesTestBundle.java =================================================================== --- lucene/src/test/org/apache/lucene/messages/MessagesTestBundle.java (revision 1215327) +++ lucene/src/test/org/apache/lucene/messages/MessagesTestBundle.java (working copy) @@ -1,40 +0,0 @@ -package org.apache.lucene.messages; - -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with this - * work for additional information regarding copyright ownership. The ASF - * licenses this file to You 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 class MessagesTestBundle extends NLS { - - private static final String BUNDLE_NAME = MessagesTestBundle.class.getName(); - - private MessagesTestBundle() { - // should never be instantiated - } - - static { - // register all string ids with NLS class and initialize static string - // values - NLS.initializeMessages(BUNDLE_NAME, MessagesTestBundle.class); - } - - // static string must match the strings in the property files. - public static String Q0001E_INVALID_SYNTAX; - public static String Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION; - - // this message is missing from the properties file - public static String Q0005E_MESSAGE_NOT_IN_BUNDLE; -} Index: lucene/src/test/org/apache/lucene/messages/MessagesTestBundle_ja.properties =================================================================== --- lucene/src/test/org/apache/lucene/messages/MessagesTestBundle_ja.properties (revision 1215327) +++ lucene/src/test/org/apache/lucene/messages/MessagesTestBundle_ja.properties (working copy) @@ -1,3 +0,0 @@ -Q0001E_INVALID_SYNTAX = \u69cb\u6587\u30a8\u30e9\u30fc: {0} - -Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION = \u5207\u308a\u6368\u3066\u3089\u308c\u305f\u30e6\u30cb\u30b3\u30fc\u30c9\u30fb\u30a8\u30b9\u30b1\u30fc\u30d7\u30fb\u30b7\u30fc\u30b1\u30f3\u30b9\u3002 Index: lucene/src/test/org/apache/lucene/messages/TestNLS.java =================================================================== --- lucene/src/test/org/apache/lucene/messages/TestNLS.java (revision 1215327) +++ lucene/src/test/org/apache/lucene/messages/TestNLS.java (working copy) @@ -1,106 +0,0 @@ -package org.apache.lucene.messages; - -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ - -import java.util.Locale; - -import org.apache.lucene.util.LuceneTestCase; - -/** - */ -public class TestNLS extends LuceneTestCase { - public void testMessageLoading() { - Message invalidSyntax = new MessageImpl( - MessagesTestBundle.Q0001E_INVALID_SYNTAX, "XXX"); - /* - * if the default locale is ja, you get ja as a fallback: - * see ResourceBundle.html#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) - */ - if (!Locale.getDefault().getLanguage().equals("ja")) - assertEquals("Syntax Error: XXX", invalidSyntax.getLocalizedMessage(Locale.ENGLISH)); - } - - public void testMessageLoading_ja() { - Message invalidSyntax = new MessageImpl( - MessagesTestBundle.Q0001E_INVALID_SYNTAX, "XXX"); - assertEquals("構文エラー: XXX", invalidSyntax - .getLocalizedMessage(Locale.JAPANESE)); - } - - public void testNLSLoading() { - String message = NLS - .getLocalizedMessage(MessagesTestBundle.Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION, Locale.ENGLISH); - /* - * if the default locale is ja, you get ja as a fallback: - * see ResourceBundle.html#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) - */ - if (!Locale.getDefault().getLanguage().equals("ja")) - assertEquals("Truncated unicode escape sequence.", message); - - message = NLS.getLocalizedMessage(MessagesTestBundle.Q0001E_INVALID_SYNTAX, Locale.ENGLISH, - "XXX"); - /* - * if the default locale is ja, you get ja as a fallback: - * see ResourceBundle.html#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) - */ - if (!Locale.getDefault().getLanguage().equals("ja")) - assertEquals("Syntax Error: XXX", message); - } - - public void testNLSLoading_ja() { - String message = NLS.getLocalizedMessage( - MessagesTestBundle.Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION, - Locale.JAPANESE); - assertEquals("切り捨てられたユニコード・エスケープ・シーケンス。", message); - - message = NLS.getLocalizedMessage(MessagesTestBundle.Q0001E_INVALID_SYNTAX, - Locale.JAPANESE, "XXX"); - assertEquals("構文エラー: XXX", message); - } - - public void testNLSLoading_xx_XX() { - Locale locale = new Locale("xx", "XX", ""); - String message = NLS.getLocalizedMessage( - MessagesTestBundle.Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION, - locale); - /* - * if the default locale is ja, you get ja as a fallback: - * see ResourceBundle.html#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) - */ - if (!Locale.getDefault().getLanguage().equals("ja")) - assertEquals("Truncated unicode escape sequence.", message); - - message = NLS.getLocalizedMessage(MessagesTestBundle.Q0001E_INVALID_SYNTAX, - locale, "XXX"); - /* - * if the default locale is ja, you get ja as a fallback: - * see ResourceBundle.html#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) - */ - if (!Locale.getDefault().getLanguage().equals("ja")) - assertEquals("Syntax Error: XXX", message); - } - - public void testMissingMessage() { - Locale locale = Locale.ENGLISH; - String message = NLS.getLocalizedMessage( - MessagesTestBundle.Q0005E_MESSAGE_NOT_IN_BUNDLE, locale); - - assertEquals("Message with key:Q0005E_MESSAGE_NOT_IN_BUNDLE and locale: " - + locale.toString() + " not found.", message); - } -} Index: lucene/src/test/org/apache/lucene/messages/MessagesTestBundle.properties =================================================================== --- lucene/src/test/org/apache/lucene/messages/MessagesTestBundle.properties (revision 1215327) +++ lucene/src/test/org/apache/lucene/messages/MessagesTestBundle.properties (working copy) @@ -1,3 +0,0 @@ -Q0001E_INVALID_SYNTAX = Syntax Error: {0} - -Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION = Truncated unicode escape sequence. Index: lucene/src/test/org/apache/lucene/messages/MessagesTestBundle.java =================================================================== --- lucene/src/test/org/apache/lucene/messages/MessagesTestBundle.java (revision 1215327) +++ lucene/src/test/org/apache/lucene/messages/MessagesTestBundle.java (working copy) @@ -1,40 +0,0 @@ -package org.apache.lucene.messages; - -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with this - * work for additional information regarding copyright ownership. The ASF - * licenses this file to You 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 class MessagesTestBundle extends NLS { - - private static final String BUNDLE_NAME = MessagesTestBundle.class.getName(); - - private MessagesTestBundle() { - // should never be instantiated - } - - static { - // register all string ids with NLS class and initialize static string - // values - NLS.initializeMessages(BUNDLE_NAME, MessagesTestBundle.class); - } - - // static string must match the strings in the property files. - public static String Q0001E_INVALID_SYNTAX; - public static String Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION; - - // this message is missing from the properties file - public static String Q0005E_MESSAGE_NOT_IN_BUNDLE; -} Index: lucene/src/test/org/apache/lucene/messages/MessagesTestBundle.properties =================================================================== --- lucene/src/test/org/apache/lucene/messages/MessagesTestBundle.properties (revision 1215327) +++ lucene/src/test/org/apache/lucene/messages/MessagesTestBundle.properties (working copy) @@ -1,3 +0,0 @@ -Q0001E_INVALID_SYNTAX = Syntax Error: {0} - -Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION = Truncated unicode escape sequence. Index: lucene/src/test/org/apache/lucene/messages/MessagesTestBundle_ja.properties =================================================================== --- lucene/src/test/org/apache/lucene/messages/MessagesTestBundle_ja.properties (revision 1215327) +++ lucene/src/test/org/apache/lucene/messages/MessagesTestBundle_ja.properties (working copy) @@ -1,3 +0,0 @@ -Q0001E_INVALID_SYNTAX = \u69cb\u6587\u30a8\u30e9\u30fc: {0} - -Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION = \u5207\u308a\u6368\u3066\u3089\u308c\u305f\u30e6\u30cb\u30b3\u30fc\u30c9\u30fb\u30a8\u30b9\u30b1\u30fc\u30d7\u30fb\u30b7\u30fc\u30b1\u30f3\u30b9\u3002 Index: lucene/src/test/org/apache/lucene/messages/TestNLS.java =================================================================== --- lucene/src/test/org/apache/lucene/messages/TestNLS.java (revision 1215327) +++ lucene/src/test/org/apache/lucene/messages/TestNLS.java (working copy) @@ -1,106 +0,0 @@ -package org.apache.lucene.messages; - -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ - -import java.util.Locale; - -import org.apache.lucene.util.LuceneTestCase; - -/** - */ -public class TestNLS extends LuceneTestCase { - public void testMessageLoading() { - Message invalidSyntax = new MessageImpl( - MessagesTestBundle.Q0001E_INVALID_SYNTAX, "XXX"); - /* - * if the default locale is ja, you get ja as a fallback: - * see ResourceBundle.html#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) - */ - if (!Locale.getDefault().getLanguage().equals("ja")) - assertEquals("Syntax Error: XXX", invalidSyntax.getLocalizedMessage(Locale.ENGLISH)); - } - - public void testMessageLoading_ja() { - Message invalidSyntax = new MessageImpl( - MessagesTestBundle.Q0001E_INVALID_SYNTAX, "XXX"); - assertEquals("構文エラー: XXX", invalidSyntax - .getLocalizedMessage(Locale.JAPANESE)); - } - - public void testNLSLoading() { - String message = NLS - .getLocalizedMessage(MessagesTestBundle.Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION, Locale.ENGLISH); - /* - * if the default locale is ja, you get ja as a fallback: - * see ResourceBundle.html#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) - */ - if (!Locale.getDefault().getLanguage().equals("ja")) - assertEquals("Truncated unicode escape sequence.", message); - - message = NLS.getLocalizedMessage(MessagesTestBundle.Q0001E_INVALID_SYNTAX, Locale.ENGLISH, - "XXX"); - /* - * if the default locale is ja, you get ja as a fallback: - * see ResourceBundle.html#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) - */ - if (!Locale.getDefault().getLanguage().equals("ja")) - assertEquals("Syntax Error: XXX", message); - } - - public void testNLSLoading_ja() { - String message = NLS.getLocalizedMessage( - MessagesTestBundle.Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION, - Locale.JAPANESE); - assertEquals("切り捨てられたユニコード・エスケープ・シーケンス。", message); - - message = NLS.getLocalizedMessage(MessagesTestBundle.Q0001E_INVALID_SYNTAX, - Locale.JAPANESE, "XXX"); - assertEquals("構文エラー: XXX", message); - } - - public void testNLSLoading_xx_XX() { - Locale locale = new Locale("xx", "XX", ""); - String message = NLS.getLocalizedMessage( - MessagesTestBundle.Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION, - locale); - /* - * if the default locale is ja, you get ja as a fallback: - * see ResourceBundle.html#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) - */ - if (!Locale.getDefault().getLanguage().equals("ja")) - assertEquals("Truncated unicode escape sequence.", message); - - message = NLS.getLocalizedMessage(MessagesTestBundle.Q0001E_INVALID_SYNTAX, - locale, "XXX"); - /* - * if the default locale is ja, you get ja as a fallback: - * see ResourceBundle.html#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) - */ - if (!Locale.getDefault().getLanguage().equals("ja")) - assertEquals("Syntax Error: XXX", message); - } - - public void testMissingMessage() { - Locale locale = Locale.ENGLISH; - String message = NLS.getLocalizedMessage( - MessagesTestBundle.Q0005E_MESSAGE_NOT_IN_BUNDLE, locale); - - assertEquals("Message with key:Q0005E_MESSAGE_NOT_IN_BUNDLE and locale: " - + locale.toString() + " not found.", message); - } -} Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/builders/QueryTreeBuilder.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/builders/QueryTreeBuilder.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/builders/QueryTreeBuilder.java (working copy) @@ -20,7 +20,7 @@ import java.util.HashMap; import java.util.List; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.QueryNodeException; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; import org.apache.lucene.queryparser.flexible.core.nodes.FieldableNode; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/messages/QueryParserMessages.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/messages/QueryParserMessages.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/messages/QueryParserMessages.java (working copy) @@ -17,7 +17,7 @@ * limitations under the License. */ -import org.apache.lucene.messages.NLS; +import org.apache.lucene.queryparser.flexible.messages.NLS; /** * Flexible Query Parser message bundle class Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/BoostQueryNode.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/BoostQueryNode.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/BoostQueryNode.java (working copy) @@ -19,7 +19,7 @@ import java.util.List; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.QueryNodeError; import org.apache.lucene.queryparser.flexible.core.QueryNodeException; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/GroupQueryNode.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/GroupQueryNode.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/GroupQueryNode.java (working copy) @@ -20,7 +20,7 @@ import java.util.ArrayList; import java.util.List; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.QueryNodeError; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; import org.apache.lucene.queryparser.flexible.core.parser.EscapeQuerySyntax; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/ModifierQueryNode.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/ModifierQueryNode.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/ModifierQueryNode.java (working copy) @@ -20,7 +20,7 @@ import java.util.ArrayList; import java.util.List; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.parser.EscapeQuerySyntax; import org.apache.lucene.queryparser.flexible.core.QueryNodeError; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/PhraseSlopQueryNode.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/PhraseSlopQueryNode.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/PhraseSlopQueryNode.java (working copy) @@ -17,7 +17,7 @@ * limitations under the License. */ -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.QueryNodeError; import org.apache.lucene.queryparser.flexible.core.QueryNodeException; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/ProximityQueryNode.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/ProximityQueryNode.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/ProximityQueryNode.java (working copy) @@ -19,7 +19,7 @@ import java.util.List; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.parser.EscapeQuerySyntax; import org.apache.lucene.queryparser.flexible.core.QueryNodeError; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/QueryNodeImpl.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/QueryNodeImpl.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/QueryNodeImpl.java (working copy) @@ -23,7 +23,7 @@ import java.util.Map; import java.util.ResourceBundle; -import org.apache.lucene.messages.NLS; +import org.apache.lucene.queryparser.flexible.messages.NLS; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; import org.apache.lucene.queryparser.flexible.core.util.StringUtils; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/SlopQueryNode.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/SlopQueryNode.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/SlopQueryNode.java (working copy) @@ -17,7 +17,7 @@ * limitations under the License. */ -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.parser.EscapeQuerySyntax; import org.apache.lucene.queryparser.flexible.core.QueryNodeError; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/QueryNodeError.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/QueryNodeError.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/QueryNodeError.java (working copy) @@ -17,8 +17,8 @@ * limitations under the License. */ -import org.apache.lucene.messages.Message; -import org.apache.lucene.messages.NLSException; +import org.apache.lucene.queryparser.flexible.messages.Message; +import org.apache.lucene.queryparser.flexible.messages.NLSException; /** * Error class with NLS support Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/QueryNodeException.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/QueryNodeException.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/QueryNodeException.java (working copy) @@ -19,10 +19,10 @@ import java.util.Locale; -import org.apache.lucene.messages.Message; -import org.apache.lucene.messages.MessageImpl; -import org.apache.lucene.messages.NLS; -import org.apache.lucene.messages.NLSException; +import org.apache.lucene.queryparser.flexible.messages.Message; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.NLS; +import org.apache.lucene.queryparser.flexible.messages.NLSException; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; import org.apache.lucene.queryparser.flexible.core.nodes.QueryNode; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/QueryNodeParseException.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/QueryNodeParseException.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/QueryNodeParseException.java (working copy) @@ -17,8 +17,8 @@ * limitations under the License. */ -import org.apache.lucene.messages.Message; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.Message; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; import org.apache.lucene.queryparser.flexible.core.parser.SyntaxParser; import org.apache.lucene.queryparser.flexible.core.nodes.QueryNode; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/Message.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/Message.java (revision 0) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/Message.java (working copy) @@ -0,0 +1,36 @@ +package org.apache.lucene.queryparser.flexible.messages; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +import java.util.Locale; + +/** + * Message Interface for a lazy loading. + * For Native Language Support (NLS), system of software internationalization. + */ +public interface Message { + + public String getKey(); + + public Object[] getArguments(); + + public String getLocalizedMessage(); + + public String getLocalizedMessage(Locale locale); + +} Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/Message.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/Message.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/Message.java (working copy) Property changes on: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/Message.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/MessageImpl.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/MessageImpl.java (revision 0) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/MessageImpl.java (working copy) @@ -0,0 +1,70 @@ +package org.apache.lucene.queryparser.flexible.messages; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +import java.util.Locale; + +/** + * Default implementation of Message interface. + * For Native Language Support (NLS), system of software internationalization. + */ +public class MessageImpl implements Message { + + private String key; + + private Object[] arguments = new Object[0]; + + public MessageImpl(String key) { + this.key = key; + + } + + public MessageImpl(String key, Object... args) { + this(key); + this.arguments = args; + } + + public Object[] getArguments() { + return this.arguments; + } + + public String getKey() { + return this.key; + } + + public String getLocalizedMessage() { + return getLocalizedMessage(Locale.getDefault()); + } + + public String getLocalizedMessage(Locale locale) { + return NLS.getLocalizedMessage(getKey(), locale, getArguments()); + } + + @Override + public String toString() { + Object[] args = getArguments(); + StringBuilder sb = new StringBuilder(getKey()); + if (args != null) { + for (int i = 0; i < args.length; i++) { + sb.append(i == 0 ? " " : ", ").append(args[i]); + } + } + return sb.toString(); + } + +} Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/MessageImpl.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/MessageImpl.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/MessageImpl.java (working copy) Property changes on: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/MessageImpl.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/NLS.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/NLS.java (revision 0) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/NLS.java (working copy) @@ -0,0 +1,206 @@ +package org.apache.lucene.queryparser.flexible.messages; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.text.MessageFormat; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Locale; +import java.util.Map; +import java.util.MissingResourceException; +import java.util.ResourceBundle; + +/** + * MessageBundles classes extend this class, to implement a bundle. + * + * For Native Language Support (NLS), system of software internationalization. + * + * This interface is similar to the NLS class in eclipse.osgi.util.NLS class - + * initializeMessages() method resets the values of all static strings, should + * only be called by classes that extend from NLS (see TestMessages.java for + * reference) - performs validation of all message in a bundle, at class load + * time - performs per message validation at runtime - see NLSTest.java for + * usage reference + * + * MessageBundle classes may subclass this type. + */ +public class NLS { + + private static Map> bundles = + new HashMap>(0); + + protected NLS() { + // Do not instantiate + } + + public static String getLocalizedMessage(String key) { + return getLocalizedMessage(key, Locale.getDefault()); + } + + public static String getLocalizedMessage(String key, Locale locale) { + Object message = getResourceBundleObject(key, locale); + if (message == null) { + return "Message with key:" + key + " and locale: " + locale + + " not found."; + } + return message.toString(); + } + + public static String getLocalizedMessage(String key, Locale locale, + Object... args) { + String str = getLocalizedMessage(key, locale); + + if (args.length > 0) { + str = MessageFormat.format(str, args); + } + + return str; + } + + public static String getLocalizedMessage(String key, Object... args) { + return getLocalizedMessage(key, Locale.getDefault(), args); + } + + /** + * Initialize a given class with the message bundle Keys Should be called from + * a class that extends NLS in a static block at class load time. + * + * @param bundleName + * Property file with that contains the message bundle + * @param clazz + * where constants will reside + */ + protected static void initializeMessages(String bundleName, Class clazz) { + try { + load(clazz); + if (!bundles.containsKey(bundleName)) + bundles.put(bundleName, clazz); + } catch (Throwable e) { + // ignore all errors and exceptions + // because this function is supposed to be called at class load time. + } + } + + private static Object getResourceBundleObject(String messageKey, Locale locale) { + + // slow resource checking + // need to loop thru all registered resource bundles + for (Iterator it = bundles.keySet().iterator(); it.hasNext();) { + Class clazz = bundles.get(it.next()); + ResourceBundle resourceBundle = ResourceBundle.getBundle(clazz.getName(), + locale); + if (resourceBundle != null) { + try { + Object obj = resourceBundle.getObject(messageKey); + if (obj != null) + return obj; + } catch (MissingResourceException e) { + // just continue it might be on the next resource bundle + } + } + } + // if resource is not found + return null; + } + + /** + * @param clazz + */ + private static void load(Class clazz) { + final Field[] fieldArray = clazz.getDeclaredFields(); + + boolean isFieldAccessible = (clazz.getModifiers() & Modifier.PUBLIC) != 0; + + // build a map of field names to Field objects + final int len = fieldArray.length; + Map fields = new HashMap(len * 2); + for (int i = 0; i < len; i++) { + fields.put(fieldArray[i].getName(), fieldArray[i]); + loadfieldValue(fieldArray[i], isFieldAccessible, clazz); + } + } + + /** + * @param field + * @param isFieldAccessible + */ + private static void loadfieldValue(Field field, boolean isFieldAccessible, + Class clazz) { + int MOD_EXPECTED = Modifier.PUBLIC | Modifier.STATIC; + int MOD_MASK = MOD_EXPECTED | Modifier.FINAL; + if ((field.getModifiers() & MOD_MASK) != MOD_EXPECTED) + return; + + // Set a value for this empty field. + if (!isFieldAccessible) + makeAccessible(field); + try { + field.set(null, field.getName()); + validateMessage(field.getName(), clazz); + } catch (IllegalArgumentException e) { + // should not happen + } catch (IllegalAccessException e) { + // should not happen + } + } + + /** + * @param key + * - Message Key + */ + private static void validateMessage(String key, Class clazz) { + // Test if the message is present in the resource bundle + try { + ResourceBundle resourceBundle = ResourceBundle.getBundle(clazz.getName(), + Locale.getDefault()); + if (resourceBundle != null) { + Object obj = resourceBundle.getObject(key); + if (obj == null) + System.err.println("WARN: Message with key:" + key + " and locale: " + + Locale.getDefault() + " not found."); + } + } catch (MissingResourceException e) { + System.err.println("WARN: Message with key:" + key + " and locale: " + + Locale.getDefault() + " not found."); + } catch (Throwable e) { + // ignore all other errors and exceptions + // since this code is just a test to see if the message is present on the + // system + } + } + + /* + * Make a class field accessible + */ + private static void makeAccessible(final Field field) { + if (System.getSecurityManager() == null) { + field.setAccessible(true); + } else { + AccessController.doPrivileged(new PrivilegedAction() { + public Void run() { + field.setAccessible(true); + return null; + } + }); + } + } +} Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/NLS.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/NLS.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/NLS.java (working copy) Property changes on: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/NLS.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/NLSException.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/NLSException.java (revision 0) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/NLSException.java (working copy) @@ -0,0 +1,34 @@ +package org.apache.lucene.queryparser.flexible.messages; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +/** + * Interface that exceptions should implement to support lazy loading of messages. + * + * For Native Language Support (NLS), system of software internationalization. + * + * This Interface should be implemented by all exceptions that require + * translation + * + */ +public interface NLSException { + /** + * @return a instance of a class that implements the Message interface + */ + public Message getMessageObject(); +} Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/NLSException.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/NLSException.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/NLSException.java (working copy) Property changes on: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/NLSException.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/package.html =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/package.html (revision 0) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/package.html (working copy) @@ -0,0 +1,99 @@ + + + + + + + + +For Native Language Support (NLS), system of software internationalization. + +

NLS message API

+

+This utility API, adds support for NLS messages in the apache code. +It is currently used by the lucene "New Flexible Query PArser". +

+

+Features: +

    +
  1. Message reference in the code, using static Strings
  2. +
  3. Message resource validation at class load time, for easier debugging
  4. +
  5. Allows for message IDs to be re-factored using eclipse or other code re-factor tools
  6. +
  7. Allows for reference count on messages, just like code
  8. +
  9. Lazy loading of Message Strings
  10. +
  11. Normal loading Message Strings
  12. +
+

+ +
+
+

+Lazy loading of Message Strings + +

+	public class MessagesTestBundle extends NLS {
+	
+	  private static final String BUNDLE_NAME = MessagesTestBundle.class.getName();
+	
+	  private MessagesTestBundle() {
+	    // should never be instantiated
+	  }
+	
+	  static {
+	    // register all string ids with NLS class and initialize static string
+	    // values
+	    NLS.initializeMessages(BUNDLE_NAME, MessagesTestBundle.class);
+	  }
+	
+	  // static string must match the strings in the property files.
+	  public static String Q0001E_INVALID_SYNTAX;
+	  public static String Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION;
+	
+	  // this message is missing from the properties file
+	  public static String Q0005E_MESSAGE_NOT_IN_BUNDLE;
+	}
+
+    // Create a message reference
+    Message invalidSyntax = new MessageImpl(MessagesTestBundle.Q0001E_INVALID_SYNTAX, "XXX");
+    
+    // Do other stuff in the code...
+    // when is time to display the message to the user or log the message on a file
+    // the message is loaded from the correct bundle
+    
+    String message1 = invalidSyntax.getLocalizedMessage();
+    String message2 = invalidSyntax.getLocalizedMessage(Locale.JAPANESE);
+
+

+ +
+
+

+Normal loading of Message Strings + +

+	String message1 = NLS.getLocalizedMessage(MessagesTestBundle.Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION);
+	String message2 = NLS.getLocalizedMessage(MessagesTestBundle.Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION, Locale.JAPANESE);
+
+

+ +

+The org.apache.lucene.messages.TestNLS junit contains several other examples. +The TestNLS java code is available from the Apache Lucene code repository. +

+ + Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/package.html =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/package.html (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/package.html (working copy) Property changes on: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/messages/package.html ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/AnyQueryNodeBuilder.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/AnyQueryNodeBuilder.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/AnyQueryNodeBuilder.java (working copy) @@ -19,7 +19,7 @@ import java.util.List; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.QueryNodeException; import org.apache.lucene.queryparser.flexible.core.builders.QueryTreeBuilder; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/BooleanQueryNodeBuilder.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/BooleanQueryNodeBuilder.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/BooleanQueryNodeBuilder.java (working copy) @@ -19,7 +19,7 @@ import java.util.List; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.QueryNodeException; import org.apache.lucene.queryparser.flexible.core.builders.QueryTreeBuilder; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/MatchAllDocsQueryNodeBuilder.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/MatchAllDocsQueryNodeBuilder.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/MatchAllDocsQueryNodeBuilder.java (working copy) @@ -17,7 +17,7 @@ * limitations under the License. */ -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.QueryNodeException; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; import org.apache.lucene.queryparser.flexible.core.nodes.MatchAllDocsQueryNode; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/MatchNoDocsQueryNodeBuilder.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/MatchNoDocsQueryNodeBuilder.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/MatchNoDocsQueryNodeBuilder.java (working copy) @@ -17,7 +17,7 @@ * limitations under the License. */ -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.QueryNodeException; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; import org.apache.lucene.queryparser.flexible.core.nodes.MatchNoDocsQueryNode; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/NumericRangeQueryNodeBuilder.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/NumericRangeQueryNodeBuilder.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/NumericRangeQueryNodeBuilder.java (working copy) @@ -18,7 +18,7 @@ */ import org.apache.lucene.document.NumericField; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.QueryNodeException; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; import org.apache.lucene.queryparser.flexible.core.nodes.QueryNode; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/StandardBooleanQueryNodeBuilder.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/StandardBooleanQueryNodeBuilder.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/builders/StandardBooleanQueryNodeBuilder.java (working copy) @@ -19,7 +19,7 @@ import java.util.List; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.QueryNodeException; import org.apache.lucene.queryparser.flexible.core.builders.QueryTreeBuilder; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/nodes/NumericRangeQueryNode.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/nodes/NumericRangeQueryNode.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/nodes/NumericRangeQueryNode.java (working copy) @@ -1,7 +1,7 @@ package org.apache.lucene.queryparser.flexible.standard.nodes; import org.apache.lucene.document.NumericField; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.QueryNodeException; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; import org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/EscapeQuerySyntaxImpl.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/EscapeQuerySyntaxImpl.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/EscapeQuerySyntaxImpl.java (working copy) @@ -19,7 +19,7 @@ import java.util.Locale; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; import org.apache.lucene.queryparser.flexible.core.parser.EscapeQuerySyntax; import org.apache.lucene.queryparser.flexible.core.util.UnescapedCharSequence; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/ParseException.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/ParseException.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/ParseException.java (working copy) @@ -2,8 +2,8 @@ /* JavaCCOptions:KEEP_LINE_COL=null */ package org.apache.lucene.queryparser.flexible.standard.parser; - import org.apache.lucene.messages.Message; - import org.apache.lucene.messages.MessageImpl; + import org.apache.lucene.queryparser.flexible.messages.Message; + import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.*; import org.apache.lucene.queryparser.flexible.core.messages.*; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParser.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParser.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParser.java (working copy) @@ -21,8 +21,8 @@ import java.io.StringReader; import java.util.Vector; -import org.apache.lucene.messages.Message; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.Message; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.QueryNodeParseException; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; import org.apache.lucene.queryparser.flexible.core.nodes.AndQueryNode; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParser.jj =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParser.jj (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParser.jj (working copy) @@ -33,8 +33,8 @@ import java.io.StringReader; import java.util.Vector; -import org.apache.lucene.messages.Message; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.Message; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.QueryNodeParseException; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; import org.apache.lucene.queryparser.flexible.core.nodes.AndQueryNode; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParserTokenManager.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParserTokenManager.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParserTokenManager.java (working copy) @@ -19,8 +19,8 @@ import java.io.StringReader; import java.util.Vector; -import org.apache.lucene.messages.Message; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.Message; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.QueryNodeParseException; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; import org.apache.lucene.queryparser.flexible.core.nodes.AndQueryNode; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/AllowLeadingWildcardProcessor.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/AllowLeadingWildcardProcessor.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/AllowLeadingWildcardProcessor.java (working copy) @@ -19,7 +19,7 @@ import java.util.List; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.QueryNodeException; import org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/NumericQueryNodeProcessor.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/NumericQueryNodeProcessor.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/NumericQueryNodeProcessor.java (working copy) @@ -21,7 +21,7 @@ import java.text.ParseException; import java.util.List; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.QueryNodeException; import org.apache.lucene.queryparser.flexible.core.QueryNodeParseException; import org.apache.lucene.queryparser.flexible.core.config.FieldConfig; Index: modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/NumericRangeQueryNodeProcessor.java =================================================================== --- modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/NumericRangeQueryNodeProcessor.java (revision 1215327) +++ modules/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/NumericRangeQueryNodeProcessor.java (working copy) @@ -21,7 +21,7 @@ import java.text.ParseException; import java.util.List; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.QueryNodeException; import org.apache.lucene.queryparser.flexible.core.QueryNodeParseException; import org.apache.lucene.queryparser.flexible.core.config.FieldConfig; Index: modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle.java =================================================================== --- modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle.java (revision 0) +++ modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle.java (working copy) @@ -0,0 +1,40 @@ +package org.apache.lucene.queryparser.flexible.messages; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You 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 class MessagesTestBundle extends NLS { + + private static final String BUNDLE_NAME = MessagesTestBundle.class.getName(); + + private MessagesTestBundle() { + // should never be instantiated + } + + static { + // register all string ids with NLS class and initialize static string + // values + NLS.initializeMessages(BUNDLE_NAME, MessagesTestBundle.class); + } + + // static string must match the strings in the property files. + public static String Q0001E_INVALID_SYNTAX; + public static String Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION; + + // this message is missing from the properties file + public static String Q0005E_MESSAGE_NOT_IN_BUNDLE; +} Index: modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle.java =================================================================== --- modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle.java (revision 1215327) +++ modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle.java (working copy) Property changes on: modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native Index: modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle.properties =================================================================== --- modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle.properties (revision 0) +++ modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle.properties (working copy) @@ -0,0 +1,3 @@ +Q0001E_INVALID_SYNTAX = Syntax Error: {0} + +Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION = Truncated unicode escape sequence. Index: modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle.properties =================================================================== --- modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle.properties (revision 1215327) +++ modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle.properties (working copy) Property changes on: modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle.properties ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native Index: modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle_ja.properties =================================================================== --- modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle_ja.properties (revision 0) +++ modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle_ja.properties (working copy) @@ -0,0 +1,3 @@ +Q0001E_INVALID_SYNTAX = \u69cb\u6587\u30a8\u30e9\u30fc: {0} + +Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION = \u5207\u308a\u6368\u3066\u3089\u308c\u305f\u30e6\u30cb\u30b3\u30fc\u30c9\u30fb\u30a8\u30b9\u30b1\u30fc\u30d7\u30fb\u30b7\u30fc\u30b1\u30f3\u30b9\u3002 Index: modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle_ja.properties =================================================================== --- modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle_ja.properties (revision 1215327) +++ modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle_ja.properties (working copy) Property changes on: modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/MessagesTestBundle_ja.properties ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native Index: modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/TestNLS.java =================================================================== --- modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/TestNLS.java (revision 0) +++ modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/TestNLS.java (working copy) @@ -0,0 +1,106 @@ +package org.apache.lucene.queryparser.flexible.messages; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +import java.util.Locale; + +import org.apache.lucene.util.LuceneTestCase; + +/** + */ +public class TestNLS extends LuceneTestCase { + public void testMessageLoading() { + Message invalidSyntax = new MessageImpl( + MessagesTestBundle.Q0001E_INVALID_SYNTAX, "XXX"); + /* + * if the default locale is ja, you get ja as a fallback: + * see ResourceBundle.html#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) + */ + if (!Locale.getDefault().getLanguage().equals("ja")) + assertEquals("Syntax Error: XXX", invalidSyntax.getLocalizedMessage(Locale.ENGLISH)); + } + + public void testMessageLoading_ja() { + Message invalidSyntax = new MessageImpl( + MessagesTestBundle.Q0001E_INVALID_SYNTAX, "XXX"); + assertEquals("構文エラー: XXX", invalidSyntax + .getLocalizedMessage(Locale.JAPANESE)); + } + + public void testNLSLoading() { + String message = NLS + .getLocalizedMessage(MessagesTestBundle.Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION, Locale.ENGLISH); + /* + * if the default locale is ja, you get ja as a fallback: + * see ResourceBundle.html#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) + */ + if (!Locale.getDefault().getLanguage().equals("ja")) + assertEquals("Truncated unicode escape sequence.", message); + + message = NLS.getLocalizedMessage(MessagesTestBundle.Q0001E_INVALID_SYNTAX, Locale.ENGLISH, + "XXX"); + /* + * if the default locale is ja, you get ja as a fallback: + * see ResourceBundle.html#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) + */ + if (!Locale.getDefault().getLanguage().equals("ja")) + assertEquals("Syntax Error: XXX", message); + } + + public void testNLSLoading_ja() { + String message = NLS.getLocalizedMessage( + MessagesTestBundle.Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION, + Locale.JAPANESE); + assertEquals("切り捨てられたユニコード・エスケープ・シーケンス。", message); + + message = NLS.getLocalizedMessage(MessagesTestBundle.Q0001E_INVALID_SYNTAX, + Locale.JAPANESE, "XXX"); + assertEquals("構文エラー: XXX", message); + } + + public void testNLSLoading_xx_XX() { + Locale locale = new Locale("xx", "XX", ""); + String message = NLS.getLocalizedMessage( + MessagesTestBundle.Q0004E_INVALID_SYNTAX_ESCAPE_UNICODE_TRUNCATION, + locale); + /* + * if the default locale is ja, you get ja as a fallback: + * see ResourceBundle.html#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) + */ + if (!Locale.getDefault().getLanguage().equals("ja")) + assertEquals("Truncated unicode escape sequence.", message); + + message = NLS.getLocalizedMessage(MessagesTestBundle.Q0001E_INVALID_SYNTAX, + locale, "XXX"); + /* + * if the default locale is ja, you get ja as a fallback: + * see ResourceBundle.html#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) + */ + if (!Locale.getDefault().getLanguage().equals("ja")) + assertEquals("Syntax Error: XXX", message); + } + + public void testMissingMessage() { + Locale locale = Locale.ENGLISH; + String message = NLS.getLocalizedMessage( + MessagesTestBundle.Q0005E_MESSAGE_NOT_IN_BUNDLE, locale); + + assertEquals("Message with key:Q0005E_MESSAGE_NOT_IN_BUNDLE and locale: " + + locale.toString() + " not found.", message); + } +} Index: modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/TestNLS.java =================================================================== --- modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/TestNLS.java (revision 1215327) +++ modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/TestNLS.java (working copy) Property changes on: modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/messages/TestNLS.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native Index: modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/spans/SpansValidatorQueryNodeProcessor.java =================================================================== --- modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/spans/SpansValidatorQueryNodeProcessor.java (revision 1215327) +++ modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/spans/SpansValidatorQueryNodeProcessor.java (working copy) @@ -19,7 +19,7 @@ import java.util.List; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.QueryNodeException; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; import org.apache.lucene.queryparser.flexible.core.nodes.AndQueryNode; Index: modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestQPHelper.java =================================================================== --- modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestQPHelper.java (revision 1215327) +++ modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestQPHelper.java (working copy) @@ -38,7 +38,7 @@ import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; -import org.apache.lucene.messages.MessageImpl; +import org.apache.lucene.queryparser.flexible.messages.MessageImpl; import org.apache.lucene.queryparser.flexible.core.QueryNodeException; import org.apache.lucene.queryparser.flexible.core.messages.QueryParserMessages; import org.apache.lucene.queryparser.flexible.core.nodes.FuzzyQueryNode;