NCBI C++ ToolKit
moduleset.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: moduleset.cpp 79463 2017-09-12 17:04:06Z gouriano $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author: Eugene Vasilchenko
27 *
28 * File Description:
29 * Set of modules: equivalent of ASN.1 source file
30 *
31 */
32 
33 #include <ncbi_pch.hpp>
34 #include <corelib/ncbiapp.hpp>
35 #include <corelib/ncbiargs.hpp>
36 #include <corelib/ncbifile.hpp>
37 #include <typeinfo>
38 #include "moduleset.hpp"
39 #include "module.hpp"
40 #include "type.hpp"
41 #include "exceptions.hpp"
42 #include "fileutil.hpp"
43 #include <serial/error_codes.hpp>
44 
45 
46 #define NCBI_USE_ERRCODE_X Serial_Modules
47 
49 
50 CFileModules::CFileModules(const string& name)
51  : m_SourceFileName(name)
52 {
53 }
54 
56 {
57  module->SetModuleContainer(this);
58  CDataTypeModule*& mptr = m_ModulesByName[module->GetName()];
59  if ( mptr ) {
60  ERR_POST_X(4, GetSourceFileName() << ": duplicate module: " <<
61  module->GetName());
62  }
63  else {
64  mptr = module.get();
65  m_Modules.push_back(module);
66  }
67 }
68 
69 bool CFileModules::Check(void) const
70 {
71  bool ok = true;
72  ITERATE ( TModules, mi, m_Modules ) {
73  if ( !(*mi)->Check() )
74  ok = false;
75  }
76  return ok;
77 }
78 
79 bool CFileModules::CheckNames(void) const
80 {
81  bool ok = true;
82  ITERATE ( TModules, mi, m_Modules ) {
83  if ( !(*mi)->CheckNames() )
84  ok = false;
85  }
86  return ok;
87 }
88 
89 void CFileModules::PrintSampleDEF(const string& rootdir) const
90 {
91  ITERATE ( TModules, mi, m_Modules ) {
92  string fileName = MakeAbsolutePath(
93  Path(rootdir, Path(GetFileNamePrefix(), (*mi)->GetName() + "._sample_def")));
94  CNcbiOfstream out(fileName.c_str());
95  (*mi)->PrintSampleDEF(out);
96  if ( !out )
97  ERR_POST_X(5, Fatal << "Cannot write to file "<<fileName);
98  }
99 }
100 
102 {
103  ITERATE ( TModules, mi, m_Modules ) {
105  (*mi)->PrintASN(out);
106  }
108 }
109 
111 {
112  ITERATE ( TModules, mi, m_Modules ) {
113  out << "M," << mi->get()->GetSourceLine() << ',';
115  << (*mi)->GetName();
116  (*mi)->PrintSpecDump(out);
117  }
118 }
119 
121 {
122  ITERATE ( TModules, mi, m_Modules ) {
123  (*mi)->PrintJSONSchema(out);
124  }
125 }
126 
127 // XML schema generator submitted by
128 // Marc Dumontier, Blueprint initiative, dumontier@mshri.on.ca
130 {
132  ITERATE ( TModules, mi, m_Modules ) {
133  (*mi)->PrintXMLSchema(out);
134  }
136  EndXMLSchema(out);
137 }
138 
139 void CFileModules::GetRefInfo(list<string>& info) const
140 {
141  info.clear();
142  string s, h("::DATATOOL:: ");
143  s = h + "Generated from \"" + GetSourceFileName() + "\"";
144  info.push_back(s);
145  s = h + "by application DATATOOL version ";
147  info.push_back(s);
148  s = h + "on " + CTime(CTime::eCurrent).AsString();
149  info.push_back(s);
150 }
151 
153 {
154  list<string> info;
155  GetRefInfo(info);
156  out << "-- ============================================\n";
157  ITERATE(list<string>, i, info) {
158  out << "-- " << *i << "\n";
159  }
160  out << "-- ============================================\n\n";
161 }
162 
164 {
165  list<string> info;
166  GetRefInfo(info);
167  out << "<!-- ============================================\n";
168  ITERATE(list<string>, i, info) {
169  out << " " << *i << "\n";
170  }
171  out << " ============================================ -->\n\n";
172 }
173 
175 {
176  ITERATE ( TModules, mi, m_Modules ) {
178  (*mi)->PrintDTD(out);
179  }
181 }
182 
184 {
185  ITERATE ( TModules, mi, m_Modules ) {
186  string fileNameBase = MakeAbsolutePath(
187  (*mi)->GetDTDFileNameBase() + (*mi)->GetModuleFileSuffix());
188  {
189  string fileName = fileNameBase + ".mod.dtd";
190  CNcbiOfstream out(fileName.c_str());
192  (*mi)->PrintDTD(out);
193  if ( !out )
194  ERR_POST_X(5, Fatal << "Cannot write to file "<<fileName);
195  }
196  {
197  string fileName = fileNameBase + ".dtd";
198  CNcbiOfstream out(fileName.c_str());
200  (*mi)->PrintDTDModular(out);
201  if ( !out )
202  ERR_POST_X(6, Fatal << "Cannot write to file "<<fileName);
203  }
204  }
205 }
206 
208 {
209  ITERATE ( TModules, mi, m_Modules ) {
210  string fileNameBase = MakeAbsolutePath(
211  (*mi)->GetDTDFileNameBase() + (*mi)->GetModuleFileSuffix());
212  {
213  string fileName = fileNameBase + ".mod.xsd";
214  CNcbiOfstream out(fileName.c_str());
215  if ( !out )
216  ERR_POST_X(7, Fatal << "Cannot write to file "<<fileName);
218  (*mi)->PrintXMLSchema(out);
219  EndXMLSchema(out);
220  }
221  {
222  string fileName = fileNameBase + ".xsd";
223  CNcbiOfstream out(fileName.c_str());
224  if ( !out )
225  ERR_POST_X(8, Fatal << "Cannot write to file "<<fileName);
227  (*mi)->PrintXMLSchemaModular(out);
228  EndXMLSchema(out);
229  }
230  }
231 }
232 
234 {
235  string nsName("http://www.ncbi.nlm.nih.gov");
236  string nsNcbi(nsName);
237  string elementForm("qualified");
238  string attributeForm("unqualified");
239  if (!m_Modules.empty()) {
240  const CDataTypeModule::TDefinitions& defs =
241  m_Modules.front()->GetDefinitions();
242  if (!defs.empty()) {
243  const string& ns = defs.front().second->GetNamespaceName();
244  if (!ns.empty()) {
245  nsName = ns;
246  }
247  if (defs.front().second->IsNsQualified() == eNSUnqualified) {
248  elementForm = "unqualified";
249  }
250  }
251  }
252  const CArgs& args = CNcbiApplication::Instance()->GetArgs();
253  if ( const CArgValue& px_ns = args["xmlns"] ) {
254  nsName = px_ns.AsString();
255  }
256  out << "<?xml version=\"1.0\" ?>\n";
258  out << "<xs:schema\n"
259  << " xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"\n"
260  << " xmlns:ncbi=\"" << nsNcbi << "\"\n";
261  if (!nsName.empty()) {
262  out << " xmlns=\"" << nsName << "\"\n"
263  << " targetNamespace=\"" << nsName << "\"\n";
264  }
265  out << " elementFormDefault=\"" << elementForm << "\"\n"
266  << " attributeFormDefault=\"" << attributeForm << "\">\n\n";
267 }
268 
270 {
271  out << "</xs:schema>\n";
272 }
273 
274 const string& CFileModules::GetSourceFileName(void) const
275 {
276  return m_SourceFileName;
277 }
278 
280 {
282  if ( m_PrefixFromSourceFileName.empty() ) {
285  // path absent or non local
288  }
289  }
290  _TRACE("file " << m_SourceFileName << ": \"" << m_PrefixFromSourceFileName << "\"");
291  if ( UseAllFileNamePrefixes() ) {
294  }
295  else {
297  }
298  }
300 }
301 
302 CDataType* CFileModules::ExternalResolve(const string& moduleName,
303  const string& typeName,
304  bool allowInternal) const
305 {
306  // find module definition
308  if ( mi == m_ModulesByName.end() ) {
309  // no such module
311  "module not found: "+moduleName+" for type "+typeName);
312  }
313  return mi->second->ExternalResolve(typeName, allowInternal);
314 }
315 
317  bool allowInternal) const
318 {
319  CResolvedTypeSet types(typeName);
320  ITERATE ( TModules, i, m_Modules ) {
321  try {
322  types.Add((*i)->ExternalResolve(typeName, allowInternal));
323  }
324  catch ( CAmbiguiousTypes& exc ) {
325  types.Add(exc);
326  }
327  catch ( CNotFoundException& /* ignored */) {
328  }
329  }
330  return types.GetType();
331 }
332 
334 {
335  ITERATE ( TModules, i, m_Modules ) {
336  (*i)->CollectAllTypeinfo(types);
337  }
338 }
339 
341 {
342  moduleSet->SetModuleContainer(this);
343  m_ModuleSets.push_back(moduleSet);
344 }
345 
346 void CFileSet::PrintSampleDEF(const string& rootdir) const
347 {
349  (*i)->PrintSampleDEF(rootdir);
350  }
351 }
352 
354 {
356  (*i)->PrintASN(out);
357  }
358 }
359 
361 {
363  (*i)->PrintSpecDump(out);
364  }
365 }
366 
368 {
370  (*i)->PrintJSONSchema(out);
371  }
372 }
373 
374 // XML schema generator submitted by
375 // Marc Dumontier, Blueprint initiative, dumontier@mshri.on.ca
377 {
379  (*i)->PrintXMLSchema(out);
380  }
381 }
382 
384 {
385 #if 0
386  out <<
387  "<!-- ======================== -->\n"
388  "<!-- NCBI DTD -->\n"
389  "<!-- NCBI ASN.1 mapped to XML -->\n"
390  "<!-- ======================== -->\n"
391  "\n"
392  "<!-- Entities used to give specificity to #PCDATA -->\n"
393  "<!ENTITY % INTEGER '#PCDATA'>\n"
394  "<!ENTITY % ENUM 'EMPTY'>\n"
395  "<!ENTITY % BOOLEAN 'EMPTY'>\n"
396  "<!ENTITY % NULL 'EMPTY'>\n"
397  "<!ENTITY % REAL '#PCDATA'>\n"
398  "<!ENTITY % OCTETS '#PCDATA'>\n"
399  "<!-- ============================================ -->\n"
400  "\n";
401 #endif
403  (*i)->PrintDTD(out);
404  }
405 }
406 
408 {
410  (*i)->PrintDTDModular();
411  }
412 }
413 
415 {
417  (*i)->PrintXMLSchemaModular();
418  }
419 }
420 
421 CDataType* CFileSet::ExternalResolve(const string& module, const string& name,
422  bool allowInternal) const
423 {
424  CResolvedTypeSet types(module, name);
426  try {
427  types.Add((*i)->ExternalResolve(module, name, allowInternal));
428  }
429  catch ( CAmbiguiousTypes& exc ) {
430  types.Add(exc);
431  }
432  catch ( CNotFoundException& /* ignored */) {
433  }
434  }
435  return types.GetType();
436 }
437 
439  bool allowInternal) const
440 {
441  CResolvedTypeSet types(name);
443  try {
444  types.Add((*i)->ResolveInAnyModule(name, allowInternal));
445  }
446  catch ( CAmbiguiousTypes& exc ) {
447  types.Add(exc);
448  }
449  catch ( CNotFoundException& /* ignored */) {
450  }
451  }
452  return types.GetType();
453 }
454 
455 bool CFileSet::Check(void) const
456 {
457  bool ok = true;
458  ITERATE ( TModuleSets, mi, m_ModuleSets ) {
459  if ( !(*mi)->Check() )
460  ok = false;
461  }
462  return ok;
463 }
464 
465 bool CFileSet::CheckNames(void) const
466 {
467  bool ok = true;
468  ITERATE ( TModuleSets, mi, m_ModuleSets ) {
469  if ( !(*mi)->CheckNames() )
470  ok = false;
471  }
472  return ok;
473 }
474 
476 {
478  (*i)->CollectAllTypeinfo(types);
479  }
480 }
481 
AutoPtr –.
Definition: ncbimisc.hpp:401
CArgValue –.
Definition: ncbiargs.hpp:184
CArgs –.
Definition: ncbiargs.hpp:379
CNcbiOstream & PrintASN(CNcbiOstream &out, int indent, int flags=0) const
Definition: comments.cpp:156
CNcbiOstream & PrintDTD(CNcbiOstream &out, int flags=0) const
Definition: comments.cpp:121
@ eMultiline
Definition: comments.hpp:57
list< pair< string, AutoPtr< CDataType > > > TDefinitions
Definition: module.hpp:70
CDirEntry –.
Definition: ncbifile.hpp:262
void PrintXMLSchemaModular(void) const
Definition: moduleset.cpp:207
void PrintASN(CNcbiOstream &out) const
Definition: moduleset.cpp:101
void PrintXMLSchema(CNcbiOstream &out) const
Definition: moduleset.cpp:129
list< AutoPtr< CDataTypeModule > > TModules
Definition: moduleset.hpp:55
void PrintJSONSchema(CNcbiOstream &out) const
Definition: moduleset.cpp:120
void PrintDTDModular(void) const
Definition: moduleset.cpp:183
bool CheckNames(void) const
Definition: moduleset.cpp:79
CDataType * ExternalResolve(const string &moduleName, const string &typeName, bool allowInternal=false) const
Definition: moduleset.cpp:302
virtual const string & GetSourceFileName(void) const override
Definition: moduleset.cpp:274
void PrintSampleDEF(const string &rootdir) const
Definition: moduleset.cpp:89
void PrintXMLRefInfo(CNcbiOstream &out) const
Definition: moduleset.cpp:163
string m_PrefixFromSourceFileName
Definition: moduleset.hpp:107
void EndXMLSchema(CNcbiOstream &out) const
Definition: moduleset.cpp:269
void PrintASNRefInfo(CNcbiOstream &out) const
Definition: moduleset.cpp:152
void CollectAllTypeinfo(set< TTypeInfo > &types) const
Definition: moduleset.cpp:333
void AddModule(const AutoPtr< CDataTypeModule > &module)
Definition: moduleset.cpp:55
CComments m_LastComments
Definition: moduleset.hpp:106
CFileModules(const string &fileName)
Definition: moduleset.cpp:50
TModulesByName m_ModulesByName
Definition: moduleset.hpp:104
virtual string GetFileNamePrefix(void) const override
Definition: moduleset.cpp:279
TModules m_Modules
Definition: moduleset.hpp:103
void PrintSpecDump(CNcbiOstream &out) const
Definition: moduleset.cpp:110
void PrintDTD(CNcbiOstream &out) const
Definition: moduleset.cpp:174
string m_SourceFileName
Definition: moduleset.hpp:105
void BeginXMLSchema(CNcbiOstream &out) const
Definition: moduleset.cpp:233
void GetRefInfo(list< string > &info) const
Definition: moduleset.cpp:139
CDataType * ResolveInAnyModule(const string &fullName, bool allowInternal=false) const
Definition: moduleset.cpp:316
bool Check(void) const
Definition: moduleset.cpp:69
void PrintDTD(CNcbiOstream &out) const
Definition: moduleset.cpp:383
void CollectAllTypeinfo(set< TTypeInfo > &types) const
Definition: moduleset.cpp:475
void PrintSpecDump(CNcbiOstream &out) const
Definition: moduleset.cpp:360
void PrintJSONSchema(CNcbiOstream &out) const
Definition: moduleset.cpp:367
void AddFile(const AutoPtr< CFileModules > &moduleSet)
Definition: moduleset.cpp:340
void PrintXMLSchema(CNcbiOstream &out) const
Definition: moduleset.cpp:376
TModuleSets m_ModuleSets
Definition: moduleset.hpp:149
list< AutoPtr< CFileModules > > TModuleSets
Definition: moduleset.hpp:115
void PrintDTDModular(void) const
Definition: moduleset.cpp:407
CDataType * ResolveInAnyModule(const string &fullName, bool allowInternal=false) const
Definition: moduleset.cpp:438
void PrintXMLSchemaModular(void) const
Definition: moduleset.cpp:414
bool Check(void) const
Definition: moduleset.cpp:455
void PrintSampleDEF(const string &rootdir) const
Definition: moduleset.cpp:346
bool CheckNames(void) const
Definition: moduleset.cpp:465
void PrintASN(CNcbiOstream &out) const
Definition: moduleset.cpp:353
CDataType * ExternalResolve(const string &moduleName, const string &typeName, bool allowInternal=false) const
Definition: moduleset.cpp:421
virtual string GetFileNamePrefix(void) const
Definition: mcontainer.cpp:74
const CModuleContainer & GetModuleContainer(void) const
Definition: mcontainer.cpp:54
bool UseAllFileNamePrefixes(void) const
Definition: mcontainer.hpp:72
bool MakeFileNamePrefixFromSourceFileName(void) const
Definition: mcontainer.hpp:62
static CNcbiApplication * Instance(void)
Singleton method.
Definition: ncbiapp.cpp:244
CTime –.
Definition: ncbitime.hpp:296
const_iterator end() const
Definition: map.hpp:152
const_iterator find(const key_type &key) const
Definition: map.hpp:153
Definition: set.hpp:45
std::ofstream out("events_result.xml")
main entry point for tests
string DirName(const string &path)
Definition: fileutil.cpp:276
string Path(const string &dir, const string &file)
Definition: fileutil.cpp:243
string MakeAbsolutePath(const string &path)
Definition: fileutil.cpp:233
bool IsLocalPath(const string &path)
Definition: fileutil.cpp:198
virtual const CArgs & GetArgs(void) const
Get parsed command line arguments.
Definition: ncbiapp.cpp:285
CVersionInfo GetVersion(void) const
Get the program version information.
Definition: ncbiapp.cpp:1164
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
element_type * get(void) const
Get pointer.
Definition: ncbimisc.hpp:469
#define _TRACE(message)
Definition: ncbidbg.hpp:122
#define ERR_POST_X(err_subcode, message)
Error posting with default error code and given error subcode.
Definition: ncbidiag.hpp:550
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
void Fatal(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1209
string GetName(void) const
Get the base entry name with extension (if any).
Definition: ncbifile.hpp:3916
@ eNSUnqualified
Definition: serialdef.hpp:200
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ofstream CNcbiOfstream
Portable alias for ofstream.
Definition: ncbistre.hpp:500
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
string AsString(const CTimeFormat &format=kEmptyStr, TSeconds out_tz=eCurrentTimeZone) const
Transform time to string.
Definition: ncbitime.cpp:1511
@ eCurrent
Use current time. See also CCurrentTime.
Definition: ncbitime.hpp:300
virtual string Print(void) const
Print version information.
Definition: version.cpp:120
Definition of all error codes used in serial libraries (xser.lib, xcser.lib).
int i
static MDB_envinfo info
Definition: mdb_load.c:37
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
Defines command line argument related classes.
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
static const struct type types[]
Definition: type.c:22
Modified on Wed Dec 06 07:15:59 2023 by modify_doxy.py rev. 669887