26 #include <QTextStream> 27 #include <QDomElement> 28 #include <QDataStream> 29 #include <QStringList> 57 retval = this->getDocument(filename);
60 retval = this->readFromFile(filename);
61 this->addDocument(filename, retval);
69 QDomDocument getDocument(
const QString& filename)
71 QMutexLocker lock(&mDocumentMutex);
72 DocumentMap::iterator iter = mDocuments.find(filename);
74 if (iter != mDocuments.end())
76 return iter->second.ownerDocument();
78 return QDomDocument();
81 void addDocument(
const QString& filename, QDomDocument document)
83 QMutexLocker lock(&mDocumentMutex);
84 mDocuments[filename] = document.documentElement();
87 QDomDocument readFromFile(QString filename)
90 if (!file.open(QIODevice::ReadOnly))
93 doc.appendChild(doc.createElement(
"root"));
97 QDomDocument loadedDoc;
100 if (!loadedDoc.setContent(&file, &error, &line, &col))
102 QString msg = QString(
"error setting xml content [%1,%2] %3").arg(line).arg(col).arg(error);
110 typedef std::map<QString, QDomElement> DocumentMap;
111 QMutex mDocumentMutex;
112 DocumentMap mDocuments;
122 QString XmlOptionItem::SerializeDataToB64String(
const QVariant& data)
124 QByteArray byteArray;
125 QBuffer writeBuffer(&byteArray);
126 writeBuffer.open(QIODevice::WriteOnly);
127 QDataStream out(&writeBuffer);
133 QString s = QString(byteArray.toBase64());
141 QVariant XmlOptionItem::DeserializeB64String(
const QString& serializedVariant)
143 QByteArray readArr = QByteArray::fromBase64(serializedVariant.toUtf8());
144 QBuffer readBuffer(&readArr);
145 readBuffer.open(QIODevice::ReadOnly);
146 QDataStream in(&readBuffer);
158 mUid(uid), mRoot(root)
168 QVariant val = DeserializeB64String(text);
174 QString text = SerializeDataToB64String(val);
181 QDomElement item = this->findElemFromUid(mUid, mRoot);
182 if (!item.isNull() && item.hasAttribute(
"value"))
184 return item.attribute(
"value");
193 QDomElement item = findElemFromUid(mUid, mRoot);
197 item = mRoot.ownerDocument().createElement(
"option");
198 item.setAttribute(
"id", mUid);
199 mRoot.appendChild(item);
201 item.setAttribute(
"value", val);
204 QDomElement XmlOptionItem::findElemFromUid(
const QString& uid, QDomNode root)
const 206 QDomNodeList
settings = root.childNodes();
207 for (
int i = 0; i < settings.size(); ++i)
209 QDomElement item = settings.item(i).toElement();
210 if (item.attribute(
"id") == uid)
213 return QDomElement();
223 retval.mDocument = QDomDocument();
224 retval.mCurrentElement = QDomElement();
230 mDocument.appendChild(mDocument.createElement(
"root"));
231 mCurrentElement = mDocument.documentElement();
239 mCurrentElement = mDocument.documentElement();
241 if (mCurrentElement.isNull())
243 mDocument.appendChild(mDocument.createElement(
"root"));
244 mCurrentElement = mDocument.documentElement();
259 if(mCurrentElement.isNull() || mDocument.isNull())
267 retval.mCurrentElement = mDocument.documentElement();
274 retval.mCurrentElement = retval.
getElement(element);
286 QDomElement current = retval.
getDocument().createElement(element);
287 current.setAttribute(attributeName, attributeValue);
288 retval.mCurrentElement.appendChild(current);
289 retval.mCurrentElement = current;
297 QDomNodeList presetNodeList = retval.
getElement().elementsByTagName(element);
298 for (
int i = 0; i < presetNodeList.count(); ++i)
300 QDomElement current = presetNodeList.item(i).toElement();
301 QString name = current.attribute(attributeName);
302 if (attributeValue == name)
304 retval.mCurrentElement = current;
315 retval.mCurrentElement = mCurrentElement.parentNode().toElement();
316 if (retval.mCurrentElement.isNull())
325 QDomElement child = parent.namedItem(childName).toElement();
329 child = mDocument.createElement(childName);
330 parent.appendChild(child);
348 QTextStream stream(stdout);
349 stream <<
"\nTEST" << document.toString(4) <<
"\n";
354 QTextStream stream(stdout);
356 element.save(stream, 4);
367 return mCurrentElement;
372 QDomElement elem1 = this->
safeGetElement(mCurrentElement, level1);
378 QDomElement elem1 = this->
safeGetElement(mCurrentElement, level1);
385 while (mCurrentElement.hasChildNodes())
386 mCurrentElement.removeChild(mCurrentElement.firstChild());
391 QDomNode parentNode = mCurrentElement.parentNode();
392 parentNode.removeChild(mCurrentElement);
393 mCurrentElement = QDomElement();
398 if (mFilename.isEmpty())
404 QString path = QFileInfo(mFilename).absolutePath();
406 QFile file(mFilename);
407 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate))
409 QTextStream stream(&file);
410 stream << mDocument.toString(4);
416 reportError(
"XmlOptionFile::save() Could not open " + file.fileName() +
" Error: " 417 + file.errorString());
void reportError(QString msg)
void writeVariant(const QVariant &val)
QDomElement getElement()
return the current element
QDomDocument loadDocument(const QString &filename)
void printDocument()
print the entire document
void reportWarning(QString msg)
static XmlOptionFile createNull()
create an empty document
QDomElement safeGetElement(QDomElement parent, QString childName)
XmlOptionFile ascend() const
step one level up in the xml tree
Settings * settings()
Shortcut for accessing the settings instance.
void printElement()
print just the current element
static SharedDocuments * getInstance()
void writeValue(const QString &val)
QDomDocument getDocument()
returns the document
void deleteNode()
Delete the current node.
void removeChildren()
remove all child nodes of the current element.
void save()
save entire document.
QVariant readVariant(const QVariant &defval=QVariant()) const
bool isNull() const
checks if this is null
XmlOptionFile root() const
set the current element to root
XmlOptionFile tryDescend(QString element, QString attributeName, QString attributeValue) const
Helper class for xml files used to store ssc/cx data.
QString readValue(const QString &defval) const
XmlOptionFile descend(QString element) const
step one level down in the xml tree
Namespace for all CustusX production code.