Geant4 9.6.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 22 of file XMLWriter.h.

Constructor & Destructor Documentation

◆ XMLWriter()

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

Definition at line 18 of file XMLWriter.cc.

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

◆ ~XMLWriter()

cheprep::XMLWriter::~XMLWriter ( )
virtual

Definition at line 26 of file XMLWriter.cc.

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

Member Function Documentation

◆ checkNameValid()

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

Definition at line 280 of file XMLWriter.cc.

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

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

◆ close()

void cheprep::XMLWriter::close ( )
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 31 of file XMLWriter.cc.

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

◆ closeDoc()

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

Implements cheprep::AbstractXMLWriter.

Definition at line 70 of file XMLWriter.cc.

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}

Referenced by close().

◆ closeTag()

void cheprep::XMLWriter::closeTag ( )
virtual

Implements cheprep::AbstractXMLWriter.

Definition at line 119 of file XMLWriter.cc.

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}

Referenced by closeDoc().

◆ normalize()

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

Definition at line 201 of file XMLWriter.cc.

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}
#define buffer
Definition: xmlparse.cc:611

Referenced by printAttributes().

◆ normalizeText()

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

Definition at line 247 of file XMLWriter.cc.

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}

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 36 of file XMLWriter.cc.

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}
std::string getIndentString() const

◆ openTag() [1/2]

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

Implements cheprep::AbstractXMLWriter.

Definition at line 107 of file XMLWriter.cc.

107 {
108 checkNameValid(name);
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}
void checkNameValid(std::string s)
Definition: XMLWriter.cc:280
void printAttributes(int tagLength)
Definition: XMLWriter.cc:176

Referenced by openTag().

◆ openTag() [2/2]

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

Implements cheprep::AbstractXMLWriter.

Definition at line 54 of file XMLWriter.h.

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

◆ print()

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

Definition at line 98 of file XMLWriter.cc.

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

Referenced by println().

◆ printAttributes()

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

Definition at line 176 of file XMLWriter.cc.

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}
std::string normalize(std::string s)
Definition: XMLWriter.cc:201

Referenced by openTag(), and printTag().

◆ printComment()

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

Definition at line 87 of file XMLWriter.cc.

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}

◆ println()

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

Definition at line 102 of file XMLWriter.cc.

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

◆ printPlain()

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

Definition at line 94 of file XMLWriter.cc.

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

◆ printTag() [1/2]

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

Implements cheprep::AbstractXMLWriter.

Definition at line 130 of file XMLWriter.cc.

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

Referenced by printTag().

◆ printTag() [2/2]

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

Implements cheprep::AbstractXMLWriter.

Definition at line 57 of file XMLWriter.h.

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

◆ referToDTD() [1/2]

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

Definition at line 54 of file XMLWriter.cc.

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}

◆ referToDTD() [2/2]

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

Definition at line 62 of file XMLWriter.cc.

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}

◆ setAttribute() [1/9]

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

Implements cheprep::AbstractXMLWriter.

Definition at line 166 of file XMLWriter.cc.

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

◆ setAttribute() [2/9]

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

Implements cheprep::AbstractXMLWriter.

Definition at line 137 of file XMLWriter.cc.

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

Referenced by setAttribute().

◆ setAttribute() [3/9]

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

Implements cheprep::AbstractXMLWriter.

Definition at line 171 of file XMLWriter.cc.

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

◆ setAttribute() [4/9]

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

Implements cheprep::AbstractXMLWriter.

Definition at line 156 of file XMLWriter.cc.

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

◆ setAttribute() [5/9]

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

Implements cheprep::AbstractXMLWriter.

Definition at line 151 of file XMLWriter.cc.

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

◆ setAttribute() [6/9]

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}

◆ setAttribute() [7/9]

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

Implements cheprep::AbstractXMLWriter.

Definition at line 146 of file XMLWriter.cc.

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

◆ setAttribute() [8/9]

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

Implements cheprep::AbstractXMLWriter.

Definition at line 63 of file XMLWriter.h.

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

◆ setAttribute() [9/9]

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.

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

Member Data Documentation

◆ closed

bool cheprep::XMLWriter::closed
protected

Definition at line 70 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: