You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ni...@apache.org on 2018/03/13 18:15:27 UTC

[tika] 04/13: Pull out deep Metadata clone to a utils method for re-use

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

nick pushed a commit to branch multiple-parsers
in repository https://gitbox.apache.org/repos/asf/tika.git

commit d5a06ba6d17b0846cfc58b2e3c0a3df6abc31b0c
Author: Nick Burch <ni...@gagravarr.org>
AuthorDate: Tue Mar 13 15:02:31 2018 +0000

    Pull out deep Metadata clone to a utils method for re-use
---
 .../apache/tika/parser/RecursiveParserWrapper.java | 24 ++----------
 .../java/org/apache/tika/utils/ParserUtils.java    | 43 ++++++++++++++++++++++
 2 files changed, 46 insertions(+), 21 deletions(-)

diff --git a/tika-core/src/main/java/org/apache/tika/parser/RecursiveParserWrapper.java b/tika-core/src/main/java/org/apache/tika/parser/RecursiveParserWrapper.java
index 3cba1f1..1e8e5b1 100644
--- a/tika-core/src/main/java/org/apache/tika/parser/RecursiveParserWrapper.java
+++ b/tika-core/src/main/java/org/apache/tika/parser/RecursiveParserWrapper.java
@@ -32,6 +32,7 @@ import org.apache.tika.metadata.TikaCoreProperties;
 import org.apache.tika.mime.MediaType;
 import org.apache.tika.sax.ContentHandlerFactory;
 import org.apache.tika.utils.ExceptionUtils;
+import org.apache.tika.utils.ParserUtils;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.DefaultHandler;
@@ -169,7 +170,7 @@ public class RecursiveParserWrapper implements Parser {
             if (hitMaxEmbeddedResources) {
                 metadata.set(EMBEDDED_RESOURCE_LIMIT_REACHED, "true");
             }
-            metadatas.add(0, deepCopy(metadata));
+            metadatas.add(0, ParserUtils.cloneMetadata(metadata));
         }
     }
 
@@ -226,23 +227,6 @@ public class RecursiveParserWrapper implements Parser {
         }
     }
     
-    //defensive copy
-    private Metadata deepCopy(Metadata m) {
-        Metadata clone = new Metadata();
-        
-        for (String n : m.names()){
-            if (! m.isMultiValued(n)) {
-                clone.set(n, m.get(n));
-            } else {
-                String[] vals = m.getValues(n);
-                for (int i = 0; i < vals.length; i++) {
-                    clone.add(n, vals[i]);
-                }
-            }
-        }
-        return clone;
-    }
-    
     private String getResourceName(Metadata metadata) {
         String objectName = "";
         if (metadata.get(TikaCoreProperties.RESOURCE_NAME_KEY) != null) {
@@ -348,9 +332,7 @@ public class RecursiveParserWrapper implements Parser {
                 return;
             }
             addContent(localHandler, metadata);
-            metadatas.add(deepCopy(metadata));
+            metadatas.add(ParserUtils.cloneMetadata(metadata));
         }        
     }
-
-
 }
diff --git a/tika-core/src/main/java/org/apache/tika/utils/ParserUtils.java b/tika-core/src/main/java/org/apache/tika/utils/ParserUtils.java
new file mode 100644
index 0000000..289cbc2
--- /dev/null
+++ b/tika-core/src/main/java/org/apache/tika/utils/ParserUtils.java
@@ -0,0 +1,43 @@
+/*
+ * 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.utils;
+
+import org.apache.tika.metadata.Metadata;
+
+/**
+ * Helper util methods for Parsers themselves.
+ */
+public class ParserUtils {
+    /**
+     * Does a deep clone of a Metadata object.
+     */
+    public static Metadata cloneMetadata(Metadata m) {
+        Metadata clone = new Metadata();
+        
+        for (String n : m.names()){
+            if (! m.isMultiValued(n)) {
+                clone.set(n, m.get(n));
+            } else {
+                String[] vals = m.getValues(n);
+                for (int i = 0; i < vals.length; i++) {
+                    clone.add(n, vals[i]);
+                }
+            }
+        }
+        return clone;
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
nick@apache.org.