Search in sources :

Example 71 with StringInputSource

use of org.exist.util.StringInputSource in project exist by eXist-db.

the class UpdateRecoverTest method store.

private void store(final BrokerPool pool) throws EXistException, PermissionDeniedException, IOException, SAXException, LockException, ParserConfigurationException, XPathException {
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        DocumentImpl doc;
        try (final Txn transaction = transact.beginTransaction()) {
            final Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
            assertNotNull(root);
            broker.saveCollection(transaction, root);
            final Collection test2 = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI2);
            assertNotNull(test2);
            broker.saveCollection(transaction, test2);
            broker.storeDocument(transaction, TestConstants.TEST_XML_URI, new StringInputSource(TEST_XML), MimeType.XML_TYPE, test2);
            doc = test2.getDocument(broker, TestConstants.TEST_XML_URI);
            // TODO : unlock the collection here ?
            transact.commit(transaction);
        }
        try (final Txn transaction = transact.beginTransaction()) {
            final MutableDocumentSet docs = new DefaultDocumentSet();
            docs.add(doc);
            final XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
            assertNotNull(proc);
            // insert some nodes
            for (int i = 1; i <= 200; i++) {
                final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:insert-before select=\"/products/product[1]\">" + "       <product>" + "           <description>Product " + i + "</description>" + "           <price>" + (i * 2.5) + "</price>" + "           <stock>" + (i * 10) + "</stock>" + "       </product>" + "   </xu:insert-before>" + "</xu:modifications>";
                proc.setBroker(broker);
                proc.setDocumentSet(docs);
                final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
                assertNotNull(modifications);
                modifications[0].process(transaction);
                proc.reset();
            }
            // add attribute
            for (int i = 1; i <= 200; i++) {
                final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:append select=\"/products/product[" + i + "]\">" + "         <xu:attribute name=\"id\">" + i + "</xu:attribute>" + " </xu:append>" + "</xu:modifications>";
                proc.setBroker(broker);
                proc.setDocumentSet(docs);
                final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
                assertNotNull(modifications);
                modifications[0].process(transaction);
                proc.reset();
            }
            // replace some
            for (int i = 1; i <= 100; i++) {
                final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:replace select=\"/products/product[" + i + "]\">" + "     <product id=\"" + i + "\">" + "         <description>Replaced product</description>" + "         <price>" + (i * 0.75) + "</price>" + "     </product>" + " </xu:replace>" + "</xu:modifications>";
                proc.setBroker(broker);
                proc.setDocumentSet(docs);
                final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
                assertNotNull(modifications);
                long mods = modifications[0].process(transaction);
                proc.reset();
            }
            // remove some
            for (int i = 1; i <= 100; i++) {
                final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:remove select=\"/products/product[last()]\"/>" + "</xu:modifications>";
                proc.setBroker(broker);
                proc.setDocumentSet(docs);
                final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
                assertNotNull(modifications);
                modifications[0].process(transaction);
                proc.reset();
            }
            for (int i = 1; i <= 100; i++) {
                final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:append select=\"/products\">" + "       <product>" + "           <xu:attribute name=\"id\"><xu:value-of select=\"count(/products/product) + 1\"/></xu:attribute>" + "           <description>Product " + i + "</description>" + "           <price>" + (i * 2.5) + "</price>" + "           <stock>" + (i * 10) + "</stock>" + "       </product>" + "   </xu:append>" + "</xu:modifications>";
                proc.setBroker(broker);
                proc.setDocumentSet(docs);
                final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
                assertNotNull(modifications);
                modifications[0].process(transaction);
                proc.reset();
            }
            // rename element "description" to "descript"
            String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:rename select=\"/products/product/description\">descript</xu:rename>" + "</xu:modifications>";
            proc.setBroker(broker);
            proc.setDocumentSet(docs);
            Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
            assertNotNull(modifications);
            modifications[0].process(transaction);
            proc.reset();
            // update attribute values
            for (int i = 1; i <= 200; i++) {
                xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:update select=\"/products/product[" + i + "]/@id\">" + i + "u</xu:update>" + "</xu:modifications>";
                proc.setBroker(broker);
                proc.setDocumentSet(docs);
                modifications = proc.parse(new InputSource(new StringReader(xupdate)));
                assertNotNull(modifications);
                long mods = modifications[0].process(transaction);
                proc.reset();
            }
            // append new element to records
            for (int i = 1; i <= 200; i++) {
                xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:append select=\"/products/product[" + i + "]\">" + "       <date><xu:value-of select=\"current-dateTime()\"/></date>" + "   </xu:append>" + "</xu:modifications>";
                proc.setBroker(broker);
                proc.setDocumentSet(docs);
                modifications = proc.parse(new InputSource(new StringReader(xupdate)));
                assertNotNull(modifications);
                modifications[0].process(transaction);
                proc.reset();
            }
            // update element content
            for (int i = 1; i <= 200; i++) {
                xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + "   <xu:update select=\"/products/product[" + i + "]/price\">19.99</xu:update>" + "</xu:modifications>";
                proc.setBroker(broker);
                proc.setDocumentSet(docs);
                modifications = proc.parse(new InputSource(new StringReader(xupdate)));
                assertNotNull(modifications);
                long mods = modifications[0].process(transaction);
                proc.reset();
            }
            transact.commit(transaction);
        }
    }
}
Also used : XUpdateProcessor(org.exist.xupdate.XUpdateProcessor) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) Modification(org.exist.xupdate.Modification) StringInputSource(org.exist.util.StringInputSource) InputSource(org.xml.sax.InputSource) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) Txn(org.exist.storage.txn.Txn) DocumentImpl(org.exist.dom.persistent.DocumentImpl) StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) StringReader(java.io.StringReader) Collection(org.exist.collections.Collection)

