Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
cheprep::XMLWriter Class Reference

#include <XMLWriter.h>

+ Inheritance diagram for cheprep::XMLWriter:

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 ()
 
virtual void openTag (std::string ns, std::string name)=0
 
virtual void printTag (std::string ns, std::string name)=0
 
virtual void setAttribute (std::string ns, std::string name, std::string value)=0
 
virtual void setAttribute (std::string ns, std::string name, double value)=0
 
virtual void close ()=0
 
virtual void openDoc (std::string version="1.0", std::string encoding="", bool standalone=false)=0
 
virtual void closeDoc (bool force=false)=0
 
virtual void openTag (std::string name)=0
 
virtual void closeTag ()=0
 
virtual void printTag (std::string name)=0
 
virtual void setAttribute (std::string name, char *value)=0
 
virtual void setAttribute (std::string name, std::string value)=0
 
virtual void setAttribute (std::string name, std::vector< double > value)=0
 
virtual void setAttribute (std::string name, int64 value)=0
 
virtual void setAttribute (std::string name, int value)=0
 
virtual void setAttribute (std::string name, bool value)=0
 
virtual void setAttribute (std::string name, double value)=0
 

Protected Attributes

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

Detailed Description

Definition at line 21 of file XMLWriter.h.

Constructor & Destructor Documentation

◆ XMLWriter()

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

Definition at line 17 of file XMLWriter.cc.

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

◆ ~XMLWriter()

cheprep::XMLWriter::~XMLWriter ( )
virtual

Definition at line 25 of file XMLWriter.cc.

25 {
26 writer->close();
27 delete writer;
28}

Member Function Documentation

◆ checkNameValid()

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

Definition at line 279 of file XMLWriter.cc.

279 {
280// Could be added.
281// if (!XMLCharacterProperties.validName(s)) throw new RuntimeException("Invalid name: "+s);
282}

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

◆ close()

void cheprep::XMLWriter::close ( )
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 30 of file XMLWriter.cc.

30 {
31 closeDoc();
32 writer->close();
33}
void closeDoc(bool force=false)
Definition: XMLWriter.cc:69

◆ closeDoc()

void cheprep::XMLWriter::closeDoc ( bool  force = false)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 69 of file XMLWriter.cc.

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

Referenced by close().

◆ closeTag()

void cheprep::XMLWriter::closeTag ( )
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 118 of file XMLWriter.cc.

118 {
119 if (openTags.empty()) {
120 writer->close();
121 cerr << "XMLWriter::closeTag(), No open tags" << endl;
122 }
123 string name = openTags.top();
124 openTags.pop();
125 writer->outdent();
126 *writer << "</" << name.c_str() << ">" << endl;
127}
const char * name(G4int ptype)

Referenced by closeDoc().

◆ normalize()

string cheprep::XMLWriter::normalize ( std::string  s)

Definition at line 200 of file XMLWriter.cc.

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

Referenced by printAttributes().

◆ normalizeText()

string cheprep::XMLWriter::normalizeText ( std::string  s)

Definition at line 246 of file XMLWriter.cc.

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

Referenced by print(), and printComment().

◆ openDoc()

void cheprep::XMLWriter::openDoc ( std::string  version = "1.0",
std::string  encoding = "",
bool  standalone = false 
)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 35 of file XMLWriter.cc.

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

◆ openTag() [1/2]

void cheprep::XMLWriter::openTag ( std::string  name)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 106 of file XMLWriter.cc.

106 {
107 checkNameValid(name);
108 if (openTags.empty() && dtdName.compare("") && dtdName.compare(name)) {
109 cerr << "XMLWriter::openTag(), First tag: '" << name << "' not equal to DTD id: '" << dtdName << "'" << endl;
110 }
111 *writer << "<" << name.c_str();
112 printAttributes(name.length());
113 *writer << ">" << endl;
114 writer->indent();
115 openTags.push(name);
116}
void checkNameValid(std::string s)
Definition: XMLWriter.cc:279
void printAttributes(int tagLength)
Definition: XMLWriter.cc:175

Referenced by openTag().

◆ openTag() [2/2]

void cheprep::XMLWriter::openTag ( std::string  ns,
std::string  name 
)
inlinevirtual

Implements cheprep::AbstractXMLWriter.

Definition at line 53 of file XMLWriter.h.

53 {
54 openTag(ns == defaultNameSpace ? name : ns.append(":").append(name));
55 }
void openTag(std::string name)
Definition: XMLWriter.cc:106
#define ns
Definition: xmlparse.cc:614

◆ print()

void cheprep::XMLWriter::print ( std::string  text)

Definition at line 97 of file XMLWriter.cc.

97 {
98 *writer << normalizeText(text).c_str();
99}
std::string normalizeText(std::string s)
Definition: XMLWriter.cc:246

Referenced by println().

◆ printAttributes()

void cheprep::XMLWriter::printAttributes ( int  tagLength)

Definition at line 175 of file XMLWriter.cc.

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

Referenced by openTag(), and printTag().

◆ printComment()

void cheprep::XMLWriter::printComment ( std::string  comment)

Definition at line 86 of file XMLWriter.cc.

86 {
87 if (comment.find("--") != string::npos) {
88 cerr << "XMLWriter::printComment '--' sequence not allowed in comment" << endl;
89 }
90 *writer << "<!--" << normalizeText(comment).c_str() << "-->" << endl;
91}

