Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Protected Attributes
cheprep::XMLWriter Class Reference

#include <XMLWriter.h>

Inheritance diagram for cheprep::XMLWriter:
cheprep::AbstractXMLWriter

Public Member Functions

 XMLWriter (std::ostream *out, std::string indentString=" ", std::string defaultNameSpace="")
 
virtual ~XMLWriter ()
 
void close ()
 
void openDoc (std::string version="1.0", std::string encoding="", bool standalone=false)
 
void referToDTD (std::string name, std::string pid, std::string ref)
 
void referToDTD (std::string name, std::string system)
 
void closeDoc (bool force=false)
 
void printComment (std::string comment)
 
void printPlain (std::string text)
 
void print (std::string text)
 
void println (std::string text)
 
void openTag (std::string name)
 
void closeTag ()
 
void printTag (std::string name)
 
void setAttribute (std::string name, char *value)
 
void setAttribute (std::string name, std::string value)
 
void setAttribute (std::string name, std::vector< double > value)
 
void setAttribute (std::string name, int64 value)
 
void setAttribute (std::string name, int value)
 
void setAttribute (std::string name, bool value)
 
void setAttribute (std::string name, double value)
 
void printAttributes (int tagLength)
 
std::string normalize (std::string s)
 
std::string normalizeText (std::string s)
 
void checkNameValid (std::string s)
 
void openTag (std::string ns, std::string name)
 
void printTag (std::string ns, std::string name)
 
void setAttribute (std::string ns, std::string name, std::string value)
 
void setAttribute (std::string ns, std::string name, double value)
 
- Public Member Functions inherited from cheprep::AbstractXMLWriter
 AbstractXMLWriter (std::string aDefaultNameSpace)
 
virtual ~AbstractXMLWriter ()
 

Protected Attributes

bool closed
 
IndentPrintWriterwriter
 
- Protected Attributes inherited from cheprep::AbstractXMLWriter
std::string defaultNameSpace
 

Detailed Description

Definition at line 22 of file XMLWriter.h.

Constructor & Destructor Documentation

cheprep::XMLWriter::XMLWriter ( std::ostream *  out,
std::string  indentString = "  ",
std::string  defaultNameSpace = "" 
)

Definition at line 18 of file XMLWriter.cc.

References closed, cheprep::IndentPrintWriter::setIndentString(), and writer.

19  : AbstractXMLWriter(aDefaultNameSpace) {
20  writer = new IndentPrintWriter(out);
21  writer->setIndentString(indentString);
22  closed = false;
23  dtdName = "";
24 }
AbstractXMLWriter(std::string aDefaultNameSpace)
void setIndentString(const std::string &indentString)
IndentPrintWriter * writer
Definition: XMLWriter.h:71
cheprep::XMLWriter::~XMLWriter ( )
virtual

Definition at line 26 of file XMLWriter.cc.

References cheprep::IndentPrintWriter::close(), and writer.

26  {
27  writer->close();
28  delete writer;
29 }
IndentPrintWriter * writer
Definition: XMLWriter.h:71

Member Function Documentation

void cheprep::XMLWriter::checkNameValid ( std::string  s)

Definition at line 280 of file XMLWriter.cc.

Referenced by openTag(), printAttributes(), and printTag().

280  {
281 // Could be added.
282 // if (!XMLCharacterProperties.validName(s)) throw new RuntimeException("Invalid name: "+s);
283 }
void cheprep::XMLWriter::close ( )
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 31 of file XMLWriter.cc.

References cheprep::IndentPrintWriter::close(), closeDoc(), and writer.

31  {
32  closeDoc();
33  writer->close();
34 }
void closeDoc(bool force=false)
Definition: XMLWriter.cc:70
IndentPrintWriter * writer
Definition: XMLWriter.h:71
void cheprep::XMLWriter::closeDoc ( bool  force = false)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 70 of file XMLWriter.cc.

References closed, and closeTag().

Referenced by close().

70  {
71  if (!closed) {
72  if (!openTags.empty()) {
73  if (!force) cerr << "Not all tags were closed before closing XML document:" << endl;
74  while (!openTags.empty()) {
75  if (force) {
76  closeTag();
77  } else {
78  cerr << " </" << openTags.top().c_str() << ">" << endl;
79  openTags.pop();
80  }
81  }
82  }
83  closed = true;
84  }
85 }
void cheprep::XMLWriter::closeTag ( )
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 119 of file XMLWriter.cc.

References cheprep::IndentPrintWriter::close(), cheprep::IndentPrintWriter::outdent(), and writer.

Referenced by closeDoc().

119  {
120  if (openTags.empty()) {
121  writer->close();
122  cerr << "XMLWriter::closeTag(), No open tags" << endl;
123  }
124  string name = openTags.top();
125  openTags.pop();
126  writer->outdent();
127  *writer << "</" << name.c_str() << ">" << endl;
128 }
const XML_Char * name
IndentPrintWriter * writer
Definition: XMLWriter.h:71
string cheprep::XMLWriter::normalize ( std::string  s)

Definition at line 201 of file XMLWriter.cc.

References buffer.

Referenced by printAttributes().

201  {
202  string str = "";
203  char buffer[20];
204 
205  int len = s.length();
206  for (int i = 0; i < len; i++) {
207  char ch = s[i];
208  switch (ch) {
209  case '<': {
210  str.append("&lt;");
211  break;
212  }
213  case '>': {
214  str.append("&gt;");
215  break;
216  }
217  case '&': {
218  str.append("&amp;");
219  break;
220  }
221  case '"': {
222  str.append("&quot;");
223  break;
224  }
225  case '\r':
226  case '\n': {
227  sprintf(buffer, "&#%ud", ch);
228  str.append(buffer);
229  str.append(";");
230  break;
231  }
232  default: {
233 // if (ch > 0x00FF) {
234 // sprintf(buffer, "&#x%4.4x", ch);
235 // str.append(buffer);
236 // str.append(";");
237 // } else {
238  str.append(&ch, 1);
239 // }
240  }
241  }
242  }
243 
244  return str;
245 }
const XML_Char * s
#define buffer
Definition: xmlparse.cc:611
const XML_Char int len
string cheprep::XMLWriter::normalizeText ( std::string  s)

Definition at line 247 of file XMLWriter.cc.

Referenced by print(), and printComment().

247  {
248  string str = "";
249 
250  int len = s.length();
251  for (int i = 0; i < len; i++) {
252  char ch = s[i];
253  switch (ch) {
254  case '<': {
255  str.append("&lt;");
256  break;
257  }
258  case '>': {
259  str.append("&gt;");
260  break;
261  }
262  case '&': {
263  str.append("&amp;");
264  break;
265  }
266  default: {
267 // if (ch > 0x00FF) {
268 // sprintf(buffer, "&#x%4.4x", ch);
269 // str.append(buffer);
270 // str.append(";");
271 // } else {
272  str.append(&ch, 1);
273 // }
274  }
275  }
276  }
277  return str;
278 }
const XML_Char * s
const XML_Char int len
void cheprep::XMLWriter::openDoc ( std::string  version = "1.0",
std::string  encoding = "",
bool  standalone = false 
)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 36 of file XMLWriter.cc.

References cheprep::IndentPrintWriter::getIndentString(), cheprep::IndentPrintWriter::setIndentString(), and writer.

36  {
37  string indentString = writer->getIndentString();
38  writer->setIndentString(indentString);
39 
40 // if (!XMLCharacterProperties.validVersionNum(version)) throw new RuntimeException("Invalid version number: "+version);
41  *writer << "<?xml version=\"" << version.c_str() << "\" ";
42  if (encoding.compare("") != 0) {
43 // if (!XMLCharacterProperties.validEncName(encoding)) throw new RuntimeException("Invalid encoding name: "+encoding);
44  *writer << "encoding=\"" << encoding.c_str() << "\" ";
45  }
46  if (standalone) {
47  *writer << "standalone=\"yes\" ";
48  }
49  *writer << "?>";
50  *writer << endl;
51  writer->setIndentString(indentString);
52 }
const XML_Char * version
std::string getIndentString() const
void setIndentString(const std::string &indentString)
IndentPrintWriter * writer
Definition: XMLWriter.h:71
const XML_Char const XML_Char int standalone
void cheprep::XMLWriter::openTag ( std::string  name)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 107 of file XMLWriter.cc.

References checkNameValid(), cheprep::IndentPrintWriter::indent(), printAttributes(), and writer.

Referenced by openTag().

107  {
109  if (openTags.empty() && dtdName.compare("") && dtdName.compare(name)) {
110  cerr << "XMLWriter::openTag(), First tag: '" << name << "' not equal to DTD id: '" << dtdName << "'" << endl;
111  }
112  *writer << "<" << name.c_str();
113  printAttributes(name.length());
114  *writer << ">" << endl;
115  writer->indent();
116  openTags.push(name);
117 }
const XML_Char * name
void printAttributes(int tagLength)
Definition: XMLWriter.cc:176
IndentPrintWriter * writer
Definition: XMLWriter.h:71
void checkNameValid(std::string s)
Definition: XMLWriter.cc:280
void cheprep::XMLWriter::openTag ( std::string  ns,
std::string  name 
)
inlinevirtual

Implements cheprep::AbstractXMLWriter.

Definition at line 54 of file XMLWriter.h.

References cheprep::AbstractXMLWriter::defaultNameSpace, and openTag().

54  {
55  openTag(ns == defaultNameSpace ? name : ns.append(":").append(name));
56  }
const XML_Char * name
void openTag(std::string name)
Definition: XMLWriter.cc:107
#define ns
Definition: xmlparse.cc:597
void cheprep::XMLWriter::print ( std::string  text)

Definition at line 98 of file XMLWriter.cc.

References normalizeText(), and writer.

Referenced by println().

98  {
99  *writer << normalizeText(text).c_str();
100 }
std::string normalizeText(std::string s)
Definition: XMLWriter.cc:247
IndentPrintWriter * writer
Definition: XMLWriter.h:71
void cheprep::XMLWriter::printAttributes ( int  tagLength)

Definition at line 176 of file XMLWriter.cc.

References checkNameValid(), cheprep::IndentPrintWriter::getIndent(), cheprep::IndentPrintWriter::indent(), normalize(), cheprep::IndentPrintWriter::outdent(), width, and writer.

Referenced by openTag(), and printTag().

176  {
177  int width = tagLength + 1;
178  bool extraIndent = false;
179  for (map<string,string>::iterator i = attributes.begin(); i != attributes.end(); i++) {
180  string key = i->first;
181  checkNameValid(key);
182  string value = normalize(i->second);
183  int length = key.length() + value.length() + 3;
184  if (width > 0 && width + length + 2*writer->getIndent() > 60) {
185  width = 0;
186  *writer << endl;
187  if (!extraIndent) {
188  writer->indent();
189  extraIndent = true;
190  }
191  } else {
192  width += length;
193  *writer << " ";
194  }
195  *writer << key.c_str() << "=\"" << value.c_str() << "\"";
196  }
197  attributes.clear();
198  if (extraIndent) writer->outdent();
199 }
#define width
IndentPrintWriter * writer
Definition: XMLWriter.h:71
std::string normalize(std::string s)
Definition: XMLWriter.cc:201
const XML_Char int const XML_Char * value
void checkNameValid(std::string s)
Definition: XMLWriter.cc:280
void cheprep::XMLWriter::printComment ( std::string  comment)

Definition at line 87 of file XMLWriter.cc.

References normalizeText(), and writer.

87  {
88  if (comment.find("--") != string::npos) {
89  cerr << "XMLWriter::printComment '--' sequence not allowed in comment" << endl;
90  }
91  *writer << "<!--" << normalizeText(comment).c_str() << "-->" << endl;
92 }
std::string normalizeText(std::string s)
Definition: XMLWriter.cc:247
IndentPrintWriter * writer
Definition: XMLWriter.h:71
void cheprep::XMLWriter::println ( std::string  text)

Definition at line 102 of file XMLWriter.cc.

References print(), and writer.

102  {
103  print(text);
104  *writer << endl;
105 }
void print(std::string text)
Definition: XMLWriter.cc:98
IndentPrintWriter * writer
Definition: XMLWriter.h:71
void cheprep::XMLWriter::printPlain ( std::string  text)

Definition at line 94 of file XMLWriter.cc.

References writer.

94  {
95  *writer << text.c_str();
96 }
IndentPrintWriter * writer
Definition: XMLWriter.h:71
void cheprep::XMLWriter::printTag ( std::string  name)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 130 of file XMLWriter.cc.

References checkNameValid(), printAttributes(), and writer.

Referenced by printTag().

130  {
132  *writer << "<" << name.c_str();
133  printAttributes(name.length());
134  *writer << "/>" << endl;
135 }
const XML_Char * name
void printAttributes(int tagLength)
Definition: XMLWriter.cc:176
IndentPrintWriter * writer
Definition: XMLWriter.h:71
void checkNameValid(std::string s)
Definition: XMLWriter.cc:280
void cheprep::XMLWriter::printTag ( std::string  ns,
std::string  name 
)
inlinevirtual

Implements cheprep::AbstractXMLWriter.

Definition at line 57 of file XMLWriter.h.

References cheprep::AbstractXMLWriter::defaultNameSpace, and printTag().

57  {
58  printTag(ns == defaultNameSpace ? name : ns.append(":").append(name));
59  }
void printTag(std::string name)
Definition: XMLWriter.cc:130
const XML_Char * name
#define ns
Definition: xmlparse.cc:597
void cheprep::XMLWriter::referToDTD ( std::string  name,
std::string  pid,
std::string  ref 
)

Definition at line 54 of file XMLWriter.cc.

References writer.

54  {
55  if (dtdName != "") {
56  cerr << "XMLWriter::ReferToDTD cannot be called twice" << endl;
57  }
58  dtdName = name;
59  *writer << "<!DOCTYPE " << name.c_str() << " PUBLIC \"" << pid.c_str() << "\" \"" << ref.c_str() << "\">" << endl;
60 }
const XML_Char * name
IndentPrintWriter * writer
Definition: XMLWriter.h:71
void cheprep::XMLWriter::referToDTD ( std::string  name,
std::string  system 
)

