Exemplo n.º 1
0
QDebug operator<<(QDebug dbg, const QXmlStreamReader& reader)
{
    dbg.nospace() << "QXmlStreamReader(";
    if (reader.isStartElement()) {
        dbg.nospace() << "<";
        dbg.nospace() << reader.qualifiedName().toString().toLocal8Bit().constData();
        QString attrsString;
        const QXmlStreamAttributes& attrs = reader.attributes();
        for (int i = 0; i < attrs.count(); i++) {
            dbg.nospace() << " " << attrs[i].qualifiedName().toString().toLocal8Bit().constData();
            dbg.nospace() << "=";
            dbg.nospace() << attrs[i].value().toString();
        }
        if (reader.isEndElement()) {
            dbg.nospace() << "/>)";
        } else {
            dbg.nospace() << ">)";
        }
    } else if (reader.isEndElement()) {
        dbg.nospace() << "</" << reader.qualifiedName().toString().toLocal8Bit().constData() << ">)";
    } else if (reader.isCharacters()) {
        dbg.nospace() << "characters:" << reader.text() << ")";
    } else if (reader.isComment()) {
        dbg.nospace() << "<!-- " << reader.text().toString().toLocal8Bit().constData() << " -->)";
    } else if (reader.isCDATA()) {
        dbg.nospace() << "CDATA:" << reader.text() << ")";
    } else if (reader.isWhitespace()) {
        dbg.nospace() << "whitespace:" << reader.text() << ")";
    } else {
        dbg.nospace() << reader.tokenString() << reader.text();
    }
    return dbg.space();
}
Exemplo n.º 2
0
void APIController::getAnimeID(QString name)
{
    QFile f("C:/reports.xml");
    QXmlStreamReader reader;
    f.open(QIODevice::ReadOnly);
    reader.setDevice(&f);
    int aid;
    while (!reader.atEnd()) {
        reader.readNext();
        if (reader.name() == "id" && reader.tokenString()=="StartElement") {
            reader.readNext();
            aid = reader.text().toInt();
        }
        if (reader.text().toString().toUpper() == name.toUpper())
        {
            path="C:/"+QString::number(aid);
            file = new QFile(path);
            file->open(QIODevice::ReadWrite);
            url.setUrl("http://cdn.animenewsnetwork.com/encyclopedia/api.xml?anime="+QString::number(aid));
            reply = qnam.get(QNetworkRequest(url));
            QObject::connect(reply, SIGNAL(readyRead()),
                             this, SLOT(httpReadyRead()));
            QObject::connect(reply, SIGNAL(finished()),
                             this, SLOT(httpFinished()));
            qDebug()<<QString::number(aid);
            return;
        }
    }

    if (reader.hasError()) {
        qDebug()<<reader.error();
    }
}
// Forward a stream reader across end elements looking for the
// next start element of a desired type.
static bool readStartElement(QXmlStreamReader &r, const char *name)
{
    while (r.tokenType() != QXmlStreamReader::StartElement
            || r.name() != QLatin1String(name))
        switch (r.readNext()) {
        case QXmlStreamReader::EndDocument:
            return false;
        case QXmlStreamReader::NoToken:
        case QXmlStreamReader::Invalid:
            qWarning("'%s'/'%s' encountered while looking for start element '%s'.",
                    qPrintable(r.tokenString()),
                    qPrintable(r.name().toString()), name);
            return false;
        default:
            break;
        }
    return true;
}
Exemplo n.º 4
0
/**
 * @brief  Convert an XML stream to a hierarchical QVariantMap.
 *
 * This function is used internally to embed opaque XML structures, such as the
 * SQS service's ErrorResponse::Error::Detail, which the SQS schema defines as
 * an arbitrary complex type.
 *
 * @note   This static function exists within the AwsAbstractResponse for
 *         historic reasons.  It should probably be moved our of this class, and
 *         into a more generic utility space at some point.
 *
 * @param  xml       XML stream to convert.
 * @param  prefix    Prefix to apply to special (non-element) child entry names.
 * @param  maxDepth  Maximum depth to traverse before truncating the tree.
 *
 * @return A QVariantMap representing the \a xml XML fragment.
 *
 * @todo   Move this toVariant function to somewhere more generic.
 */
