3 #include "cheprep/config.h"
7 #include "cheprep/DefaultHepRepAttValue.h"
8 #include "cheprep/XMLWriter.h"
18 XMLWriter::XMLWriter(ostream* out,
string indentString,
string aDefaultNameSpace)
19 : AbstractXMLWriter(aDefaultNameSpace) {
20 writer =
new IndentPrintWriter(out);
21 writer->setIndentString(indentString);
26 XMLWriter::~XMLWriter() {
31 void XMLWriter::close() {
36 void XMLWriter::openDoc(
string version,
string encoding,
bool standalone) {
37 string indentString = writer->getIndentString();
38 writer->setIndentString(indentString);
41 *writer <<
"<?xml version=\"" << version.c_str() <<
"\" ";
42 if (encoding.compare(
"") != 0) {
44 *writer <<
"encoding=\"" << encoding.c_str() <<
"\" ";
47 *writer <<
"standalone=\"yes\" ";
51 writer->setIndentString(indentString);
54 void XMLWriter::referToDTD(
string name,
string pid,
string ref) {
56 cerr <<
"XMLWriter::ReferToDTD cannot be called twice" << endl;
59 *writer <<
"<!DOCTYPE " << name.c_str() <<
" PUBLIC \"" << pid.c_str() <<
"\" \"" << ref.c_str() <<
"\">" << endl;
62 void XMLWriter::referToDTD(
string name,
string system) {
64 cerr <<
"XMLWriter::ReferToDTD cannot be called twice";
67 *writer <<
"<!DOCTYPE " << name.c_str() <<
" SYSTEM \"" << system.c_str() <<
"\">" << endl;
70 void XMLWriter::closeDoc(
bool force) {
72 if (!openTags.empty()) {
73 if (!force) cerr <<
"Not all tags were closed before closing XML document:" << endl;
74 while (!openTags.empty()) {
78 cerr <<
" </" << openTags.top().c_str() <<
">" << endl;
87 void XMLWriter::printComment(
string comment) {
88 if (comment.find(
"--") != string::npos) {
89 cerr <<
"XMLWriter::printComment '--' sequence not allowed in comment" << endl;
91 *writer <<
"<!--" << normalizeText(comment).c_str() <<
"-->" << endl;
94 void XMLWriter::printPlain(
string text) {
95 *writer << text.c_str();
99 *writer << normalizeText(text).c_str();
102 void XMLWriter::println(
string text) {
107 void XMLWriter::openTag(
string name) {
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;
112 *writer <<
"<" << name.c_str();
113 printAttributes(name.length());
114 *writer <<
">" << endl;
119 void XMLWriter::closeTag() {
120 if (openTags.empty()) {
122 cerr <<
"XMLWriter::closeTag(), No open tags" << endl;
124 string name = openTags.top();
127 *writer <<
"</" << name.c_str() <<
">" << endl;
130 void XMLWriter::printTag(
string name) {
131 checkNameValid(name);
132 *writer <<
"<" << name.c_str();
133 printAttributes(name.length());
134 *writer <<
"/>" << endl;
137 void XMLWriter::setAttribute(
string name,
char* value) {
138 setAttribute(name, (
string)value);
141 void XMLWriter::setAttribute(
string name,
string value) {
142 attributes[
name] = value;
146 void XMLWriter::setAttribute(std::string name, std::vector<double> value) {
147 if (name ==
"value") setAttribute(
"type", (std::string)
"Color");
148 setAttribute(name, DefaultHepRepAttValue::getAsString(value));
151 void XMLWriter::setAttribute(std::string name, int64 value) {
152 if (name ==
"value") setAttribute(
"type", (std::string)
"long");
153 setAttribute(name, DefaultHepRepAttValue::getAsString(value));
156 void XMLWriter::setAttribute(std::string name,
int value) {
157 if (name ==
"showlabel") {
158 string label = DefaultHepRepAttValue::toShowLabel(value);
159 setAttribute(
"showlabel", label);
161 if (name ==
"value") setAttribute(
"type", (std::string)
"int");
162 setAttribute(name, DefaultHepRepAttValue::getAsString(value));
166 void XMLWriter::setAttribute(std::string name,
bool value) {
167 if (name ==
"value") setAttribute(
"type", (std::string)
"boolean");
168 setAttribute(name, DefaultHepRepAttValue::getAsString(value));
171 void XMLWriter::setAttribute(
string name,
double value) {
172 if (name ==
"value") setAttribute(
"type", (std::string)
"double");
173 setAttribute(name, DefaultHepRepAttValue::getAsString(value));
176 void XMLWriter::printAttributes(
int tagLength) {
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;
182 string value = normalize(i->second);
183 int length = key.length() + value.length() + 3;
184 if (width > 0 && width + length + 2*writer->getIndent() > 60) {
195 *writer << key.c_str() <<
"=\"" << value.c_str() <<
"\"";
198 if (extraIndent) writer->outdent();
201 string XMLWriter::normalize(
string s) {
205 int len = s.length();
206 for (
int i = 0; i < len; i++) {
222 str.append(
""");
227 sprintf(buffer,
"&#%ud", ch);
247 string XMLWriter::normalizeText(
string s) {
250 int len = s.length();
251 for (
int i = 0; i < len; i++) {
280 void XMLWriter::checkNameValid(
string) {
void print(G4double elem)