You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ju...@apache.org on 2009/04/03 00:46:24 UTC

svn commit: r761459 - /lucene/tika/branches/TIKA-204/src/main/java/org/apache/tika/parser/pkg/ArParser.java

Author: jukka
Date: Thu Apr  2 22:46:23 2009
New Revision: 761459

URL: http://svn.apache.org/viewvc?rev=761459&view=rev
Log:
TIKA-204: Use commons-compress for parsing packages

Added an ar archive parser based on code in commons-compress.

Added:
    lucene/tika/branches/TIKA-204/src/main/java/org/apache/tika/parser/pkg/ArParser.java

Added: lucene/tika/branches/TIKA-204/src/main/java/org/apache/tika/parser/pkg/ArParser.java
URL: http://svn.apache.org/viewvc/lucene/tika/branches/TIKA-204/src/main/java/org/apache/tika/parser/pkg/ArParser.java?rev=761459&view=auto
==============================================================================
--- lucene/tika/branches/TIKA-204/src/main/java/org/apache/tika/parser/pkg/ArParser.java (added)
+++ lucene/tika/branches/TIKA-204/src/main/java/org/apache/tika/parser/pkg/ArParser.java Thu Apr  2 22:46:23 2009
@@ -0,0 +1,66 @@
+/*
+ * 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.pkg;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.commons.compress.archivers.ar.ArArchiveEntry;
+import org.apache.commons.compress.archivers.ar.ArArchiveInputStream;
+import org.apache.commons.io.input.CloseShieldInputStream;
+import org.apache.tika.exception.TikaException;
+import org.apache.tika.metadata.Metadata;
+import org.apache.tika.sax.XHTMLContentHandler;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+/**
+ * Ar archive parser.
+ */
+public class ArParser extends PackageParser {
+
+    /**
+     * Parses the given stream as an ar archive.
+     */
+    public void parse(
+            InputStream stream, ContentHandler handler, Metadata metadata)
+            throws IOException, TikaException, SAXException {
+        XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
+        xhtml.startDocument();
+
+        // At the end we want to close the ar stream to release any associated
+        // resources, but the underlying document stream should not be closed
+        ArArchiveInputStream ar =
+            new ArArchiveInputStream(new CloseShieldInputStream(stream));
+        try {
+            ArArchiveEntry entry = ar.getNextArEntry();
+            while (entry != null) {
+                if (!entry.isDirectory()) {
+                    Metadata entrydata = new Metadata();
+                    entrydata.set(Metadata.RESOURCE_NAME_KEY, entry.getName());
+                    parseEntry(ar, xhtml, entrydata);
+                }
+                entry = ar.getNextArEntry();
+            }
+        } finally {
+            ar.close();
+        }
+
+        xhtml.endDocument();
+    }
+
+}