Definition at line 62 of file XMLWriter.cc.

References writer.

62  {
63  if (dtdName != "") {
64  cerr << "XMLWriter::ReferToDTD cannot be called twice";
65  }
66  dtdName = name;
67  *writer << "<!DOCTYPE " << name.c_str() << " SYSTEM \"" << system.c_str() << "\">" << endl;
68 }
const XML_Char * name
IndentPrintWriter * writer
Definition: XMLWriter.h:71
void cheprep::XMLWriter::setAttribute ( std::string  name,
char *  value 
)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 137 of file XMLWriter.cc.

Referenced by setAttribute().

137  {
138  setAttribute(name, (string)value);
139 }
const XML_Char * name
void setAttribute(std::string name, char *value)
Definition: XMLWriter.cc:137
const XML_Char int const XML_Char * value
void cheprep::XMLWriter::setAttribute ( std::string  name,
std::string  value 
)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 141 of file XMLWriter.cc.

141  {
142  attributes[name] = value;
143  // NOTE: never set type here
144 }
const XML_Char * name
const XML_Char int const XML_Char * value
void cheprep::XMLWriter::setAttribute ( std::string  name,
std::vector< double >  value 
)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 146 of file XMLWriter.cc.

References cheprep::DefaultHepRepAttValue::getAsString(), and setAttribute().

146  {
147  if (name == "value") setAttribute("type", (std::string)"Color");
149 }
const XML_Char * name
void setAttribute(std::string name, char *value)
Definition: XMLWriter.cc:137
const XML_Char int const XML_Char * value
void cheprep::XMLWriter::setAttribute ( std::string  name,
int64  value 
)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 151 of file XMLWriter.cc.

References cheprep::DefaultHepRepAttValue::getAsString(), and setAttribute().

151  {
152  if (name == "value") setAttribute("type", (std::string)"long");
154 }
const XML_Char * name
void setAttribute(std::string name, char *value)
Definition: XMLWriter.cc:137
const XML_Char int const XML_Char * value
void cheprep::XMLWriter::setAttribute ( std::string  name,
int  value 
)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 156 of file XMLWriter.cc.

References cheprep::DefaultHepRepAttValue::getAsString(), setAttribute(), and cheprep::DefaultHepRepAttValue::toShowLabel().

156  {
157  if (name == "showlabel") {
159  setAttribute("showlabel", label);
160  } else {
161  if (name == "value") setAttribute("type", (std::string)"int");
163  }
164 }
const XML_Char * name
void setAttribute(std::string name, char *value)
Definition: XMLWriter.cc:137
const XML_Char int const XML_Char * value
void cheprep::XMLWriter::setAttribute ( std::string  name,
bool  value 
)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 166 of file XMLWriter.cc.

References cheprep::DefaultHepRepAttValue::getAsString(), and setAttribute().

166  {
167  if (name == "value") setAttribute("type", (std::string)"boolean");
169 }
const XML_Char * name
void setAttribute(std::string name, char *value)
Definition: XMLWriter.cc:137
const XML_Char int const XML_Char * value
void cheprep::XMLWriter::setAttribute ( std::string  name,
double  value 
)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 171 of file XMLWriter.cc.

References cheprep::DefaultHepRepAttValue::getAsString(), and setAttribute().

171  {
172  if (name == "value") setAttribute("type", (std::string)"double");
174 }
const XML_Char * name
void setAttribute(std::string name, char *value)
Definition: XMLWriter.cc:137
const XML_Char int const XML_Char * value
void cheprep::XMLWriter::setAttribute ( std::string  ns,
std::string  name,
std::string  value 
)
inlinevirtual

Implements cheprep::AbstractXMLWriter.

Definition at line 60 of file XMLWriter.h.

References setAttribute().

60  {
61  setAttribute(ns.append(":").append(name), value);
62  }
const XML_Char * name
void setAttribute(std::string name, char *value)
Definition: XMLWriter.cc:137
const XML_Char int const XML_Char * value
#define ns
Definition: xmlparse.cc:597
void cheprep::XMLWriter::setAttribute ( std::string  ns,
std::string  name,
double  value 
)
inlinevirtual

Implements cheprep::AbstractXMLWriter.

Definition at line 63 of file XMLWriter.h.

References setAttribute().

63  {
64  setAttribute(ns.append(":").append(name), value);
65  }
const XML_Char * name
void setAttribute(std::string name, char *value)
Definition: XMLWriter.cc:137
const XML_Char int const XML_Char * value
#define ns
Definition: xmlparse.cc:597

Field Documentation

bool cheprep::XMLWriter::closed
protected

Definition at line 70 of file XMLWriter.h.

Referenced by closeDoc(), and XMLWriter().

IndentPrintWriter* cheprep::XMLWriter::writer
protected

The documentation for this class was generated from the following files: