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 | ||
35 | /** |
|
36 | * Finds all supported locales. |
|
37 | */ |
|
38 | final class LocaleFinder |
|
39 | { |
|
40 | // ********************************* Fields ********************************* |
|
41 | ||
42 | // --- constants ------------------------------------------------------------ |
|
43 | ||
44 | /** |
|
45 | * Helper to get listing of a folder on the class path. |
|
46 | */ |
|
47 | private static final ClassPathListing CLASS_PATH_LISTING; |
|
48 | ||
49 | // --- members -------------------------------------------------------------- |
|
50 | ||
51 | /** |
|
52 | * Locale comparator. |
|
53 | */ |
|
54 | 0 | private static final Comparator<Locale> COMPARATOR = new Comparator<Locale>() |
55 | 0 | { |
56 | @Override |
|
57 | public int compare(final Locale o1, final Locale o2) |
|
58 | { |
|
59 | 0 | return o1.toString().compareTo(o2.toString()); |
60 | } |
|
61 | }; |
|
62 | ||
63 | // ****************************** Initializer ******************************* |
|
64 | ||
65 | static |
|
66 | { |
|
67 | 0 | final ClassPathListingFactory factory = new ClassPathListingFactory(); |
68 | 0 | CLASS_PATH_LISTING = factory.create(JarAndFileClassPathListing.class); |
69 | 0 | } |
70 | ||
71 | // ****************************** Constructors ****************************** |
|
72 | ||
73 | /** |
|
74 | * Default constructor. |
|
75 | */ |
|
76 | LocaleFinder() |
|
77 | 0 | { |
78 | 0 | } |
79 | ||
80 | // ****************************** Inner Classes ***************************** |
|
81 | ||
82 | // ********************************* Methods ******************************** |
|
83 | ||
84 | // --- init ----------------------------------------------------------------- |
|
85 | ||
86 | // --- get&set -------------------------------------------------------------- |
|
87 | ||
88 | // --- business ------------------------------------------------------------- |
|
89 | ||
90 | List<Locale> find(final String systemId, final ClassPathContext context) |
|
91 | throws ConfigException |
|
92 | { |
|
93 | 0 | final Set<Locale> locales = new HashSet<Locale>(); |
94 | 0 | locales.add(new Locale("")); |
95 | ||
96 | 0 | final List<String> listing = |
97 | CLASS_PATH_LISTING.list(context, |
|
98 | PropertiesContext.META_INF_PROPERTY_SET_REPORT); |
|
99 | ||
100 | 0 | for (final String path : listing) |
101 | { |
|
102 | 0 | final Locale locale = calcLocale(path); |
103 | 0 | if (locale != null) |
104 | { |
|
105 | 0 | locales.add(locale); |
106 | } |
|
107 | 0 | } |
108 | ||
109 | 0 | final List<Locale> list = new ArrayList<Locale>(locales); |
110 | 0 | Collections.sort(list, COMPARATOR); |
111 | 0 | return list; |
112 | } |
|
113 | ||
114 | Locale calcLocale(final String urlString) |
|
115 | { |
|
116 | 0 | final String fileName = FilenameUtils.getBaseName(urlString); |
117 | 0 | final int urlStringLength = urlString.length(); |
118 | 0 | int separatorIndex = fileName.indexOf('_'); |
119 | 0 | Locale locale = null; |
120 | ||
121 | 0 | while (separatorIndex != -1) |
122 | { |
|
123 | 0 | final String localeString = fileName.substring(separatorIndex + 1); |
124 | 0 | locale = constructLocale(localeString); |
125 | 0 | separatorIndex = |
126 | locale != null || separatorIndex + 1 >= urlStringLength ? -1 |
|
127 | : fileName.indexOf('_', separatorIndex + 1); |
|
128 | 0 | } |
129 | 0 | return locale; |
130 | } |
|
131 | ||
132 | private Locale constructLocale(final String localeString) |
|
133 | { |
|
134 | try |
|
135 | { |
|
136 | 0 | final Locale locale = LocaleUtils.toLocale(localeString); |
137 | 0 | return locale; |
138 | } |
|
139 | 0 | catch (final IllegalArgumentException e) |
140 | { |
|
141 | 0 | return null; |
142 | } |
|
143 | } |
|
144 | ||
145 | // --- object basics -------------------------------------------------------- |
|
146 | ||
147 | } |