Main Page | Modules | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | Related Pages

xml.h

00001 /*
00002 
00003   MusicXML Library
00004   Copyright (C) 2003  Grame
00005 
00006   This library is free software; you can redistribute it and/or
00007   modify it under the terms of the GNU Lesser General Public
00008   License as published by the Free Software Foundation; either
00009   version 2.1 of the License, or (at your option) any later version.
00010 
00011   This library is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   Lesser General Public License for more details.
00015 
00016   You should have received a copy of the GNU Lesser General Public
00017   License along with this library; if not, write to the Free Software
00018   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020   Grame Research Laboratory, 9 rue du Garet, 69001 Lyon - France
00021   grame@grame.fr
00022 
00023 */
00024 
00025 #ifndef __xml__
00026 #define __xml__
00027 
00028 #include "exports.h"
00029 #include "bimap.h"
00030 #include "smartpointer.h"
00031 #include <iostream>
00032 #include <string>
00033 #include <vector>
00034 
00035 using namespace std;
00036 
00037 namespace MusicXML 
00038 {
00039 
00040 class xmlelement;
00041 class xmlattribute;
00042 class xmlable;
00043 class xmlheader;
00044 
00045 typedef SMARTP<xmlattribute>    Sxmlattribute;
00046 typedef SMARTP<xmlelement>      Sxmlelement;
00047 typedef SMARTP<xmlheader>       Sxmlheader;
00048 
00049 ostream& operator<< (ostream& os, const Sxmlattribute& attr);
00050 EXP ostream& operator<< (ostream& os, const Sxmlelement& elt);
00051 EXP ostream& operator<< (ostream& os, const Sxmlheader& header);
00052 
00061 class xmlendl {
00062     public:
00063         static xmlendl& get() { if (!fInstance) fInstance = new xmlendl; return *fInstance; }
00064 
00066         xmlendl& operator++ (int)  { fIndent++; return *this; }
00068         xmlendl& operator-- (int)  { fIndent--; return *this; }
00070         void reset()               { fIndent = 0; }
00071         void print(ostream& os) const { int i = fIndent;
00072                                         os << std::endl;
00073                                         while (i-- > 0)  os << "    "; }
00074     protected:
00075         xmlendl() : fIndent(0) {}
00076     private:
00077         int fIndent;
00078         static xmlendl * fInstance;
00079 };
00080 
00086 class xmlattribute : public smartable {
00087     protected:
00088         xmlattribute(string name, string value) : fName(name), fValue(value) {}
00089         xmlattribute(string name, long value) : fName(name) { setValue(value); }
00090     public:
00091         EXP friend SMARTP<xmlattribute> new_xmlattribute(string name, string value);
00092         EXP friend SMARTP<xmlattribute> new_xmlattribute(string name, long value);
00093 
00094         void setName (const string& name);
00095         void setValue (const string& value);
00096         void setValue (long value);
00097 
00098         const string& getName () const  { return fName; }
00099         const string& getValue () const { return fValue; }
00100         void print (ostream& os) const;
00101     private:
00103         string  fName;
00105         string  fValue;
00106 };
00107 
00116 class xmlelement : public smartable {
00117 
00118     public:
00119         EXP friend SMARTP<xmlelement> new_xmlelement(string name);
00120         EXP friend SMARTP<xmlelement> new_xmlelement(string name, unsigned long val);
00121         EXP friend SMARTP<xmlelement> new_xmlelement(string name, long val);
00122         EXP friend SMARTP<xmlelement> new_xmlelement(string name, int val);
00123         EXP friend SMARTP<xmlelement> new_xmlelement(string name, float val);
00124         EXP friend SMARTP<xmlelement> new_xmlelement(string name, string val);
00125 
00126         void setValue (unsigned long value);
00127         void setValue (long value);
00128         void setValue (int value);
00129         void setValue (float value);
00130         void setValue (const string& value);
00131         void setName (const string& name);
00132 
00133         const string& getName () const              { return fName; }
00134         const string& getValue () const             { return fValue; }
00135 
00136         long add (const Sxmlattribute& attr);
00137         long add (const Sxmlelement& elt);
00138         
00139         const vector<Sxmlelement>& elements() const         { return fElements; }
00140         const vector<Sxmlattribute>& attributes() const     { return fAttributes; }
00141         
00142         bool empty () const { return fValue.empty() && fElements.empty(); }
00143         void print (ostream& os) const;
00144 
00145     protected:
00146         xmlelement(string name) : fName(name) {}
00147         xmlelement(string name, unsigned long value) : fName(name) { setValue(value); }
00148         xmlelement(string name, long value) : fName(name) { setValue(value); }
00149         xmlelement(string name, int value) : fName(name) { setValue(value); }
00150         xmlelement(string name, float value) : fName(name) { setValue(value); }
00151         xmlelement(string name, string value) : fName(name) { setValue(value); }
00152         virtual ~xmlelement() {}
00153 
00154     private:
00156         string  fName;
00158         string  fValue;
00160         vector<Sxmlattribute> fAttributes;
00162         vector<Sxmlelement>   fElements;
00163 };
00164 
00168 class EXP xmlheader : public smartable {
00169      protected:
00170         xmlheader (int mode, string loc) : fMode(mode), fLocation(loc) {}
00171         virtual ~xmlheader () {}
00172 
00173      public:
00174         enum { partwise=1, timewise, last=timewise };
00175         
00176         EXP friend SMARTP<xmlheader> new_xmlheader(int mode=partwise, string loc="http://www.musicxml.org/dtds/");
00177     
00179         void setMode (int mode);
00181         void setLocation (const string& loc);
00182 
00184                 int     getMode () const            { return fMode; }
00186         const string&   getLocation () const        { return fLocation; }
00188         const string    MusicXMLVersion () const    { return "0.8"; }
00189         void print (ostream& os) const;
00190         
00192     static const string scoreMode (int mode);
00194     static int      scoreMode (string mode);
00195 
00196     private:
00198         int     fMode;
00200         string  fLocation;
00201 
00202     static bimap<string, int> fMode2String;
00203     static int      fModeTbl[];
00204     static string   fModeStrings[];
00205 };
00206 
00214 /*
00215 class xmlable {
00216      public:
00217         virtual Sxmlelement xml() const = 0;
00218 };
00219 */
00220 
00221 }
00222 
00223 #endif

Generated on Tue Mar 23 09:49:44 2004 for LibMusicXML by doxygen 1.3.3