Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
LocaleFinder |
|
|
2.4;2,4 | ||||
LocaleFinder$1 |
|
|
2.4;2,4 |
1 | /* |
|
2 | * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | */ |
|
16 | package de.smartics.properties.spi.core.context; |
|
17 | ||
18 | import java.util.ArrayList; |
|
19 | import java.util.Collections; |
|
20 | import java.util.Comparator; |
|
21 | import java.util.HashSet; |
|
22 | import java.util.List; |
|
23 | import java.util.Locale; |
|
24 | import java.util.Set; |
|
25 | ||
26 | import org.apache.commons.io.FilenameUtils; |
|
27 | import org.apache.commons.lang.LocaleUtils; |
|
28 | ||
29 | import de.smartics.properties.api.core.domain.ConfigException; |
|
30 | import de.smartics.properties.api.core.domain.PropertiesContext; |
|
31 | import de.smartics.util.lang.classpath.ClassPathContext; |
|
32 | import de.smartics.util.lang.classpath.ClassPathListing; |
|
33 | import de.smartics.util.lang.classpath.JarAndFileClassPathListing; |
|
34 | import de.smartics.util.util.GenericServiceFactory; |
|
35 | ||
36 | /** |
|
37 | * Finds all supported locales. |
|
38 | */ |
|
39 | final class LocaleFinder |
|
40 | { |
|
41 | // ********************************* Fields ********************************* |
|
42 | ||
43 | // --- constants ------------------------------------------------------------ |
|
44 | ||
45 | /** |
|
46 | * Helper to get listing of a folder on the class path. |
|
47 | */ |
|
48 | private static final ClassPathListing CLASS_PATH_LISTING; |
|
49 | ||
50 | // --- members -------------------------------------------------------------- |
|
51 | ||
52 | /** |
|
53 | * Locale comparator. |
|
54 | */ |
|
55 | 1 | private static final Comparator<Locale> COMPARATOR = new Comparator<Locale>() |
56 | 19 | { |
57 | @Override |
|
58 | public int compare(final Locale o1, final Locale o2) |
|
59 | { |
|
60 | 18 | return o1.toString().compareTo(o2.toString()); |
61 | } |
|
62 | }; |
|
63 | ||
64 | // ****************************** Initializer ******************************* |
|
65 | ||
66 | static |
|
67 | { |
|
68 | 1 | ClassPathListing service = null; |
69 | try |
|
70 | { |
|
71 | 1 | service = |
72 | new GenericServiceFactory<ClassPathListing>(ClassPathListing.class) |
|
73 | .create(); |
|
74 | } |
|
75 | 1 | catch (final IllegalStateException e) |
76 | { |
|
77 | 1 | service = new JarAndFileClassPathListing(); |
78 | 0 | } |
79 | ||
80 | 1 | CLASS_PATH_LISTING = service; |
81 | 1 | } |
82 | ||
83 | // ****************************** Constructors ****************************** |
|
84 | ||
85 | /** |
|
86 | * Default constructor. |
|
87 | */ |
|
88 | LocaleFinder() |
|
89 | 19 | { |
90 | 19 | } |
91 | ||
92 | // ****************************** Inner Classes ***************************** |
|
93 | ||
94 | // ********************************* Methods ******************************** |
|
95 | ||
96 | // --- init ----------------------------------------------------------------- |
|
97 | ||
98 | // --- get&set -------------------------------------------------------------- |
|
99 | ||
100 | // --- business ------------------------------------------------------------- |
|
101 | ||
102 | List<Locale> find(final String systemId, final ClassPathContext context) |
|
103 | throws ConfigException |
|
104 | { |
|
105 | 7 | final Set<Locale> locales = new HashSet<Locale>(); |
106 | 7 | locales.add(new Locale("")); |
107 | ||
108 | 7 | final List<String> listing = |
109 | CLASS_PATH_LISTING.list(context, |
|
110 | PropertiesContext.META_INF_PROPERTY_SET_REPORT); |
|
111 | ||
112 | 7 | for (final String path : listing) |
113 | { |
|
114 | 18 | final Locale locale = calcLocale(path); |
115 | 18 | if (locale != null) |
116 | { |
|
117 | 12 | locales.add(locale); |
118 | } |
|
119 | 18 | } |
120 | ||
121 | 7 | final List<Locale> list = new ArrayList<Locale>(locales); |
122 | 7 | Collections.sort(list, COMPARATOR); |
123 | 7 | return list; |
124 | } |
|
125 | ||
126 | Locale calcLocale(final String urlString) |
|
127 | { |
|
128 | 28 | final String fileName = FilenameUtils.getBaseName(urlString); |
129 | 28 | final int urlStringLength = urlString.length(); |
130 | 28 | int separatorIndex = fileName.indexOf('_'); |
131 | 28 | Locale locale = null; |
132 | ||
133 | 49 | while (separatorIndex != -1) |
134 | { |
|
135 | 21 | final String localeString = fileName.substring(separatorIndex + 1); |
136 | 21 | locale = constructLocale(localeString); |
137 | 21 | separatorIndex = |
138 | locale != null || separatorIndex + 1 >= urlStringLength ? -1 |
|
139 | : fileName.indexOf('_', separatorIndex + 1); |
|
140 | 21 | } |
141 | 28 | return locale; |
142 | } |
|
143 | ||
144 | private Locale constructLocale(final String localeString) |
|
145 | { |
|
146 | try |
|
147 | { |
|
148 | 21 | final Locale locale = LocaleUtils.toLocale(localeString); |
149 | 18 | return locale; |
150 | } |
|
151 | 3 | catch (final IllegalArgumentException e) |
152 | { |
|
153 | 3 | return null; |
154 | } |
|
155 | } |
|
156 | ||
157 | // --- object basics -------------------------------------------------------- |
|
158 | ||
159 | } |