-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from cboehme/range-expander-collector
Added a metamorph collector to output ranges.
- Loading branch information
Showing
5 changed files
with
253 additions
and
4 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/main/java/org/culturegraph/mf/morph/collectors/Range.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,71 @@ | ||
/* | ||
* Copyright 2013 Deutsche Nationalbibliothek | ||
* | ||
* Licensed 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.culturegraph.mf.morph.collectors; | ||
|
||
import java.util.SortedSet; | ||
import java.util.TreeSet; | ||
|
||
import org.culturegraph.mf.morph.Metamorph; | ||
import org.culturegraph.mf.morph.NamedValueSource; | ||
|
||
|
||
/** | ||
* Corresponds to the <code><range></code> tag. | ||
* | ||
* @author Christoph Böhme | ||
*/ | ||
public final class Range extends AbstractCollect { | ||
private final SortedSet<Integer> values = new TreeSet<Integer>(); | ||
|
||
private Integer first; | ||
|
||
public Range(final Metamorph metamorph) { | ||
super(metamorph); | ||
setNamedValueReceiver(metamorph); | ||
} | ||
|
||
@Override | ||
protected void emit() { | ||
for (final Integer i: values) { | ||
getNamedValueReceiver().receive(getName(), i.toString(), this, getRecordCount(), getEntityCount()); | ||
} | ||
} | ||
|
||
@Override | ||
protected boolean isComplete() { | ||
return false; | ||
} | ||
|
||
@Override | ||
protected void receive(final String name, final String value, final NamedValueSource source) { | ||
if (first == null) { | ||
first = Integer.valueOf(value); | ||
} else { | ||
final int last = Integer.valueOf(value).intValue(); | ||
for (int i = first.intValue(); i <= last; ++i) { | ||
values.add(Integer.valueOf(i)); | ||
} | ||
first = null; | ||
} | ||
} | ||
|
||
@Override | ||
protected void clear() { | ||
values.clear(); | ||
first = null; | ||
} | ||
|
||
} |
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
164 changes: 164 additions & 0 deletions
164
src/test/java/org/culturegraph/mf/morph/collectors/RangeTest.xml
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,164 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<metamorph-test version="1.0" | ||
xmlns="http://www.culturegraph.org/metamorph-test" xmlns:mm="http://www.culturegraph.org/metamorph" | ||
xmlns:cgxml="http://www.culturegraph.org/cgxml"> | ||
|
||
<test-case name="should output all numbers between first and last (including first and last)"> | ||
<input type="text/x-cg+xml"> | ||
<cgxml:cgxml version="1.0"> | ||
<cgxml:records> | ||
<cgxml:record id="1"> | ||
<cgxml:literal name="first" value="1789" /> | ||
<cgxml:literal name="last" value="1794" /> | ||
</cgxml:record> | ||
</cgxml:records> | ||
</cgxml:cgxml> | ||
</input> | ||
|
||
<transformation type="text/x-metamorph+xml"> | ||
<mm:metamorph version="1"> | ||
<mm:rules> | ||
<mm:range name="range" flushWith="record"> | ||
<mm:data source="first" /> | ||
<mm:data source="last" /> | ||
</mm:range> | ||
</mm:rules> | ||
</mm:metamorph> | ||
</transformation> | ||
|
||
<result type="text/x-cg+xml"> | ||
<cgxml:cgxml version="1.0"> | ||
<cgxml:records> | ||
<cgxml:record id="1"> | ||
<cgxml:literal name="range" value="1789" /> | ||
<cgxml:literal name="range" value="1790" /> | ||
<cgxml:literal name="range" value="1791" /> | ||
<cgxml:literal name="range" value="1792" /> | ||
<cgxml:literal name="range" value="1793" /> | ||
<cgxml:literal name="range" value="1794" /> | ||
</cgxml:record> | ||
</cgxml:records> | ||
</cgxml:cgxml> | ||
</result> | ||
</test-case> | ||
|
||
<test-case name="should output first if last equals first"> | ||
<input type="text/x-cg+xml"> | ||
<cgxml:cgxml version="1.0"> | ||
<cgxml:records> | ||
<cgxml:record id="1"> | ||
<cgxml:literal name="first" value="1989" /> | ||
<cgxml:literal name="last" value="1989" /> | ||
</cgxml:record> | ||
</cgxml:records> | ||
</cgxml:cgxml> | ||
</input> | ||
|
||
<transformation type="text/x-metamorph+xml"> | ||
<mm:metamorph version="1"> | ||
<mm:rules> | ||
<mm:range name="range" flushWith="record"> | ||
<mm:data source="first" /> | ||
<mm:data source="last" /> | ||
</mm:range> | ||
</mm:rules> | ||
</mm:metamorph> | ||
</transformation> | ||
|
||
<result type="text/x-cg+xml"> | ||
<cgxml:cgxml version="1.0"> | ||
<cgxml:records> | ||
<cgxml:record id="1"> | ||
<cgxml:literal name="range" value="1989" /> | ||
</cgxml:record> | ||
</cgxml:records> | ||
</cgxml:cgxml> | ||
</result> | ||
</test-case> | ||
|
||
<test-case name="should output multiple ranges"> | ||
<input type="text/x-cg+xml"> | ||
<cgxml:cgxml version="1.0"> | ||
<cgxml:records> | ||
<cgxml:record id="1"> | ||
<cgxml:literal name="first" value="1789" /> | ||
<cgxml:literal name="last" value="1792" /> | ||
<cgxml:literal name="first" value="1794" /> | ||
<cgxml:literal name="last" value="1799" /> | ||
</cgxml:record> | ||
</cgxml:records> | ||
</cgxml:cgxml> | ||
</input> | ||
|
||
<transformation type="text/x-metamorph+xml"> | ||
<mm:metamorph version="1"> | ||
<mm:rules> | ||
<mm:range name="range" flushWith="record"> | ||
<mm:data source="first" /> | ||
<mm:data source="last" /> | ||
</mm:range> | ||
</mm:rules> | ||
</mm:metamorph> | ||
</transformation> | ||
|
||
<result type="text/x-cg+xml"> | ||
<cgxml:cgxml version="1.0"> | ||
<cgxml:records> | ||
<cgxml:record id="1"> | ||
<cgxml:literal name="range" value="1789" /> | ||
<cgxml:literal name="range" value="1790" /> | ||
<cgxml:literal name="range" value="1791" /> | ||
<cgxml:literal name="range" value="1792" /> | ||
|
||
<cgxml:literal name="range" value="1794" /> | ||
<cgxml:literal name="range" value="1795" /> | ||
<cgxml:literal name="range" value="1796" /> | ||
<cgxml:literal name="range" value="1797" /> | ||
<cgxml:literal name="range" value="1798" /> | ||
<cgxml:literal name="range" value="1799" /> | ||
</cgxml:record> | ||
</cgxml:records> | ||
</cgxml:cgxml> | ||
</result> | ||
</test-case> | ||
|
||
<test-case name="should remove duplicate numbers from overlapping ranges"> | ||
<input type="text/x-cg+xml"> | ||
<cgxml:cgxml version="1.0"> | ||
<cgxml:records> | ||
<cgxml:record id="1"> | ||
<cgxml:literal name="first" value="1789" /> | ||
<cgxml:literal name="last" value="1792" /> | ||
<cgxml:literal name="first" value="1790" /> | ||
<cgxml:literal name="last" value="1791" /> | ||
</cgxml:record> | ||
</cgxml:records> | ||
</cgxml:cgxml> | ||
</input> | ||
|
||
<transformation type="text/x-metamorph+xml"> | ||
<mm:metamorph version="1"> | ||
<mm:rules> | ||
<mm:range name="range" flushWith="record"> | ||
<mm:data source="first" /> | ||
<mm:data source="last" /> | ||
</mm:range> | ||
</mm:rules> | ||
</mm:metamorph> | ||
</transformation> | ||
|
||
<result type="text/x-cg+xml"> | ||
<cgxml:cgxml version="1.0"> | ||
<cgxml:records> | ||
<cgxml:record id="1"> | ||
<cgxml:literal name="range" value="1789" /> | ||
<cgxml:literal name="range" value="1790" /> | ||
<cgxml:literal name="range" value="1791" /> | ||
<cgxml:literal name="range" value="1792" /> | ||
</cgxml:record> | ||
</cgxml:records> | ||
</cgxml:cgxml> | ||
</result> | ||
</test-case> | ||
|
||
</metamorph-test> |