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/fxfa/parser/cxfa_nodelocale.h"
8
9 #include <utility>
10
11 #include "fxjs/xfa/cjx_object.h"
12 #include "xfa/fxfa/parser/cxfa_calendarsymbols.h"
13 #include "xfa/fxfa/parser/cxfa_datetimesymbols.h"
14 #include "xfa/fxfa/parser/cxfa_document.h"
15 #include "xfa/fxfa/parser/cxfa_localemgr.h"
16 #include "xfa/fxfa/parser/cxfa_node.h"
17 #include "xfa/fxfa/parser/cxfa_timezoneprovider.h"
18 #include "xfa/fxfa/parser/xfa_utils.h"
19
20 namespace {
21
22 const wchar_t g_FX_Percent[] = L"z,zzz,zzz,zzz,zzz,zzz%";
23 const wchar_t g_FX_Currency[] = L"$z,zzz,zzz,zzz,zzz,zz9.99";
24 const wchar_t g_FX_Decimal[] = L"z,zzz,zzz,zzz,zzz,zz9.zzz";
25 const wchar_t g_FX_Integer[] = L"z,zzz,zzz,zzz,zzz,zzz";
26
27 } // namespace
28
XFA_PatternToString(FX_LOCALENUMSUBCATEGORY category)29 WideString XFA_PatternToString(FX_LOCALENUMSUBCATEGORY category) {
30 switch (category) {
31 case FX_LOCALENUMPATTERN_Percent:
32 return g_FX_Percent;
33 case FX_LOCALENUMPATTERN_Currency:
34 return g_FX_Currency;
35 case FX_LOCALENUMPATTERN_Decimal:
36 return g_FX_Decimal;
37 case FX_LOCALENUMPATTERN_Integer:
38 return g_FX_Integer;
39 }
40 return WideString();
41 }
42
CXFA_NodeLocale(CXFA_Node * pLocale)43 CXFA_NodeLocale::CXFA_NodeLocale(CXFA_Node* pLocale) : m_pLocale(pLocale) {}
44
~CXFA_NodeLocale()45 CXFA_NodeLocale::~CXFA_NodeLocale() {}
46
GetName() const47 WideString CXFA_NodeLocale::GetName() const {
48 return WideString(m_pLocale
49 ? m_pLocale->JSObject()->GetCData(XFA_Attribute::Name)
50 : nullptr);
51 }
52
GetDecimalSymbol() const53 WideString CXFA_NodeLocale::GetDecimalSymbol() const {
54 return GetSymbol(XFA_Element::NumberSymbols, L"decimal");
55 }
56
GetGroupingSymbol() const57 WideString CXFA_NodeLocale::GetGroupingSymbol() const {
58 return GetSymbol(XFA_Element::NumberSymbols, L"grouping");
59 }
60
GetPercentSymbol() const61 WideString CXFA_NodeLocale::GetPercentSymbol() const {
62 return GetSymbol(XFA_Element::NumberSymbols, L"percent");
63 }
64
GetMinusSymbol() const65 WideString CXFA_NodeLocale::GetMinusSymbol() const {
66 return GetSymbol(XFA_Element::NumberSymbols, L"minus");
67 }
68
GetCurrencySymbol() const69 WideString CXFA_NodeLocale::GetCurrencySymbol() const {
70 return GetSymbol(XFA_Element::CurrencySymbols, L"symbol");
71 }
72
GetDateTimeSymbols() const73 WideString CXFA_NodeLocale::GetDateTimeSymbols() const {
74 CXFA_DateTimeSymbols* pSymbols =
75 m_pLocale ? m_pLocale->GetChild<CXFA_DateTimeSymbols>(
76 0, XFA_Element::DateTimeSymbols, false)
77 : nullptr;
78 return pSymbols ? pSymbols->JSObject()->GetContent(false) : WideString();
79 }
80
GetMonthName(int32_t nMonth,bool bAbbr) const81 WideString CXFA_NodeLocale::GetMonthName(int32_t nMonth, bool bAbbr) const {
82 return GetCalendarSymbol(XFA_Element::MonthNames, nMonth, bAbbr);
83 }
84
GetDayName(int32_t nWeek,bool bAbbr) const85 WideString CXFA_NodeLocale::GetDayName(int32_t nWeek, bool bAbbr) const {
86 return GetCalendarSymbol(XFA_Element::DayNames, nWeek, bAbbr);
87 }
88
GetMeridiemName(bool bAM) const89 WideString CXFA_NodeLocale::GetMeridiemName(bool bAM) const {
90 return GetCalendarSymbol(XFA_Element::MeridiemNames, bAM ? 0 : 1, false);
91 }
92
GetTimeZone() const93 FX_TIMEZONE CXFA_NodeLocale::GetTimeZone() const {
94 return CXFA_TimeZoneProvider().GetTimeZone();
95 }
96
GetEraName(bool bAD) const97 WideString CXFA_NodeLocale::GetEraName(bool bAD) const {
98 return GetCalendarSymbol(XFA_Element::EraNames, bAD ? 1 : 0, false);
99 }
100
GetDatePattern(FX_LOCALEDATETIMESUBCATEGORY eType) const101 WideString CXFA_NodeLocale::GetDatePattern(
102 FX_LOCALEDATETIMESUBCATEGORY eType) const {
103 switch (eType) {
104 case FX_LOCALEDATETIMESUBCATEGORY_Short:
105 return GetSymbol(XFA_Element::DatePatterns, L"short");
106 case FX_LOCALEDATETIMESUBCATEGORY_Medium:
107 case FX_LOCALEDATETIMESUBCATEGORY_Default:
108 return GetSymbol(XFA_Element::DatePatterns, L"med");
109 case FX_LOCALEDATETIMESUBCATEGORY_Full:
110 return GetSymbol(XFA_Element::DatePatterns, L"full");
111 case FX_LOCALEDATETIMESUBCATEGORY_Long:
112 return GetSymbol(XFA_Element::DatePatterns, L"long");
113 }
114 return WideString();
115 }
116
GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY eType) const117 WideString CXFA_NodeLocale::GetTimePattern(
118 FX_LOCALEDATETIMESUBCATEGORY eType) const {
119 switch (eType) {
120 case FX_LOCALEDATETIMESUBCATEGORY_Short:
121 return GetSymbol(XFA_Element::TimePatterns, L"short");
122 case FX_LOCALEDATETIMESUBCATEGORY_Medium:
123 case FX_LOCALEDATETIMESUBCATEGORY_Default:
124 return GetSymbol(XFA_Element::TimePatterns, L"med");
125 case FX_LOCALEDATETIMESUBCATEGORY_Full:
126 return GetSymbol(XFA_Element::TimePatterns, L"full");
127 case FX_LOCALEDATETIMESUBCATEGORY_Long:
128 return GetSymbol(XFA_Element::TimePatterns, L"long");
129 }
130 return WideString();
131 }
132
GetNumPattern(FX_LOCALENUMSUBCATEGORY eType) const133 WideString CXFA_NodeLocale::GetNumPattern(FX_LOCALENUMSUBCATEGORY eType) const {
134 return XFA_PatternToString(eType);
135 }
136
GetNodeByName(CXFA_Node * pParent,WideStringView wsName) const137 CXFA_Node* CXFA_NodeLocale::GetNodeByName(CXFA_Node* pParent,
138 WideStringView wsName) const {
139 CXFA_Node* pChild = pParent ? pParent->GetFirstChild() : nullptr;
140 while (pChild) {
141 if (pChild->JSObject()->GetAttribute(XFA_Attribute::Name) == wsName)
142 return pChild;
143
144 pChild = pChild->GetNextSibling();
145 }
146 return nullptr;
147 }
148
GetSymbol(XFA_Element eElement,WideStringView symbol_type) const149 WideString CXFA_NodeLocale::GetSymbol(XFA_Element eElement,
150 WideStringView symbol_type) const {
151 CXFA_Node* pSymbols =
152 m_pLocale ? m_pLocale->GetChild<CXFA_Node>(0, eElement, false) : nullptr;
153 CXFA_Node* pSymbol = GetNodeByName(pSymbols, symbol_type);
154 return pSymbol ? pSymbol->JSObject()->GetContent(false) : WideString();
155 }
156
GetCalendarSymbol(XFA_Element eElement,int index,bool bAbbr) const157 WideString CXFA_NodeLocale::GetCalendarSymbol(XFA_Element eElement,
158 int index,
159 bool bAbbr) const {
160 CXFA_CalendarSymbols* pCalendar =
161 m_pLocale ? m_pLocale->GetChild<CXFA_CalendarSymbols>(
162 0, XFA_Element::CalendarSymbols, false)
163 : nullptr;
164 if (!pCalendar)
165 return WideString();
166
167 for (CXFA_Node* pNode = pCalendar->GetFirstChildByClass<CXFA_Node>(eElement);
168 pNode; pNode = pNode->GetNextSameClassSibling<CXFA_Node>(eElement)) {
169 if (pNode->JSObject()->GetBoolean(XFA_Attribute::Abbr) == bAbbr) {
170 CXFA_Node* pSymbol =
171 pNode->GetChild<CXFA_Node>(index, XFA_Element::Unknown, false);
172 return pSymbol ? pSymbol->JSObject()->GetContent(false) : WideString();
173 }
174 }
175 return WideString();
176 }
177