001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.cp;
014
015import static org.apache.juneau.internal.StringUtils.*;
016import static org.apache.juneau.internal.ResourceBundleUtils.*;
017
018import java.util.*;
019
020import org.apache.juneau.collections.*;
021import org.apache.juneau.internal.*;
022
023/**
024 * Builder for {@link Messages} objects.
025 */
026public class MessagesBuilder {
027
028   private Class<?> forClass;
029   private Locale locale = Locale.getDefault();
030   private String name;
031   private Messages parent;
032
033   private String[] baseNames = {"{package}.{name}","{package}.i18n.{name}","{package}.nls.{name}","{package}.messages.{name}"};
034
035   MessagesBuilder(Class<?> forClass) {
036      this.forClass = forClass;
037      this.name = forClass.getSimpleName();
038   }
039
040   /**
041    * Adds a parent bundle.
042    *
043    * @param parent The parent bundle.  Can be <jk>null</jk>.
044    * @return This object (for method chaining).
045    */
046   public MessagesBuilder parent(Messages parent) {
047      this.parent = parent;
048      return this;
049   }
050
051   /**
052    * Specifies the bundle name (e.g. <js>"Messages"</js>).
053    *
054    * @param name
055    *    The bundle name.
056    *    <br>If <jk>null</jk>, the forClass class name is used.
057    * @return This object (for method chaining).
058    */
059   public MessagesBuilder name(String name) {
060      this.name = isEmpty(name) ? forClass.getSimpleName() : name;
061      return this;
062   }
063
064   /**
065    * Specifies the base name patterns to use for finding the resource bundle.
066    *
067    * @param baseNames
068    *    The bundle base names.
069    *    <br>The default is the following:
070    *    <ul>
071    *       <li><js>"{package}.{name}"</js>
072    *       <li><js>"{package}.i18n.{name}"</js>
073    *       <li><js>"{package}.nls.{name}"</js>
074    *       <li><js>"{package}.messages.{name}"</js>
075    *    </ul>
076    * @return This object (for method chaining).
077    */
078   public MessagesBuilder baseNames(String...baseNames) {
079      this.baseNames = baseNames == null ? new String[]{} : baseNames;
080      return this;
081   }
082
083   /**
084    * Specifies the locale.
085    *
086    * @param locale
087    *    The locale.
088    *    If <jk>null</jk>, the default locale is used.
089    * @return This object (for method chaining).
090    */
091   public MessagesBuilder locale(Locale locale) {
092      this.locale = locale == null ? Locale.getDefault() : locale;
093      return this;
094   }
095
096   /**
097    * Creates a new {@link Messages} based on the setting of this builder.
098    *
099    * @return A new {@link Messages} object.
100    */
101   public Messages build() {
102      return new Messages(forClass, getBundle(), locale, parent);
103   }
104
105   private ResourceBundle getBundle() {
106      ClassLoader cl = forClass.getClassLoader();
107      OMap m = OMap.of("name", name, "package", forClass.getPackage().getName());
108      for (String bn : baseNames) {
109         bn = StringUtils.replaceVars(bn, m);
110         ResourceBundle rb = findBundle(bn, locale, cl);
111         if (rb != null)
112            return rb;
113      }
114      return null;
115   }
116}