Author: jdonnerstag
Date: Thu Jan  1 12:42:46 2009
New Revision: 730602

URL: http://svn.apache.org/viewvc?rev=730602&view=rev
Log:
fixed wicket-1888: FormComponents (and subclasses) should be able to provide 
their own resource bundles
- added log.debug messages to provide more insight in the properties finding 
process
- fixed property finding in FormComponent

Modified:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/Localizer.java
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/resource/loader/ComponentStringResourceLoader.java
    
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/ValidatorPropertiesTest.java
    
wicket/trunk/wicket/src/test/java/org/apache/wicket/properties/TestForm.properties
    wicket/trunk/wicket/src/test/java/org/apache/wicket/properties/TestPage.java

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/Localizer.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/Localizer.java?rev=730602&r1=730601&r2=730602&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/Localizer.java 
(original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/Localizer.java Thu Jan  
1 12:42:46 2009
@@ -52,7 +52,7 @@
  */
 public class Localizer
 {
-       private static final Logger logger = 
LoggerFactory.getLogger(Localizer.class);
+       private static final Logger log = 
LoggerFactory.getLogger(Localizer.class);
 
        /** ConcurrentHashMap does not allow null values */
        private static final String NULL_VALUE = "<null-value>";
@@ -193,7 +193,7 @@
 
                        if (!addedToPage)
                        {
-                               logger.warn(
+                               log.warn(
                                        "Tried to retrieve a localized string 
for a component that has not yet been added to the page. "
                                                + "This can sometimes lead to 
an invalid or no localized resource returned. "
                                                + "Make sure you are not 
calling Component#getString() inside your Component's constructor. "
@@ -215,16 +215,24 @@
                if ((cacheKey != null) && cache.containsKey(cacheKey))
                {
                        value = getFromCache(cacheKey);
+                       if (log.isDebugEnabled())
+                       {
+                               log.debug("Property found in cache: '" + key + 
"'; Component: '" +
+                                       (component != null ? 
component.toString(false) : null) + "'; value: '" + value +
+                                       "'");
+                       }
                }
                else
                {
+                       if (log.isDebugEnabled())
+                       {
+                               log.debug("Locate property: key: '" + key + "'; 
Component: '" +
+                                       (component != null ? 
component.toString(false) : null) + "'");
+                       }
+
                        // Iterate over all registered string resource loaders 
until the
                        // property has been found
-                       Iterator<IStringResourceLoader> iter = Application.get()
-                               .getResourceSettings()
-                               .getStringResourceLoaders()
-                               .iterator();
-
+                       Iterator<IStringResourceLoader> iter = 
getStringResourceLoaders();
                        while (iter.hasNext())
                        {
                                IStringResourceLoader loader = iter.next();
@@ -240,6 +248,12 @@
                        {
                                putIntoCache(cacheKey, value);
                        }
+
+                       if ((value == null) && log.isDebugEnabled())
+                       {
+                               log.debug("Property not found; key: '" + key + 
"'; Component: '" +
+                                       (component != null ? 
component.toString(false) : null) + "'");
+                       }
                }
 
                if (value == null)
@@ -258,6 +272,20 @@
        }
 
        /**
+        * In case you want to provide your own list of string resource loaders
+        * 
+        * @return Iterator
+        */
+       protected Iterator<IStringResourceLoader> getStringResourceLoaders()
+       {
+               Iterator<IStringResourceLoader> iter = Application.get()
+                       .getResourceSettings()
+                       .getStringResourceLoaders()
+                       .iterator();
+               return iter;
+       }
+
+       /**
         * Get the localized string using all of the supplied parameters. This 
method is left public to
         * allow developers full control over string resource loading. However, 
it is recommended that
         * one of the other convenience methods in the class are used as they 
handle all of the work
@@ -351,7 +379,9 @@
        protected String getFromCache(final String cacheKey)
        {
                if (cache == null)
+               {
                        return null;
+               }
 
                final String value = cache.get(cacheKey);
 
@@ -360,10 +390,7 @@
                {
                        return null;
                }
-               else
-               {
-                       return value;
-               }
+               return value;
        }
 
        /**

Modified: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java?rev=730602&r1=730601&r2=730602&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java
 (original)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/FormComponent.java
 Thu Jan  1 12:42:46 2009
@@ -148,51 +148,27 @@
                {
                        final FormComponent<T> formComponent = 
FormComponent.this;
 
+                       // Use the following log4j config for detailed logging 
on the property resolution
+                       // process
+                       // log4j.logger.org.apache.wicket.resource.loader=DEBUG
+                       // log4j.logger.org.apache.wicket.Localizer=DEBUG
+
+                       final Localizer localizer = 
formComponent.getLocalizer();
+
                        // retrieve prefix that will be used to construct 
message keys
                        String prefix = formComponent.getValidatorKeyPrefix();
-                       if (Strings.isEmpty(prefix))
+                       String message = null;
+                       if (!Strings.isEmpty(prefix))
                        {
-                               prefix = "";
+                               String resource = prefix + "." + key;
+                               message = getString(localizer, resource, 
formComponent);
                        }
 
-                       final Localizer localizer = 
formComponent.getLocalizer();
-
-                       String resource = prefix + getId() + "." + key;
-
-                       // First use the parent for resolving so that
-                       // form1.textfield1.Required can be used.
-
-                       String message = getString(localizer, resource, 
formComponent.getParent());
-
                        // If not found, than ...
                        if (Strings.isEmpty(message))
                        {
                                // Try a variation of the resource key
-                               resource = prefix + key;
-
-                               message = getString(localizer, resource, 
formComponent.getParent());
-                       }
-
-                       if (Strings.isEmpty(message))
-                       {
-                               // If still empty then use default
-
-                               resource = prefix + getId() + "." + key;
-
-                               // Note: It is important that the default value 
of "" is
-                               // provided
-                               // to getString() not to throw a 
MissingResourceException or to
-                               // return a default string like "[Warning: 
String ..."
-                               message = getString(localizer, resource, 
formComponent);
-
-                               // If not found, than ...
-                               if (Strings.isEmpty(message))
-                               {
-                                       // Try a variation of the resource key
-
-                                       resource = prefix + key;
-                                       message = getString(localizer, 
resource, formComponent);
-                               }
+                               message = getString(localizer, key, 
formComponent);
                        }
 
                        // convert empty string to null in case our default 
value of "" was
@@ -214,6 +190,10 @@
                private String getString(Localizer localizer, String key, 
Component component)
                {
                        triedKeys.add(key);
+
+                       // Note: It is important that the default value of "" is
+                       // provided to getString() not to throw a 
MissingResourceException or to
+                       // return a default string like "[Warning: String ..."
                        return localizer.getString(key, component, "");
                }
 
@@ -271,7 +251,6 @@
                        return fullParams;
                }
 
-
                /**
                 * @return value of label param for this form component
                 */
@@ -324,7 +303,6 @@
         */
        private class ValidatableAdapter implements IValidatable<T>
        {
-
                /**
                 * @see 
org.apache.wicket.validation.IValidatable#error(org.apache.wicket.validation.IValidationError)
                 */
@@ -341,11 +319,13 @@
                        return getConvertedInput();
                }
 
+               /**
+                * @see org.apache.wicket.validation.IValidatable#isValid()
+                */
                public boolean isValid()
                {
                        return FormComponent.this.isValid();
                }
-
        }
 
        /**
@@ -364,7 +344,6 @@
        /** Whether or not this component's value is required (non-empty) */
        private static final short FLAG_REQUIRED = FLAG_RESERVED3;
 
-
        private static final String NO_RAW_INPUT = "[-NO-RAW-INPUT-]";
 
        private static final long serialVersionUID = 1L;
@@ -395,7 +374,12 @@
                visitFormComponentsPostOrderHelper(component, visitor);
        }
 
-
+       /**
+        * 
+        * @param component
+        * @param visitor
+        * @return Object
+        */
        private static final Object 
visitFormComponentsPostOrderHelper(Component component,
                final FormComponent.IVisitor visitor)
        {
@@ -455,7 +439,12 @@
                visitComponentsPostOrderHelper(component, visitor);
        }
 
-
+       /**
+        * 
+        * @param component
+        * @param visitor
+        * @return Object
+        */
        private static final Object visitComponentsPostOrderHelper(Component 
component,
                final Component.IVisitor<Component> visitor)
        {
@@ -974,7 +963,6 @@
                return getFlag(FLAG_REQUIRED);
        }
 
-
        /**
         * Gets whether this component is 'valid'. Valid in this context means 
that no validation errors
         * were reported the last time the form component was processed. This 
variable not only is
@@ -1323,6 +1311,11 @@
                }
        }
 
+       /**
+        * 
+        * @param e
+        * @param error
+        */
        private void reportValidationError(ConversionException e, 
ValidationError error)
        {
                final Locale locale = e.getLocale();
@@ -1485,7 +1478,6 @@
                convertedInput = null;
        }
 
-
        /**
         * Called by {...@link #onComponentTag(ComponentTag)} when the 
component is disabled. By default,
         * this method will add a disabled="disabled" attribute to the tag. 
Components may override this
@@ -1659,6 +1651,4 @@
        {
                setDefaultModelObject(object);
        }
-
-
 }

Modified: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/resource/loader/ComponentStringResourceLoader.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/resource/loader/ComponentStringResourceLoader.java?rev=730602&r1=730601&r2=730602&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/resource/loader/ComponentStringResourceLoader.java
 (original)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/resource/loader/ComponentStringResourceLoader.java
 Thu Jan  1 12:42:46 2009
@@ -118,11 +118,14 @@
                        return null;
                }
 
-               // Load the properties associated with the path
-               IPropertiesFactory propertiesFactory = Application.get()
-                       .getResourceSettings()
-                       .getPropertiesFactory();
+               if (log.isDebugEnabled())
+               {
+                       log.debug("key: '" + key + "'; class: '" + 
clazz.getName() + "'; locale: '" + locale +
+                               "'; Style: '" + style + "'");
+               }
 
+               // Load the properties associated with the path
+               IPropertiesFactory propertiesFactory = getPropertiesFactory();
                while (true)
                {
                        // Create the base path
@@ -143,11 +146,27 @@
                                        {
                                                if (log.isDebugEnabled())
                                                {
-                                                       log.debug("Found 
resource from: " + props + "; key: " + key);
+                                                       log.debug("Found 
property '" + key + "' in: '" + props + "'" +
+                                                               "; value: '" + 
value + "'");
                                                }
 
                                                return value;
                                        }
+                                       else
+                                       {
+                                               if (log.isDebugEnabled())
+                                               {
+                                                       log.debug("Found 
properties file: '" + newPath +
+                                                               "' but it 
doesn't contain the property");
+                                               }
+                                       }
+                               }
+                               else
+                               {
+                                       if (log.isDebugEnabled())
+                                       {
+                                               // log.debug("Properties file 
not found: '" + newPath + "'");
+                                       }
                                }
                        }
 
@@ -172,6 +191,17 @@
        }
 
        /**
+        * Get the properties file factory which loads the properties based on 
locale and style from
+        * *.properties and *.xml files
+        * 
+        * @return properties factory
+        */
+       protected IPropertiesFactory getPropertiesFactory()
+       {
+               return 
Application.get().getResourceSettings().getPropertiesFactory();
+       }
+
+       /**
         * 
         * @see 
org.apache.wicket.resource.loader.IStringResourceLoader#loadStringResource(org.apache.wicket.Component,
         *      java.lang.String)
@@ -183,6 +213,11 @@
                        return null;
                }
 
+               if (log.isDebugEnabled())
+               {
+                       log.debug("component: '" + component.toString(false) + 
"'; key: '" + key + "'");
+               }
+
                // The return value
                String string = null;
                Locale locale = component.getLocale();
@@ -196,6 +231,9 @@
                // walk it downwards starting with Page down to the Component
                List<Class<?>> searchStack = getComponentStack(component);
 
+               // TODO Should be changed to false in 1.5
+               final boolean old = true;
+
                // Walk the component hierarchy down from page to the component
                for (int i = searchStack.size() - 1; (i >= 0) && (string == 
null); i--)
                {
@@ -215,8 +253,20 @@
 
                        // If not found, than check if a property with the 
'key' provided by
                        // the user can be found.
-                       if (string == null)
+                       if ((string == null) && old)
+                       {
+                               string = loadStringResource(clazz, key, locale, 
style);
+                       }
+               }
+
+               // If not found, than check if a property with the 'key' 
provided by
+               // the user can be found.
+               if ((string == null) && !old)
+               {
+                       // Walk the component hierarchy down from page to the 
component
+                       for (int i = searchStack.size() - 1; (i >= 0) && 
(string == null); i--)
                        {
+                               Class<?> clazz = searchStack.get(i);
                                string = loadStringResource(clazz, key, locale, 
style);
                        }
                }

Modified: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/ValidatorPropertiesTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/ValidatorPropertiesTest.java?rev=730602&r1=730601&r2=730602&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/ValidatorPropertiesTest.java
 (original)
+++ 
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/ValidatorPropertiesTest.java
 Thu Jan  1 12:42:46 2009
@@ -28,6 +28,10 @@
 
 /**
  * 
+ * Use the following log4j config for detailed logging on the property 
resolution process
+ * log4j.logger.org.apache.wicket.resource.loader=DEBUG
+ * log4j.logger.org.apache.wicket.Localizer=DEBUG
+ * 
  * @author Juergen Donnerstag
  */
 public class ValidatorPropertiesTest extends TestCase
@@ -51,8 +55,7 @@
         */
        public void test1()
        {
-               tester.setupRequestAndResponse();
-               WebRequestCycle cycle = tester.createRequestCycle();
+               WebRequestCycle cycle = tester.setupRequestAndResponse();
                cycle.getSession().setLocale(Locale.ENGLISH);
 
                // test English/ default
@@ -85,6 +88,10 @@
                page.getText11().validateRequired();
                page.getText12().setInput("");
                page.getText12().validateRequired();
+               page.getText13().setInput("");
+               page.getText13().validateRequired();
+               page.getText14().setInput("");
+               page.getText14().validateRequired();
 
                assertEquals("text1label is required", page.getText1()
                        .getFeedbackMessage()
@@ -102,7 +109,8 @@
                        .getFeedbackMessage()
                        .getMessage()
                        .toString());
-               assertEquals("ok: text is missing", page.getText5()
+               assertEquals("Default message: text5 required", page.getText5()
+               // assertEquals("ok: 555text555 is missing", page.getText5()
                        .getFeedbackMessage()
                        .getMessage()
                        .toString());
@@ -110,7 +118,7 @@
                        .getFeedbackMessage()
                        .getMessage()
                        .toString());
-               assertEquals("input for text7-Label is missing", page.getText7()
+               assertEquals("Default message: text7-Label required", 
page.getText7()
                        .getFeedbackMessage()
                        .getMessage()
                        .toString());
@@ -118,11 +126,12 @@
                        .getFeedbackMessage()
                        .getMessage()
                        .toString());
-               assertEquals("found it in panel", page.getText9()
+               assertEquals("Default message: text9 required", page.getText9()
+               // assertEquals("found it in panel", page.getText9()
                        .getFeedbackMessage()
                        .getMessage()
                        .toString());
-               assertEquals("found it in form", page.getText10()
+               assertEquals("Default message: text10 required", 
page.getText10()
                        .getFeedbackMessage()
                        .getMessage()
                        .toString());
@@ -130,7 +139,15 @@
                        .getFeedbackMessage()
                        .getMessage()
                        .toString());
-               assertEquals("found it in page", page.getText12()
+               assertEquals("Default message: text12 required", 
page.getText12()
+                       .getFeedbackMessage()
+                       .getMessage()
+                       .toString());
+               assertEquals("found text-13 property", page.getText13()
+                       .getFeedbackMessage()
+                       .getMessage()
+                       .toString());
+               assertEquals("Default message: text14 required", 
page.getText14()
                        .getFeedbackMessage()
                        .getMessage()
                        .toString());
@@ -189,12 +206,16 @@
                        .getFeedbackMessage()
                        .getMessage()
                        .toString());
-               assertEquals("ok: text mist", 
page.getText5().getFeedbackMessage().getMessage().toString());
+               assertEquals("Default message: text5 verplicht", page.getText5()
+               // assertEquals("ok: 555text555 mist", page.getText5()
+                       .getFeedbackMessage()
+                       .getMessage()
+                       .toString());
                assertEquals("Default message: text6 verplicht", page.getText6()
                        .getFeedbackMessage()
                        .getMessage()
                        .toString());
-               assertEquals("input for text7-Label mist", page.getText7()
+               assertEquals("Default message: text7-Label verplicht", 
page.getText7()
                        .getFeedbackMessage()
                        .getMessage()
                        .toString());
@@ -202,11 +223,12 @@
                        .getFeedbackMessage()
                        .getMessage()
                        .toString());
-               assertEquals("gevonden in panel", page.getText9()
+               assertEquals("Default message: text9 verplicht", page.getText9()
+               // assertEquals("gevonden in panel", page.getText9()
                        .getFeedbackMessage()
                        .getMessage()
                        .toString());
-               assertEquals("gevonden in form", page.getText10()
+               assertEquals("Default message: text10 verplicht", 
page.getText10()
                        .getFeedbackMessage()
                        .getMessage()
                        .toString());
@@ -214,7 +236,7 @@
                        .getFeedbackMessage()
                        .getMessage()
                        .toString());
-               assertEquals("gevonden in page", page.getText12()
+               assertEquals("Default message: text12 verplicht", 
page.getText12()
                        .getFeedbackMessage()
                        .getMessage()
                        .toString());

Modified: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/properties/TestForm.properties
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/properties/TestForm.properties?rev=730602&r1=730601&r2=730602&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/test/java/org/apache/wicket/properties/TestForm.properties
 (original)
+++ 
wicket/trunk/wicket/src/test/java/org/apache/wicket/properties/TestForm.properties
 Thu Jan  1 12:42:46 2009
@@ -4,3 +4,5 @@
 panel1.text4.Required = ok testForm: ${label} is missing
 text10.Required = found it in form
 panel2.text11.Required = found it in page
+
+text13.myValidator.Required = found text-13 property
\ No newline at end of file

Modified: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/properties/TestPage.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/properties/TestPage.java?rev=730602&r1=730601&r2=730602&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/test/java/org/apache/wicket/properties/TestPage.java 
(original)
+++ 
wicket/trunk/wicket/src/test/java/org/apache/wicket/properties/TestPage.java 
Thu Jan  1 12:42:46 2009
@@ -61,6 +61,24 @@
                panel2.add(new MyTextField("text10", "input-10"));
                panel2.add(new MyTextField("text11", "input-11"));
                panel2.add(new MyTextField("text12", "input-12"));
+
+               Form form3 = new TestForm("form3")
+               {
+                       private static final long serialVersionUID = 1L;
+
+                       /**
+                        * @see 
org.apache.wicket.markup.html.form.Form#getValidatorKeyPrefix()
+                        */
+                       @Override
+                       public String getValidatorKeyPrefix()
+                       {
+                               return "myValidator";
+                       }
+               };
+
+               add(form3);
+               form3.add(new MyTextField("text13", "input-13"));
+               form3.add(new MyTextField("text14", "input-14"));
        }
 
        /**
@@ -173,6 +191,24 @@
 
        /**
         * 
+        * @return xxx
+        */
+       public MyTextField getText13()
+       {
+               return (MyTextField)get("form3:text13");
+       }
+
+       /**
+        * 
+        * @return xxx
+        */
+       public MyTextField getText14()
+       {
+               return (MyTextField)get("form3:text14");
+       }
+
+       /**
+        * 
         */
        public static class MyTextField extends TextField
        {
@@ -197,6 +233,7 @@
                /**
                 * @see 
org.apache.wicket.markup.html.form.FormComponent#getInput()
                 */
+               @Override
                public String getInput()
                {
                        return input;


Reply via email to