Skip to content

Commit 7e766ad

Browse files
committed
Added source location infos to the morph pipeline.
Commit e10de395592d082596354646e555083e4aa5e1e6 annotated the Metamorph DOM with information about the location of each element in the source file. This commit adds this information to the data, collector and function objects which are created based on the morph pipeline described by the DOM. The location information is made available through a new interface called `KnowsSourceLocation`. It offers methods for setting and getting location information from an object. `NamedValueReceiver` and `NamedValueSource` extend this interface. The location information is set via a setter method instead of via the constructor to avoid having to add a constructor to every concrete collector or function class.
1 parent 498730b commit 7e766ad

File tree

8 files changed

+113
-4
lines changed

8 files changed

+113
-4
lines changed

src/main/java/org/culturegraph/mf/morph/AbstractNamedValuePipe.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
*/
1616
package org.culturegraph.mf.morph;
1717

18+
import org.culturegraph.mf.util.xml.Location;
19+
1820
/**
19-
* Base class for {@link NamedValuePipe}s
21+
* Base class for {@link NamedValuePipe}s.
22+
*
2023
* @author Markus Michael Geipel
2124
* @author Chistohp Böhme
2225
*
@@ -25,6 +28,8 @@ public abstract class AbstractNamedValuePipe implements NamedValuePipe {
2528

2629
private NamedValueReceiver namedValueReceiver;
2730

31+
private Location sourceLocation;
32+
2833
@Override
2934
public final <R extends NamedValueReceiver> R setNamedValueReceiver(
3035
final R receiver) {
@@ -41,6 +46,16 @@ public final void addNamedValueSource(final NamedValueSource namedValueSource) {
4146
onNamedValueSourceAdded(namedValueSource);
4247
}
4348

49+
@Override
50+
public void setSourceLocation(final Location sourceLocation) {
51+
this.sourceLocation = sourceLocation;
52+
}
53+
54+
@Override
55+
public Location getSourceLocation() {
56+
return sourceLocation;
57+
}
58+
4459
protected void onNamedValueSourceAdded(final NamedValueSource namedValueSource) {
4560
// Default implementation does nothing
4661
}

src/main/java/org/culturegraph/mf/morph/Flush.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,22 @@
1616

1717
package org.culturegraph.mf.morph;
1818

19+
import org.culturegraph.mf.util.xml.Location;
20+
1921

2022
/**
2123
* Flushes a {@link FlushListener}
2224
*
23-
* @author markus geipel
25+
* @author Markus Geipel
26+
* @author Christoph Böhme
2427
*
2528
*/
2629
public final class Flush implements NamedValueReceiver {
2730

2831
private final FlushListener listener;
2932

33+
private Location sourceLocation;
34+
3035
public Flush(final FlushListener listener) {
3136
this.listener = listener;
3237
}
@@ -41,4 +46,14 @@ public void addNamedValueSource(final NamedValueSource namedValueSource) {
4146
// Nothing to do
4247
}
4348

49+
@Override
50+
public void setSourceLocation(final Location sourceLocation) {
51+
this.sourceLocation = sourceLocation;
52+
}
53+
54+
@Override
55+
public Location getSourceLocation() {
56+
return sourceLocation;
57+
}
58+
4459
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2014 Christoph Böhme
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+
package org.culturegraph.mf.morph;
17+
18+
import org.culturegraph.mf.util.xml.Location;
19+
20+
/**
21+
* Metamorph elements which know about their location in the
22+
* morph definition file should implement this interface to
23+
* make the information about their location available in a
24+
* standardised way.
25+
*
26+
* @author Christoph Böhme
27+
*
28+
*/
29+
public interface KnowsSourceLocation {
30+
31+
/**
32+
* Sets the {@link Location} object which describes
33+
* where in the morph definition file the implementing
34+
* object is defined.
35+
*
36+
* @param sourceLocation a source location
37+
*/
38+
void setSourceLocation(final Location sourceLocation);
39+
40+
/**
41+
* @return a {@link Location} object for the
42+
* location in the morph definition file
43+
* where the implementing object instance
44+
* is defined.
45+
*/
46+
Location getSourceLocation();
47+
48+
}

src/main/java/org/culturegraph/mf/morph/Metamorph.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,4 +347,18 @@ public <R extends NamedValueReceiver> R setNamedValueReceiver(final R receiver)
347347
throw new UnsupportedOperationException("The Metamorph object cannot act as a NamedValueSender");
348348
}
349349

