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 "core/include/fxcrt/fx_xml.h"
8#include "xfa/src/fgas/src/fgas_base.h"
9#include "fx_localemgr.h"
10
11IFX_LocaleMgr* FX_LocaleMgr_Create(const FX_WCHAR* pszLocalPath,
12                                   FX_WORD wDefaultLCID) {
13  void* pPathHandle = FX_OpenFolder(pszLocalPath);
14  if (!pPathHandle) {
15    return NULL;
16  }
17  CFX_LocaleMgr* pLocaleMgr = new CFX_LocaleMgr(wDefaultLCID);
18  CFX_WideString wsFileName;
19  FX_BOOL bFolder = FALSE;
20  while (FX_GetNextFile(pPathHandle, wsFileName, bFolder)) {
21    if (!bFolder) {
22      if (wsFileName.GetLength() < 4) {
23        continue;
24      }
25      CFX_WideString wsExt = wsFileName.Right(4);
26      wsExt.MakeLower();
27      if (wsExt != L".xml") {
28        continue;
29      }
30      CFX_WideString wsFullPath(pszLocalPath);
31      wsFullPath += L"\\" + wsFileName;
32      IFX_FileRead* pRead = FX_CreateFileRead(wsFullPath);
33      if (!pRead) {
34        continue;
35      }
36      CXML_Element* pXmlLocale = CXML_Element::Parse(pRead);
37      pRead->Release();
38      CFX_ByteString bssp = pXmlLocale->GetNamespace();
39      if (bssp == "http://www.foxitsoftware.com/localization") {
40        CFX_WideString wsLCID = pXmlLocale->GetAttrValue("", "lcid");
41        wchar_t* pEnd = NULL;
42        FX_DWORD dwLCID = wcstol(wsLCID, &pEnd, 16);
43        if (pLocaleMgr->m_lcid2xml.GetValueAt((void*)(uintptr_t)dwLCID)) {
44          delete pXmlLocale;
45        } else {
46          pLocaleMgr->m_lcid2xml.SetAt((void*)(uintptr_t)dwLCID, pXmlLocale);
47        }
48      } else {
49        delete pXmlLocale;
50      }
51    }
52  }
53  FX_CloseFolder(pPathHandle);
54  return pLocaleMgr;
55}
56CFX_LocaleMgr::CFX_LocaleMgr(FX_WORD wDefLCID) : m_wDefLCID(wDefLCID) {}
57CFX_LocaleMgr::~CFX_LocaleMgr() {
58  FX_POSITION ps = m_lcid2locale.GetStartPosition();
59  while (ps) {
60    void* plcid;
61    IFX_Locale* pLocale = NULL;
62    m_lcid2locale.GetNextAssoc(ps, plcid, (void*&)pLocale);
63    pLocale->Release();
64  }
65  m_lcid2locale.RemoveAll();
66  ps = m_lcid2xml.GetStartPosition();
67  while (ps) {
68    void* plcid;
69    CXML_Element* pxml = NULL;
70    m_lcid2xml.GetNextAssoc(ps, plcid, (void*&)pxml);
71    delete pxml;
72  }
73  m_lcid2xml.RemoveAll();
74}
75FX_WORD CFX_LocaleMgr::GetDefLocaleID() {
76  return m_wDefLCID;
77}
78IFX_Locale* CFX_LocaleMgr::GetDefLocale() {
79  return GetLocale(m_wDefLCID);
80}
81IFX_Locale* CFX_LocaleMgr::GetLocale(FX_WORD lcid) {
82  IFX_Locale* pLocale =
83      (IFX_Locale*)m_lcid2locale.GetValueAt((void*)(uintptr_t)lcid);
84  if (!pLocale) {
85    CXML_Element* pxml =
86        (CXML_Element*)m_lcid2xml.GetValueAt((void*)(uintptr_t)lcid);
87    if (pxml) {
88      pLocale = IFX_Locale::Create(pxml);
89      m_lcid2locale.SetAt((void*)(uintptr_t)lcid, pLocale);
90    }
91  }
92  return pLocale;
93}
94IFX_Locale* CFX_LocaleMgr::GetLocaleByName(
95    const CFX_WideStringC& wsLocaleName) {
96  return NULL;
97}
98