-
Notifications
You must be signed in to change notification settings - Fork 34
Data lookup
A certain group of functions takes a map/dictionary as argument: <lookup>
, <whitelist>
, <blacklist>
etc.
In this section the usage of such maps will be explained. We start with a simple example of data lookup.
Take for instance an operation in which you want to replace values according to a lookup table: Value 'A' maps to 'print', 'B' maps to 'audiovisual' an so forth. This is accomplished by the <lookup>
function. The lookup table is defined inside the <lookup>
tag. The following code snippet depicts this situation.
<data source="[email protected]" name="dcterms:format">
<substring start="0" end="1" />
<lookup>
<entry name="A" value="print" />
<entry name="B" value="audiovisual" />
<entry name="O" value="online" />
</lookup>
</data>
The same lookup tables may used in different places in a Metamorph definition. To enable reuse, a map/dictionary can be defined separately from the respective lookup function.
In the following listing the <lookup>
function refers to the table using the name material.
<lookup in="material">
To define a map, there are mainly three options (more to come in the future).
With the <map>
tag the contents of the map can be defined right away in the Metamorph definition.
<map name="material">
<entry name="A" value="print" />
<entry name="B" value="audiovisual" />
<entry name="O" value="online" />
</map>
Sometimes, it is more convenient to store mappings in a separate text file:
<filemap name="map1" files="maps/MARC-country-codes.txt" separator="\t"/>
The situation might arise that the data cannot be hard-coded in xml/text etc; or at least hard-coding it would be very inconvenient. Imagine we want to resolve author ids (> 5 Million) to author names: Putting all the id-name mappings into the Metamorph definition file or text file is certainly not desirable.
To address this issue, Metamorph allows you to load any class which implements the Map
interface.
<javamap name="map-name" class="org.mydomain.MyMap" parameter1="xy" />
Map
s can also be added to Metamorph programmatically in Java:
//create a Map. Any object implementing Map<String, String> will do
final Map<String, String> map = new HashMap<String, String>();
map.put("one key", "first String");
map.put("another key", "another String");
//tell metamorph to use it during lookup operations
metamorph.putMap("name of map", map);
Important: If your Map
implementation allocates resources which need to be closed eventually (database connections etc.), let it implement the java.io.Closeable
interface. Metamorph keeps track of all Closable
s and will call the close()
method upon closeStream()
.
Fix or Metamorph - you can choose between one of them. Fix is recommended for the most users.
Metamorph or Fix - you can choose between one of them. Fix is recommended for the most users.