Search in sources :

Example 1 with AddStatementOperation

use of org.eclipse.rdf4j.http.protocol.transaction.operations.AddStatementOperation in project rdf4j by eclipse.

the class TransactionSAXParser method createAddStatementOperation.

private TransactionOperation createAddStatementOperation() throws SAXException {
    if (parsedValues.size() < 3) {
        throw new SAXException("At least three values required for AddStatementOperation, found: " + parsedValues.size());
    }
    try {
        Resource subject = (Resource) parsedValues.get(0);
        IRI predicate = (IRI) parsedValues.get(1);
        Value object = parsedValues.get(2);
        Resource[] contexts = createContexts(3);
        parsedValues.clear();
        if (subject == null || predicate == null || object == null) {
            throw new SAXException("Subject, predicate and object cannot be null for an AddStatementOperation");
        }
        return new AddStatementOperation(subject, predicate, object, contexts);
    } catch (ClassCastException e) {
        throw new SAXException("Invalid argument(s) for AddStatementOperation", e);
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) AddStatementOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.AddStatementOperation) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value) SAXException(org.xml.sax.SAXException)

Example 2 with AddStatementOperation

use of org.eclipse.rdf4j.http.protocol.transaction.operations.AddStatementOperation in project rdf4j by eclipse.

the class TransactionReaderTest method testRoundtrip.

@Test
public void testRoundtrip() throws Exception {
    AddStatementOperation operation = new AddStatementOperation(bob, knows, alice, context1, context2);
    List<TransactionOperation> txn = new ArrayList<TransactionOperation>();
    txn.add(operation);
    ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
    TransactionWriter w = new TransactionWriter();
    w.serialize(txn, out);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    TransactionReader r = new TransactionReader();
    Collection<TransactionOperation> parsedTxn = r.parse(in);
    assertNotNull(parsedTxn);
    for (TransactionOperation op : parsedTxn) {
        assertTrue(op instanceof AddStatementOperation);
        AddStatementOperation addOp = (AddStatementOperation) op;
        Resource[] contexts = addOp.getContexts();
        assertEquals(2, contexts.length);
        assertTrue(contexts[0].equals(context1) || contexts[1].equals(context1));
        assertTrue(contexts[0].equals(context2) || contexts[1].equals(context2));
    }
}
Also used : AddStatementOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.AddStatementOperation) TransactionOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.TransactionOperation) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) Resource(org.eclipse.rdf4j.model.Resource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 3 with AddStatementOperation

use of org.eclipse.rdf4j.http.protocol.transaction.operations.AddStatementOperation in project rdf4j by eclipse.

the class HTTPRepositoryConnection method addWithoutCommit.

/*
	 * @Override public void remove(Resource subject, URI predicate, Value object, Resource... contexts)
	 * throws RepositoryException { if (!isActive()) { // operation is not part of a transaction - just send
	 * directly OpenRDFUtil.verifyContextNotNull(contexts); if (subject == null) { subject = SESAME.WILDCARD;
	 * } if (predicate == null) { predicate = SESAME.WILDCARD; } if (object == null) { object =
	 * SESAME.WILDCARD; } final Model m = new LinkedHashModel(); m.add(subject, predicate, object, contexts);
	 * removeModel(m); } else { super.remove(subject, predicate, object, contexts); } }
	 */
@Override
protected void addWithoutCommit(Resource subject, IRI predicate, Value object, Resource... contexts) throws RepositoryException {
    if (this.getRepository().useCompatibleMode()) {
        txn.add(new AddStatementOperation(subject, predicate, object, contexts));
        return;
    }
    flushTransactionState(Protocol.Action.ADD);
    if (toAdd == null) {
        toAdd = new LinkedHashModel();
    }
    toAdd.add(subject, predicate, object, contexts);
}
Also used : AddStatementOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.AddStatementOperation) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel)

Example 4 with AddStatementOperation

use of org.eclipse.rdf4j.http.protocol.transaction.operations.AddStatementOperation in project rdf4j by eclipse.

the class TransactionReaderTest method testControlCharHandling.

@Test
public void testControlCharHandling() throws Exception {
    AddStatementOperation operation = new AddStatementOperation(bob, knows, controlCharText);
    List<TransactionOperation> txn = new ArrayList<TransactionOperation>();
    txn.add(operation);
    ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
    TransactionWriter w = new TransactionWriter();
    w.serialize(txn, out);
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    TransactionReader r = new TransactionReader();
    Collection<TransactionOperation> parsedTxn = r.parse(in);
    assertNotNull(parsedTxn);
    for (TransactionOperation op : parsedTxn) {
        assertTrue(op instanceof AddStatementOperation);
        AddStatementOperation addOp = (AddStatementOperation) op;
        assertTrue(addOp.getObject().equals(controlCharText));
    }
}
Also used : AddStatementOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.AddStatementOperation) TransactionOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.TransactionOperation) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

AddStatementOperation (org.eclipse.rdf4j.http.protocol.transaction.operations.AddStatementOperation)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2 TransactionOperation (org.eclipse.rdf4j.http.protocol.transaction.operations.TransactionOperation)2 Resource (org.eclipse.rdf4j.model.Resource)2 Test (org.junit.Test)2 IRI (org.eclipse.rdf4j.model.IRI)1 Value (org.eclipse.rdf4j.model.Value)1 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)1 SAXException (org.xml.sax.SAXException)1