Commit a9d4efc3 authored by Oliver Kellogg's avatar Oliver Kellogg
Browse files

umbrello/umldoc.h

- Add ENC_WINDOWS for use by encoding(QIODevice&)

umbrello/umldoc.cpp
- short UMLDoc::encoding(QIODevice & file) :
  - Introduce variable 'enc' with initial value ENC_UNKNOWN
  - In loop advancing over comments and processing instructions, if
    node.isProcessingInstruction() then analyze the
    node.toProcessingInstruction().data(); if attribute "encoding" is
    found then extract its value. Assign ENC_UNICODE to enc if value is
    "UTF-8"; assign ENC_WINDOWS to enc if value is "windows-1252"; else
    assign ENC_OLD_ENC to enc (indicating a not yet implemented encoding.)
  - Change uWarning() messages added in commit 732b50aa to uDebug()
  - For premature returns, return variable 'enc' instead of ENC_UNKNOWN.
    Reason: Returning ENC_UNKNOWN here is overly strict, it precludes any
    further attempts at loading foreign XMI files. Rather, use the encoding
    attribute from the processing instruction if found.
- bool UMLDoc::loadFromXMI(QIODevice & file, short encode) :
  Extend check which makes sure it is an XMI file to permit "xmi:XMI" as
  root.tagName()

CCBUG:56184
parent c65ae98e
......@@ -1848,35 +1848,52 @@ short UMLDoc::encoding(QIODevice & file)
// we start at the beginning and go to the point in the header where we can
// find out if the file was saved using Unicode
QDomNode node = doc.firstChild();
short enc = ENC_UNKNOWN;
while (node.isComment() || node.isProcessingInstruction()) {
if (node.isProcessingInstruction()) {
const QDomProcessingInstruction& pi = node.toProcessingInstruction();
QRegExp rx("\\bencoding=['\"]([^'\"]+)['\"]");
const int pos = rx.indexIn(pi.data());
if (pos >= 0) {
const QString& encData = rx.cap(1);
if (encData == "UTF-8") {
enc = ENC_UNICODE;
} else if (encData == "windows-1252") {
enc = ENC_WINDOWS;
} else {
uDebug() << "ProcessingInstruction encoding=" << encData << " is not yet implemented";
enc = ENC_OLD_ENC;
}
}
}
node = node.nextSibling();
}
QDomElement root = node.toElement();
if (root.isNull()) {
uWarning() << "Null element at " << node.nodeName() << " : " << node.nodeValue();
return ENC_UNKNOWN;
uDebug() << "Null element at " << node.nodeName() << " : " << node.nodeValue();
return enc;
}
// make sure it is an XMI file
if (root.tagName() != "XMI" && root.tagName() != "xmi:XMI") {
uWarning() << "Unknown tag at " << root.tagName();
return ENC_UNKNOWN;
uDebug() << "Unknown tag at " << root.tagName();
return enc;
}
if (node.firstChild().isNull()) {
uWarning() << "No child at " << node.nodeName() << " : " << node.nodeValue();
return ENC_UNKNOWN;
uDebug() << "No child at " << node.nodeName() << " : " << node.nodeValue();
return enc;
}
node = node.firstChild();
QDomElement element = node.toElement();
// check header
if (element.isNull()) {
uWarning() << "No element at " << node.nodeName() << " : " << node.nodeValue();
return ENC_UNKNOWN;
uDebug() << "No element at " << node.nodeName() << " : " << node.nodeValue();
return enc;
}
if (element.tagName() != "XMI.header") {
uWarning() << "Expecting XMI.header at " << element.tagName();
return ENC_UNKNOWN;
uDebug() << "Expecting XMI.header at " << element.tagName();
return enc;
}
QDomNode headerNode = node.firstChild();
......@@ -1896,7 +1913,7 @@ short UMLDoc::encoding(QIODevice & file)
// mark a file as saved with Unicode
if (! docuElement.isNull() &&
docuElement.tagName() == "XMI.exporterEncoding") {
// at the moment this if isn't really necessary, but maybe
// at the moment this isn't really necessary but maybe
// later we will have other encoding standards
if (docuElement.text() == QString("UnicodeUTF8")) {
return ENC_UNICODE; // stop here
......@@ -1932,6 +1949,8 @@ bool UMLDoc::loadFromXMI(QIODevice & file, short encode)
QTextStream stream(&file);
if (encode == ENC_UNICODE) {
stream.setCodec("UTF-8");
} else if (encode == ENC_WINDOWS) {
stream.setCodec("windows-1252");
}
QString data = stream.readAll();
......@@ -1956,7 +1975,7 @@ bool UMLDoc::loadFromXMI(QIODevice & file, short encode)
return false;
}
// make sure it is an XMI file
if(root.tagName() != "XMI") {
if (root.tagName() != "XMI" && root.tagName() != "xmi:XMI") {
return false;
}
......
......@@ -32,7 +32,8 @@
#define ENC_UNKNOWN 0
#define ENC_UNICODE 1
#define ENC_OLD_ENC 2
#define ENC_WINDOWS 2
#define ENC_OLD_ENC 3
// forward declarations
class QDomNode;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment