-
Notifications
You must be signed in to change notification settings - Fork 795
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TIKA-4330 -- Add a MetadataListFilter
- Loading branch information
Showing
11 changed files
with
300 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
tika-core/src/main/java/org/apache/tika/metadata/listfilter/CompositeMetadataListFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* 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.metadata.listfilter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apache.tika.exception.TikaException; | ||
import org.apache.tika.metadata.Metadata; | ||
|
||
public class CompositeMetadataListFilter extends MetadataListFilter { | ||
|
||
//no longer final to allow for no arg initialization during serialization | ||
private List<MetadataListFilter> filters; | ||
|
||
public CompositeMetadataListFilter() { | ||
filters = new ArrayList<>(); | ||
} | ||
public CompositeMetadataListFilter(List<MetadataListFilter> filters) { | ||
this.filters = filters; | ||
} | ||
|
||
public void setFilters(List<MetadataListFilter> filters) { | ||
this.filters.clear(); | ||
this.filters.addAll(filters); | ||
} | ||
|
||
public List<MetadataListFilter> getFilters() { | ||
return filters; | ||
} | ||
|
||
@Override | ||
public List<Metadata> filter(List<Metadata> metadataList) throws TikaException { | ||
for (MetadataListFilter filter : filters) { | ||
metadataList = filter.filter(metadataList); | ||
} | ||
return metadataList; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "CompositeMetadataListFilter{" + "filters=" + filters + '}'; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
tika-core/src/main/java/org/apache/tika/metadata/listfilter/MetadataListFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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.metadata.listfilter; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.util.List; | ||
|
||
import org.w3c.dom.Element; | ||
|
||
import org.apache.tika.config.ConfigBase; | ||
import org.apache.tika.exception.TikaConfigException; | ||
import org.apache.tika.exception.TikaException; | ||
import org.apache.tika.metadata.Metadata; | ||
import org.apache.tika.metadata.filter.MetadataFilter; | ||
|
||
public abstract class MetadataListFilter extends ConfigBase implements Serializable { | ||
/** | ||
* Loads the metadata list filter from the config file if it exists, otherwise returns NoOpFilter | ||
* @param root | ||
* @return | ||
* @throws TikaConfigException | ||
* @throws IOException | ||
*/ | ||
public static MetadataListFilter load(Element root, boolean allowMissing) throws TikaConfigException, | ||
IOException { | ||
try { | ||
return buildComposite("metadataListFilters", CompositeMetadataListFilter.class, | ||
"metadataListFilter", MetadataFilter.class, root); | ||
} catch (TikaConfigException e) { | ||
if (allowMissing && e.getMessage().contains("could not find metadataListFilters")) { | ||
return new NoOpListFilter(); | ||
} | ||
throw e; | ||
} | ||
} | ||
public abstract List<Metadata> filter(List<Metadata> metadataList) throws TikaException; | ||
} |
28 changes: 28 additions & 0 deletions
28
tika-core/src/main/java/org/apache/tika/metadata/listfilter/NoOpListFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* 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.metadata.listfilter; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.tika.metadata.Metadata; | ||
|
||
public class NoOpListFilter extends MetadataListFilter { | ||
@Override | ||
public List<Metadata> filter(List<Metadata> metadataList) { | ||
return metadataList; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...-core/src/test/java/org/apache/tika/metadata/listfilter/AttachmentCountingListFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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.metadata.listfilter; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.tika.exception.TikaException; | ||
import org.apache.tika.metadata.Metadata; | ||
|
||
public class AttachmentCountingListFilter extends MetadataListFilter { | ||
@Override | ||
public List<Metadata> filter(List<Metadata> metadataList) throws TikaException { | ||
if (metadataList == null || metadataList.isEmpty()) { | ||
return metadataList; | ||
} | ||
metadataList.get(0).set("X-TIKA:attachment_count", Integer.toString(metadataList.size() - 1)); | ||
return metadataList; | ||
} | ||
} |
Oops, something went wrong.