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