47 #include <QTextStream> 48 #include <QDomElement> 49 #include <QDataStream> 50 #include <QStringList> 78 retval = this->getDocument(filename);
81 retval = this->readFromFile(filename);
82 this->addDocument(filename, retval);
90 QDomDocument getDocument(
const QString& filename)
92 QMutexLocker lock(&mDocumentMutex);
93 DocumentMap::iterator iter = mDocuments.find(filename);
95 if (iter != mDocuments.end())
97 return iter->second.ownerDocument();
99 return QDomDocument();
102 void addDocument(
const QString& filename, QDomDocument document)
104 QMutexLocker lock(&mDocumentMutex);
105 mDocuments[filename] = document.documentElement();
108 QDomDocument readFromFile(QString filename)
110 QFile file(filename);
111 if (!file.open(QIODevice::ReadOnly))
114 doc.appendChild(doc.createElement(
"root"));
118 QDomDocument loadedDoc;
121 if (!loadedDoc.setContent(&file, &error, &line, &col))
123 QString msg = QString(
"error setting xml content [%1,%2] %3").arg(line).arg(col).arg(error);
131 typedef std::map<QString, QDomElement> DocumentMap;
132 QMutex mDocumentMutex;
133 DocumentMap mDocuments;
143 QString XmlOptionItem::SerializeDataToB64String(
const QVariant& data)
145 QByteArray byteArray;
146 QBuffer writeBuffer(&byteArray);
147 writeBuffer.open(QIODevice::WriteOnly);
148 QDataStream out(&writeBuffer);
154 QString s = QString(byteArray.toBase64());
162 QVariant XmlOptionItem::DeserializeB64String(
const QString& serializedVariant)
164 QByteArray readArr = QByteArray::fromBase64(serializedVariant.toUtf8());
165 QBuffer readBuffer(&readArr);
166 readBuffer.open(QIODevice::ReadOnly);
167 QDataStream in(&readBuffer);
179 mUid(uid), mRoot(root)
189 QVariant val = DeserializeB64String(text);
195 QString text = SerializeDataToB64String(val);
202 QDomElement item = this->findElemFromUid(mUid, mRoot);
203 if (!item.isNull() && item.hasAttribute(
"value"))
205 return item.attribute(
"value");
214 QDomElement item = findElemFromUid(mUid, mRoot);
218 item = mRoot.ownerDocument().createElement(
"option");
219 item.setAttribute(
"id", mUid);
220 mRoot.appendChild(item);
222 item.setAttribute(
"value", val);
225 QDomElement XmlOptionItem::findElemFromUid(
const QString& uid, QDomNode root)
const 227 QDomNodeList
settings = root.childNodes();
228 for (
int i = 0; i < settings.size(); ++i)
230 QDomElement item = settings.item(i).toElement();
231 if (item.attribute(
"id") == uid)
234 return QDomElement();
244 retval.mDocument = QDomDocument();
245 retval.mCurrentElement = QDomElement();
251 mDocument.appendChild(mDocument.createElement(
"root"));
252 mCurrentElement = mDocument.documentElement();
260 mCurrentElement = mDocument.documentElement();
262 if (mCurrentElement.isNull())
264 mDocument.appendChild(mDocument.createElement(
"root"));
265 mCurrentElement = mDocument.documentElement();
280 if(mCurrentElement.isNull() || mDocument.isNull())
288 retval.mCurrentElement = mDocument.documentElement();
295 retval.mCurrentElement = retval.
getElement(element);
307 QDomElement current = retval.
getDocument().createElement(element);
308 current.setAttribute(attributeName, attributeValue);
309 retval.mCurrentElement.appendChild(current);
310 retval.mCurrentElement = current;
318 QDomNodeList presetNodeList = retval.
getElement().elementsByTagName(element);
319 for (
int i = 0; i < presetNodeList.count(); ++i)
321 QDomElement current = presetNodeList.item(i).toElement();
322 QString name = current.attribute(attributeName);
323 if (attributeValue == name)
325 retval.mCurrentElement = current;
336 retval.mCurrentElement = mCurrentElement.parentNode().toElement();
337 if (retval.mCurrentElement.isNull())
346 QDomElement child = parent.namedItem(childName).toElement();
350 child = mDocument.createElement(childName);
351 parent.appendChild(child);
369 QTextStream stream(stdout);
370 stream <<
"\nTEST" << document.toString(4) <<
"\n";
375 QTextStream stream(stdout);
377 element.save(stream, 4);
388 return mCurrentElement;
393 QDomElement elem1 = this->
safeGetElement(mCurrentElement, level1);
399 QDomElement elem1 = this->
safeGetElement(mCurrentElement, level1);
406 while (mCurrentElement.hasChildNodes())
407 mCurrentElement.removeChild(mCurrentElement.firstChild());
412 QDomNode parentNode = mCurrentElement.parentNode();
413 parentNode.removeChild(mCurrentElement);
414 mCurrentElement = QDomElement();
419 if (mFilename.isEmpty())
425 QString path = QFileInfo(mFilename).absolutePath();
427 QFile file(mFilename);
428 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate))
430 QTextStream stream(&file);
431 stream << mDocument.toString(4);
437 reportError(
"XmlOptionFile::save() Could not open " + file.fileName() +
" Error: " 438 + 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.