CLAM-Development  1.4.0
XMLStorage.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
23 // XMLStorage.cxx
24 // Description: A storage in XML format for CLAM objects
26 
27 #ifndef _XMLStorage_hxx_
28 #define _XMLStorage_hxx_
29 
30 #include "Storage.hxx"
31 #include "XmlStorageErr.hxx"
32 #include <iosfwd>
33 #include <string>
34 
35 
36 namespace CLAM
37 {
38 
39 class XMLable;
40 class Component;
41 class DomDocumentHandler;
42 
43 
44 class XmlStorage : public Storage
45 {
46  DomDocumentHandler * _documentHandler;
47  bool _lastWasContent;
48  bool _useIndentation;
49  std::string _errors;
55 // @{
56 public:
57  XmlStorage();
58  ~XmlStorage();
59 
60 public:
61  void Read(std::istream & is);
62  void Create(const std::string & name);
63  void WriteSelection(std::ostream & os);
64  void WriteDocument(std::ostream & os);
65  void DumpObject(const Component & component);
66  void RestoreObject(Component & component);
67 
81  void Select(const std::string & path);
82 
89  void UseIndentation(bool useIndentation);
90 // @}
91 
97 // @{
98 public:
99 
103  static void Dump(const Component & obj, const std::string & rootName, std::ostream & os, bool indentate=true)
104  {
105  XmlStorage storage;
106  storage.Create(rootName);
107  storage.DumpObject(obj);
108  storage.UseIndentation(indentate);
109  storage.WriteDocument(os);
110  }
111 
115  static void Restore(Component & obj, std::istream & is)
116  {
117  XmlStorage storage;
118  storage.Read(is);
119  storage.RestoreObject(obj);
120  }
121 
125  static void RestoreFromFragment(Component & obj, const std::string & path, std::istream & is)
126  {
127  XmlStorage storage;
128  storage.Read(is);
129  storage.Select(path);
130  storage.RestoreObject(obj);
131  }
132 
137  static void AppendToDocument(const Component & obj, const std::string & xpath, const std::string & filename);
138 
142  static void Dump(const Component & obj, const std::string & rootName, const std::string & filename);
143 
147  static void Restore(Component & obj, const std::string & filename);
148 // @}
149 
155 // @{
156 public:
161  void Store(const Storable & storable);
166  bool Load(Storable & storable);
167 // @}
168 
169 // Private helper functions
170 private:
171  bool LoadContentAndChildren(XMLable* xmlable);
172  void StoreContentAndChildren(const XMLable * xmlable);
173  void StoreChildrenIfComponent(const XMLable * xmlable);
174  void AddContentToElement(const std::string & content);
175 
176 };
177 
180 
181 } // namespace CLAM
182 
183 
184 
690 #endif//_XMLStorage_hxx_
691