Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
CaseDocumentLangTranslator.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* CaseDocumentLangTranslator.cc (C) 2000-2022 */
9/* */
10/* Class managing the translation of a dataset into another language. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/Collection.h"
15#include "arcane/utils/Enumerator.h"
16#include "arcane/utils/OStringStream.h"
17#include "arcane/utils/ArgumentException.h"
18#include "arcane/utils/ScopedPtr.h"
19
20#include "arcane/impl/CaseDocumentLangTranslator.h"
21
22#include "arcane/core/AbstractCaseDocumentVisitor.h"
23#include "arcane/core/CaseOptions.h"
24#include "arcane/core/CaseOptionService.h"
25#include "arcane/core/ICaseMng.h"
26#include "arcane/core/ICaseDocument.h"
27#include "arcane/core/CaseNodeNames.h"
28
29/*---------------------------------------------------------------------------*/
30/*---------------------------------------------------------------------------*/
31
32namespace Arcane
33{
34
35/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37
38class CaseDocumentLangTranslatorVisitor
40, public TraceAccessor
41{
42 public:
43
44 CaseDocumentLangTranslatorVisitor(ITraceMng* tm, const String& new_lang)
45 : TraceAccessor(tm)
46 {
47 m_new_lang = new_lang;
48 }
49
50 public:
51
52 void beginVisit(const ICaseOptions* opt) override
53 {
54 info() << "BeginOpt " << _getName(opt) << " {";
55 }
56 void endVisit(const ICaseOptions* opt) override
57 {
58 info() << "EndOptList " << opt->rootTagName() << " }";
59 }
60 void applyVisitor(const CaseOptionSimple* opt) override
61 {
62 info() << "SimpleOpt " << _getName(opt);
63 }
64 void applyVisitor(const CaseOptionMultiSimple* opt) override
65 {
66 info() << "MultiSimple " << _getName(opt);
67 }
68 void applyVisitor(const CaseOptionMultiExtended* opt) override
69 {
70 info() << "MultiExtended " << _getName(opt);
71 }
72 void applyVisitor(const CaseOptionExtended* opt) override
73 {
74 info() << "Extended " << _getName(opt);
75 }
76 void applyVisitor(const CaseOptionMultiEnum* opt) override
77 {
78 info() << "MultiEnum " << _getName(opt);
79 info() << "WARNING: MultiEnum not handled in translator";
80 }
81 void applyVisitor(const CaseOptionEnum* opt) override
82 {
83 info() << "Enum " << _getName(opt);
84 _manageEnum(opt);
85 }
86 void beginVisit(const CaseOptionServiceImpl* opt) override
87 {
88 info() << "BeginService " << _getName(opt);
89 }
90 void endVisit(const CaseOptionServiceImpl* opt) override
91 {
92 info() << "EndService " << _getName(opt);
93 }
94 void beginVisit(const CaseOptionMultiServiceImpl* opt, Integer index) override
95 {
96 info() << "BeginMultiService " << _getName(opt) << " index=" << index;
97 }
98 void endVisit(const CaseOptionMultiServiceImpl* opt, Integer index) override
99 {
100 info() << "EndMultiService " << _getName(opt) << " index=" << index;
101 }
102 String _getName(const CaseOptionBase* opt)
103 {
104 String full_xpath = opt->rootElement().xpathFullName();
105 String name = opt->name();
106 String new_name = opt->translatedName(m_new_lang);
107
108 if (name != new_name)
109 m_stream() << full_xpath << "/" << name << ":" << new_name << '\n';
110 return name;
111 }
112 String _getName(const ICaseOptions* opt)
113 {
114 String full_xpath = opt->configList()->rootElement().xpathFullName();
115 String name = opt->rootTagName();
116 String new_name = opt->translatedName(m_new_lang);
117
118 if (name != new_name)
119 m_stream() << full_xpath << ":" << new_name << '\n';
120 return name;
121 }
122 void _manageEnum(const CaseOptionEnum* opt)
123 {
124 // Nothing to convert if no element is associated with the option
125 // or if the value is invalid.
126 if (!opt->isPresent())
127 return;
128 if (!opt->hasValidValue())
129 return;
130
131 int v = opt->enumValueAsInt();
132 String new_name = opt->enumValues()->nameOfValue(v, m_new_lang);
133 m_stream() << opt->xpathFullName() << ":text#" << new_name << '\n';
134 }
135
136 public:
137
138 void printAll()
139 {
140 info() << "ALL: " << m_stream.str();
141 }
142
143 public:
144
145 String convertString() { return m_stream.str(); }
146
147 private:
148
149 OStringStream m_stream;
150 String m_new_lang;
151};
152
153/*---------------------------------------------------------------------------*/
154/*---------------------------------------------------------------------------*/
155
156CaseDocumentLangTranslator::
157CaseDocumentLangTranslator(ITraceMng* tm)
158: TraceAccessor(tm)
159{
160}
161
162/*---------------------------------------------------------------------------*/
163/*---------------------------------------------------------------------------*/
164
165CaseDocumentLangTranslator::
166~CaseDocumentLangTranslator()
167{
168}
169
170/*---------------------------------------------------------------------------*/
171/*---------------------------------------------------------------------------*/
172
173void CaseDocumentLangTranslator::
174build()
175{
176}
177
178/*---------------------------------------------------------------------------*/
179/*---------------------------------------------------------------------------*/
180
181String CaseDocumentLangTranslator::
182translate(ICaseMng* cm, const String& new_lang)
183{
184 if (new_lang.null())
185 throw ArgumentException(A_FUNCINFO, "Invalid value for langage");
186 CaseDocumentLangTranslatorVisitor my_visitor(traceMng(), new_lang);
187 CaseOptionsCollection opts = cm->blocks();
188 for (CaseOptionsCollection::Enumerator i(opts); ++i;) {
189 ICaseOptions* o = *i;
190 info() << " OptName=" << o->rootTagName();
191 o->visit(&my_visitor);
192 }
193 my_visitor.printAll();
194
195 ICaseDocument* cd = cm->caseDocument();
196 CaseNodeNames* current_cnn = cd->caseNodeNames();
197 ScopedPtrT<CaseNodeNames> cnn{ new CaseNodeNames(new_lang) };
198
199 // NOTE: These conversions depend on CaseDocument and must be
200 // updated if the latter changes (as well as CaseNodeNames)
201
202 _addConvert(cd->fragment()->rootElement(), cnn->root);
203 _addConvert(cd->timeloopElement(), cnn->timeloop);
204 _addConvert(cd->titleElement(), cnn->title);
205 _addConvert(cd->descriptionElement(), cnn->description);
206 _addConvert(cd->modulesElement(), cnn->modules);
207
208 String slash = "/";
209 const XmlNodeList& mesh_elems = cd->meshElements();
210 for (Integer i = 0, n = mesh_elems.size(); i < n; ++i) {
211 XmlNode xnode(mesh_elems.node(i));
212 _addConvert(xnode, cnn->mesh);
213 _addConvert(xnode.child(current_cnn->mesh_file), cnn->mesh_file);
214 }
215
216 _addConvert(cd->functionsElement(), cnn->functions);
217
218 // TODO: Handle TiedInterface + CaseFunctions + following Attributes:
219 // TODO: Use JSON format to output conversion information.
220
221 /*m_user_class = m_root_elem.attrValue(cnn->user_class);
222 m_code_name = m_root_elem.attrValue(cnn->code_name);
223 m_code_version = m_root_elem.attrValue(cnn->code_version);
224 m_code_unit_system = m_root_elem.attrValue(cnn->code_unit);*/
225
226 return my_visitor.convertString() + m_global_convert_string;
227}
228
229/*---------------------------------------------------------------------------*/
230/*---------------------------------------------------------------------------*/
231
232void CaseDocumentLangTranslator::
233_addConvert(XmlNode node, const String& new_name)
234{
235 if (!node.null())
236 m_global_convert_string = m_global_convert_string + node.xpathFullName() + ":" + new_name + "\n";
237}
238
239/*---------------------------------------------------------------------------*/
240/*---------------------------------------------------------------------------*/
241
242} // End namespace Arcane
243
244/*---------------------------------------------------------------------------*/
245/*---------------------------------------------------------------------------*/
Abstract visitor for a scalar data point.
Base class for a data set option.
String name() const
Returns the option name corresponding to the data set language.
String translatedName(const String &lang) const
Name of the option in the language lang. Returns name() if no translation exists.
XmlNode rootElement() const
Returns the root element of the DOM.
String nameOfValue(int value, const String &lang) const
Returns the name corresponding to the value value for the language lang.
Enumerated data set option.
Option for the extended type dataset.
Multi-enumeration data set option.
Option for the extended list of types dataset.
Base class for a service option that can appear multiple times.
Base class for implementing options using services.
Base class for simple options (single value).
String xpathFullName() const
Full name in the format provided by the XPath standard.
bool hasValidValue() const
Indicates if the option has an invalid value.
bool isPresent() const
Returns true if the option is present.
Case manager interface.
Definition ICaseMng.h:57
virtual XmlNode rootElement() const =0
Returns the element associated with this options list.
Interface for a list of data set options.
virtual String rootTagName() const =0
Name of the element in the data set language.
virtual String translatedName(const String &lang) const =0
Name in the language lang of the option. Returns rootTagTrueName() if no translation exists.
Output stream linked to a String.
TraceAccessor(ITraceMng *m)
Constructs an accessor via the trace manager m.
TraceMessage info() const
Flow for an information message.
ITraceMng * traceMng() const
Trace manager.
Node of a DOM tree.
Definition XmlNode.h:51
String xpathFullName() const
XPath name of the node with its ancestors.
Definition XmlNode.cc:152
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
Collection< ICaseOptions * > CaseOptionsCollection
Collection of dataset options.