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