7 #include "fwAtomsBoostIO/Writer.hpp" 9 #include <fwAtoms/Base.hpp> 10 #include <fwAtoms/Blob.hpp> 11 #include <fwAtoms/Boolean.hpp> 12 #include <fwAtoms/Map.hpp> 13 #include <fwAtoms/Numeric.hpp> 14 #include <fwAtoms/Object.hpp> 15 #include <fwAtoms/Sequence.hpp> 16 #include <fwAtoms/String.hpp> 17 #include <fwMemory/BufferManager.hpp> 19 #include <fwMemory/BufferManager.hpp> 20 #include <fwTools/UUID.hpp> 22 #include <fwZip/IWriteArchive.hpp> 24 #include <boost/filesystem/operations.hpp> 25 #include <boost/filesystem/path.hpp> 26 #include <boost/lexical_cast.hpp> 27 #include <boost/property_tree/json_parser.hpp> 28 #include <boost/property_tree/xml_parser.hpp> 42 typedef std::map< ::fwAtoms::Base::sptr, ::boost::property_tree::ptree > PropTreeCacheType;
44 PropTreeCacheType m_cache;
45 ::fwZip::IWriteArchive::sptr m_archive;
46 const std::string m_dirPrefix;
48 AtomVisitor(const ::fwZip::IWriteArchive::sptr& archive,
const std::string &dirPrefix) :
50 m_dirPrefix(dirPrefix)
56 PropTreeCacheType::mapped_type hitCache(
const PropTreeCacheType::key_type &atom)
const 58 PropTreeCacheType::const_iterator iter = m_cache.find(atom);
59 if(iter != m_cache.end())
63 return PropTreeCacheType::mapped_type();
68 void cache(
const PropTreeCacheType::key_type &atom,
const std::string &ptpath)
70 ::boost::property_tree::ptree ref;
71 ref.put(
"ref", ptpath );
72 m_cache.insert( PropTreeCacheType::value_type( atom, ref ) );
77 ::boost::property_tree::ptree visit(const ::fwAtoms::Boolean::sptr &atom,
const std::string &ptpath)
79 ::boost::property_tree::ptree pt;
80 this->cache(atom, ptpath);
81 pt.put(
"boolean.value", atom->getValue() ?
"true" :
"false");
87 ::boost::property_tree::ptree visit(const ::fwAtoms::Numeric::sptr &atom,
const std::string &ptpath)
89 ::boost::property_tree::ptree pt;
90 this->cache(atom, ptpath);
91 pt.put(
"numeric.value", atom->getString());
97 ::boost::property_tree::ptree visit(const ::fwAtoms::String::sptr &atom,
const std::string &ptpath)
99 ::boost::property_tree::ptree pt;
100 this->cache(atom, ptpath);
101 pt.put(
"string.value", atom->getString());
107 ::boost::property_tree::ptree visit(const ::fwAtoms::Map::sptr &atom,
const std::string &ptpath)
109 ::boost::property_tree::ptree pt;
110 ::boost::property_tree::ptree map;
111 this->cache(atom, ptpath);
112 std::string path = ptpath + (ptpath.empty() ?
"" :
".") +
"map";
113 unsigned long long count = 0;
114 for(const ::fwAtoms::Map::MapType::value_type& elt : atom->getValue())
116 const std::string nodeName =
"item_" + ::boost::lexical_cast< std::string >(count++);
117 ::boost::property_tree::ptree mapChild;
118 mapChild.put(
"key", elt.first);
119 mapChild.add_child(
"value", this->visit(elt.second, path +
"." + nodeName +
".value"));
121 map.add_child(nodeName, mapChild);
123 pt.add_child(
"map", map);
129 ::boost::property_tree::ptree visit(const ::fwAtoms::Sequence::sptr &atom,
const std::string &ptpath)
131 ::boost::property_tree::ptree pt;
132 ::boost::property_tree::ptree seq;
133 this->cache(atom, ptpath);
134 std::string path = ptpath + (ptpath.empty() ?
"" :
".") +
"sequence";
136 unsigned long long count = 0;
137 for( const ::fwAtoms::Sequence::SequenceType::value_type& elt : atom->getValue())
139 const std::string nodeName = ::boost::lexical_cast< std::string >(count++);
140 seq.add_child(nodeName, this->visit(elt, path +
"." + nodeName));
142 pt.add_child(
"sequence", seq);
148 ::boost::property_tree::ptree visit(const ::fwAtoms::Object::sptr &atom,
const std::string &ptpath)
150 ::boost::property_tree::ptree pt;
151 ::boost::property_tree::ptree object;
152 this->cache(atom, ptpath);
153 std::string path = ptpath + (ptpath.empty() ?
"" :
".") +
"object";
155 const ::fwAtoms::Object::MetaInfosType& metaInfos = atom->getMetaInfos();
156 ::boost::property_tree::ptree metaInfosPt;
157 unsigned long long count = 0;
158 for(const ::fwAtoms::Object::MetaInfosType::value_type& info : metaInfos)
160 const std::string nodeName =
"item_" + ::boost::lexical_cast< std::string >(count++);
161 ::boost::property_tree::ptree item;
162 item.put(
"key", info.first);
163 item.put(
"value", info.second);
164 metaInfosPt.push_back(::boost::property_tree::ptree::value_type(nodeName, item));
166 object.add_child(
"meta_infos", metaInfosPt);
168 const ::fwAtoms::Object::AttributesType& attributes = atom->getAttributes();
169 ::boost::property_tree::ptree attributesPt;
170 for(const ::fwAtoms::Object::AttributesType::value_type& attr : attributes)
172 ::boost::property_tree::ptree childAttributes =
173 this->visit(attr.second, path +
".attributes." + attr.first);
174 attributesPt.add_child(attr.first, childAttributes);
176 object.add_child(
"attributes", attributesPt);
178 pt.add_child(
"object",
object);
185 ::boost::property_tree::ptree visit(const ::fwAtoms::Blob::sptr &atom,
const std::string &ptpath)
187 ::boost::property_tree::ptree pt;
188 this->cache(atom, ptpath);
189 std::string path = ptpath + (ptpath.empty() ?
"" :
".") +
"blob";
191 std::string bufType =
"raw";
192 pt.put(
"blob.buffer_type", bufType);
194 ::fwMemory::BufferObject::sptr buffObj = atom->getBufferObject();
195 if (!buffObj || buffObj->getSize() == 0)
197 pt.put(
"blob.buffer_size", 0);
201 ::boost::filesystem::path bufFile = m_dirPrefix;
202 size_t buffSize = buffObj->getSize();
204 const ::fwMemory::BufferManager::StreamInfo streamInfo = buffObj->getStreamInfo();
205 const ::boost::filesystem::path dumpedFile = streamInfo.fsFile;
206 const ::fwMemory::FileFormatType& format = streamInfo.format;
210 if ( !dumpedFile.empty() && (format & ::fwMemory::RAW) )
212 m_archive->putFile(dumpedFile, bufFile);
216 SPTR(std::istream) is = streamInfo.stream;
219 SPTR(std::ostream) os = m_archive->createFile(bufFile);
223 pt.put(
"blob.buffer_size", buffSize);
224 pt.put(
"blob.buffer", bufFile.generic_string());
232 ::boost::property_tree::ptree visit(const ::fwAtoms::Base::sptr &atom, std::string ptpath =
"")
234 ::boost::property_tree::ptree pt;
235 ::boost::property_tree::ptree ref;
242 ref = this->hitCache(atom);
250 case ::fwAtoms::Base::BOOLEAN:
251 pt = this->visit(::fwAtoms::Boolean::dynamicCast(atom), ptpath);
253 case ::fwAtoms::Base::NUMERIC:
254 pt = this->visit(::fwAtoms::Numeric::dynamicCast(atom), ptpath);
256 case ::fwAtoms::Base::STRING:
257 pt = this->visit(::fwAtoms::String::dynamicCast(atom), ptpath);
259 case ::fwAtoms::Base::OBJECT:
260 pt = this->visit(::fwAtoms::Object::dynamicCast(atom), ptpath);
262 case ::fwAtoms::Base::SEQUENCE:
263 pt = this->visit(::fwAtoms::Sequence::dynamicCast(atom), ptpath);
265 case ::fwAtoms::Base::MAP:
266 pt = this->visit(::fwAtoms::Map::dynamicCast(atom), ptpath);
268 case ::fwAtoms::Base::BLOB:
269 pt = this->visit(::fwAtoms::Blob::dynamicCast(atom), ptpath);
272 FW_RAISE(
"Atome type '"<<atom->type()<<
"' is not supported");
281 void Writer::write( const ::fwZip::IWriteArchive::sptr& archive,
282 const ::boost::filesystem::path& rootFilename,
283 FormatType format )
const 285 ::boost::property_tree::ptree root;
286 AtomVisitor visitor(archive, rootFilename.stem().string() +
"-" + ((format==JSON) ?
"json" :
"xml"));
288 root = visitor.visit(m_atom);
290 ::boost::property_tree::ptree versions;
292 versions.put(s_WRITER_VERSION_KEY, Writer::s_VERSION);
294 root.add_child(
"versions", versions);
296 SPTR(std::ostream) os = archive->createFile(rootFilename);
300 ::boost::property_tree::json_parser::write_json(*os, root,
false);
304 ::boost::property_tree::xml_writer_settings<std::string> settings(
' ', 4);
305 ::boost::property_tree::xml_parser::write_xml(*os, root, settings);
309 FW_RAISE(
"Archive format '"<<format<<
"' is not supported");
The namespace fwAtomsBoostIO contains atom reader and writer.
static FWATOMSBOOSTIO_API const std::string s_ATOMS_VERSION_KEY
Defines key to retrieve fwAtoms version from file.
static FWATOMSBOOSTIO_API const std::string s_WRITER_VERSION_KEY
Defines key to retrieve writer version from file.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
static FWATOMSBOOSTIO_API const std::string s_VERSION
Defines writer version.
static FWATOMS_API const std::string s_VERSION
Defines fwAtoms version.