◆ println()

void cheprep::XMLWriter::println ( std::string  text)

Definition at line 101 of file XMLWriter.cc.

101 {
102 print(text);
103 *writer << endl;
104}
void print(std::string text)
Definition: XMLWriter.cc:97

◆ printPlain()

void cheprep::XMLWriter::printPlain ( std::string  text)

Definition at line 93 of file XMLWriter.cc.

93 {
94 *writer << text.c_str();
95}

◆ printTag() [1/2]

void cheprep::XMLWriter::printTag ( std::string  name)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 129 of file XMLWriter.cc.

129 {
130 checkNameValid(name);
131 *writer << "<" << name.c_str();
132 printAttributes(name.length());
133 *writer << "/>" << endl;
134}

Referenced by printTag().

◆ printTag() [2/2]

void cheprep::XMLWriter::printTag ( std::string  ns,
std::string  name 
)
inlinevirtual

Implements cheprep::AbstractXMLWriter.

Definition at line 56 of file XMLWriter.h.

56 {
57 printTag(ns == defaultNameSpace ? name : ns.append(":").append(name));
58 }
void printTag(std::string name)
Definition: XMLWriter.cc:129

◆ referToDTD() [1/2]

void cheprep::XMLWriter::referToDTD ( std::string  name,
std::string  pid,
std::string  ref 
)

Definition at line 53 of file XMLWriter.cc.

53 {
54 if (dtdName != "") {
55 cerr << "XMLWriter::ReferToDTD cannot be called twice" << endl;
56 }
57 dtdName = name;
58 *writer << "<!DOCTYPE " << name.c_str() << " PUBLIC \"" << pid.c_str() << "\" \"" << ref.c_str() << "\">" << endl;
59}

◆ referToDTD() [2/2]

void cheprep::XMLWriter::referToDTD ( std::string  name,
std::string  system 
)

Definition at line 61 of file XMLWriter.cc.

61 {
62 if (dtdName != "") {
63 cerr << "XMLWriter::ReferToDTD cannot be called twice";
64 }
65 dtdName = name;
66 *writer << "<!DOCTYPE " << name.c_str() << " SYSTEM \"" << system.c_str() << "\">" << endl;
67}

◆ setAttribute() [1/9]

void cheprep::XMLWriter::setAttribute ( std::string  name,
bool  value 
)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 165 of file XMLWriter.cc.

165 {
166 if (name == "value") setAttribute("type", (std::string)"boolean");
168}
void setAttribute(std::string name, char *value)
Definition: XMLWriter.cc:136

◆ setAttribute() [2/9]

void cheprep::XMLWriter::setAttribute ( std::string  name,
char *  value 
)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 136 of file XMLWriter.cc.

136 {
137 setAttribute(name, (string)value);
138}

Referenced by setAttribute().

◆ setAttribute() [3/9]

void cheprep::XMLWriter::setAttribute ( std::string  name,
double  value 
)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 170 of file XMLWriter.cc.

170 {
171 if (name == "value") setAttribute("type", (std::string)"double");
173}

◆ setAttribute() [4/9]

void cheprep::XMLWriter::setAttribute ( std::string  name,
int  value 
)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 155 of file XMLWriter.cc.

155 {
156 if (name == "showlabel") {
157 string label = DefaultHepRepAttValue::toShowLabel(value);
158 setAttribute("showlabel", label);
159 } else {
160 if (name == "value") setAttribute("type", (std::string)"int");
162 }
163}

◆ setAttribute() [5/9]

void cheprep::XMLWriter::setAttribute ( std::string  name,
int64  value 
)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 150 of file XMLWriter.cc.

150 {
151 if (name == "value") setAttribute("type", (std::string)"long");
153}

◆ setAttribute() [6/9]

void cheprep::XMLWriter::setAttribute ( std::string  name,
std::string  value 
)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 140 of file XMLWriter.cc.

140 {
141 attributes[name] = value;
142 // NOTE: never set type here
143}

◆ setAttribute() [7/9]

void cheprep::XMLWriter::setAttribute ( std::string  name,
std::vector< double >  value 
)
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 145 of file XMLWriter.cc.

145 {
146 if (name == "value") setAttribute("type", (std::string)"Color");
148}

◆ setAttribute() [8/9]

void cheprep::XMLWriter::setAttribute ( std::string  ns,
std::string  name,
double  value 
)
inlinevirtual

Implements cheprep::AbstractXMLWriter.

Definition at line 62 of file XMLWriter.h.

62 {
63 setAttribute(ns.append(":").append(name), value);
64 }

◆ setAttribute() [9/9]

void cheprep::XMLWriter::setAttribute ( std::string  ns,
std::string  name,
std::string  value 
)
inlinevirtual

Implements cheprep::AbstractXMLWriter.

Definition at line 59 of file XMLWriter.h.

59 {
60 setAttribute(ns.append(":").append(name), value);
61 }

Member Data Documentation

◆ closed

bool cheprep::XMLWriter::closed
protected

Definition at line 69 of file XMLWriter.h.

Referenced by closeDoc(), and XMLWriter().

◆ writer

IndentPrintWriter* cheprep::XMLWriter::writer
protected

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