Exemple #1
0
//! function
static bool nodeToVariant(const QDomNode &aNode,QVariant &aValue) {
    bool vRetval = false;
    QString vType, vValue;

    aValue = QVariant();
    if(!vRetval && aNode.isCDATASection()) {
        vValue = aNode.toCDATASection().data();
        vRetval = true;
    }

    if(!vRetval && aNode.isText()) {
        vValue = aNode.toText().data();
        vRetval = true;
    }

    if(!vRetval) return vRetval;
    if(vValue.isEmpty()) return false; // ????

    const QDomNode vParent = aNode.parentNode();
    if(vParent.isElement()) {
        vType = vParent.toElement().attribute(QString::fromLatin1("type"));
    }

    if(vType == QString::fromLatin1("bytearray")) {
        aValue = QVariant(vValue.toLatin1());
    }
    else if(vType == QString::fromLatin1("variant")) {
        QByteArray vArray(vValue.toLatin1());
        QDataStream vStream(&vArray, QIODevice::ReadOnly);
        vStream >> aValue;
    }
Exemple #2
0
bool QDomNodeProto:: isCDATASection() const
{
  QDomNode *item = qscriptvalue_cast<QDomNode*>(thisObject());
  if (item)
    return item->isCDATASection();
  return false;
}
/**
 * @brief XmlEditWidgetPrivate::findDomNodeScan find the nearest match to a position
 * @param node
 * @param nodeTarget
 * @param lineSearched
 * @param columnSearched
 * @param lastKnownNode: last known "good" position
 * @return
 */
bool XmlEditWidgetPrivate::findDomNodeScan(QDomNode node, QDomNode nodeTarget, const int lineSearched, const int columnSearched, FindNodeWithLocationInfo &info)
{
    int row = node.lineNumber();
    int col = node.columnNumber();
    if(!node.isDocument()) {
        if((lineSearched == row) && (columnSearched == col)) {
            info.matchedNode = nodeTarget ;
            return true ;
        }

        if((lineSearched == row) && (columnSearched == col)) {
            info.matchedNode = nodeTarget ;
            return true ;
        }
        if((lineSearched == row) && (columnSearched < col)) {
            info.matchedNode = nodeTarget ;
            return true ;
        }
        if((lineSearched < row)) {
            info.matchedNode = nodeTarget ;
            return true ;
        }
        if((lineSearched <= row)) {
            info.lastKnownNode = nodeTarget ;
        }

        if(node.nodeType() == QDomNode::ElementNode) {
            QDomElement element = node.toElement();
            QDomNamedNodeMap attributes = element.attributes();
            int numAttrs = attributes.length();
            for(int i = 0 ; i < numAttrs ; i++) {
                QDomNode node = attributes.item(i);
                QDomAttr attr = node.toAttr();
                if(findDomNodeScan(attr, nodeTarget, lineSearched, columnSearched, info)) {
                    return true;
                }
            } // for
        }
    }

    int nodes = node.childNodes().count();
    for(int i = 0 ; i < nodes ; i ++) {
        QDomNode childNode = node.childNodes().item(i) ;
        if(childNode.isText() || childNode.isCDATASection()) {
            if(findDomNodeScan(childNode, nodeTarget, lineSearched, columnSearched, info)) {
                return true;
            }
        } else {
            if(findDomNodeScan(childNode, childNode, lineSearched, columnSearched, info)) {
                return true ;
            }
        }
    }
    return false ;
}
bool Ersky9xInterface::loadModelDataXML(QDomDocument * qdoc, ModelData *model, int modelNum, int stickMode)
{
  T ersky9xModel;
  memset(&ersky9xModel, 0, sizeof(ersky9xModel));

  //look for MODEL_DATA with modelNum attribute.
  //if modelNum = -1 then just pick the first one
  QDomNodeList ndl = qdoc->elementsByTagName("MODEL_DATA");

  //cycle through nodes to find correct model number
  QDomNode k = ndl.at(0);
  if (modelNum>=0) {
    while(!k.isNull()) {
      int a = k.toElement().attribute("number").toInt();
      if(a==modelNum)
        break;
      k = k.nextSibling();
    }
  }

  if (k.isNull()) // couldn't find
    return false;

  //load cdata into tgen
  QDomNode n = k.toElement().elementsByTagName("Data").at(0).firstChild();// get all children in Data
  while (!n.isNull()) {
    if (n.isCDATASection()) {
      QString ds = n.toCDATASection().data();
      QByteArray ba = QByteArray::fromBase64(ds.toAscii());
      const char * data = ba.data();
      memcpy(&ersky9xModel, data, std::min(ba.size(), (int)sizeof(ersky9xModel)));
      break;
    }
    n = n.nextSibling();
  }

  applyStickModeToModel(ersky9xModel, stickMode);
  *model = ersky9xModel;
  return true;
}
bool Ersky9xInterface::loadGeneralDataXML(QDomDocument * qdoc, Ersky9xGeneral * tgen)
{
  //look for "GENERAL_DATA" tag
  QDomElement gde = qdoc->elementsByTagName("GENERAL_DATA").at(0).toElement();

  if(gde.isNull()) // couldn't find
    return false;

  //load cdata into tgen
  QDomNode n = gde.elementsByTagName("Data").at(0).firstChild();// get all children in Data
  while (!n.isNull()) {
    if (n.isCDATASection()) {
      QString ds = n.toCDATASection().data();
      QByteArray ba = QByteArray::fromBase64(ds.toAscii());
      const char * data = ba.data();
      memcpy(tgen, data, std::min((unsigned int)ba.size(), (unsigned int)sizeof(Ersky9xGeneral)));
      break;
    }
    n = n.nextSibling();
  }
  //check version?
  return true;
}
void ReadAndWriteXML::parseEntry(const QDomElement &element, QTreeWidgetItem *parent, int parentLevel)
{
 //   item->setText(0, element.attribute("term"));
    QDomNode node = element.firstChild();
    while (!node.isNull()) {
        //qDebug() << "DomParser::parseEntry  node " << node.toElement().tagName();
    //    qDebug() << "DomParser::parseEntry  parsing node " << node.nodeName();
         if (node.isElement()) {
            //qDebug() << "DomParser::parseEntry "<< node.nodeName()<< " is element ";
            QTreeWidgetItem *childitem=new QTreeWidgetItem(parent, QStringList(QString("Type: %1").arg(node.nodeName())));
            parseEntry(node.toElement(), childitem,++parentLevel);
            QString _attr="";
            if (node.hasAttributes()) {
               childitem=new QTreeWidgetItem(childitem, QStringList(QString("Attributes node %1").arg(node.nodeName())));
               parseAttribute(node.attributes(), childitem, parentLevel);
            }
            node = node.nextSibling();
            continue;
         }
         if (node.isCDATASection()) {
             //qDebug() << "DomParser::parseEntry "<< node.nodeName()<< " is CDATASection --" << node.toText().data() << "--";
             new QTreeWidgetItem(parent, QStringList(QString("Type: %1 Value: %2").arg(node.nodeName(),node.toText().data())));
             node = node.nextSibling();
             continue;
         }
         if (node.isComment()) {
            //qDebug() << "DomParser::parseEntry "<< node.nodeName()<< " is comment --" << node.toText().data() << "--";
            new QTreeWidgetItem(parent, QStringList(QString("Type: %1 Value: %2").arg(node.nodeName(),node.toText().data())));
            node = node.nextSibling();
            continue;
         }
         if (node.isText()) {
            //qDebug() << "DomParser::parseEntry "<< node.nodeName()<< " text --" << node.toText().data() << "--";
            new QTreeWidgetItem(parent, QStringList(QString("Type: %1 Value: %2").arg(node.nodeName(),node.toText().data())));
            node = node.nextSibling();
            continue;
         }
         if (node.isDocument()) {
            //qDebug() << "DomParser::parseEntry "<< node.nodeName()<< " is document ";
            QTreeWidgetItem *childitem=new QTreeWidgetItem(parent, QStringList(QString("Type: %1").arg(node.nodeName())));
            parseEntry(node.toElement(), childitem,++parentLevel);
            node = node.nextSibling();
            continue;
         }
         if (node.isDocumentType()) {
            qDebug() << "DomParser::parseEntry "<< node.nodeName()<< " is  document type";
            QTreeWidgetItem *childitem=new QTreeWidgetItem(parent, QStringList(QString("Type: %1").arg(node.nodeName())));
            parseEntry(node.toElement(), childitem,++parentLevel);
            node = node.nextSibling();
            continue;
         }
         if (node.isDocumentFragment()) {
            qDebug() << "DomParser::parseEntry "<< node.nodeName()<< " is  document fragment";
            QTreeWidgetItem *childitem=new QTreeWidgetItem(parent, QStringList(QString("Type: %1").arg(node.nodeName())));
            parseEntry(node.toElement(), childitem,++parentLevel);
            node = node.nextSibling();
            continue;
         }
         if (node.isEntity() || node.isEntityReference () || node.isNotation () || node.isProcessingInstruction ()) {
             qDebug() << "DomParser::parseEntry "<< node.nodeName()<< " is  not supported";
             node = node.nextSibling();
             continue;
         }

        node = node.nextSibling();
    }
}
bool ImageOptions::loadXmlElement(QDomElement element)
{
  bool result = false;

  auto readStringFromNode = [this](QDomElement element, const QString elementName, QString * result) {
    if (element.tagName() == elementName) {
      QDomNode dataNode = element.firstChild();

      if (dataNode.isCDATASection()) {
        QDomCDATASection cdataSection = dataNode.toCDATASection();
        QString strValue = cdataSection.data();
        *result = this->unescapeEmpty(strValue);
      } else {
        QString strValue = element.text();
        *result = this->unescapeEmpty(strValue);
      }
    }
  };


  QDomNode nodeSett = element.firstChild();

  while (!nodeSett.isNull()) {
    QDomElement e = nodeSett.toElement();

    if (e.tagName() == ImageOptions::GroupName) {
      break;
    }

    nodeSett = nodeSett.nextSibling();
  }

  if (nodeSett.isNull()) {
    return result;
  }

  quint32 uBytesOrder = 0, uBlockSize = 0, uBlockDefaultOnes = 0, uSplitToRows = 0;
  quint32 uCompressionRle = 0, uCompressionRleMinLength = 2;
  QString sBlockPrefix = "0x", sBlockSuffix, sBlockDelimiter = ", ";
  QString sPreviewPrefix, sPreviewSuffix, sPreviewDelimiter, sPreviewLevels;

  QDomNode nodeValue = nodeSett.firstChild();

  while (!nodeValue.isNull()) {
    QDomElement e = nodeValue.toElement();

    if (!e.isNull()) {
      if (e.tagName() == ImageOptions::FieldBlockSize) {
        QString str = e.text();
        uBlockSize = str.toUInt(&result);
      }

      if (e.tagName() == ImageOptions::FieldBytesOrder) {
        QString str = e.text();
        uBytesOrder = str.toUInt(&result);
      }

      if (e.tagName() == ImageOptions::FieldSplitToRows) {
        QString str = e.text();
        uSplitToRows = str.toUInt(&result);
      }

      if (e.tagName() == ImageOptions::FieldCompressionRle) {
        QString str = e.text();
        uCompressionRle = str.toUInt(&result);
      }

      if (e.tagName() == ImageOptions::FieldCompressionRleMinLength) {
        QString str = e.text();
        uCompressionRleMinLength = str.toUInt(&result);
      }

      if (e.tagName() == ImageOptions::FieldBlockDefaultOnes) {
        QString str = e.text();
        uBlockDefaultOnes = str.toUInt(&result);
      }

      readStringFromNode(e, ImageOptions::FieldBlockPrefix, &sBlockPrefix);
      readStringFromNode(e, ImageOptions::FieldBlockSuffix, &sBlockSuffix);
      readStringFromNode(e, ImageOptions::FieldBlockDelimiter, &sBlockDelimiter);
      readStringFromNode(e, ImageOptions::FieldPreviewPrefix, &sPreviewPrefix);
      readStringFromNode(e, ImageOptions::FieldPreviewSuffix, &sPreviewSuffix);
      readStringFromNode(e, ImageOptions::FieldPreviewDelimiter, &sPreviewDelimiter);
      readStringFromNode(e, ImageOptions::FieldPreviewLevels, &sPreviewLevels);

      if (!result) {
        break;
      }
    }

    nodeValue = nodeValue.nextSibling();
  }

  if (result) {
    this->setBlockSize((Parsing::Conversion::Options::DataBlockSize)uBlockSize);
    this->setBlockDefaultOnes((bool)uBlockDefaultOnes);
    this->setBytesOrder((Parsing::Conversion::Options::BytesOrder)uBytesOrder);
    this->setSplitToRows((bool)uSplitToRows);
    this->setCompressionRle((bool)uCompressionRle);
    this->setCompressionRleMinLength(uCompressionRleMinLength);
    this->setBlockPrefix(sBlockPrefix);
    this->setBlockSuffix(sBlockSuffix);
    this->setBlockDelimiter(sBlockDelimiter);
    this->setPreviewPrefix(sPreviewPrefix);
    this->setPreviewSuffix(sPreviewSuffix);
    this->setPreviewDelimiter(sPreviewDelimiter);
    this->setPreviewLevels(sPreviewLevels);
  }

  return result;
}
Exemple #8
0
QString MessageValidator::validateMessage(QString message, bool* illformed, HTMLTextFormatter* formatter) {

    //    qDebug() << "IMG val0" << message;
    QDomDocument doc("document");
    *illformed = false;

    QString errorMessage;
    int line, column;
    QDomDocument tmpDoc; //used by textformatter

    xmlSource.setData(message);

    if (!doc.setContent(&xmlSource, &xmlReader, &errorMessage, &line, &column)) {
        qDebug() << errorMessage << " " << line << " " << column << message;
        *illformed = true;
        qDebug() << "WARNING: MessageValidator::validateMessage() - illformed message";
        return "illformed message!!!";
    }

    //now DOM tree will be traversed in preorder. 
    QStack<QDomElement> stack; //current element, QStack is used to avoid possible stack overflow in ordinary recursion
    stack.push(doc.documentElement());

    while (!stack.empty()) {
        QDomElement cur = stack.top();
        stack.pop();

        // Traverse through DOM Tree(cur), cut off bad elements/attributes 
        // and format text nodes using textFormatter

        //    qDebug() << QString(4, ' ') << cur.tagName();

        QString parentName = cur.tagName();
        NodeInfo curNI = allowed[parentName];

        //delete disallowed attributes
        for (int i = 0; i < cur.attributes().count(); i++) {
            QString attrName = cur.attributes().item(i).toAttr().name();

            if (!curNI.allowedAttributes.contains(attrName)) {
                //     qDebug() << "VALIDATIN ERR" << "TA" << attrName  << " in " << parentName;
                //   qDebug() << "note allowed attributes are:" << curNI.allowedAttributes;

                cur.attributes().removeNamedItem(attrName);
                i--;
            }
        }

        QDomNodeList children = cur.childNodes();

        for (int i = children.size() - 1; i >= 0; i--) {
            QDomNode node = children.at(i);

            if (node.isElement()) {
                QString childName = node.toElement().tagName();

                if (childName == "a") { // always show hyperlink destination
                    QString href = node.toElement().attribute("href");
                    node.appendChild(doc.createTextNode(" [ " + href + " ]"));
                }

                if (childName == "style") { //NOTE: this action is not XHTML-IM compliant! (css rules should be displayed, but it's stupid)
                    cur.removeChild(node);
                }
                else if (childName == "img") { //disabling images until they are whitelisted

                    QString href = node.toElement().attribute("src");

                    QDomElement newElement = doc.createElement("a");
                    newElement.setAttribute("class", "psi_disabled_image");
                    newElement.setAttribute("href", "javascript:psi_addToWhiteList('" + href + "')");
                    newElement.appendChild(doc.createTextNode("[ click here to display: " + href + " ]"));

                    cur.replaceChild(newElement, node);
                }
                else if (!curNI.allowedTags.contains(childName)) {//is subElement valid here?

                    qDebug() << "VALIDATIN ERR" << "TS" << childName << " in " << parentName;
                    qDebug() << "note allowed subElements are:" << curNI.allowedTags;

                    //append bad node's children (they will be validated in next loop iteration)
                    int j = 0;
                    while (node.hasChildNodes()) {
                        cur.insertBefore(node.firstChild(), node);
                        j++;
                    }

                    i = i + j; //j nodes were inserted

                    //delete bad node
                    cur.removeChild(node);
                }
                else {
                    stack.push(node.toElement());
                }
            }
            else if (node.isText() && !node.isCDATASection()) {
                if (!curNI.canHaveText) {
                    cur.removeChild(node);
                }
                else { //format text
                    QString formattedText = "<tmp>" + formatter->format(Qt::escape(node.toText().data()), cur) + "</tmp>";
                    //NOTE: we don't need to escape quotes, and we want this code be more reusable/decoupled, 
                    //NOTE: so we use Qt::escape() instead of TextUtil::escape()
                   
                    xmlSource.setData(formattedText);
                    tmpDoc.setContent(&xmlSource, &xmlReader);
                   
                    QDomNode tmpElement = tmpDoc.firstChild();
                    while (tmpElement.hasChildNodes()) { //append <tmp>'s children. They won't be validated
                        cur.insertBefore(tmpElement.firstChild(), node);
                    }
                    
                    cur.removeChild(node);
                }
            }
        }//foreach child
    } //stack/dfs

//    qDebug() << "IMG MV:" << doc.toString(0);
    return doc.toString(0);
}