OLD | NEW |
| (Empty) |
1 // Copyright 2014 PDFium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
6 | |
7 #include "xfa/src/fxfa/parser/xfa_locale.h" | |
8 | |
9 #include "core/include/fxcrt/fx_xml.h" | |
10 #include "xfa/src/fxfa/fm2js/xfa_fm2jsapi.h" | |
11 #include "xfa/src/fxfa/parser/xfa_docdata.h" | |
12 #include "xfa/src/fxfa/parser/xfa_doclayout.h" | |
13 #include "xfa/src/fxfa/parser/xfa_document.h" | |
14 #include "xfa/src/fxfa/parser/xfa_localemgr.h" | |
15 #include "xfa/src/fxfa/parser/xfa_object.h" | |
16 #include "xfa/src/fxfa/parser/xfa_parser.h" | |
17 #include "xfa/src/fxfa/parser/xfa_script.h" | |
18 #include "xfa/src/fxfa/parser/xfa_utils.h" | |
19 | |
20 static const FX_WCHAR g_FX_Percent[] = L"z,zzz,zzz,zzz,zzz,zzz%"; | |
21 static const FX_WCHAR g_FX_Currency[] = L"$z,zzz,zzz,zzz,zzz,zz9.99"; | |
22 static const FX_WCHAR g_FX_Decimal[] = L"z,zzz,zzz,zzz,zzz,zz9.zzz"; | |
23 static const FX_WCHAR g_FX_Integer[] = L"z,zzz,zzz,zzz,zzz,zzz"; | |
24 | |
25 CXFA_XMLLocale::CXFA_XMLLocale(CXML_Element* pLocaleData) { | |
26 m_pLocaleData = pLocaleData; | |
27 } | |
28 CXFA_XMLLocale::~CXFA_XMLLocale() { | |
29 if (m_pLocaleData) { | |
30 delete m_pLocaleData; | |
31 } | |
32 } | |
33 void CXFA_XMLLocale::Release() { | |
34 delete this; | |
35 } | |
36 CFX_WideString CXFA_XMLLocale::GetName() { | |
37 return m_pLocaleData ? m_pLocaleData->GetAttrValue("name") : CFX_WideString(); | |
38 } | |
39 void CXFA_XMLLocale::GetNumbericSymbol(FX_LOCALENUMSYMBOL eType, | |
40 CFX_WideString& wsNumSymbol) const { | |
41 CFX_ByteString bsSymbols; | |
42 CFX_WideString wsName; | |
43 switch (eType) { | |
44 case FX_LOCALENUMSYMBOL_Decimal: | |
45 bsSymbols = "numberSymbols"; | |
46 wsName = FX_WSTRC(L"decimal"); | |
47 break; | |
48 case FX_LOCALENUMSYMBOL_Grouping: | |
49 bsSymbols = "numberSymbols"; | |
50 wsName = FX_WSTRC(L"grouping"); | |
51 break; | |
52 case FX_LOCALENUMSYMBOL_Percent: | |
53 bsSymbols = "numberSymbols"; | |
54 wsName = FX_WSTRC(L"percent"); | |
55 break; | |
56 case FX_LOCALENUMSYMBOL_Minus: | |
57 bsSymbols = "numberSymbols"; | |
58 wsName = FX_WSTRC(L"minus"); | |
59 break; | |
60 case FX_LOCALENUMSYMBOL_Zero: | |
61 bsSymbols = "numberSymbols"; | |
62 wsName = FX_WSTRC(L"zero"); | |
63 break; | |
64 case FX_LOCALENUMSYMBOL_CurrencySymbol: | |
65 bsSymbols = "currencySymbols"; | |
66 wsName = FX_WSTRC(L"symbol"); | |
67 break; | |
68 case FX_LOCALENUMSYMBOL_CurrencyName: | |
69 bsSymbols = "currencySymbols"; | |
70 wsName = FX_WSTRC(L"isoname"); | |
71 break; | |
72 default: | |
73 return; | |
74 } | |
75 CXML_Element* pElement = m_pLocaleData->GetElement("", bsSymbols); | |
76 if (!pElement) { | |
77 return; | |
78 } | |
79 GetPattern(pElement, CFX_ByteStringC((const FX_CHAR*)bsSymbols, | |
80 bsSymbols.GetLength() - 1), | |
81 wsName, wsNumSymbol); | |
82 } | |
83 void CXFA_XMLLocale::GetDateTimeSymbols(CFX_WideString& wsDtSymbol) const { | |
84 if (!m_pLocaleData) { | |
85 return; | |
86 } | |
87 CFX_ByteString bsSpace; | |
88 CXML_Element* pNumberSymbols = | |
89 m_pLocaleData->GetElement(bsSpace, "dateTimeSymbols"); | |
90 if (!pNumberSymbols) { | |
91 return; | |
92 } | |
93 wsDtSymbol = pNumberSymbols->GetContent(0); | |
94 } | |
95 void CXFA_XMLLocale::GetMonthName(int32_t nMonth, | |
96 CFX_WideString& wsMonthName, | |
97 FX_BOOL bAbbr) const { | |
98 wsMonthName = GetCalendarSymbol("month", nMonth, bAbbr); | |
99 } | |
100 void CXFA_XMLLocale::GetDayName(int32_t nWeek, | |
101 CFX_WideString& wsDayName, | |
102 FX_BOOL bAbbr) const { | |
103 wsDayName = GetCalendarSymbol("day", nWeek, bAbbr); | |
104 } | |
105 void CXFA_XMLLocale::GetMeridiemName(CFX_WideString& wsMeridiemName, | |
106 FX_BOOL bAM) const { | |
107 wsMeridiemName = GetCalendarSymbol("meridiem", bAM ? 0 : 1, FALSE); | |
108 } | |
109 void CXFA_XMLLocale::GetTimeZone(FX_TIMEZONE& tz) const { | |
110 IXFA_TimeZoneProvider* pProvider = IXFA_TimeZoneProvider::Get(); | |
111 pProvider->GetTimeZone(tz); | |
112 } | |
113 void CXFA_XMLLocale::GetEraName(CFX_WideString& wsEraName, FX_BOOL bAD) const { | |
114 wsEraName = GetCalendarSymbol("era", bAD ? 1 : 0, FALSE); | |
115 } | |
116 CFX_WideString CXFA_XMLLocale::GetCalendarSymbol(const CFX_ByteStringC& symbol, | |
117 int index, | |
118 FX_BOOL bAbbr) const { | |
119 CFX_ByteString pstrSymbolNames = symbol + "Names"; | |
120 CFX_WideString wsSymbolName = L""; | |
121 if (m_pLocaleData) { | |
122 CXML_Element* pChild = m_pLocaleData->GetElement("", "calendarSymbols"); | |
123 if (pChild) { | |
124 CXML_Element* pSymbolNames = pChild->GetElement("", pstrSymbolNames); | |
125 if (pSymbolNames) { | |
126 if (pSymbolNames->GetAttrInteger("abbr") != bAbbr) { | |
127 pSymbolNames = pChild->GetElement("", pstrSymbolNames, 1); | |
128 } | |
129 if (pSymbolNames && pSymbolNames->GetAttrInteger("abbr") == bAbbr) { | |
130 CXML_Element* pSymbolName = | |
131 pSymbolNames->GetElement("", symbol, index); | |
132 if (pSymbolName) { | |
133 wsSymbolName = pSymbolName->GetContent(0); | |
134 } | |
135 } | |
136 } | |
137 } | |
138 } | |
139 return wsSymbolName; | |
140 } | |
141 void CXFA_XMLLocale::GetDatePattern(FX_LOCALEDATETIMESUBCATEGORY eType, | |
142 CFX_WideString& wsPattern) const { | |
143 CXML_Element* pElement = m_pLocaleData->GetElement("", "datePatterns"); | |
144 if (pElement == NULL) { | |
145 return; | |
146 } | |
147 CFX_WideString wsName; | |
148 switch (eType) { | |
149 case FX_LOCALEDATETIMESUBCATEGORY_Short: | |
150 wsName = L"short"; | |
151 break; | |
152 case FX_LOCALEDATETIMESUBCATEGORY_Default: | |
153 case FX_LOCALEDATETIMESUBCATEGORY_Medium: | |
154 wsName = L"med"; | |
155 break; | |
156 case FX_LOCALEDATETIMESUBCATEGORY_Full: | |
157 wsName = L"full"; | |
158 break; | |
159 case FX_LOCALEDATETIMESUBCATEGORY_Long: | |
160 wsName = L"long"; | |
161 break; | |
162 } | |
163 GetPattern(pElement, "datePattern", wsName, wsPattern); | |
164 } | |
165 void CXFA_XMLLocale::GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY eType, | |
166 CFX_WideString& wsPattern) const { | |
167 CXML_Element* pElement = m_pLocaleData->GetElement("", "timePatterns"); | |
168 if (pElement == NULL) { | |
169 return; | |
170 } | |
171 CFX_WideString wsName; | |
172 switch (eType) { | |
173 case FX_LOCALEDATETIMESUBCATEGORY_Short: | |
174 wsName = L"short"; | |
175 break; | |
176 case FX_LOCALEDATETIMESUBCATEGORY_Default: | |
177 case FX_LOCALEDATETIMESUBCATEGORY_Medium: | |
178 wsName = L"med"; | |
179 break; | |
180 case FX_LOCALEDATETIMESUBCATEGORY_Full: | |
181 wsName = L"full"; | |
182 break; | |
183 case FX_LOCALEDATETIMESUBCATEGORY_Long: | |
184 wsName = L"long"; | |
185 break; | |
186 } | |
187 GetPattern(pElement, "timePattern", wsName, wsPattern); | |
188 } | |
189 void CXFA_XMLLocale::GetNumPattern(FX_LOCALENUMSUBCATEGORY eType, | |
190 CFX_WideString& wsPattern) const { | |
191 CXML_Element* pElement = m_pLocaleData->GetElement("", "numberPatterns"); | |
192 if (pElement == NULL) { | |
193 return; | |
194 } | |
195 switch (eType) { | |
196 case FX_LOCALENUMPATTERN_Percent: | |
197 wsPattern = g_FX_Percent; | |
198 break; | |
199 case FX_LOCALENUMPATTERN_Currency: | |
200 wsPattern = g_FX_Currency; | |
201 break; | |
202 case FX_LOCALENUMPATTERN_Decimal: | |
203 wsPattern = g_FX_Decimal; | |
204 break; | |
205 case FX_LOCALENUMPATTERN_Integer: | |
206 wsPattern = g_FX_Integer; | |
207 break; | |
208 } | |
209 } | |
210 void CXFA_XMLLocale::GetPattern(CXML_Element* pElement, | |
211 const CFX_ByteStringC& bsTag, | |
212 const CFX_WideStringC& wsName, | |
213 CFX_WideString& wsPattern) const { | |
214 int32_t iCount = pElement->CountElements("", bsTag); | |
215 for (int32_t i = 0; i < iCount; i++) { | |
216 CXML_Element* pChild = pElement->GetElement("", bsTag, i); | |
217 if (pChild->GetAttrValue("name") == wsName) { | |
218 wsPattern = pChild->GetContent(0); | |
219 return; | |
220 } | |
221 } | |
222 } | |
223 CXFA_NodeLocale::CXFA_NodeLocale(CXFA_Node* pLocale) { | |
224 m_pLocale = pLocale; | |
225 } | |
226 CXFA_NodeLocale::~CXFA_NodeLocale() {} | |
227 void CXFA_NodeLocale::Release() { | |
228 delete this; | |
229 } | |
230 CFX_WideString CXFA_NodeLocale::GetName() { | |
231 return m_pLocale ? m_pLocale->GetCData(XFA_ATTRIBUTE_Name) : NULL; | |
232 } | |
233 void CXFA_NodeLocale::GetNumbericSymbol(FX_LOCALENUMSYMBOL eType, | |
234 CFX_WideString& wsNumSymbol) const { | |
235 switch (eType) { | |
236 case FX_LOCALENUMSYMBOL_Decimal: | |
237 wsNumSymbol = GetSymbol(XFA_ELEMENT_NumberSymbols, FX_WSTRC(L"decimal")); | |
238 break; | |
239 case FX_LOCALENUMSYMBOL_Grouping: | |
240 wsNumSymbol = GetSymbol(XFA_ELEMENT_NumberSymbols, FX_WSTRC(L"grouping")); | |
241 break; | |
242 case FX_LOCALENUMSYMBOL_Percent: | |
243 wsNumSymbol = GetSymbol(XFA_ELEMENT_NumberSymbols, FX_WSTRC(L"percent")); | |
244 break; | |
245 case FX_LOCALENUMSYMBOL_Minus: | |
246 wsNumSymbol = GetSymbol(XFA_ELEMENT_NumberSymbols, FX_WSTRC(L"minus")); | |
247 break; | |
248 case FX_LOCALENUMSYMBOL_Zero: | |
249 wsNumSymbol = GetSymbol(XFA_ELEMENT_NumberSymbols, FX_WSTRC(L"zero")); | |
250 break; | |
251 case FX_LOCALENUMSYMBOL_CurrencySymbol: | |
252 wsNumSymbol = GetSymbol(XFA_ELEMENT_CurrencySymbols, FX_WSTRC(L"symbol")); | |
253 break; | |
254 case FX_LOCALENUMSYMBOL_CurrencyName: | |
255 wsNumSymbol = | |
256 GetSymbol(XFA_ELEMENT_CurrencySymbols, FX_WSTRC(L"isoname")); | |
257 break; | |
258 } | |
259 } | |
260 void CXFA_NodeLocale::GetDateTimeSymbols(CFX_WideString& wsDtSymbol) const { | |
261 CXFA_Node* pSymbols = | |
262 m_pLocale ? m_pLocale->GetChild(0, XFA_ELEMENT_DateTimeSymbols) : NULL; | |
263 wsDtSymbol = pSymbols ? pSymbols->GetContent() : CFX_WideString(); | |
264 } | |
265 void CXFA_NodeLocale::GetMonthName(int32_t nMonth, | |
266 CFX_WideString& wsMonthName, | |
267 FX_BOOL bAbbr) const { | |
268 wsMonthName = GetCalendarSymbol(XFA_ELEMENT_MonthNames, nMonth, bAbbr); | |
269 } | |
270 void CXFA_NodeLocale::GetDayName(int32_t nWeek, | |
271 CFX_WideString& wsDayName, | |
272 FX_BOOL bAbbr) const { | |
273 wsDayName = GetCalendarSymbol(XFA_ELEMENT_DayNames, nWeek, bAbbr); | |
274 } | |
275 void CXFA_NodeLocale::GetMeridiemName(CFX_WideString& wsMeridiemName, | |
276 FX_BOOL bAM) const { | |
277 wsMeridiemName = | |
278 GetCalendarSymbol(XFA_ELEMENT_MeridiemNames, bAM ? 0 : 1, FALSE); | |
279 } | |
280 void CXFA_NodeLocale::GetTimeZone(FX_TIMEZONE& tz) const { | |
281 IXFA_TimeZoneProvider* pProvider = IXFA_TimeZoneProvider::Get(); | |
282 pProvider->GetTimeZone(tz); | |
283 } | |
284 void CXFA_NodeLocale::GetEraName(CFX_WideString& wsEraName, FX_BOOL bAD) const { | |
285 wsEraName = GetCalendarSymbol(XFA_ELEMENT_EraNames, bAD ? 1 : 0, FALSE); | |
286 } | |
287 void CXFA_NodeLocale::GetDatePattern(FX_LOCALEDATETIMESUBCATEGORY eType, | |
288 CFX_WideString& wsPattern) const { | |
289 switch (eType) { | |
290 case FX_LOCALEDATETIMESUBCATEGORY_Short: | |
291 wsPattern = GetSymbol(XFA_ELEMENT_DatePatterns, FX_WSTRC(L"short")); | |
292 break; | |
293 case FX_LOCALEDATETIMESUBCATEGORY_Medium: | |
294 case FX_LOCALEDATETIMESUBCATEGORY_Default: | |
295 wsPattern = GetSymbol(XFA_ELEMENT_DatePatterns, FX_WSTRC(L"med")); | |
296 break; | |
297 case FX_LOCALEDATETIMESUBCATEGORY_Full: | |
298 wsPattern = GetSymbol(XFA_ELEMENT_DatePatterns, FX_WSTRC(L"full")); | |
299 break; | |
300 case FX_LOCALEDATETIMESUBCATEGORY_Long: | |
301 wsPattern = GetSymbol(XFA_ELEMENT_DatePatterns, FX_WSTRC(L"long")); | |
302 break; | |
303 } | |
304 } | |
305 void CXFA_NodeLocale::GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY eType, | |
306 CFX_WideString& wsPattern) const { | |
307 switch (eType) { | |
308 case FX_LOCALEDATETIMESUBCATEGORY_Short: | |
309 wsPattern = GetSymbol(XFA_ELEMENT_TimePatterns, FX_WSTRC(L"short")); | |
310 break; | |
311 case FX_LOCALEDATETIMESUBCATEGORY_Medium: | |
312 case FX_LOCALEDATETIMESUBCATEGORY_Default: | |
313 wsPattern = GetSymbol(XFA_ELEMENT_TimePatterns, FX_WSTRC(L"med")); | |
314 break; | |
315 case FX_LOCALEDATETIMESUBCATEGORY_Full: | |
316 wsPattern = GetSymbol(XFA_ELEMENT_TimePatterns, FX_WSTRC(L"full")); | |
317 break; | |
318 case FX_LOCALEDATETIMESUBCATEGORY_Long: | |
319 wsPattern = GetSymbol(XFA_ELEMENT_TimePatterns, FX_WSTRC(L"long")); | |
320 break; | |
321 } | |
322 } | |
323 void CXFA_NodeLocale::GetNumPattern(FX_LOCALENUMSUBCATEGORY eType, | |
324 CFX_WideString& wsPattern) const { | |
325 switch (eType) { | |
326 case FX_LOCALENUMPATTERN_Percent: | |
327 wsPattern = g_FX_Percent; | |
328 break; | |
329 case FX_LOCALENUMPATTERN_Currency: | |
330 wsPattern = g_FX_Currency; | |
331 break; | |
332 case FX_LOCALENUMPATTERN_Decimal: | |
333 wsPattern = g_FX_Decimal; | |
334 break; | |
335 case FX_LOCALENUMPATTERN_Integer: | |
336 wsPattern = g_FX_Integer; | |
337 break; | |
338 } | |
339 } | |
340 CXFA_Node* CXFA_NodeLocale::GetNodeByName(CXFA_Node* pParent, | |
341 const CFX_WideStringC& wsName) const { | |
342 CXFA_Node* pChild = | |
343 pParent ? pParent->GetNodeItem(XFA_NODEITEM_FirstChild) : NULL; | |
344 while (pChild) { | |
345 CFX_WideString wsChild; | |
346 if (pChild->GetAttribute(XFA_ATTRIBUTE_Name, wsChild)) { | |
347 if (wsChild == wsName) { | |
348 return pChild; | |
349 } | |
350 } | |
351 pChild = pChild->GetNodeItem(XFA_NODEITEM_NextSibling); | |
352 } | |
353 return NULL; | |
354 } | |
355 CFX_WideString CXFA_NodeLocale::GetSymbol( | |
356 XFA_ELEMENT eElement, | |
357 const CFX_WideStringC& symbol_type) const { | |
358 CXFA_Node* pSymbols = m_pLocale ? m_pLocale->GetChild(0, eElement) : NULL; | |
359 CXFA_Node* pSymbol = GetNodeByName(pSymbols, symbol_type); | |
360 return pSymbol ? pSymbol->GetContent() : CFX_WideString(); | |
361 } | |
362 CFX_WideString CXFA_NodeLocale::GetCalendarSymbol(XFA_ELEMENT eElement, | |
363 int index, | |
364 FX_BOOL bAbbr) const { | |
365 CXFA_Node* pCalendar = | |
366 m_pLocale ? m_pLocale->GetChild(0, XFA_ELEMENT_CalendarSymbols) : NULL; | |
367 if (pCalendar) { | |
368 CXFA_Node* pNode = pCalendar->GetFirstChildByClass(eElement); | |
369 for (; pNode; pNode = pNode->GetNextSameClassSibling(eElement)) { | |
370 if (pNode->GetBoolean(XFA_ATTRIBUTE_Abbr) == bAbbr) { | |
371 CXFA_Node* pSymbol = pNode->GetChild(index, XFA_ELEMENT_UNKNOWN); | |
372 return pSymbol ? pSymbol->GetContent() : CFX_WideString(); | |
373 } | |
374 } | |
375 } | |
376 return CFX_WideString(); | |
377 } | |
OLD | NEW |