Skip to content
Fabian Steeg edited this page Sep 17, 2020 · 6 revisions

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.

Local 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>	

Defining Maps

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).

Maps in XML

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>

Maps in separate text files

Sometimes, it is more convenient to store mappings in a separate text file:

<filemap name="map1" files="maps/MARC-country-codes.txt" separator="\t"/>

Maps implemented by a Java class

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" />

Maps 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 Closables and will call the close() method upon closeStream().