Example 72 with StringInputSource

use of org.exist.util.StringInputSource in project exist by eXist-db.

the class CopyResourceTest method setup.

@Before
public void setup() throws EXistException, PermissionDeniedException, LockException, SAXException, IOException, AuthenticationException {
    final BrokerPool pool = existWebServer.getBrokerPool();
    // create user1 resources
    final Subject user1 = pool.getSecurityManager().authenticate(USER1_NAME, USER1_PWD);
    try (final DBBroker broker = pool.get(Optional.of(user1));
        final Txn transaction = pool.getTransactionManager().beginTransaction();
        final Collection collection = broker.openCollection(TEST_COLLECTION_URI, LockMode.WRITE_LOCK)) {
        final String u1d1xml = "<empty1/>";
        broker.storeDocument(transaction, USER1_DOC1, new StringInputSource(u1d1xml), MimeType.XML_TYPE, collection);
        chmod(broker, transaction, TEST_COLLECTION_URI.append(USER1_DOC1), USER1_DOC1_MODE);
        final String u1d2xml = "<empty2/>";
        broker.storeDocument(transaction, USER1_DOC2, new StringInputSource(u1d2xml), MimeType.XML_TYPE, collection);
        chmod(broker, transaction, TEST_COLLECTION_URI.append(USER1_DOC2), USER1_DOC2_MODE);
        final String u1d3xml = "<empty3/>";
        broker.storeDocument(transaction, USER1_DOC3, new StringInputSource(u1d3xml), MimeType.XML_TYPE, collection);
        chmod(broker, transaction, TEST_COLLECTION_URI.append(USER1_DOC3), USER1_DOC3_MODE);
        chgrp(broker, transaction, TEST_COLLECTION_URI.append(USER1_DOC3), GROUP1_NAME);
        final String u1d1bin = "bin1";
        broker.storeDocument(transaction, USER1_BIN_DOC1, new StringInputSource(u1d1bin.getBytes(UTF_8)), MimeType.TEXT_TYPE, collection);
        chmod(broker, transaction, TEST_COLLECTION_URI.append(USER1_BIN_DOC1), USER1_BIN_DOC1_MODE);
        final String u1d2bin = "bin2";
        broker.storeDocument(transaction, USER1_BIN_DOC2, new StringInputSource(u1d2bin.getBytes(UTF_8)), MimeType.TEXT_TYPE, collection);
        chmod(broker, transaction, TEST_COLLECTION_URI.append(USER1_BIN_DOC2), USER1_BIN_DOC2_MODE);
        final String u1d3bin = "bin3";
        broker.storeDocument(transaction, USER1_BIN_DOC3, new StringInputSource(u1d3bin.getBytes(UTF_8)), MimeType.TEXT_TYPE, collection);
        chmod(broker, transaction, TEST_COLLECTION_URI.append(USER1_BIN_DOC3), USER1_BIN_DOC3_MODE);
        chgrp(broker, transaction, TEST_COLLECTION_URI.append(USER1_BIN_DOC3), GROUP1_NAME);
        transaction.commit();
    }
    // create user2 resources
    final Subject user2 = pool.getSecurityManager().authenticate(USER2_NAME, USER2_PWD);
    try (final DBBroker broker = pool.get(Optional.of(user2));
        final Txn transaction = pool.getTransactionManager().beginTransaction();
        final Collection collection = broker.openCollection(TEST_COLLECTION_URI, LockMode.WRITE_LOCK)) {
        final String u2d2xml = "<empty2/>";
        broker.storeDocument(transaction, USER2_DOC2, new StringInputSource(u2d2xml), MimeType.XML_TYPE, collection);
        chmod(broker, transaction, TEST_COLLECTION_URI.append(USER2_DOC2), USER2_DOC2_MODE);
        final String u2d3xml = "<empty3/>";
        broker.storeDocument(transaction, USER2_DOC3, new StringInputSource(u2d3xml), MimeType.XML_TYPE, collection);
        chmod(broker, transaction, TEST_COLLECTION_URI.append(USER2_DOC3), USER2_DOC3_MODE);
        chgrp(broker, transaction, TEST_COLLECTION_URI.append(USER2_DOC3), GROUP1_NAME);
        final String u2d2bin = "bin2";
        broker.storeDocument(transaction, USER2_BIN_DOC2, new StringInputSource(u2d2bin.getBytes(UTF_8)), MimeType.TEXT_TYPE, collection);
        chmod(broker, transaction, TEST_COLLECTION_URI.append(USER2_BIN_DOC2), USER2_BIN_DOC2_MODE);
        final String u2d3bin = "bin3";
        broker.storeDocument(transaction, USER2_BIN_DOC3, new StringInputSource(u2d3bin.getBytes(UTF_8)), MimeType.TEXT_TYPE, collection);
        chmod(broker, transaction, TEST_COLLECTION_URI.append(USER2_BIN_DOC3), USER2_BIN_DOC3_MODE);
        chgrp(broker, transaction, TEST_COLLECTION_URI.append(USER2_BIN_DOC3), GROUP1_NAME);
        transaction.commit();
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn)

Aggregations

StringInputSource (org.exist.util.StringInputSource)72 Collection (org.exist.collections.Collection)57 Txn (org.exist.storage.txn.Txn)56 BrokerPool (org.exist.storage.BrokerPool)34 DBBroker (org.exist.storage.DBBroker)34 TransactionManager (org.exist.storage.txn.TransactionManager)27 XmldbURI (org.exist.xmldb.XmldbURI)17 Test (org.junit.Test)13 InputStream (java.io.InputStream)10 DocumentImpl (org.exist.dom.persistent.DocumentImpl)8 Sequence (org.exist.xquery.value.Sequence)7 CollectionConfigurationManager (org.exist.collections.CollectionConfigurationManager)5 EXistException (org.exist.EXistException)4 ManagedCollectionLock (org.exist.storage.lock.ManagedCollectionLock)4 LockException (org.exist.util.LockException)4 InputSource (org.xml.sax.InputSource)4 IOException (java.io.IOException)3 UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)3 LockedDocument (org.exist.dom.persistent.LockedDocument)3 PermissionDeniedException (org.exist.security.PermissionDeniedException)3