1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.jaxb;
|
11
|
|
12
|
import java.util.UUID;
|
13
|
|
14
|
import javax.xml.bind.annotation.adapters.XmlAdapter;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
|
18
|
/**
|
19
|
* @author a.mueller
|
20
|
* @since 23.07.2008
|
21
|
* @version 1.0
|
22
|
*/
|
23
|
|
24
|
|
25
|
public class UUIDAdapter extends XmlAdapter<String, UUID> {
|
26
|
private static final Logger logger = Logger.getLogger(UUIDAdapter.class);
|
27
|
|
28
|
public static String UUID_URN_PREFIX = "urn-uuid-";
|
29
|
|
30
|
@Override
|
31
|
public String marshal(UUID uuid) throws Exception {
|
32
|
if (logger.isDebugEnabled()){logger.debug("marshal");}
|
33
|
if(uuid != null) {
|
34
|
return UUIDAdapter.UUID_URN_PREFIX + uuid.toString();
|
35
|
} else {
|
36
|
return null;
|
37
|
}
|
38
|
}
|
39
|
|
40
|
@Override
|
41
|
public UUID unmarshal(String string) throws Exception {
|
42
|
if (logger.isDebugEnabled()){logger.debug("unmarshal");}
|
43
|
if(string.startsWith(UUIDAdapter.UUID_URN_PREFIX)) {
|
44
|
String uuidPart = string.substring(UUIDAdapter.UUID_URN_PREFIX.length());
|
45
|
return UUID.fromString(uuidPart);
|
46
|
} else {
|
47
|
throw new Exception("uuid attribute should start with " + UUIDAdapter.UUID_URN_PREFIX);
|
48
|
}
|
49
|
}
|
50
|
}
|