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