001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.om.page;
018:
019: import java.util.Collection;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Locale;
023: import java.util.Map;
024: import java.util.Collections;
025:
026: import org.apache.jetspeed.om.common.LocalizedField;
027: import org.apache.jetspeed.om.impl.GenericMetadataImpl;
028: import org.apache.jetspeed.util.ArgUtil;
029:
030: /**
031: * @author <a href="mailto:jford@apache.org">Jeremy Ford</a>
032: * @version $Id: PageMetadataImpl.java 568811 2007-08-23 03:00:37Z woonsan $
033: */
034: public class PageMetadataImpl extends GenericMetadataImpl {
035: private Class fieldImplClass = PageLocalizedFieldImpl.class;
036:
037: public PageMetadataImpl() {
038: }
039:
040: public PageMetadataImpl(Class fieldImplClass) {
041: this ();
042: this .fieldImplClass = fieldImplClass;
043: }
044:
045: /**
046: * localizedText - cached text metadata
047: */
048: private Map localizedText;
049:
050: /* (non-Javadoc)
051: * @see org.apache.jetspeed.om.common.GenericMetadata#createLocalizedField()
052: */
053: public LocalizedField createLocalizedField() {
054: try {
055: return (LocalizedField) fieldImplClass.newInstance();
056: } catch (Exception e) {
057: throw new RuntimeException(
058: "Failed to create LocalizedField object: "
059: + fieldImplClass.getName(), e);
060: }
061: }
062:
063: /**
064: * getText - get localized text from metadata
065: *
066: * @param name text name
067: * @param locale preferred locale
068: * @return localized text or null if not available
069: */
070: public String getText(String name, Locale locale) {
071: // validate parameters
072: ArgUtil.assertNotNull(String.class, name, this ,
073: "getText(String, Locale)");
074: ArgUtil.assertNotNull(Locale.class, locale, this ,
075: "getText(String, Locale)");
076:
077: // populate cache for named text by locale
078: Map namedLocalizedText = (Map) ((localizedText != null) ? localizedText
079: .get(name)
080: : null);
081: if ((namedLocalizedText == null) && (getFields() != null)) {
082: Collection fields = getFields(name);
083: if (fields != null) {
084: if (localizedText == null) {
085: localizedText = Collections
086: .synchronizedMap(new HashMap(getFields()
087: .size()));
088: }
089: namedLocalizedText = new HashMap(getFields().size());
090: localizedText.put(name, namedLocalizedText);
091: Iterator fieldsItr = fields.iterator();
092: while (fieldsItr.hasNext()) {
093: LocalizedField field = (LocalizedField) fieldsItr
094: .next();
095: namedLocalizedText.put(field.getLocale(), field);
096: }
097: }
098: }
099:
100: // retrieve cached named text by locale if found
101: if (namedLocalizedText != null) {
102: // test locale
103: if (namedLocalizedText.containsKey(locale)) {
104: return ((LocalizedField) namedLocalizedText.get(locale))
105: .getValue().trim();
106: }
107: // test language only locale
108: Locale languageOnly = new Locale(locale.getLanguage());
109: if (namedLocalizedText.containsKey(languageOnly)) {
110: return ((LocalizedField) namedLocalizedText
111: .get(languageOnly)).getValue().trim();
112: }
113: }
114: return null;
115: }
116: }
|