1 package eu
.etaxonomy
.cdm
.jaxb
;
3 import java
.util
.regex
.Matcher
;
4 import java
.util
.regex
.Pattern
;
6 import javax
.xml
.bind
.annotation
.adapters
.XmlAdapter
;
8 import org
.joda
.time
.DateTimeFieldType
;
9 import org
.joda
.time
.Partial
;
11 public class PartialAdapter
extends XmlAdapter
<String
, Partial
> {
13 protected static Pattern PATTERN
;
14 private static String REGEX
= "^(\\d{4})(?:\\-(\\d{2})(?:\\-(\\d{2})(?:T(\\d{2})(?:\\:(\\d{2})(?:\\:(\\d{2})(?:\\.(\\d+))?)?)?)?)?)?$";
17 PATTERN
= Pattern
.compile(REGEX
);
21 public String
marshal(Partial partial
) throws Exception
{
22 StringBuilder stringBuilder
= new StringBuilder();
23 int[] values
= partial
.getValues();
25 switch(values
.length
) {
27 stringBuilder
.append("." + values
[6]);
29 stringBuilder
.insert(0,":" + values
[5]);
31 stringBuilder
.insert(0,":" + values
[4]);
33 stringBuilder
.insert(0, "T" + values
[3]);
35 stringBuilder
.insert(0, "-" + values
[2]);
37 stringBuilder
.insert(0, "-" + values
[1]);
39 stringBuilder
.insert(0, values
[0]);
42 return stringBuilder
.toString();
46 public Partial
unmarshal(String string
) throws Exception
{
47 Matcher matcher
= PATTERN
.matcher(string
);
48 if(matcher
.matches()) {
49 int nonNullGroups
= 0;
50 for(int i
= 1; i
<= matcher
.groupCount(); i
++) {
51 if(matcher
.group(i
) != null)
54 DateTimeFieldType
[] dateTimeFieldTypes
= new DateTimeFieldType
[nonNullGroups
];
55 int[] values
= new int[nonNullGroups
];
56 switch(nonNullGroups
) {
58 dateTimeFieldTypes
[6] = DateTimeFieldType
.millisOfSecond();
59 values
[6] = Integer
.parseInt(matcher
.group(7));
61 dateTimeFieldTypes
[5] = DateTimeFieldType
.secondOfMinute();
62 values
[5] = Integer
.parseInt(matcher
.group(6));
64 dateTimeFieldTypes
[4] = DateTimeFieldType
.minuteOfHour();
65 values
[4] = Integer
.parseInt(matcher
.group(5));
67 dateTimeFieldTypes
[3] = DateTimeFieldType
.hourOfDay();
68 values
[3] = Integer
.parseInt(matcher
.group(4));
70 dateTimeFieldTypes
[2] = DateTimeFieldType
.dayOfMonth();
71 values
[2] = Integer
.parseInt(matcher
.group(3));
73 dateTimeFieldTypes
[1] = DateTimeFieldType
.monthOfYear();
74 values
[1] = Integer
.parseInt(matcher
.group(2));
76 dateTimeFieldTypes
[0] = DateTimeFieldType
.year();
77 values
[0] = Integer
.parseInt(matcher
.group(1));
79 return new Partial(dateTimeFieldTypes
, values
);
81 throw new RuntimeException("Could not parse " + string
+ " with regex " + PartialAdapter
.REGEX
);