OpenMS
SemanticValidator.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg $
6 // $Authors: Marc Sturm, Andreas Bertsch $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/FORMAT/XMLFile.h>
14 #include <map>
15 
16 
17 namespace OpenMS
18 {
19  class ControlledVocabulary;
20  namespace Internal
21  {
22 
29  class OPENMS_DLLAPI SemanticValidator :
30  protected Internal::XMLHandler,
31  public Internal::XMLFile
32  {
33 public:
40  SemanticValidator(const CVMappings & mapping, const ControlledVocabulary & cv);
41 
43  ~SemanticValidator() override;
44 
46  struct CVTerm
47  {
51  bool has_value{};
53  bool has_unit_accession{};
55  bool has_unit_name{};
56  };
57 
69  bool validate(const String & filename, StringList & errors, StringList & warnings);
70 
72  bool locateTerm(const String & path, const CVTerm & parsed_term) const;
73 
75  void setTag(const String & tag);
76 
78  void setAccessionAttribute(const String & accession);
79 
81  void setNameAttribute(const String & name);
82 
84  void setValueAttribute(const String & value);
85 
94  void setCheckTermValueTypes(bool check);
95 
103  void setCheckUnits(bool check);
104 
106  void setUnitAccessionAttribute(const String & accession);
107 
109  void setUnitNameAttribute(const String & name);
110 
111 protected:
112 
113  // Docu in base class
114  void startElement(const XMLCh * const /*uri*/, const XMLCh * const /*local_name*/, const XMLCh * const qname, const xercesc::Attributes & attributes) override;
115 
116  // Docu in base class
117  void endElement(const XMLCh * const /*uri*/, const XMLCh * const /*local_name*/, const XMLCh * const qname) override;
118 
119  // Docu in base class
120  void characters(const XMLCh * const chars, const XMLSize_t /*length*/) override;
121 
123  virtual String getPath_(UInt remove_from_end = 0) const;
124 
126  virtual void getCVTerm_(const xercesc::Attributes & attributes, CVTerm & parsed_term);
127 
128  //~ forward dekl. of a inner struct/class not possible in C++ - or our Library is overtemplated
129  //~ /// make a SemanticValidator::CVTerm from a ControlledVocabulary::CVTerm (without any value or unit), needed for writing only cvs at the right places in the xml (i.e. with cvmapping)
130  //~ virtual void makeCVTerm_(const ControlledVocabulary::CVTerm & lc, CVTerm & parsed_term);
131 
133  virtual void handleTerm_(const String & path, const CVTerm & parsed_term);
134 
137 
140 
143 
146 
149 
151  std::map<String, std::vector<CVMappingRule> > rules_;
152 
156  std::map<String, std::map<String, std::map<String, UInt> > > fulfilled_;
157 
158 
160 
170 
171 private:
172 
175 
178 
181 
182  };
183 
184  } // namespace Internal
185 
186 } // namespace OpenMS
187 
188 
Representation of controlled vocabulary mapping rules (for PSI formats)
Definition: CVMappings.h:31
Representation of a controlled vocabulary.
Definition: ControlledVocabulary.h:29
Semantically validates XML files using CVMappings and a ControlledVocabulary.
Definition: SemanticValidator.h:32
bool check_term_value_types_
Definition: SemanticValidator.h:167
virtual void getCVTerm_(const xercesc::Attributes &attributes, CVTerm &parsed_term)
Parses the CV term accession (required), name (required) and value (optional) from the XML attributes...
String unit_name
Definition: SemanticValidator.h:54
SemanticValidator(const SemanticValidator &rhs)
Not implemented.
SemanticValidator(const CVMappings &mapping, const ControlledVocabulary &cv)
Constructor.
void setUnitNameAttribute(const String &name)
Sets the name of the unit name attribute (default: 'unitName')
virtual void handleTerm_(const String &path, const CVTerm &parsed_term)
Handling of the term.
const ControlledVocabulary & cv_
Reference to the CVs.
Definition: SemanticValidator.h:139
String name_att_
Definition: SemanticValidator.h:163
String value
Definition: SemanticValidator.h:50
String unit_name_att_
Definition: SemanticValidator.h:166
void setValueAttribute(const String &value)
Sets the name of the attribute for accessions in the CV parameter tag name (default: 'value')
void setCheckUnits(bool check)
Set if CV term units should be check (disabled by default)
bool locateTerm(const String &path, const CVTerm &parsed_term) const
Checks if a CVTerm is allowed in a given path.
String unit_accession_att_
Definition: SemanticValidator.h:165
String unit_accession
Definition: SemanticValidator.h:52
StringList warnings_
Validation warnings.
Definition: SemanticValidator.h:145
std::map< String, std::map< String, std::map< String, UInt > > > fulfilled_
Definition: SemanticValidator.h:156
void setUnitAccessionAttribute(const String &accession)
Sets the name of the unit accession attribute (default: 'unitAccession')
String cv_tag_
Definition: SemanticValidator.h:161
StringList errors_
Validation errors.
Definition: SemanticValidator.h:142
void setCheckTermValueTypes(bool check)
Set if CV term value types should be check (enabled by default)
void setTag(const String &tag)
Sets the CV parameter tag name (default: 'cvParam')
StringList open_tags_
List of open tags.
Definition: SemanticValidator.h:148
String name
Definition: SemanticValidator.h:49
void startElement(const XMLCh *const, const XMLCh *const, const XMLCh *const qname, const xercesc::Attributes &attributes) override
bool validate(const String &filename, StringList &errors, StringList &warnings)
Semantically validates an XML file.
String accession
Definition: SemanticValidator.h:48
SemanticValidator & operator=(const SemanticValidator &rhs)
Not implemented.
bool check_units_
Definition: SemanticValidator.h:168
void characters(const XMLCh *const chars, const XMLSize_t) override
void endElement(const XMLCh *const, const XMLCh *const, const XMLCh *const qname) override
std::map< String, std::vector< CVMappingRule > > rules_
Rules (location => rule)
Definition: SemanticValidator.h:151
String value_att_
Definition: SemanticValidator.h:164
void setAccessionAttribute(const String &accession)
Sets the name of the attribute for accessions in the CV parameter tag name (default: 'accession')
virtual String getPath_(UInt remove_from_end=0) const
Returns the current element path.
const CVMappings & mapping_
Reference to the mappings.
Definition: SemanticValidator.h:136
void setNameAttribute(const String &name)
Sets the name of the attribute for accessions in the CV parameter tag name (default: 'name')
~SemanticValidator() override
Destructor.
String accession_att_
Definition: SemanticValidator.h:162
Representation of a parsed CV term.
Definition: SemanticValidator.h:47
Base class for loading/storing XML files that have a handler derived from XMLHandler.
Definition: XMLFile.h:23
Base class for XML handlers.
Definition: XMLHandler.h:300
A more convenient string class.
Definition: String.h:34
unsigned int UInt
Unsigned integer type.
Definition: Types.h:68
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:44
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22