You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ta...@apache.org on 2022/04/26 14:54:24 UTC

[tika] branch main updated: TIKA-3721 -- add DGN8Parser for metadata via Dan Coldrick

This is an automated email from the ASF dual-hosted git repository.

tallison pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tika.git


The following commit(s) were added to refs/heads/main by this push:
     new 1d469867e TIKA-3721 -- add DGN8Parser for metadata via Dan Coldrick
1d469867e is described below

commit 1d469867e547876536927081ee90cdde5eaf1dff
Author: tallison <ta...@apache.org>
AuthorDate: Tue Apr 26 10:54:08 2022 -0400

    TIKA-3721 -- add DGN8Parser for metadata via Dan Coldrick
---
 CHANGES.txt                                        |  2 +
 .../tika-parser-cad-module/pom.xml                 |  6 +-
 .../org/apache/tika/parser/dgn/DGN8Parser.java     | 88 ++++++++++++++++++++++
 .../services/org.apache.tika.parser.Parser         |  1 +
 .../org/apache/tika/parser/prt/DGN8ParserTest.java | 44 +++++++++++
 5 files changed, 138 insertions(+), 3 deletions(-)

diff --git a/CHANGES.txt b/CHANGES.txt
index 547383cca..6720153c1 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -30,6 +30,8 @@ Release 2.4.0 - ???
    * Add detection for DGN files with gratitude and credit
      to Steven Frew's tika-dgn-detector (TIKA-3721).
 
+   * Add parser for metadata from DGN 8 files via Dan Coldrick (TIKA-3721).
+
    * Add a fetcher and emitter for Azure blob storage (TIKA-3707).
 
    * Add detection for files encrypted by Microsoft's Rights Management Service
diff --git a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-cad-module/pom.xml b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-cad-module/pom.xml
index b9b422ac7..9317bffa5 100644
--- a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-cad-module/pom.xml
+++ b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-cad-module/pom.xml
@@ -32,9 +32,9 @@
 
   <dependencies>
     <dependency>
-      <groupId>org.apache.poi</groupId>
-      <artifactId>poi</artifactId>
-      <version>${poi.version}</version>
+      <groupId>${project.groupId}</groupId>
+      <artifactId>tika-parser-microsoft-module</artifactId>
+      <version>${project.version}</version>
     </dependency>
   </dependencies>
   <build>
diff --git a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-cad-module/src/main/java/org/apache/tika/parser/dgn/DGN8Parser.java b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-cad-module/src/main/java/org/apache/tika/parser/dgn/DGN8Parser.java
new file mode 100644
index 000000000..edb2488a3
--- /dev/null
+++ b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-cad-module/src/main/java/org/apache/tika/parser/dgn/DGN8Parser.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika.parser.dgn;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.Set;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.input.CloseShieldInputStream;
+import org.apache.poi.poifs.filesystem.DirectoryNode;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+import org.apache.tika.exception.TikaException;
+import org.apache.tika.io.TikaInputStream;
+import org.apache.tika.metadata.Metadata;
+import org.apache.tika.mime.MediaType;
+import org.apache.tika.parser.AbstractParser;
+import org.apache.tika.parser.ParseContext;
+import org.apache.tika.parser.microsoft.SummaryExtractor;
+import org.apache.tika.sax.XHTMLContentHandler;
+
+/**
+ * This is a VERY LIMITED parser. It parses metadata out of dgn8 files.
+ */
+public class DGN8Parser extends AbstractParser {
+
+    Set<MediaType> SUPPORTED_TYPES = Collections.singleton(MediaType.image("vnd.dgn; version=8"));
+
+    @Override
+    public Set<MediaType> getSupportedTypes(ParseContext context) {
+        return SUPPORTED_TYPES;
+    }
+
+    @Override
+    public void parse(InputStream stream, ContentHandler handler, Metadata metadata,
+                      ParseContext context) throws IOException, SAXException, TikaException {
+        XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
+        xhtml.startDocument();
+        SummaryExtractor summaryExtractor = new SummaryExtractor(metadata);
+        final DirectoryNode root;
+        TikaInputStream tstream = TikaInputStream.cast(stream);
+        POIFSFileSystem mustCloseFs = null;
+        try {
+            if (tstream == null) {
+                mustCloseFs = new POIFSFileSystem(new CloseShieldInputStream(stream));
+                root = mustCloseFs.getRoot();
+            } else {
+                final Object container = tstream.getOpenContainer();
+                if (container instanceof POIFSFileSystem) {
+                    root = ((POIFSFileSystem) container).getRoot();
+                } else if (container instanceof DirectoryNode) {
+                    root = (DirectoryNode) container;
+                } else {
+                    POIFSFileSystem fs = null;
+                    if (tstream.hasFile()) {
+                        fs = new POIFSFileSystem(tstream.getFile(), true);
+                    } else {
+                        fs = new POIFSFileSystem(new CloseShieldInputStream(tstream));
+                    }
+                    // tstream will close the fs, no need to close this below
+                    tstream.setOpenContainer(fs);
+                    root = fs.getRoot();                }
+            }
+            summaryExtractor.parseSummaries(root);
+        } finally {
+            IOUtils.closeQuietly(mustCloseFs);
+        }
+        xhtml.endDocument();
+    }
+}
diff --git a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-cad-module/src/main/resources/META-INF/services/org.apache.tika.parser.Parser b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-cad-module/src/main/resources/META-INF/services/org.apache.tika.parser.Parser
index 1af4369bc..b3b6bd593 100644
--- a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-cad-module/src/main/resources/META-INF/services/org.apache.tika.parser.Parser
+++ b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-cad-module/src/main/resources/META-INF/services/org.apache.tika.parser.Parser
@@ -13,6 +13,7 @@
 #  See the License for the specific language governing permissions and
 #  limitations under the License.
 
+org.apache.tika.parser.dgn.DGN8Parser
 org.apache.tika.parser.dwg.DWGParser
 org.apache.tika.parser.prt.PRTParser
 
diff --git a/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-cad-module/src/test/java/org/apache/tika/parser/prt/DGN8ParserTest.java b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-cad-module/src/test/java/org/apache/tika/parser/prt/DGN8ParserTest.java
new file mode 100644
index 000000000..afa9ddc69
--- /dev/null
+++ b/tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-cad-module/src/test/java/org/apache/tika/parser/prt/DGN8ParserTest.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika.parser.prt;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.Arrays;
+
+import org.junit.jupiter.api.Test;
+
+import org.apache.tika.TikaTest;
+import org.apache.tika.metadata.Metadata;
+import org.apache.tika.metadata.OfficeOpenXMLExtended;
+import org.apache.tika.metadata.TikaCoreProperties;
+
+public class DGN8ParserTest extends TikaTest {
+    /**
+     * Try with a simple file
+     */
+    @Test
+    public void testBasics() throws Exception {
+        Metadata metadata = getXML("testDGN8.dgn").metadata;
+        assertEquals("John.Frampton", metadata.get(TikaCoreProperties.MODIFIER));
+        assertEquals("MicroStation v8.11.0.0", metadata.get(OfficeOpenXMLExtended.APPLICATION));
+        assertContains("org.apache.tika.parser.dgn.DGN8Parser",
+                Arrays.asList(metadata.getValues(TikaCoreProperties.TIKA_PARSED_BY)));
+
+    }
+
+}