Advertisement
Guest User

ReplacingResourceModel

a guest
Feb 21st, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 KB | None | 0 0
  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.wicket.model;
  18.  
  19. import java.util.regex.Matcher;
  20. import java.util.regex.Pattern;
  21.  
  22. import org.apache.wicket.Application;
  23. import org.apache.wicket.Component;
  24.  
  25. /**
  26.  * The ReplacingResourceModel is used to replaced other keys marked up like ${key} with their
  27.  * corresponding values in the given key.The replacing mechanism also uses the default way to get
  28.  * the localized String.<br>
  29.  * <br>
  30.  *
  31.  * Example:<br>
  32.  * <br>
  33.  *
  34.  * <pre>
  35.  * key1=value of key1
  36.  * key2=value of key2 contains ${key1}
  37.  *
  38.  * Label label = new Label("label",new ReplacingResourceModel("key2"));
  39.  *
  40.  * &lt;span wicket:id="label"&gt;&lt;span&gt;
  41.  *
  42.  * Output: value of key2 contains value of key1
  43.  * </pre>
  44.  *
  45.  *
  46.  * @author Tobias Soloschenko
  47.  */
  48. public class ReplacingResourceModel extends AbstractReadOnlyModel<String> implements
  49.     IComponentAssignedModel<String>
  50. {
  51.     private static final long serialVersionUID = 1L;
  52.  
  53.     private final String resourceKey;
  54.  
  55.  
  56.     public ReplacingResourceModel(String resourceKey)
  57.     {
  58.         this.resourceKey = resourceKey;
  59.     }
  60.  
  61.     /**
  62.      * @see org.apache.wicket.model.AbstractReadOnlyModel#getObject()
  63.      */
  64.     @Override
  65.     public String getObject()
  66.     {
  67.         // this shouldn't be called always wrapped!
  68.         return getReplacedResourceString(null);
  69.     }
  70.  
  71.     /**
  72.      * @see org.apache.wicket.model.IComponentAssignedModel#wrapOnAssignment(org.apache.wicket.Component)
  73.      */
  74.     @Override
  75.     public IWrapModel<String> wrapOnAssignment(final Component component)
  76.     {
  77.         return new AssignmentWrapper(component);
  78.     }
  79.  
  80.     /**
  81.      *
  82.      */
  83.     private class AssignmentWrapper extends LoadableDetachableModel<String> implements
  84.         IWrapModel<String>
  85.     {
  86.         private static final long serialVersionUID = 1L;
  87.  
  88.         private final Component component;
  89.  
  90.         /**
  91.          * Construct.
  92.          *
  93.          * @param component
  94.          */
  95.         public AssignmentWrapper(Component component)
  96.         {
  97.             this.component = component;
  98.         }
  99.  
  100.         /**
  101.          * @see org.apache.wicket.model.IWrapModel#getWrappedModel()
  102.          */
  103.         @Override
  104.         public IModel<String> getWrappedModel()
  105.         {
  106.             return ReplacingResourceModel.this;
  107.         }
  108.  
  109.         @Override
  110.         protected String load()
  111.         {
  112.             return getReplacedResourceString(component);
  113.         }
  114.  
  115.         @Override
  116.         protected void onDetach()
  117.         {
  118.             ReplacingResourceModel.this.detach();
  119.         }
  120.  
  121.     }
  122.  
  123.     // The pattern to find keys in the given key value
  124.     private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("\\$\\{(.*?)\\}");
  125.  
  126.     /**
  127.      * Replaces all resource keys found in the given resourceKey with their localized values
  128.      *
  129.      * @param component
  130.      *            the component to look for the resourceKey and all relating resource keys
  131.      * @return the localized String
  132.      */
  133.     private String getReplacedResourceString(Component component)
  134.     {
  135.         String resourceKeyValue = Application.get()
  136.             .getResourceSettings()
  137.             .getLocalizer()
  138.             .getString(resourceKey, component, null, null, null, (IModel<String>)null);
  139.  
  140.         StringBuffer output = new StringBuffer();
  141.  
  142.         Matcher matcher = ReplacingResourceModel.PLACEHOLDER_PATTERN.matcher(resourceKeyValue);
  143.         // Search for placeholder to replace
  144.         while (matcher.find())
  145.         {
  146.             String replacedPlaceHolder = Application.get()
  147.                 .getResourceSettings()
  148.                 .getLocalizer()
  149.                 .getString(matcher.group(1), component, null, null, null, (IModel<String>)null);
  150.             matcher.appendReplacement(output, replacedPlaceHolder);
  151.         }
  152.         matcher.appendTail(output);
  153.         return output.toString();
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement