• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "core/fxcrt/xml/cxml_element.h"
12 #include "fxjs/xfa/cjx_object.h"
13 #include "xfa/fxfa/parser/cxfa_calendarsymbols.h"
14 #include "xfa/fxfa/parser/cxfa_datetimesymbols.h"
15 #include "xfa/fxfa/parser/cxfa_document.h"
16 #include "xfa/fxfa/parser/cxfa_localemgr.h"
17 #include "xfa/fxfa/parser/cxfa_node.h"
18 #include "xfa/fxfa/parser/cxfa_timezoneprovider.h"
19 #include "xfa/fxfa/parser/xfa_utils.h"
20 
21 namespace {
22 
23 const wchar_t g_FX_Percent[] = L"z,zzz,zzz,zzz,zzz,zzz%";
24 const wchar_t g_FX_Currency[] = L"$z,zzz,zzz,zzz,zzz,zz9.99";
25 const wchar_t g_FX_Decimal[] = L"z,zzz,zzz,zzz,zzz,zz9.zzz";
26 const wchar_t g_FX_Integer[] = L"z,zzz,zzz,zzz,zzz,zzz";
27 
28 }  // namespace
29 
XFA_PatternToString(FX_LOCALENUMSUBCATEGORY category)30 WideString XFA_PatternToString(FX_LOCALENUMSUBCATEGORY category) {
31   switch (category) {
32     case FX_LOCALENUMPATTERN_Percent:
33       return g_FX_Percent;
34     case FX_LOCALENUMPATTERN_Currency:
35       return g_FX_Currency;
36     case FX_LOCALENUMPATTERN_Decimal:
37       return g_FX_Decimal;
38     case FX_LOCALENUMPATTERN_Integer:
39       return g_FX_Integer;
40   }
41   return WideString();
42 }
43 
CXFA_NodeLocale(CXFA_Node * pLocale)44 CXFA_NodeLocale::CXFA_NodeLocale(CXFA_Node* pLocale) : m_pLocale(pLocale) {}
45 
~CXFA_NodeLocale()46 CXFA_NodeLocale::~CXFA_NodeLocale() {}
47 
GetName() const48 WideString CXFA_NodeLocale::GetName() const {
49   return WideString(m_pLocale
50                         ? m_pLocale->JSObject()->GetCData(XFA_Attribute::Name)
51                         : nullptr);
52 }
53 
GetNumbericSymbol(FX_LOCALENUMSYMBOL eType) const54 WideString CXFA_NodeLocale::GetNumbericSymbol(FX_LOCALENUMSYMBOL eType) const {
55   switch (eType) {
56     case FX_LOCALENUMSYMBOL_Decimal:
57       return GetSymbol(XFA_Element::NumberSymbols, L"decimal");
58     case FX_LOCALENUMSYMBOL_Grouping:
59       return GetSymbol(XFA_Element::NumberSymbols, L"grouping");
60     case FX_LOCALENUMSYMBOL_Percent:
61       return GetSymbol(XFA_Element::NumberSymbols, L"percent");
62     case FX_LOCALENUMSYMBOL_Minus:
63       return GetSymbol(XFA_Element::NumberSymbols, L"minus");
64     case FX_LOCALENUMSYMBOL_Zero:
65       return GetSymbol(XFA_Element::NumberSymbols, L"zero");
66     case FX_LOCALENUMSYMBOL_CurrencySymbol:
67       return GetSymbol(XFA_Element::CurrencySymbols, L"symbol");
68     case FX_LOCALENUMSYMBOL_CurrencyName:
69       return GetSymbol(XFA_Element::CurrencySymbols, L"isoname");
70   }
71   return WideString();
72 }
73 
GetDateTimeSymbols() const74 WideString CXFA_NodeLocale::GetDateTimeSymbols() const {
75   CXFA_DateTimeSymbols* pSymbols =
76       m_pLocale ? m_pLocale->GetChild<CXFA_DateTimeSymbols>(
77                       0, XFA_Element::DateTimeSymbols, false)
78                 : nullptr;
79   return pSymbols ? pSymbols->JSObject()->GetContent(false) : WideString();
80 }
81 
GetMonthName(int32_t nMonth,bool bAbbr) const82 WideString CXFA_NodeLocale::GetMonthName(int32_t nMonth, bool bAbbr) const {
83   return GetCalendarSymbol(XFA_Element::MonthNames, nMonth, bAbbr);
84 }
85 
GetDayName(int32_t nWeek,bool bAbbr) const86 WideString CXFA_NodeLocale::GetDayName(int32_t nWeek, bool bAbbr) const {
87   return GetCalendarSymbol(XFA_Element::DayNames, nWeek, bAbbr);
88 }
89 
GetMeridiemName(bool bAM) const90 WideString CXFA_NodeLocale::GetMeridiemName(bool bAM) const {
91   return GetCalendarSymbol(XFA_Element::MeridiemNames, bAM ? 0 : 1, false);
92 }
93 
GetTimeZone() const94 FX_TIMEZONE CXFA_NodeLocale::GetTimeZone() const {
95   return CXFA_TimeZoneProvider().GetTimeZone();
96 }
97 
GetEraName(bool bAD) const98 WideString CXFA_NodeLocale::GetEraName(bool bAD) const {
99   return GetCalendarSymbol(XFA_Element::EraNames, bAD ? 1 : 0, false);
100 }
101 
GetDatePattern(FX_LOCALEDATETIMESUBCATEGORY eType) const102 WideString CXFA_NodeLocale::GetDatePattern(
103     FX_LOCALEDATETIMESUBCATEGORY eType) const {
104   switch (eType) {
105     case FX_LOCALEDATETIMESUBCATEGORY_Short:
106       return GetSymbol(XFA_Element::DatePatterns, L"short");
107     case FX_LOCALEDATETIMESUBCATEGORY_Medium:
108     case FX_LOCALEDATETIMESUBCATEGORY_Default:
109       return GetSymbol(XFA_Element::DatePatterns, L"med");
110     case FX_LOCALEDATETIMESUBCATEGORY_Full:
111       return GetSymbol(XFA_Element::DatePatterns, L"full");
112     case FX_LOCALEDATETIMESUBCATEGORY_Long:
113       return GetSymbol(XFA_Element::DatePatterns, L"long");
114   }
115   return WideString();
116 }
117 
GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY eType) const118 WideString CXFA_NodeLocale::GetTimePattern(
119     FX_LOCALEDATETIMESUBCATEGORY eType) const {
120   switch (eType) {
121     case FX_LOCALEDATETIMESUBCATEGORY_Short:
122       return GetSymbol(XFA_Element::TimePatterns, L"short");
123     case FX_LOCALEDATETIMESUBCATEGORY_Medium:
124     case FX_LOCALEDATETIMESUBCATEGORY_Default:
125       return GetSymbol(XFA_Element::TimePatterns, L"med");
126     case FX_LOCALEDATETIMESUBCATEGORY_Full:
127       return GetSymbol(XFA_Element::TimePatterns, L"full");
128     case FX_LOCALEDATETIMESUBCATEGORY_Long:
129       return GetSymbol(XFA_Element::TimePatterns, L"long");
130   }
131   return WideString();
132 }
133 
GetNumPattern(FX_LOCALENUMSUBCATEGORY eType) const134 WideString CXFA_NodeLocale::GetNumPattern(FX_LOCALENUMSUBCATEGORY eType) const {
135   return XFA_PatternToString(eType);
136 }
137 
GetNodeByName(CXFA_Node * pParent,const WideStringView & wsName) const138 CXFA_Node* CXFA_NodeLocale::GetNodeByName(CXFA_Node* pParent,
139                                           const WideStringView& wsName) const {
140   CXFA_Node* pChild = pParent ? pParent->GetFirstChild() : nullptr;
141   while (pChild) {
142     if (pChild->JSObject()->GetAttribute(XFA_Attribute::Name) == wsName)
143       return pChild;
144 
145     pChild = pChild->GetNextSibling();
146   }
147   return nullptr;
148 }
149 
GetSymbol(XFA_Element eElement,const WideStringView & symbol_type) const150 WideString CXFA_NodeLocale::GetSymbol(XFA_Element eElement,
151                                       const WideStringView& symbol_type) const {
152   CXFA_Node* pSymbols =
153       m_pLocale ? m_pLocale->GetChild<CXFA_Node>(0, eElement, false) : nullptr;
154   CXFA_Node* pSymbol = GetNodeByName(pSymbols, symbol_type);
155   return pSymbol ? pSymbol->JSObject()->GetContent(false) : WideString();
156 }
157 
GetCalendarSymbol(XFA_Element eElement,int index,bool bAbbr) const158 WideString CXFA_NodeLocale::GetCalendarSymbol(XFA_Element eElement,
159                                               int index,
160                                               bool bAbbr) const {
161   CXFA_CalendarSymbols* pCalendar =
162       m_pLocale ? m_pLocale->GetChild<CXFA_CalendarSymbols>(
163                       0, XFA_Element::CalendarSymbols, false)
164                 : nullptr;
165   if (!pCalendar)
166     return WideString();
167 
168   CXFA_Node* pNode = pCalendar->GetFirstChildByClass<CXFA_Node>(eElement);
169   for (; pNode; pNode = pNode->GetNextSameClassSibling<CXFA_Node>(eElement)) {
170     if (pNode->JSObject()->GetBoolean(XFA_Attribute::Abbr) == bAbbr) {
171       CXFA_Node* pSymbol =
172           pNode->GetChild<CXFA_Node>(index, XFA_Element::Unknown, false);
173       return pSymbol ? pSymbol->JSObject()->GetContent(false) : WideString();
174     }
175   }
176   return WideString();
177 }
178