Example #1
0
void Xml::htmlToString(XmlReader& e, int level, QString* s)
      {
      *s += QString("<%1").arg(e.name().toString());
      QXmlStreamAttributes map = e.attributes();
      int n = map.size();
      for (int i = 0; i < n; ++i) {
            const QXmlStreamAttribute& a = map.at(i);
            *s += QString(" %1=\"%2\"").arg(a.name().toString()).arg(a.value().toString());
            }
      *s += ">";
      ++level;
      for (;;) {
            QXmlStreamReader::TokenType t = e.readNext();
            switch(t) {
                  case QXmlStreamReader::StartElement:
                        htmlToString(e, level, s);
                        break;
                  case QXmlStreamReader::EndElement:
                        *s += QString("</%1>").arg(e.name().toString());
                        --level;
                        return;
                  case QXmlStreamReader::Characters:
                        if (!e.isWhitespace())
                              *s += e.text().toString();
                        break;
                  case QXmlStreamReader::Comment:
                        break;

                  default:
                        qDebug("htmlToString: read token: %s", qPrintable(e.tokenString()));
                        return;
                  }
            }
      }
bool CommandCrossSection::readVector(const QXmlStreamAttributes &attributes, CCVector3 &P, QString element, const ccCommandLineInterface &cmd)
{
	if (attributes.size() < 3)
	{
		return cmd.error(QString("Invalid XML file (3 attributes expected for element '<%1>')").arg(element));
	}

	int count = 0;
	for (int i = 0; i < attributes.size(); ++i)
	{
		QString name = attributes[i].name().toString().toUpper();
		QString value = attributes[i].value().toString();

		bool ok = false;
		if (name == "X")
		{
			P.x = value.toDouble(&ok);
			++count;
		}
		else if (name == "Y")
		{
			P.y = value.toDouble(&ok);
			++count;
		}
		else if (name == "Z")
		{
			P.z = value.toDouble(&ok);
			++count;
		}
		else
		{
			ok = true;
		}

		if (!ok)
		{
			return cmd.error(QString("Invalid XML file (numerical attribute expected for attribute '%1' of element '<%2>')").arg(name, element));
		}
	}

	if (count < 3)
	{
		return cmd.error(QString("Invalid XML file (attributes 'X','Y' and 'Z' are mandatory for element '<%1>')").arg(element));
	}

	return true;
}
Example #3
0
QPointF XmlReader::readPoint()
      {
      Q_ASSERT(tokenType() == QXmlStreamReader::StartElement);
#ifndef NDEBUG
      if (!attributes().hasAttribute("x")) {
            QXmlStreamAttributes map = attributes();
            qDebug("XmlReader::readPoint: x attribute missing: %s (%d)",
               name().toUtf8().data(), map.size());
            for (int i = 0; i < map.size(); ++i) {
                  const QXmlStreamAttribute& a = map.at(i);
                  qDebug(" attr <%s> <%s>", a.name().toUtf8().data(), a.value().toUtf8().data());
                  }
            unknown();
            }
      if (!attributes().hasAttribute("y")) {
            qDebug("XmlReader::readPoint: y attribute missing: %s", name().toUtf8().data());
            unknown();
            }
#endif
      qreal x = doubleAttribute("x", 0.0);
      qreal y = doubleAttribute("y", 0.0);
      readNext();
      return QPointF(x, y);
      }
Example #4
0
void
DirectParser::ParseSendfile (QXmlStreamReader & xread, 
                            DirectMessage    & msg,
                            qint64           & offset,
                            bool             & complete,
                            bool             & good,
                            QXmlStreamAttributes & atts)
{
  QString subop = atts.value("subop").toString().toLower();
  msg.SetSubop (subop);
  bool valid (false);
  if (subop == "chunk-data") {
    ReadNext (xread);
    if (xread.isCharacters ()) {
      msg.SetData (xread.text().toString().toUtf8());
      valid = true;
      ReadNext (xread);
    }
    offset = xread.characterOffset ();
  } else {
    valid= ( subop == "sendreq"
          || subop == "samreq"
          || subop == "goahead"
          || subop == "deny"
          || subop == "chunk-ack"
          || subop == "snd-done"
          || subop == "abort"
           );
    ReadNext (xread);
  }
  if (valid) {
    for (int i=0; i< atts.size(); i++) {
      msg.SetAttribute (atts[i].name().toString().toLower(),
                        atts[i].value().toString());
    }
    good = true;
    complete = true;
    offset = xread.characterOffset ();
  } 
}
Example #5
0
int Dialog::initCdr(VCCdrDefines *pvcCdrDefines, QString fileName)
{
    LOG_FUNC(Dialog::initCdr);
    //初始化返回参数
    pvcCdrDefines->clear();

    CCdrBaseInfo BaseInfo;

    //打开xml配置文件
    QFile file;
    //QMessageBox mb;
    //mb.setText(QDir::currentPath() + "/cdrcomfig.xml");
    //mb.exec();
    file.setFileName(fileName);
    if(!file.open(QIODevice::ReadOnly))
    {
        file.setFileName(XML_LOCATION);
        if(!file.open(QIODevice::ReadOnly))
        {
            return FAIL;
        }
    }

    //创建一个新的节点用来处理
    QXmlStreamReader xmlReader(&file);

    CCdrDefine* cCdeDefine = 0;
    VCAttributes vcAttributes;

    for(QXmlStreamReader::TokenType type = xmlReader.readNext();!xmlReader.atEnd(); type = xmlReader.readNext())
    {
        switch(type)
        {
        //开始节点
        case QXmlStreamReader::StartElement:
            #ifdef DEBUG
            qDebug()<<"\t\t\ttype = QXmlStreamReader::StartElement; name = "<<xmlReader.name();
            #endif
            //cdr表示一张话单的开始
            if(!xmlReader.name().compare("cdr"))
                //todo 这里处理一个话单开始的工作,比如创建一个新的对象
            {
                #ifdef DEBUG
                qDebug()<<"new cdr"<<xmlReader.name();
                #endif
                cCdeDefine = new CCdrDefine;

                QXmlStreamAttributes qStreamAttr = xmlReader.attributes();

                //遍历所有的属性,插入可以使用的属性
                for(QXmlStreamAttributes::iterator iter = qStreamAttr.begin();iter != qStreamAttr.end();iter++)
                {
                    if(BaseInfo.checkTypeAttr(BaseInfo.cdr(),iter->name().toString()))
                    {
                        cCdeDefine->vcCdrAttr.addOne(iter->name().toString(),iter->value().toString());
                    }
                }
            }
            else if(!xmlReader.name().compare("filename"))
                //如果这里是filename,就应该创建一个节点,用来存储相关的信息;
                ;
            else if(!xmlReader.name().compare("element"))
                //如果是element,表示是字段了.
                ;
            else if(!xmlReader.name().compare("name") && cCdeDefine != 0)
            {
                //清空上次使用的attributes
                vcAttributes.clear();

                QXmlStreamAttributes qStreamAttr = xmlReader.attributes();

                //在第一个位置添加这个节点的值
                vcAttributes.addOne(xmlReader.name().toString(),xmlReader.readElementText());

                //遍历所有的属性,插入可以使用的属性
                for(QXmlStreamAttributes::iterator iter = qStreamAttr.begin();iter != qStreamAttr.end();iter++)
                {
                    if(BaseInfo.checkTypeAttr(BaseInfo.fileName(),iter->name().toString()))
                    {
                        vcAttributes.addOne(iter->name().toString(),iter->value().toString());
                    }
                }
                #ifdef DEBUG
                qDebug()<<"\tinsert into NameDef, name = "<<xmlReader.name();
                #endif
                //插入到链表中去
                cCdeDefine->addNameDef(vcAttributes);


            }
            else if(!xmlReader.name().compare("field"))
            {
                //清空上次使用的attributes
                vcAttributes.clear();

                QXmlStreamAttributes qStreamAttr = xmlReader.attributes();
                #ifdef DEBUG
                qDebug()<<"\txmlReader.attributes()="<<qStreamAttr.size();
                #endif

                //在第一个位置添加这个节点的值
                vcAttributes.addOne(xmlReader.name().toString(),xmlReader.readElementText());

                //遍历所有的属性,插入可以使用的属性
                for(QXmlStreamAttributes::iterator iter = qStreamAttr.begin();iter != qStreamAttr.end();iter++)
                {
                    if(BaseInfo.checkTypeAttr(BaseInfo.element(),iter->name().toString()))
                    {
                        vcAttributes.addOne(iter->name().toString(),iter->value().toString());
                    }
                }

                #ifdef DEBUG
                qDebug()<<"\tinsert into field, name = "<<xmlReader.name();
                #endif
                //插入到链表中去
                cCdeDefine->addField(vcAttributes);
            }
            break;

        //结束节点
        case QXmlStreamReader::EndElement:
            if(!xmlReader.name().compare("cdr"))
                //todo 这里处理一个话单开始的工作,比如创建一个新的对象
            {
                if(cCdeDefine != 0)
                {
                    #ifdef DEBUG
                    qDebug()<<"push_back cdr"<<xmlReader.name();
                    #endif
                    pvcCdrDefines->push_back(*cCdeDefine);
                }
                delete cCdeDefine;
                cCdeDefine = 0;
            }

            else if(!xmlReader.name().compare("filename"))
                //如果这里是filename,就应该创建一个节点,用来存储相关的信息;
                ;
            else if(!xmlReader.name().compare("element"))
                //如果是element,表示是字段了.
                ;
            else if(!xmlReader.name().compare("name"))
                ;
            else if(!xmlReader.name().compare("field"))
                ;
            break;

        //对于其他类型的节点不用处理
        default:
            ;
        };
    }
    if(xmlReader.hasError())
    {
        qDebug()<<xmlReader.error()<<":"<<xmlReader.errorString();
    }

    return SUCCESS;
}
bool fb2Creator::create(const QString& path, int width, int height, QImage& img)
{
    QFile file(path);
    
    KZip zip(path);
    QIODevice *device;
    const KArchiveDirectory *dir;
    const KZipFileEntry *fb2File;
    
    QXmlStreamReader qxml;
    
    QString fileExt = QFileInfo(path).suffix().toLower();
    if (fileExt == "fb2")
    {
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            qDebug() << "[fb2 thumbnailer]" << "Couldn't open" << path;
            return false;
        }
        else
        {
            qDebug() << "[fb2 thumbnailer]" << "Reading" << path;
            qxml.setDevice(&file);
        }
    }
    else //if *.fb2.zip
    {
        if (!zip.open(QIODevice::ReadOnly))
        {
            qDebug() << "[fb2 thumbnailer]" << "Couldn't open" << path;
            return false;
        }
        else
        {
            qDebug() << "[fb2 thumbnailer]" << "Reading" << path;

            dir = zip.directory();

            QStringList fileList = dir->entries();

            for (int i=0; i < fileList.count(); i++)
            {
                if (fileList.at(i).endsWith(".fb2"))
                {
                    fb2File = static_cast<const KZipFileEntry*>(dir->entry(fileList.at(i)));
                    device = fb2File->createDevice();
                    qxml.setDevice(device);

                    break;
                }
            }
        }
    }
    
    //----

    bool inCoverpage = false;
    QString coverId = "";
    QByteArray coverBase64;
    
    while(!qxml.atEnd() && !qxml.hasError())
    {
        qxml.readNext();

        if (qxml.name() == "coverpage")
        {
            if (qxml.isStartElement())
                inCoverpage = true;
            else
                inCoverpage = false;
        }

        if (qxml.name() == "image" && qxml.isStartElement() && inCoverpage == true)
        {
            //various namespaces: xlink, l, NS2
            QXmlStreamAttributes qxmlAttributes = qxml.attributes();

            for (int pos = 0; pos < qxmlAttributes.size(); pos++)
            {
                if (qxmlAttributes.at(pos).name() == "href")
                {
                    coverId = qxmlAttributes.at(pos).value().toString();
                    break;
                }
            }

            if (coverId != "")
            {
                coverId.remove("#");
                qDebug() << "[fb2 thumbnailer]" << "Found cover id:" << coverId;
            }
        }

        if (qxml.name() == "binary" && qxml.isStartElement())
        {
            if (coverId != "")
            {
                if (qxml.attributes().value("id") == coverId)
                {
                    qDebug() << "[fb2 thumbnailer]" << "Found cover data";

                    coverBase64 = qxml.readElementText().toAscii();

                    QImage coverImage;
                    coverImage.loadFromData(QByteArray::fromBase64(coverBase64));
                    
                    img = coverImage.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);

                    break;
                }
            }
            else //if coverId not found then the file doesn't follow the specification, try a workaround
            {
                qDebug() << "[fb2 thumbnailer]" << "Cover id not found";
                qDebug() << "[fb2 thumbnailer]" << "Using first image as cover";

                coverBase64 = qxml.readElementText().toAscii();

                QImage coverImage;
                coverImage.loadFromData(QByteArray::fromBase64(coverBase64));

                img = coverImage.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);

                break;
            }
        }
    }

    if (coverBase64.isEmpty())
        qDebug() << "[fb2 thumbnailer]" << "Cover data not found";

    if (qxml.hasError())
        qDebug() << "[fb2 thumbnailer]" << "Parsing error:" << qxml.errorString();

    qxml.clear();

    if (fileExt == "fb2")
        file.close();
    else
    {
        device->close();
        delete device;
        //delete fb2File;
        //delete dir;
    }

    return !img.isNull();
}