|
| 1 | +/* |
| 2 | + * eXist-db Open Source Native XML Database |
| 3 | + * Copyright (C) 2001 The eXist-db Authors |
| 4 | + * |
| 5 | + * info@exist-db.org |
| 6 | + * http://www.exist-db.org |
| 7 | + * |
| 8 | + * This library is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public |
| 10 | + * License as published by the Free Software Foundation; either |
| 11 | + * version 2.1 of the License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This library is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | + * Lesser General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public |
| 19 | + * License along with this library; if not, write to the Free Software |
| 20 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | + */ |
| 22 | +package org.exist.contentextraction.xquery; |
| 23 | + |
| 24 | +import com.evolvedbinary.j8fu.tuple.Tuple2; |
| 25 | +import org.exist.EXistException; |
| 26 | +import org.exist.collections.Collection; |
| 27 | +import org.exist.collections.triggers.TriggerException; |
| 28 | +import org.exist.security.PermissionDeniedException; |
| 29 | +import org.exist.source.Source; |
| 30 | +import org.exist.source.StringSource; |
| 31 | +import org.exist.storage.BrokerPool; |
| 32 | +import org.exist.storage.DBBroker; |
| 33 | +import org.exist.storage.lock.Lock; |
| 34 | +import org.exist.storage.txn.Txn; |
| 35 | +import org.exist.test.ExistEmbeddedServer; |
| 36 | +import org.exist.util.LockException; |
| 37 | +import org.exist.xmldb.XmldbURI; |
| 38 | +import org.exist.xquery.XPathException; |
| 39 | +import org.exist.xquery.value.Sequence; |
| 40 | +import org.junit.*; |
| 41 | + |
| 42 | +import java.io.IOException; |
| 43 | +import java.io.InputStream; |
| 44 | +import java.util.Optional; |
| 45 | + |
| 46 | +import static com.evolvedbinary.j8fu.tuple.Tuple.Tuple; |
| 47 | +import static org.exist.contentextraction.xquery.Util.executeQuery; |
| 48 | +import static org.exist.contentextraction.xquery.Util.withCompiledQuery; |
| 49 | +import static org.junit.Assert.assertEquals; |
| 50 | +import static org.junit.Assert.assertNotNull; |
| 51 | + |
| 52 | + |
| 53 | +public class ContentFunctionsTest { |
| 54 | + |
| 55 | + @ClassRule |
| 56 | + public static final ExistEmbeddedServer existEmbeddedServer = new ExistEmbeddedServer(true, true); |
| 57 | + |
| 58 | + @BeforeClass |
| 59 | + public static void setup() throws EXistException, PermissionDeniedException, IOException, TriggerException, LockException { |
| 60 | + final BrokerPool pool = existEmbeddedServer.getBrokerPool(); |
| 61 | + try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject())); |
| 62 | + final Txn transaction = pool.getTransactionManager().beginTransaction()) { |
| 63 | + |
| 64 | + try (final Collection collection = broker.getOrCreateCollection(transaction, XmldbURI.create("/db/content-functions-test"))) { |
| 65 | + |
| 66 | + try (final InputStream is = ContentFunctionsTest.class.getResourceAsStream("minimal.pdf")) { |
| 67 | + assertNotNull(is); |
| 68 | + collection.addBinaryResource(transaction, broker, XmldbURI.create("minimal.pdf"), is, "application/pdf", -1); |
| 69 | + } |
| 70 | + |
| 71 | + try (final InputStream is = ContentFunctionsTest.class.getResourceAsStream("test.xlsx")) { |
| 72 | + assertNotNull(is); |
| 73 | + collection.addBinaryResource(transaction, broker, XmldbURI.create("test.xlsx"), is, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", -1); |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + transaction.commit(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @AfterClass |
| 83 | + public static void teardown() throws EXistException, PermissionDeniedException, IOException, TriggerException { |
| 84 | + final BrokerPool pool = existEmbeddedServer.getBrokerPool(); |
| 85 | + try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject())); |
| 86 | + final Txn transaction = pool.getTransactionManager().beginTransaction()) { |
| 87 | + |
| 88 | + try (final Collection collection = broker.openCollection(XmldbURI.create("/db/content-functions-test"), Lock.LockMode.WRITE_LOCK)) { |
| 89 | + if (collection != null) { |
| 90 | + broker.removeCollection(transaction, collection); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void getMetadataFromPdf() throws EXistException, XPathException, PermissionDeniedException, IOException { |
| 98 | + final String mainQuery = |
| 99 | + "declare namespace html = \"http://www.w3.org/1999/xhtml\";\n" + |
| 100 | + "declare namespace contentextraction = \"http://exist-db.org/xquery/contentextraction\";\n" + |
| 101 | + "declare namespace util = \"http://exist-db.org/xquery/util\";\n" + |
| 102 | + "let $bin := util:binary-doc(\"/db/content-functions-test/minimal.pdf\")\n" + |
| 103 | + " return\n" + |
| 104 | + " contentextraction:get-metadata($bin)//html:meta[@name = (\"xmpTPg:NPages\", \"Content-Type\")]/@content"; |
| 105 | + |
| 106 | + final BrokerPool pool = existEmbeddedServer.getBrokerPool(); |
| 107 | + final Source mainQuerySource = new StringSource(mainQuery); |
| 108 | + try (final DBBroker broker = pool.getBroker(); |
| 109 | + final Txn transaction = pool.getTransactionManager().beginTransaction()) { |
| 110 | + |
| 111 | + final Tuple2<Integer, String> metadata = withCompiledQuery(broker, mainQuerySource, mainCompiledQuery -> { |
| 112 | + final Sequence result = executeQuery(broker, mainCompiledQuery); |
| 113 | + assertEquals(2, result.getItemCount()); |
| 114 | + |
| 115 | + return Tuple(result.itemAt(0).toJavaObject(int.class), result.itemAt(1).getStringValue()); |
| 116 | + }); |
| 117 | + |
| 118 | + transaction.commit(); |
| 119 | + |
| 120 | + assertEquals(1, metadata._1.intValue()); |
| 121 | + assertEquals("application/pdf", metadata._2); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void getMetadataAndContentFromPdf() throws EXistException, XPathException, PermissionDeniedException, IOException { |
| 127 | + final String mainQuery = |
| 128 | + "declare namespace html = \"http://www.w3.org/1999/xhtml\";\n" + |
| 129 | + "declare namespace contentextraction = \"http://exist-db.org/xquery/contentextraction\";\n" + |
| 130 | + "declare namespace util = \"http://exist-db.org/xquery/util\";\n" + |
| 131 | + "let $bin := util:binary-doc(\"/db/content-functions-test/minimal.pdf\")\n" + |
| 132 | + " return\n" + |
| 133 | + " contentextraction:get-metadata-and-content($bin)//html:p[2]/string()"; |
| 134 | + |
| 135 | + final BrokerPool pool = existEmbeddedServer.getBrokerPool(); |
| 136 | + final Source mainQuerySource = new StringSource(mainQuery); |
| 137 | + try (final DBBroker broker = pool.getBroker(); |
| 138 | + final Txn transaction = pool.getTransactionManager().beginTransaction()) { |
| 139 | + |
| 140 | + final String content = withCompiledQuery(broker, mainQuerySource, mainCompiledQuery -> { |
| 141 | + final Sequence result = executeQuery(broker, mainCompiledQuery); |
| 142 | + assertEquals(1, result.getItemCount()); |
| 143 | + |
| 144 | + return result.itemAt(0).getStringValue(); |
| 145 | + }); |
| 146 | + |
| 147 | + transaction.commit(); |
| 148 | + |
| 149 | + assertEquals("Hello World", content); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Ignore("see https://github.com/eXist-db/exist/issues/3835") |
| 154 | + @Test |
| 155 | + public void getMetadataFromXlsx() throws EXistException, XPathException, PermissionDeniedException, IOException { |
| 156 | + final String mainQuery = |
| 157 | + "declare namespace html = \"http://www.w3.org/1999/xhtml\";\n" + |
| 158 | + "declare namespace contentextraction = \"http://exist-db.org/xquery/contentextraction\";\n" + |
| 159 | + "declare namespace util = \"http://exist-db.org/xquery/util\";\n" + |
| 160 | + "let $bin := util:binary-doc(\"/db/content-functions-test/test.xlsx\")\n" + |
| 161 | + " return\n" + |
| 162 | + " contentextraction:get-metadata($bin)//html:meta[@name = (\"xmpTPg:NPages\", \"Content-Type\")]/@content"; |
| 163 | + |
| 164 | + final BrokerPool pool = existEmbeddedServer.getBrokerPool(); |
| 165 | + final Source mainQuerySource = new StringSource(mainQuery); |
| 166 | + try (final DBBroker broker = pool.getBroker(); |
| 167 | + final Txn transaction = pool.getTransactionManager().beginTransaction()) { |
| 168 | + |
| 169 | + final Tuple2<Integer, String> metadata = withCompiledQuery(broker, mainQuerySource, mainCompiledQuery -> { |
| 170 | + final Sequence result = executeQuery(broker, mainCompiledQuery); |
| 171 | + assertEquals(2, result.getItemCount()); |
| 172 | + |
| 173 | + return Tuple(result.itemAt(0).toJavaObject(int.class), result.itemAt(1).getStringValue()); |
| 174 | + }); |
| 175 | + |
| 176 | + transaction.commit(); |
| 177 | + |
| 178 | + assertEquals(1, metadata._1.intValue()); |
| 179 | + assertEquals("application/pdf", metadata._2); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments