1
2
3
4 package edu.virginia.lib.ole.akubra;
5
6 import java.io.UnsupportedEncodingException;
7 import java.net.URI;
8 import java.net.URLDecoder;
9 import java.net.URLEncoder;
10 import java.util.zip.Adler32;
11
12 import org.akubraproject.map.IdMapper;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16
17
18
19
20
21 public class BitStringMapper implements IdMapper {
22
23 private static final Logger log = LoggerFactory
24 .getLogger(BitStringMapper.class);
25
26
27
28
29 public URI getExternalId(URI internalId) throws NullPointerException {
30 String uriandpath = internalId.toString();
31 String uri = "";
32 try {
33 uri = URLDecoder.decode(
34 uriandpath.substring(uriandpath.lastIndexOf('/') + 1),
35 "UTF-8");
36 } catch (UnsupportedEncodingException e) {
37
38 }
39 return URI.create(uri);
40 }
41
42
43
44
45 public URI getInternalId(URI externalId) throws NullPointerException {
46 log.debug("Entering BitStringMapper.getInternalId()");
47 String characters = "";
48 try {
49 characters = URLEncoder.encode(externalId.toString(), "UTF-8");
50 } catch (UnsupportedEncodingException e) {
51
52 }
53 String hashcharacters = Long.toBinaryString(hash(characters));
54 log.debug("Using hashcharacters: " + hashcharacters);
55 StringBuffer path = new StringBuffer();
56 for (int i = 0; i < hashcharacters.length() - Constants.CHUNK; i = i
57 + Constants.CHUNK) {
58 String section = hashcharacters.substring(i, i + Constants.CHUNK)
59 + "/";
60 log.debug("Appending: '" + section + "' to path");
61 path.append(section);
62 }
63 String uristring = path.toString() + characters;
64 log.debug("Exiting BitStringMapper.getInternalId() with: " + uristring);
65 return URI.create(String.valueOf(uristring));
66 }
67
68
69
70
71
72
73 public String getInternalPrefix(String externalPrefix)
74 throws NullPointerException {
75 return "";
76 }
77
78
79
80
81
82
83 private Long hash(String characters) {
84 Adler32 hasher = new Adler32();
85 hasher.reset();
86 hasher.update(characters.getBytes());
87 return hasher.getValue();
88 }
89
90 }