View Javadoc

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.jetspeed.om.page;
18  
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Locale;
23  import java.util.Map;
24  
25  import org.apache.jetspeed.om.common.LocalizedField;
26  import org.apache.jetspeed.om.impl.GenericMetadataImpl;
27  import org.apache.jetspeed.util.ArgUtil;
28  
29  /***
30   * @author <a href="mailto:jford@apache.org">Jeremy Ford</a>
31   * @version $Id: PageMetadataImpl.java 516448 2007-03-09 16:25:47Z ate $
32   */
33  public class PageMetadataImpl extends GenericMetadataImpl
34  {
35      private Class fieldImplClass = PageLocalizedFieldImpl.class;
36  
37      public PageMetadataImpl()
38      {
39      }
40  
41      public PageMetadataImpl(Class fieldImplClass)
42      {
43          this();
44          this.fieldImplClass = fieldImplClass;
45      }
46  
47      /***
48       * localizedText - cached text metadata
49       */
50      private Map localizedText;
51  
52      /* (non-Javadoc)
53       * @see org.apache.jetspeed.om.common.GenericMetadata#createLocalizedField()
54       */
55      public LocalizedField createLocalizedField()
56      {
57          try
58          {
59              return (LocalizedField)fieldImplClass.newInstance();
60          }
61          catch (Exception e)
62          {
63              throw new RuntimeException("Failed to create LocalizedField object: " + fieldImplClass.getName(), e);
64          }
65      }
66  
67      /***
68       * getText - get localized text from metadata
69       * 
70       * @param name text name
71       * @param locale preferred locale
72       * @return localized text or null if not available
73       */
74      public String getText(String name, Locale locale)
75      {
76          // validate parameters
77          ArgUtil.assertNotNull(String.class, name, this, "getText(String, Locale)");
78          ArgUtil.assertNotNull(Locale.class, locale, this, "getText(String, Locale)");
79  
80          // populate cache for named text by locale
81          Map namedLocalizedText = (Map)((localizedText != null) ? localizedText.get(name) : null);
82          if ((namedLocalizedText == null) && (getFields() != null))
83          {
84              Collection fields = getFields(name);
85              if (fields != null)
86              {
87                  if (localizedText == null)
88                  {
89                      localizedText = new HashMap(getFields().size());
90                  }
91                  namedLocalizedText = new HashMap(getFields().size());
92                  localizedText.put(name, namedLocalizedText);
93                  Iterator fieldsItr = fields.iterator();
94                  while (fieldsItr.hasNext())
95                  {
96                      LocalizedField field = (LocalizedField)fieldsItr.next();
97                      namedLocalizedText.put(field.getLocale(), field);
98                  }
99              }
100         }
101 
102         // retrieve cached named text by locale if found
103         if (namedLocalizedText != null)
104         {
105             // test locale
106             if (namedLocalizedText.containsKey(locale) )
107             {
108                 return ((LocalizedField)namedLocalizedText.get(locale)).getValue().trim();
109             }
110             // test language only locale
111             Locale languageOnly = new Locale(locale.getLanguage());
112             if (namedLocalizedText.containsKey(languageOnly))
113             {
114                 return ((LocalizedField)namedLocalizedText.get(languageOnly)).getValue().trim();
115             }
116         }
117         return null;
118     }
119 }