%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.jetspeed.om.page.PageMetadataImpl |
|
|
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 | 0 | private Class fieldImplClass = PageLocalizedFieldImpl.class; |
37 | ||
38 | public PageMetadataImpl() |
|
39 | 0 | { |
40 | 0 | } |
41 | ||
42 | public PageMetadataImpl(Class fieldImplClass) |
|
43 | { |
|
44 | 0 | this(); |
45 | 0 | this.fieldImplClass = fieldImplClass; |
46 | 0 | } |
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 | 0 | return (LocalizedField)fieldImplClass.newInstance(); |
61 | } |
|
62 | 0 | catch (Exception e) |
63 | { |
|
64 | 0 | 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 | 0 | ArgUtil.assertNotNull(String.class, name, this, "getText(String, Locale)"); |
79 | 0 | ArgUtil.assertNotNull(Locale.class, locale, this, "getText(String, Locale)"); |
80 | ||
81 | // populate cache for named text by locale |
|
82 | 0 | Map namedLocalizedText = (Map)((localizedText != null) ? localizedText.get(name) : class="keyword">null); |
83 | 0 | if ((namedLocalizedText == null) && (getFields() != class="keyword">null)) |
84 | { |
|
85 | 0 | Collection fields = getFields(name); |
86 | 0 | if (fields != null) |
87 | { |
|
88 | 0 | if (localizedText == null) |
89 | { |
|
90 | 0 | localizedText = Collections.synchronizedMap(new HashMap(getFields().size())); |
91 | } |
|
92 | 0 | namedLocalizedText = new HashMap(getFields().size()); |
93 | 0 | localizedText.put(name, namedLocalizedText); |
94 | 0 | Iterator fieldsItr = fields.iterator(); |
95 | 0 | while (fieldsItr.hasNext()) |
96 | { |
|
97 | 0 | LocalizedField field = (LocalizedField)fieldsItr.next(); |
98 | 0 | namedLocalizedText.put(field.getLocale(), field); |
99 | 0 | } |
100 | } |
|
101 | } |
|
102 | ||
103 | // retrieve cached named text by locale if found |
|
104 | 0 | if (namedLocalizedText != null) |
105 | { |
|
106 | // test locale |
|
107 | 0 | if (namedLocalizedText.containsKey(locale) ) |
108 | { |
|
109 | 0 | return ((LocalizedField)namedLocalizedText.get(locale)).getValue().trim(); |
110 | } |
|
111 | // test language only locale |
|
112 | 0 | Locale languageOnly = new Locale(locale.getLanguage()); |
113 | 0 | if (namedLocalizedText.containsKey(languageOnly)) |
114 | { |
|
115 | 0 | return ((LocalizedField)namedLocalizedText.get(languageOnly)).getValue().trim(); |
116 | } |
|
117 | } |
|
118 | 0 | return null; |
119 | } |
|
120 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |