示例#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;
                  }
            }
      }
示例#2
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);
      }
示例#3
0
void XmlConsole::stackProcess(const QByteArray &data, bool incoming)
{
	StackEnvironment *d = &(incoming ? m_stackIncoming : m_stackOutgoing);
	d->reader.addData(data);
	StackToken *token;
//	debug() << incoming << data;
//	debug() << "==================================================================";
	while (d->reader.readNext() > QXmlStreamReader::Invalid) {
//		qDebug() << incoming << d->reader.tokenString();
		switch(d->reader.tokenType()) {
		case QXmlStreamReader::StartElement:
//			dbg << d->reader.name().toString() << d->depth
//					<< d->reader.attributes().value(QLatin1String("from")).toString();
			d->depth++;
			if (d->depth > 1) {
				if (!d->tokens.isEmpty() && d->tokens.last()->type == QXmlStreamReader::Characters)
					delete d->tokens.takeLast();
				d->tokens << new StackToken(d->reader);
			}
			break;
		case QXmlStreamReader::EndElement:
//			dbg << d->reader.name().toString() << d->depth;
			if (d->tokens.isEmpty())
				break;
			token = d->tokens.last();
			if (token->type == QXmlStreamReader::StartElement && !token->startTag.empty)
				token->startTag.empty = true;
			else if (d->depth > 1)
				d->tokens << new StackToken(d->reader);
			if (d->depth == 2) {
				QTextCursor cursor(m_ui->xmlBrowser->document());
				cursor.movePosition(QTextCursor::End);
				cursor.beginEditBlock();
				QTextCharFormat zeroFormat = cursor.charFormat();
				zeroFormat.setForeground(QColor(Qt::white));
				QTextCharFormat bodyFormat = zeroFormat;
				bodyFormat.setForeground(d->bodyColor);
				QTextCharFormat tagFormat = zeroFormat;
				tagFormat.setForeground(d->tagColor);
				QTextCharFormat attributeFormat = zeroFormat;
				attributeFormat.setForeground(d->attributeColor);
				QTextCharFormat paramsFormat = zeroFormat;
				paramsFormat.setForeground(d->paramColor);
				QTextCharFormat bracketFormat = zeroFormat;
				bracketFormat.setForeground(m_stackBracketsColor);
				QString singleSpace = QLatin1String(" ");
				cursor.insertBlock();
				int depth = 0;
				QString currentXmlns;
				QXmlStreamReader::TokenType lastType = QXmlStreamReader::StartElement;
				for (int i = 0; i < d->tokens.size(); i++) {
					token = d->tokens.at(i);
					if (token->type == QXmlStreamReader::StartElement) {
						QString space = generate_stacked_space(depth);
						cursor.insertText(QLatin1String("\n"));
						cursor.insertText(space);
						cursor.insertText(QLatin1String("<"), bracketFormat);
						cursor.insertText(token->startTag.name->toString(), tagFormat);
						const QStringRef &xmlns = *token->startTag.xmlns;
						if (i == 0 || xmlns != currentXmlns) {
							currentXmlns = xmlns.toString();
							cursor.insertText(singleSpace);
							cursor.insertText(QLatin1String("xmlns"), attributeFormat);
							cursor.insertText(QLatin1String("="), zeroFormat);
							cursor.insertText(QLatin1String("'"), paramsFormat);
							cursor.insertText(currentXmlns, paramsFormat);
							cursor.insertText(QLatin1String("'"), paramsFormat);
						}
						QXmlStreamAttributes *attributes = token->startTag.attributes;
						for (int j = 0; j < attributes->count(); j++) {
							const QXmlStreamAttribute &attr = attributes->at(j);
							cursor.insertText(singleSpace);
							cursor.insertText(attr.name().toString(), attributeFormat);
							cursor.insertText(QLatin1String("="), zeroFormat);
							cursor.insertText(QLatin1String("'"), paramsFormat);
							cursor.insertText(attr.value().toString(), paramsFormat);
							cursor.insertText(QLatin1String("'"), paramsFormat);
						}
						if (token->startTag.empty) {
							cursor.insertText(QLatin1String("/>"), bracketFormat);
						} else {
							cursor.insertText(QLatin1String(">"), bracketFormat);
							depth++;
						}
					} else if (token->type == QXmlStreamReader::EndElement) {
						if (lastType == QXmlStreamReader::EndElement) {
							QString space = generate_stacked_space(depth - 1);
							cursor.insertText(QLatin1String("\n"));
							cursor.insertText(space);
						}
						cursor.insertText(QLatin1String("</"), bracketFormat);
						cursor.insertText(token->endTag.name->toString(), tagFormat);
						cursor.insertText(QLatin1String(">"), bracketFormat);
						depth--;
					} else if (token->type == QXmlStreamReader::Characters) {
						cursor.setCharFormat(bodyFormat);
						QString text = token->charachters.text->toString();
						if (text.contains(QLatin1Char('\n'))) {
							QString space = generate_stacked_space(depth);
							space.prepend(QLatin1Char('\n'));
							QStringList lines = text.split(QLatin1Char('\n'));
							for (int j = 0; j < lines.size(); j++) {
								cursor.insertText(space);
								cursor.insertText(lines.at(j));
							}
							space.chop(1);
							cursor.insertText(space);
						} else {
							cursor.insertText(text);
						}
					}
					lastType = token->type;
					if (lastType == QXmlStreamReader::StartElement && token->startTag.empty)
						lastType = QXmlStreamReader::EndElement;
					delete token;
				}
				cursor.endEditBlock();
				d->tokens.clear();
			}
			d->depth--;
			break;
		case QXmlStreamReader::Characters:
			token = d->tokens.isEmpty() ? 0 : d->tokens.last();
			if (token && token->type == QXmlStreamReader::StartElement && !token->startTag.empty) {
				if (*token->startTag.name == QLatin1String("auth")
						&& *token->startTag.xmlns == QLatin1String("urn:ietf:params:xml:ns:xmpp-sasl")) {
					d->tokens << new StackToken(QLatin1String("<<Private data>>"));
				} else {
					d->tokens << new StackToken(d->reader);
				}
			}
			break;
		default:
			break;
		}
	}
//	qDebug() << d->reader.tokenString();
//	if (d->reader.tokenType() == QXmlStreamReader::Invalid)
//		dbg << d->reader.error() << d->reader.errorString();
	if (!incoming && d->depth > 1) {
		qFatal("outgoing depth %d on\n\"%s\"", d->depth,
			   qPrintable(QString::fromUtf8(data, data.size())));
	}
}
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();
}