Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XMLHepRepWriter.cc
Go to the documentation of this file.
1 // Copyright FreeHEP, 2005.
2 
4 #include "cheprep/XMLWriter.h"
6 
9 
10 #define NAMESPACE "heprep"
11 
12 using namespace std;
13 using namespace HEPREP;
14 
15 /**
16  * @author Mark Donszelmann
17  * @version $Id: XMLHepRepWriter.cc 68043 2013-03-13 14:27:49Z gcosmo $
18  */
19 namespace cheprep {
20 
21 XMLHepRepWriter::XMLHepRepWriter(ostream* os, bool randomAccess, bool useCompression)
22  : out(os),
23  compress(useCompression),
24  xml(0) {
25 
26  this->nameSpace = NAMESPACE;
27 
28  if (randomAccess) {
29  zip = new ZipOutputStream(*os);
30  out = zip;
31  gz = NULL;
32  } else {
33  zip = NULL;
34  if (useCompression) {
35 #ifndef CHEPREP_NO_ZLIB
36  gz = new GZIPOutputStream(*os);
37  out = gz;
38 #else
39  cerr << "WARNING: the .gz output stream you are creating will be a plain file," << endl;
40  cerr << "since compression support (ZLIB) was not compiled into the library." << endl;
41  cerr << "To add ZLIB support, you need to undefine CHEPREP_NO_ZLIB." << endl;
42  gz = NULL;
43 #endif
44  } else {
45  gz = NULL;
46  }
47  }
48 }
49 
51  delete gz;
52  delete zip;
53 }
54 
55 bool XMLHepRepWriter::addProperty(std::string key, std::string value) {
56  properties[key] = value;
57  return true;
58 }
59 
61  if (zip != NULL) {
62  zip->putNextEntry("heprep.properties", true);
63 
64  map<string, string>::iterator i = properties.begin();
65  while (i != properties.end()) {
66  *zip << (*i).first << "=" << (*i).second << endl;
67  i++;
68  }
69  zip->closeEntry();
70  zip->close();
71  }
72 
73  if (gz != NULL) {
74  gz->close();
75  }
76  return true;
77 }
78 
79 bool XMLHepRepWriter::write(HepRep* heprep, string name) {
80  if (zip != NULL) {
81  zip->putNextEntry(name, compress);
82  }
83 
84  if (name.rfind(".bheprep") == name.length()-8) {
85  xml = new BHepRepWriter(*out);
86  } else {
87  xml = new XMLWriter(out, " ", NAMESPACE);
88  }
89 
90  xml->openDoc();
91  xml->setAttribute("version", (string)"2.0");
92  xml->setAttribute("xmlns", (string)"http://java.freehep.org/schemas/heprep/2.0");
93  xml->setAttribute("xmlns", "xsi", "http://www.w3.org/2001/XMLSchema-instance");
94  xml->setAttribute("xsi", "schemaLocation", "http://java.freehep.org/schemas/heprep/2.0 http://java.freehep.org/schemas/heprep/2.0/HepRep.xsd");
95  xml->openTag(nameSpace, "heprep");
96  write(heprep->getLayerOrder());
97  vector<HepRepTypeTree*> typeTreeSet = heprep->getTypeTreeList();
98  for (vector<HepRepTypeTree*>::iterator i1=typeTreeSet.begin(); i1 != typeTreeSet.end(); i1++) {
99  write(*i1);
100  }
101  vector<HepRepInstanceTree*> instanceTreeSet = heprep->getInstanceTreeList();
102  for (vector<HepRepInstanceTree*>::iterator i2=instanceTreeSet.begin(); i2 != instanceTreeSet.end(); i2++) {
103  write(*i2);
104  }
105  xml->closeTag();
106  xml->closeDoc();
107 // xml->close();
108  delete xml;
109 
110  if (zip != NULL) {
111  zip->closeEntry();
112  }
113 
114  return true;
115 }
116 
117 bool XMLHepRepWriter::write(vector<string> layers) {
118  string layerOrder = "";
119  bool comma = false;
120  for (vector<string>::iterator i=layers.begin(); i != layers.end(); i++) {
121  if (comma) {
122  layerOrder.append(", ");
123  }
124  layerOrder.append(*i);
125  comma = true;
126  }
127  xml->setAttribute("order", layerOrder);
128  xml->printTag(nameSpace, "layer");
129  return true;
130 }
131 
133  xml->setAttribute("name", typeTree->getName());
134  xml->setAttribute("version", typeTree->getVersion());
135  xml->openTag(nameSpace, "typetree");
136 
137  vector<HepRepType*> types = typeTree->getTypeList();
138  for (vector<HepRepType*>::iterator i=types.begin(); i != types.end(); i++) {
139  write(*i);
140  }
141 
142  xml->closeTag();
143  return true;
144 }
145 
147  xml->setAttribute("name", type->getName());
148  xml->openTag(nameSpace, "type");
149  write((HepRepDefinition*)type);
150  write((HepRepAttribute*)type);
151 
152  vector<HepRepType*> types = type->getTypeList();
153  for (vector<HepRepType*>::iterator i=types.begin(); i != types.end(); i++) {
154  write(*i);
155  }
156  xml->closeTag();
157  return true;
158 }
159 
161  xml->setAttribute("qualifier", treeID->getQualifier());
162  xml->setAttribute("name", treeID->getName());
163  xml->setAttribute("version", treeID->getVersion());
164  xml->printTag(nameSpace, "treeid");
165  return true;
166 }
167 
169  xml->setAttribute("name", action->getName());
170  xml->setAttribute("expression", action->getExpression());
171  xml->printTag(nameSpace, "action");
172  return true;
173 }
174 
176  xml->setAttribute("name", instanceTree->getName());
177  xml->setAttribute("version", instanceTree->getVersion());
178  xml->setAttribute("typetreename", instanceTree->getTypeTree()->getName());
179  xml->setAttribute("typetreeversion", instanceTree->getTypeTree()->getVersion());
180  xml->openTag(nameSpace, "instancetree");
181  // refs
182  vector<HepRepTreeID*> instanceTreeSet = instanceTree->getInstanceTreeList();
183  for (vector<HepRepTreeID*>::iterator i1=instanceTreeSet.begin(); i1 != instanceTreeSet.end(); i1++) {
184  write(*i1);
185  }
186 
187  // instances
188  vector<HepRepInstance*> instanceList = instanceTree->getInstances();
189  for (vector<HepRepInstance*>::iterator i2=instanceList.begin(); i2 != instanceList.end(); i2++) {
190  write(*i2);
191  }
192  xml->closeTag();
193  return true;
194 }
195 
197  // FIXME FREEHEP-356
198  xml->setAttribute("type", instance->getType()->getFullName());
199  xml->openTag(nameSpace, "instance");
200  write((HepRepAttribute*)instance);
201 
202  vector<HepRepPoint*> pointList = instance->getPoints();
203  for (vector<HepRepPoint*>::iterator i1=pointList.begin(); i1 != pointList.end(); i1++) {
204  write(*i1);
205  }
206 
207  vector<HepRepInstance*> instanceList = instance->getInstances();
208  for (vector<HepRepInstance*>::iterator i2=instanceList.begin(); i2 != instanceList.end(); i2++) {
209  write(*i2);
210  }
211  xml->closeTag();
212  return true;
213 }
214 
216  xml->setAttribute("x", point->getX());
217  xml->setAttribute("y", point->getY());
218  xml->setAttribute("z", point->getZ());
219  if (point->getAttValuesFromNode().size() != 0) {
220  xml->openTag(nameSpace, "point");
221  write((HepRepAttribute*)point);
222  xml->closeTag();
223  } else {
224  xml->printTag(nameSpace, "point");
225  }
226  return true;
227 }
228 
230  // BUG FIX. Do something special for layers, because these do not end
231  // up in the normal iteration.
232  HepRepAttValue* layerAtt = attribute->getAttValueFromNode("layer");
233  if (layerAtt != NULL) write(layerAtt);
234 
235  set<HepRepAttValue*> attSet = attribute->getAttValuesFromNode();
236  for (set<HepRepAttValue*>::iterator i=attSet.begin(); i != attSet.end(); i++) {
237  write(*i);
238  }
239  return true;
240 }
241 
243  set<HepRepAttDef*> list = definition->getAttDefsFromNode();
244  for (set<HepRepAttDef*>::iterator i=list.begin(); i != list.end(); i++) {
245  write(*i);
246  }
247  return true;
248 }
249 
251  string name = attValue->getName();
252 
253  xml->setAttribute("name", name);
254 
255  switch(attValue->getType()) {
256  default: xml->setAttribute("value", attValue->getAsString());
257  break;
258  case HepRepConstants::TYPE_STRING: xml->setAttribute("value", attValue->getString());
259  break;
260  case HepRepConstants::TYPE_LONG: xml->setAttribute("value", attValue->getLong());
261  break;
262  case HepRepConstants::TYPE_INT: xml->setAttribute("value", attValue->getInteger());
263  break;
264  case HepRepConstants::TYPE_DOUBLE: xml->setAttribute("value", attValue->getDouble());
265  break;
266  case HepRepConstants::TYPE_BOOLEAN: xml->setAttribute("value", attValue->getBoolean());
267  break;
268  case HepRepConstants::TYPE_COLOR: xml->setAttribute("value", attValue->getColor());
269  }
270 
271  if (attValue->showLabel() != HepRepConstants::SHOW_NONE) {
272  xml->setAttribute("showlabel", attValue->showLabel());
273  }
274 
275  xml->printTag(nameSpace, "attvalue");
276  return true;
277 }
278 
280  xml->setAttribute("name", attDef->getName());
281  xml->setAttribute("desc", attDef->getDescription());
282  xml->setAttribute("category", attDef->getCategory());
283  xml->setAttribute("extra", attDef->getExtra());
284  xml->printTag(nameSpace, "attdef");
285  return true;
286 }
287 
288 } // cheprep
289 
virtual std::string getExpression()=0
virtual double getY()=0
virtual void openTag(std::string ns, std::string name)=0
virtual std::string getName()=0
virtual HepRepTreeID * getTypeTree()=0
virtual std::string getName()=0
virtual std::string getAsString()=0
bool addProperty(std::string key, std::string value)
virtual std::vector< HepRepInstance * > getInstances()=0
virtual std::string getName()=0
virtual std::string getString()=0
virtual std::string getExtra()=0
const XML_Char * name
virtual void printTag(std::string ns, std::string name)=0
virtual std::vector< HepRepPoint * > getPoints()=0
virtual std::vector< HepRepTypeTree * > getTypeTreeList()=0
virtual std::vector< double > getColor()=0
virtual int showLabel()=0
void putNextEntry(const std::string &name, bool compress)
virtual void closeTag()=0
virtual double getDouble()=0
virtual HepRepAttValue * getAttValueFromNode(std::string lowerCaseName)=0
virtual std::string getCategory()=0
virtual int getType()=0
virtual int getInteger()=0
virtual std::vector< HepRepType * > getTypeList()=0
virtual void setAttribute(std::string ns, std::string name, std::string value)=0
virtual HepRepType * getType()=0
virtual void closeDoc(bool force=false)=0
virtual int64 getLong()=0
virtual std::string getName()=0
virtual std::vector< HepRepInstance * > getInstances()=0
virtual std::string getDescription()=0
virtual double getX()=0
virtual void openDoc(std::string version="1.0", std::string encoding="", bool standalone=false)=0
virtual bool getBoolean()=0
virtual std::string getName()=0
#define NAMESPACE
virtual std::string getQualifier()=0
bool write(HEPREP::HepRep *heprep, std::string name)
virtual std::vector< std::string > getLayerOrder()=0
int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
Definition: compress.cc:57
virtual double getZ()=0
const XML_Char int const XML_Char * value
virtual std::string getFullName()=0
virtual std::string getVersion()=0
virtual std::vector< HepRepType * > getTypeList()=0
virtual std::vector< HepRepTreeID * > getInstanceTreeList()=0
virtual std::set< HepRepAttDef * > getAttDefsFromNode()=0
virtual std::set< HepRepAttValue * > getAttValuesFromNode()=0
virtual std::vector< HepRepInstanceTree * > getInstanceTreeList()=0