1 karl 1.16 //%2005////////////////////////////////////////////////////////////////////////
|
2 humberto 1.7 //
|
3 karl 1.15 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
|
6 karl 1.9 // IBM Corp.; EMC Corporation, The Open Group.
|
7 karl 1.15 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
|
9 karl 1.16 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
|
11 humberto 1.7 //
12 // Permission is hereby granted, free of charge, to any person obtaining a copy
13 // of this software and associated documentation files (the "Software"), to
14 // deal in the Software without restriction, including without limitation the
15 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 // sell copies of the Software, and to permit persons to whom the Software is
17 // furnished to do so, subject to the following conditions:
18 //
19 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
20 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
21 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
22 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
23 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
27 karl 1.9 //
|
28 humberto 1.7 //==============================================================================
|
29 kumpf 1.18 //
|
30 humberto 1.7 // Author: Humberto Rivero (hurivero@us.ibm.com)
|
31 kumpf 1.18 //
|
32 aruran.ms 1.20 // Modified By: Aruran, IBM (ashanmug@in.ibm.com) for Bug# 3514
|
33 humberto 1.7 //
34 //%/////////////////////////////////////////////////////////////////////////////
|
35 humberto 1.11
|
36 humberto 1.7 #include <Pegasus/Common/AcceptLanguages.h>
|
37 humberto 1.14 #include <Pegasus/Common/LanguageParser.h>
|
38 humberto 1.7 #include <Pegasus/Common/Tracer.h>
|
39 humberto 1.14 #include <Pegasus/Common/InternalException.h>
40 #include <Pegasus/Common/LanguageElementContainerRep.h>
|
41 humberto 1.7 #ifdef PEGASUS_HAS_ICU
|
42 kumpf 1.18 # include <unicode/locid.h>
|
43 humberto 1.7 #endif
|
44 humberto 1.8 #if defined(PEGASUS_OS_OS400)
|
45 kumpf 1.18 # include "OS400ConvertChar.h"
|
46 humberto 1.8 #endif
|
47 humberto 1.7
48 PEGASUS_NAMESPACE_BEGIN
49
50 //////////////////////////////////////////////////////////////
51 //
|
52 kumpf 1.18 // AcceptLanguages
|
53 humberto 1.7 //
54 //////////////////////////////////////////////////////////////
55
56 const AcceptLanguages AcceptLanguages::EMPTY = AcceptLanguages();
57
|
58 kumpf 1.18 AcceptLanguages::AcceptLanguages()
59 : LanguageElementContainer()
60 {
61 }
62
|
63 aruran.ms 1.20 AcceptLanguages::AcceptLanguages(const String& hdr)
|
64 kumpf 1.18 {
65 if (hdr.size() > 0)
66 {
67 Array<String> values;
68 LanguageParser lp;
69 lp.parseHdr(values,hdr);
70 buildLanguageElements(values);
71 prioritize();
72 }
73 }
74
75 AcceptLanguages::AcceptLanguages(const Array<LanguageElement>& aContainer)
76 : LanguageElementContainer(aContainer)
77 {
78 prioritize();
79 }
80
81 AcceptLanguages::AcceptLanguages(const Array<AcceptLanguageElement>& aContainer)
82 {
83 for (Uint32 i = 0; i < aContainer.size(); i++)
84 {
85 kumpf 1.18 LanguageElementContainer::append(aContainer[i]);
86 }
87 prioritize();
88 }
89
90 AcceptLanguages::AcceptLanguages(const AcceptLanguages& rhs)
91 : LanguageElementContainer(rhs)
92 {
93 }
94
95 AcceptLanguages::~AcceptLanguages()
96 {
97 }
98
99 String AcceptLanguages::toString() const
100 {
101 String s;
102 for (Uint32 i = 0; i < _rep->container.size(); i++)
103 {
104 s.append(_rep->container[i].toString());
105 if ((_rep->container[i].getLanguage() != "*") &&
106 kumpf 1.18 (_rep->container[i].getQuality() != 1))
107 {
108 char q[6];
109 sprintf(q,"%4.3f", _rep->container[i].getQuality());
110 s.append(";q=").append(q);
|
111 humberto 1.14 }
|
112 kumpf 1.18 if (i < _rep->container.size() - 1)
113 {
114 s.append(",");
|
115 humberto 1.14 }
|
116 kumpf 1.18 }
117 return s;
|
118 humberto 1.7 }
119
|
120 kumpf 1.18 PEGASUS_STD(ostream)& operator<<(
121 PEGASUS_STD(ostream)& stream,
122 const AcceptLanguages& al)
123 {
124 stream << al.toString();
125 return stream;
126 }
127
|
128 kumpf 1.23 AcceptLanguages& AcceptLanguages::operator=(const AcceptLanguages& rhs)
|
129 kumpf 1.18 {
130 if (&rhs != this)
131 {
132 LanguageElementContainer::operator=(rhs);
133 }
134 return *this;
135 }
136
137 AcceptLanguageElement AcceptLanguages::getLanguageElement(Uint32 index) const
138 {
139 return AcceptLanguageElement(_rep->getLanguageElement(index));
140 }
141
142 void AcceptLanguages::getAllLanguageElements(
143 Array<AcceptLanguageElement>& elements) const
144 {
145 Array<LanguageElement> tmp = _rep->getAllLanguageElements();
146 for (Uint32 i = 0; i < tmp.size(); i++)
147 {
148 elements.append(AcceptLanguageElement(tmp[i]));
149 }
150 kumpf 1.18 }
151
152 Array<AcceptLanguageElement> AcceptLanguages::getAllLanguageElements() const
153 {
154 Array<AcceptLanguageElement> elements;
155 Array<LanguageElement> tmp = _rep->getAllLanguageElements();
156 for (Uint32 i = 0; i < tmp.size(); i++)
157 {
158 elements.append(AcceptLanguageElement(tmp[i]));
159 }
160 return elements;
161 }
162
163 AcceptLanguageElement AcceptLanguages::itrNext()
164 {
165 return AcceptLanguageElement(_rep->itrNext());
166 }
167
168 void AcceptLanguages::insert(const AcceptLanguageElement& element)
169 {
170 _rep->append(element);
171 kumpf 1.18 prioritize();
172 }
173
174 void AcceptLanguages::remove(Uint32 index)
175 {
176 _rep->remove(index);
177 prioritize();
178 }
179
180 Uint32 AcceptLanguages::remove(const AcceptLanguageElement& element)
181 {
182 int rc;
183 if ((rc = _rep->remove(element)) != -1)
184 {
185 prioritize();
186 }
187 return rc;
188 }
189
|
190 joyce.j 1.19 Uint32 AcceptLanguages::find(const AcceptLanguageElement& element) const
|
191 kumpf 1.18 {
192 for (Uint32 i = 0; i < _rep->container.size(); i++)
193 {
194 if (element.getTag() == _rep->container[i].getTag())
195 {
196 if (element.getQuality() == _rep->container[i].getQuality())
197 {
198 return i;
199 }
|
200 humberto 1.14 }
|
201 kumpf 1.18 }
|
202 joyce.j 1.19 return PEG_NOT_FOUND;
|
203 humberto 1.7 }
204
|
205 joyce.j 1.22 Uint32 AcceptLanguages::find(const String & language_tag, Real32 quality) const
|
206 kumpf 1.18 {
207 return find(AcceptLanguageElement(language_tag,quality));
208 }
209
|
210 joyce.j 1.22 void AcceptLanguages::buildLanguageElements(const Array<String>& values)
|
211 kumpf 1.18 {
212 for (Uint32 i = 0; i < values.size(); i++)
213 {
214 String language_tag;
215 Real32 quality;
216 LanguageParser lp;
217 quality = lp.parseAcceptLanguageValue(language_tag,values[i]);
218 append(AcceptLanguageElement(language_tag,quality));
219 }
220 }
221
222 AcceptLanguages AcceptLanguages::getDefaultAcceptLanguages()
223 {
224 #if defined(PEGASUS_HAS_MESSAGES) && defined(PEGASUS_HAS_ICU)
225 Locale default_loc = Locale::getDefault();
226
227 # ifdef PEGASUS_OS_OS400
228 char* tmp = (char*)default_loc.getName();
229 char tmp_[100];
230 EtoA(strcpy(tmp_,tmp));
231 try
232 kumpf 1.18 {
233 return AcceptLanguages(tmp_);
234 }
235 # else
236 try
237 {
238 return AcceptLanguages(default_loc.getName());
239 }
240 # endif
241 catch (const InvalidAcceptLanguageHeader& e)
242 {
243 Logger::put_l(Logger::ERROR_LOG,System::CIMSERVER,Logger::SEVERE,
244 "src.Server.cimserver.FAILED_TO_GET_PROCESS_LOCALE",
245 "Could not convert the system locale to a valid accept-language "
246 "format");
247 Logger::put(Logger::ERROR_LOG, System::CIMSERVER, Logger::SEVERE,
248 e.getMessage());
249 return AcceptLanguages("*");
250 }
251 #endif
|
252 humberto 1.7
|
253 kumpf 1.18 return AcceptLanguages();
|
254 humberto 1.7 }
255
|
256 kumpf 1.18 void AcceptLanguages::prioritize()
257 {
258 // sort according to quality value; if two qualities are equal then
259 // preference is given to the existing order of the objects in the array.
260
261 //array is already sorted
262 if (_rep->container.size() <= 1)
263 {
264 return;
265 }
266
267 Boolean changed;
268 int n = _rep->container.size();
269 do
270 {
|
271 humberto 1.14 n--; // make loop smaller each time.
272 changed = false; // assume this is last pass over array
|
273 kumpf 1.18 for (int i=0; i<n; i++)
274 {
275 if (_rep->container[i].getQuality() <
276 _rep->container[i+1].getQuality())
277 {
278 LanguageElement temp = _rep->container[i];
279 _rep->container[i] = _rep->container[i+1];
280 _rep->container[i+1] = temp;
281 changed = true; // after an exchange, must look again
282 }
|
283 humberto 1.14 }
|
284 kumpf 1.18 } while (changed);
|
285 humberto 1.7 }
286
287 PEGASUS_NAMESPACE_END
|