LibMusicXML  3.18
xml.h
1 /*
2  MusicXML Library
3  Copyright (C) Grame 2006-2013
4 
5  This Source Code Form is subject to the terms of the Mozilla Public
6  License, v. 2.0. If a copy of the MPL was not distributed with this
7  file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9  Grame Research Laboratory, 11, cours de Verdun Gensoul 69002 Lyon - France
10  research@grame.fr
11 */
12 
13 #ifndef __xml__
14 #define __xml__
15 
16 #include <string>
17 #include <vector>
18 
19 #ifdef WIN32
20 #pragma warning (disable : 4275)
21 #endif
22 
23 #include "exports.h"
24 #include "ctree.h"
25 #include "smartpointer.h"
26 
27 namespace MusicXML2
28 {
29 
35 class xmlelement;
36 class xmlattribute;
37 
38 typedef SMARTP<xmlattribute> Sxmlattribute;
39 typedef SMARTP<xmlelement> Sxmlelement;
40 
46 //______________________________________________________________________________
47 class EXP xmlattribute : public smartable {
49  std::string fName;
51  std::string fValue;
52  protected:
53  xmlattribute() {}
54  virtual ~xmlattribute() {}
55  public:
56  static SMARTP<xmlattribute> create();
57 
58  void setName (const std::string& name);
59  void setValue (const std::string& value);
60  void setValue (long value);
61  void setValue (int value);
62  void setValue (float value);
63 
64  const std::string& getName () const { return fName; }
66  const std::string& getValue () const { return fValue; }
68  operator int () const;
70  operator long () const;
72  operator float () const;
73 };
74 
75 
84 //______________________________________________________________________________
85 class EXP xmlelement : public ctree<xmlelement>, public visitable
86 {
87  private:
89  std::string fName;
91  std::string fValue;
93  std::vector<Sxmlattribute> fAttributes;
94 
95  protected:
96  // the element type
97  int fType;
98  // the input line number for messages to the user
99  int fInputLineNumber;
100 
101  xmlelement (int inputLineNumber) : fType(0), fInputLineNumber(inputLineNumber) {}
102  virtual ~xmlelement() {}
103 
104  public:
105  typedef ctree<xmlelement>::iterator iterator;
106 
107  static SMARTP<xmlelement> create (int inputLineNumber);
108 
109  virtual void acceptIn (basevisitor& visitor);
110  virtual void acceptOut (basevisitor& visitor);
111 
112  int getInputLineNumber () { return fInputLineNumber; }
113 
114  void setValue (unsigned long value);
115  void setValue (long value);
116  void setValue (int value);
117  void setValue (float value);
118  void setValue (const std::string& value);
119  void setName (const std::string& name);
120 
121  int getType () const { return fType; }
122  const std::string& getName () const { return fName; }
123 
125  const std::string& getValue () const { return fValue; }
126 
128  operator long () const;
130  operator int () const;
132  operator float () const;
134  bool operator ==(const xmlelement& elt) const;
135  bool operator !=(const xmlelement& elt) const { return !(*this == elt); }
136 
138  long add (const Sxmlattribute& attr);
139 
140  // getting information about attributes
141  const std::vector<Sxmlattribute>& attributes() const { return fAttributes; }
142  const Sxmlattribute getAttribute (const std::string& attrname) const;
143  const std::string getAttributeValue (const std::string& attrname) const;
144  long getAttributeLongValue (const std::string& attrname, long defaultvalue) const;
145  int getAttributeIntValue (const std::string& attrname, int defaultvalue) const;
146  float getAttributeFloatValue (const std::string& attrname, float defaultvalue) const;
147 
148  // finding sub elements by type
149  ctree<xmlelement>::iterator find(int type);
151 
152  // getting sub elements values
153  const std::string getValue (int subElementType);
154  int getIntValue (int subElementType, int defaultvalue);
155  long getLongValue (int subElementType, long defaultvalue);
156  float getFloatValue (int subElementType, float defaultvalue);
157  bool hasSubElement(int subElementType);
158 
159  // misc
160  bool empty () const { return fValue.empty() && elements().empty(); }
161 };
162 
165 }
166 
167 #endif
MusicXML2::xmlattribute
A generic xml attribute representation.
Definition: xml.h:47
MusicXML2::xmlelement::add
long add(const Sxmlattribute &attr)
adds an attribute to the element
MusicXML2::xmlelement::getValue
const std::string & getValue() const
returns the element value as a string
Definition: xml.h:125
MusicXML2::ctree
a simple tree representation
Definition: ctree.h:124
MusicXML2::smartable
the base class for smart pointers implementation
Definition: smartpointer.h:29
MusicXML2::xmlelement
A generic xml element representation.
Definition: xml.h:86
MusicXML2::xmlattribute::getValue
const std::string & getValue() const
returns the attribute value as a string
Definition: xml.h:66
MusicXML2::SMARTP
the smart pointer implementation
Definition: smartpointer.h:58
MusicXML2::visitor
Definition: visitor.h:27
MusicXML2::basevisitor
Definition: basevisitor.h:25
MusicXML2::visitable
base class for visitable objects
Definition: visitable.h:25