QVariantMap AwsAbstractResponse::toVariant(
    QXmlStreamReader &xml, const QString &prefix, const int maxDepth)
{
    if (maxDepth < 0) {
        qWarning() << QObject::tr("max depth exceeded");
        return QVariantMap();
    }

    if (xml.hasError()) {
        qWarning() << xml.errorString();
        return QVariantMap();
    }

    if (xml.tokenType() == QXmlStreamReader::NoToken)
        xml.readNext();

    if ((xml.tokenType() != QXmlStreamReader::StartDocument) &&
        (xml.tokenType() != QXmlStreamReader::StartElement)) {
        qWarning() << QObject::tr("unexpected XML tokenType %1 (%2)")
                      .arg(xml.tokenString()).arg(xml.tokenType());
        return QVariantMap();
    }

    QVariantMap map;
    if (xml.tokenType() == QXmlStreamReader::StartDocument) {
        map.insert(prefix + QLatin1String("DocumentEncoding"), xml.documentEncoding().toString());
        map.insert(prefix + QLatin1String("DocumentVersion"), xml.documentVersion().toString());
        map.insert(prefix + QLatin1String("StandaloneDocument"), xml.isStandaloneDocument());
    } else {
        if (!xml.namespaceUri().isEmpty())
            map.insert(prefix + QLatin1String("NamespaceUri"), xml.namespaceUri().toString());
        foreach (const QXmlStreamAttribute &attribute, xml.attributes()) {
            QVariantMap attributeMap;
            attributeMap.insert(QLatin1String("Value"), attribute.value().toString());
            if (!attribute.namespaceUri().isEmpty())
                attributeMap.insert(QLatin1String("NamespaceUri"), attribute.namespaceUri().toString());
            if (!attribute.prefix().isEmpty())
                attributeMap.insert(QLatin1String("Prefix"), attribute.prefix().toString());
            attributeMap.insert(QLatin1String("QualifiedName"), attribute.qualifiedName().toString());
            map.insertMulti(prefix + attribute.name().toString(), attributeMap);
        }
    }

    for (xml.readNext(); (!xml.atEnd()) && (xml.tokenType() != QXmlStreamReader::EndElement)
          && (xml.tokenType() != QXmlStreamReader::EndDocument); xml.readNext()) {
        switch (xml.tokenType()) {
        case QXmlStreamReader::Characters:
        case QXmlStreamReader::Comment:
        case QXmlStreamReader::DTD:
        case QXmlStreamReader::EntityReference:
            map.insertMulti(prefix + xml.tokenString(), xml.text().toString());
            break;
        case QXmlStreamReader::ProcessingInstruction:
            map.insertMulti(prefix + xml.processingInstructionTarget().toString(),
                            xml.processingInstructionData().toString());
            break;
        case QXmlStreamReader::StartElement: {
            const QString elementName = xml.name().toString();
            map.insertMulti(elementName, toVariant(xml, prefix, maxDepth-1));
            break;
        }
        default:
            qWarning() << QObject::tr("unexpected XML tokenType %1 (%2)")
                          .arg(xml.tokenString()).arg(xml.tokenType());
        }
    }
    return map;
}
Exemplo n.º 5
0
bool
DirectParser::Read (DirectMessage & msg)
{
  msg.Clear ();
  if (!bufLock.tryLock (5000)) {
    qDebug () << "WARNING: Mutex locked 5 seconds, giving up";
    return false;
  }
  QXmlStreamReader              xread (&inbuf);
  QXmlStreamReader::TokenType   tokt;
  bool finished (false);
  bool complete (false);
  bool good (false);
  bool badData (false);
  QString  topname;
  QString  bottomtag;
  QString  version;
  qint64 offset (0);
  while (!finished) {
    tokt = ReadNext (xread);
    offset = xread.characterOffset ();
    qDebug () << " Direct token " << xread.tokenString();
    switch (tokt) {
    case QXmlStreamReader::NoToken :
      qDebug () << " no token found";
      finished = true; complete = false; good = false;
      lastErr = Proto_EmptyInput;
      break;
    case QXmlStreamReader::Invalid :
      qDebug () << " bad token";
      qDebug () << " text until here: " << inbuf.buffer().left(offset);
    
      finished = true; complete = false; good = false;
      lastErr = Proto_BadTag;
      badData = true;
      break;
    case QXmlStreamReader::StartElement:
      topname = xread.name().toString().toLower();
      if (topname == oldTopTag) {
        ParseOld (xread, msg, offset, complete, good);
        msg.SetAttribute ("version","0.1");
      } else if (topname == topTag) {
        version = xread.documentVersion ().toString();
        msg.SetAttribute ("version",version);
        ParseMessage (xread, msg, offset, complete, good);
      } else {
qDebug () << " topname unknown: " << topname;
        finished = true;  complete = false; good = false;
        lastErr = Proto_WrongProtocol;
      }
      break;
    case QXmlStreamReader::EndElement:
      bottomtag = xread.name().toString().toLower();
      if (bottomtag == topname) {
        finished = true;
      }
      break;
    case QXmlStreamReader::EndDocument :
      finished = true;
      break;
    case QXmlStreamReader::Characters:
      break;
    case QXmlStreamReader::StartDocument:
    case QXmlStreamReader::Comment:
    case QXmlStreamReader::DTD:
    case QXmlStreamReader::EntityReference:
    case QXmlStreamReader::ProcessingInstruction:
      break;
    default:
      qDebug () << " unknown token type " << tokt;
      lastErr = Proto_Unknown;
      finished = true; complete = false; good = false;
      break;
    }
  }
    if (good && complete) {
    /// we have consumed a message, so get rid of the raw data
    /// so we can read the next message next time   
    qDebug () << " trailing token " << xread.tokenString ();
    while (!xread.atEnd() && xread.tokenType() != QXmlStreamReader::EndDocument) {
      xread.readNext();
      qDebug () << " consumed " << xread.tokenString ();
    }
    qDebug () << " remove " << offset << " bytes from buffer: ";
    qDebug () << inbuf.buffer().left(offset);
    inbuf.buffer().remove (0,offset+1);
    inbuf.seek (0);
  } else {
    msg.Clear ();
  }
  qDebug () << " after DirectParser::Read buffer has [[" << inbuf.buffer() << "]]";
  qDebug () << " token " << xread.tokenString() << " error " << xread.error();
  if (!good) {
    if (xread.error () == QXmlStreamReader::PrematureEndOfDocumentError) {
      complete = false;
      good = true;
    }
  }
  lastComplete = complete;
  lastGood = good;
  bufLock.unlock ();
  return good && complete;
}