|
| 1 | +/* |
| 2 | + * Copyright 2024 Tobias Bülte, hbz |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 the "License"; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.metafacture.metafix; |
| 18 | + |
| 19 | +import org.metafacture.formatting.ObjectTemplate; |
| 20 | +import org.metafacture.framework.FluxCommand; |
| 21 | +import org.metafacture.framework.StreamReceiver; |
| 22 | +import org.metafacture.framework.annotations.Description; |
| 23 | +import org.metafacture.framework.annotations.In; |
| 24 | +import org.metafacture.framework.annotations.Out; |
| 25 | +import org.metafacture.framework.MetafactureException; |
| 26 | +import org.metafacture.framework.ObjectReceiver; |
| 27 | +import org.metafacture.framework.helpers.DefaultStreamPipe; |
| 28 | +import org.metafacture.mangling.StreamFlattener; |
| 29 | +import org.metafacture.triples.StreamToTriples; |
| 30 | +import org.metafacture.triples.TripleFilter; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | + |
| 34 | +/** |
| 35 | + * Provide a user-friendly way to finds all paths that have values that match |
| 36 | + * the given pattern. |
| 37 | + * |
| 38 | + * @author Tobias Bülte |
| 39 | + */ |
| 40 | +@Description("Finds all paths that have values that match the given pattern. Allows for regex. These paths can be used in a Fix to address fields.") |
| 41 | +@In(StreamReceiver.class) |
| 42 | +@Out(String.class) |
| 43 | +@FluxCommand("find-fix-paths") |
| 44 | + |
| 45 | +public class FindFixPaths extends DefaultStreamPipe<ObjectReceiver<String>> { |
| 46 | + public FindFixPaths(final String objectPattern) { |
| 47 | + final Metafix fix; |
| 48 | + try { |
| 49 | + fix = new Metafix("nothing()"); |
| 50 | + fix.setRepeatedFieldsToEntities(true); |
| 51 | + } catch (final IOException e) { |
| 52 | + throw new MetafactureException(e); |
| 53 | + } |
| 54 | + final TripleFilter tripleFilter = new TripleFilter(); |
| 55 | + tripleFilter.setObjectPattern(objectPattern); |
| 56 | + fix |
| 57 | + .setReceiver(new StreamFlattener()) |
| 58 | + .setReceiver(new StreamToTriples()) |
| 59 | + .setReceiver(tripleFilter) |
| 60 | + .setReceiver(new ObjectTemplate<>("${p}\t ${o}")) |
| 61 | + .setReceiver(getReceiver()); |
| 62 | + } |
| 63 | +} |
0 commit comments