350+
@Override
351+
public void setSourceLocation(final Location sourceLocation) {
352+
// Nothing to do
353+
// Metamorph does not have a source location (we could
354+
// in theory use the location of the module in a flux
355+
// script)
356+
}
357+
358+
@Override
359+
public Location getSourceLocation() {
360+
// Metamorph does not have a source location
361+
return null;
362+
}
363+
350364
}

src/main/java/org/culturegraph/mf/morph/MorphBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.culturegraph.mf.morph.functions.Function;
2727
import org.culturegraph.mf.types.MultiMap;
2828
import org.culturegraph.mf.util.reflection.ObjectFactory;
29+
import org.culturegraph.mf.util.xml.Location;
2930
import org.w3c.dom.Node;
3031

3132
/**
@@ -173,6 +174,7 @@ protected void finish() {
173174
protected void enterData(final Node dataNode) {
174175
final Data data = new Data();
175176
data.setName(resolvedAttribute(dataNode, ATTRITBUTE.NAME));
177+
data.setSourceLocation((Location) dataNode.getUserData(Location.USER_DATA_ID));
176178

177179
final String source = resolvedAttribute(dataNode, ATTRITBUTE.SOURCE);
178180
metamorph.registerNamedValueReceiver(source, data);
@@ -232,6 +234,7 @@ protected void enterCollect(final Node node) {
232234
throw new IllegalArgumentException("Collector " + node.getLocalName() + NOT_FOUND);
233235
}
234236
final Collect collect = getCollectFactory().newInstance(node.getLocalName(), attributes, metamorph);
237+
collect.setSourceLocation((Location) node.getUserData(Location.USER_DATA_ID));
235238

236239
stack.push(new StackFrame(collect));
237240
}
@@ -294,6 +297,8 @@ protected void handleFunction(final Node functionNode) {
294297
throw new IllegalArgumentException(functionNode.getLocalName() + NOT_FOUND);
295298
}
296299

300+
function.setSourceLocation((Location) functionNode.getUserData(Location.USER_DATA_ID));
301+
297302
function.setMultiMap(metamorph);
298303

299304
// add key value entries...

src/main/java/org/culturegraph/mf/morph/NamedValueReceiver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @author Christoph Böhme
2323
*
2424
*/
25-
public interface NamedValueReceiver {
25+
public interface NamedValueReceiver extends KnowsSourceLocation {
2626

2727
void receive(String name, String value, NamedValueSource source, int recordCount, int entityCount);
2828

src/main/java/org/culturegraph/mf/morph/NamedValueSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @author Christoph Böhme
2424
*
2525
*/
26-
public interface NamedValueSource {
26+
public interface NamedValueSource extends KnowsSourceLocation {
2727

2828
/**
2929
* Connects a source of named values to a receiver of named values.

src/test/java/org/culturegraph/mf/morph/MetamorphBasicTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.culturegraph.mf.framework.StreamReceiver;
2323
import org.culturegraph.mf.types.MultiMap;
2424
import org.culturegraph.mf.types.NamedValue;
25+
import org.culturegraph.mf.util.xml.Location;
2526
import org.junit.Assert;
2627
import org.junit.Test;
2728

@@ -175,4 +176,15 @@ public void addNamedValueSource(final NamedValueSource namedValueSource) {
175176
namedValueSource.setNamedValueReceiver(this);
176177
}
177178

179+
@Override
180+
public void setSourceLocation(final Location sourceLocation) {
181+
// Nothing to do
182+
}
183+
184+
@Override
185+
public Location getSourceLocation() {
186+
// Nothing to do
187+
return null;
188+
}
189+
178190
}

0 commit comments

Comments
 (0)