Exemplo n.º 1
0
void TestRdf::basicload()
{
    RDEBUG;

    QString odt = QString(FILES_DATA_DIR) + "/weekend-hike.odt";
    KoDocumentRdf *rdf = loadDocument(odt);
    QVERIFY(rdf);
    QSharedPointer<Soprano::Model> m = rdf->model();
    QVERIFY(m);
    QCOMPARE (234, m->statementCount());

    StatementIterator it;
    QList<Statement> allStatements;

    // check a triple
    it = m->listStatements(
                Node::createResourceNode(QUrl("uri:james")),
                Node::createResourceNode(QUrl("http://xmlns.com/foaf/0.1/name")),
                Node());
    allStatements = it.allElements();
    QCOMPARE (allStatements.size(), 1);
    foreach (Soprano::Statement s, allStatements) {
        // RDEBUG << "HAVE:" << s;
        QVERIFY (s.object().toString() == "James Smith");
    }
void KoRdfSemanticItemViewSite::setProperty(const QString &prop, const QString &v)
{
    QString fqprop = "http://koffice.org/rdf/site#" + prop;
    KoDocumentRdf *rdf = d->m_semItem->documentRdf();
    Soprano::Model *m = rdf->model();
    Soprano::Node ls = linkingSubject();
    Soprano::Node pred = Node::createResourceNode(QUrl(fqprop));
    m->removeAllStatements(Statement(ls, pred, Node()));
    m->addStatement(ls, pred,Node::createLiteralNode(v), rdf->manifestRdfNode());
}
QString KoRdfSemanticItemViewSite::getProperty(const QString &prop, const QString &defval) const
{
    Soprano::Node ls = linkingSubject();
    QString fqprop = "http://koffice.org/rdf/site#" + prop;
    KoDocumentRdf *rdf = d->m_semItem->documentRdf();
    Soprano::Model *m = rdf->model();
    StatementIterator it = m->listStatements(ls, Node::createResourceNode(QUrl(fqprop)),
                               Node(), rdf->manifestRdfNode());
    QList<Statement> allStatements = it.allElements();
    foreach (Soprano::Statement s, allStatements) {
        return s.object().toString();
    }
    return defval;
}
Exemplo n.º 4
0
void TestKoDocumentRdf::testRememberNewInlineRdfObject()
{
    KoDocumentRdf rdfDoc;
    QTextDocument doc;

    QTextCursor cur(&doc);
    KoBookmark bm(cur);
    bm.setName("test");

    KoTextInlineRdf inlineRdf(&doc, &bm);
    inlineRdf.setXmlId(inlineRdf.createXmlId());

    rdfDoc.rememberNewInlineRdfObject(&inlineRdf);

    Q_ASSERT(&inlineRdf == rdfDoc.findInlineRdfByID(inlineRdf.xmlId()));
}
Exemplo n.º 5
0
void RdfTest::testFindMarkers()
{
    QObject parent;

    // the rdf storage. In calligra, it's part of the KoDocument.
    KoDocumentRdf rdfDoc;

    // create a document
    QTextDocument doc;

    KoTextRangeManager rangeManager(&parent);
    KoTextDocument textDoc(&doc);
    textDoc.setTextRangeManager(&rangeManager);

    KoTextEditor editor(&doc);
    textDoc.setTextEditor(&editor);

    // insert some lorem ipsum
    editor.insertText(lorem);

    // insert a table and set a bookmark on it
    QStringList idList;
    QString newId = insertTableWithSemItem(editor, rdfDoc, "test item1");
    idList << newId;

    editor.setPosition(0);

    // now use soprano to find the tables
    QList<hTestSemanticItem> semItems = TestSemanticItem::allObjects(&rdfDoc);
    Q_ASSERT(semItems.length() == 1);

    foreach(hTestSemanticItem semItem, semItems) {
        QStringList xmlidlist = semItem->xmlIdList();

        Q_ASSERT(xmlidlist.length() == 1);
        foreach(const QString xmlid, xmlidlist) {
            Q_ASSERT(idList.contains(xmlid));
            QPair<int, int> position = rdfDoc.findExtent(xmlid);

            QCOMPARE(position.first, lorem.length() + 1);
            QCOMPARE(position.second, lorem.length() + 1 + TABLESIZE - 1);

            editor.setPosition(position.first + 2);
            const QTextTable *table = editor.currentTable();
            QVERIFY(table);
            Q_UNUSED(table);
        }
Soprano::Node KoRdfSemanticItemViewSite::linkingSubject() const
{
    KoDocumentRdf *rdf = d->m_semItem->documentRdf();
    Soprano::Model *m = rdf->model();
    Node pred(QUrl("http://koffice.org/rdf/site/package/common#idref"));
    Node obj = Node::createLiteralNode(d->m_xmlid);
    Node context = rdf->manifestRdfNode();
    // try to find it if it already exists
    StatementIterator it = m->listStatements(Node(), pred, obj, context);
    QList<Statement> allStatements = it.allElements();
    foreach (Soprano::Statement s, allStatements) {
        return s.subject();
    }
    Node ret = m->createBlankNode();
    m->addStatement(ret, pred, obj, context);
    return ret;
}
void KoRdfSemanticItemViewSite::select(KoCanvasBase *host)
{
    Q_ASSERT(d->m_semItem);
    Q_ASSERT(d->m_semItem->documentRdf());
    Q_ASSERT(host);
    KoTextEditor *editor = KoDocumentRdf::ensureTextTool(host);
    KoResourceManager *provider = host->resourceManager();
    KoDocumentRdf *rdf = d->m_semItem->documentRdf();
    QPair<int, int> p = p = rdf->findExtent(d->m_xmlid);
    int startpos = p.first;
    int endpos = p.second + 1;
    if (!endpos) {
        kDebug(30015) << "No end point found for semantic item:" << d->m_semItem->name();
        kDebug(30015) << "xmlid:" << d->m_xmlid;
        return;
    }
    kDebug(30015) << "xmlid:" << d->m_xmlid;
    kDebug(30015) << "start:" << startpos << " endpos:" << endpos;
    selectRange(provider, startpos, endpos);
    kDebug(30015) << "selected text:" << editor->selectedText();
}
Exemplo n.º 8
0
QString RdfTest::insertTableWithSemItem(KoTextEditor &editor,
                               KoDocumentRdf &rdfDoc,
                               const QString name)
{
    editor.insertTable(5,10);
#define TABLESIZE (5*10)
    const QTextTable *table = editor.currentTable();

    QTextCursor cur(editor.document());
    cur.setPosition(table->firstPosition());
    KoBookmark *bookmark = new KoBookmark(cur);
    bookmark->setPositionOnlyMode(false); // we want it to be several chars long

    KoTextInlineRdf *inlineRdf(new KoTextInlineRdf(editor.document(), bookmark));
    QString newId = inlineRdf->createXmlId();
    inlineRdf->setXmlId(newId);

    bookmark->setName(newId);
    bookmark->setInlineRdf(inlineRdf);
    KoTextDocument(editor.document()).textRangeManager()->insert(bookmark);

    editor.setPosition(table->firstPosition());
    editor.movePosition(QTextCursor::PreviousCharacter);

    hTestSemanticItem testItem(new TestSemanticItem(0, &rdfDoc));
    testItem->setName(name);
    Soprano::Statement st(
                testItem->linkingSubject(), // subject
                Soprano::Node::createResourceNode(QUrl("http://docs.oasis-open.org/ns/office/1.2/meta/pkg#idref")), // predicate
                Soprano::Node::createLiteralNode(newId), // object
                rdfDoc.manifestRdfNode()); // manifest datastore
    rdfDoc.model()->addStatement(st);
    rdfDoc.rememberNewInlineRdfObject(inlineRdf);

    Q_ASSERT(rdfDoc.model()->statementCount() > 0);

    bookmark->setRangeEnd(table->lastPosition());

    return newId;
}
Exemplo n.º 9
0
void RdfTest::testCreateMarkers()
{
    QObject parent;

    // the rdf storage. In calligra, it's part of the KoDocument.
    KoDocumentRdf rdfDoc;

    // create a document
    QTextDocument doc;

    KoTextRangeManager rangeManager(&parent);
    KoTextDocument textDoc(&doc);
    textDoc.setTextRangeManager(&rangeManager);

    KoTextEditor editor(&doc);
    textDoc.setTextEditor(&editor);

    // insert some lorem ipsum
    editor.insertText(lorem);

    // insert a table and set a bookmark on it with semantics
    QString newId = insertTableWithSemItem(editor, rdfDoc, "test item1");

    // insert some more lorem before the table
    editor.insertText(lorem);

    // verify that the bookmark marks the table and only that
    QPair<int,int> position = rdfDoc.findExtent(newId);
    QCOMPARE(position.first, 2*(lorem.length()+1));
    QCOMPARE(position.second, 2*(lorem.length()+1)+TABLESIZE-1);

    editor.setPosition(position.first + 1);
    QPair<int,int> position2 = rdfDoc.findExtent(&editor);
    qDebug()<<position<<position2;
    QCOMPARE(position, position2);

    // check that the id is like we expext
    QCOMPARE(rdfDoc.findXmlId(&editor), newId);
}
Exemplo n.º 10
0
static KoDocumentRdf *loadDocument(const QString &odt)
{
    if (!QFile(odt).exists()) {
        qFatal("%s does not exist", qPrintable(odt));
        return 0;
    }

    KoStore *store = KoStore::createStore(odt, KoStore::Read, "", KoStore::Zip);
    KoOdfReadStore odfReadStore(store);
    KoXmlDocument metaDoc;
    KoDocumentRdf *rdf = new KoDocumentRdf;
    QString error;
    if (!odfReadStore.loadAndParse(error)) {
        delete store;
        return 0;
    }
    if (!rdf->loadOasis(store)) {
        delete store;
        return 0;
    }
    delete store;
    return rdf;
}
Exemplo n.º 11
0
void TestKoDocumentRdf::testCreate()
{
    KoDocumentRdf *rdfDoc = new KoDocumentRdf();
    Q_ASSERT(rdfDoc->model());
    delete rdfDoc;
}