cleanup
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / jaxb / PartialAdapter.java
1 /**
2 * Copyright (C) 2009 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.regex.Matcher;
13 import java.util.regex.Pattern;
14
15 import javax.xml.bind.annotation.adapters.XmlAdapter;
16
17 import org.joda.time.DateTimeFieldType;
18 import org.joda.time.Partial;
19
20 public class PartialAdapter extends XmlAdapter<String, Partial> {
21
22 protected static Pattern PATTERN;
23 private static String REGEX = "^(\\d{4})(?:\\-(\\d{1,2})(?:\\-(\\d{1,2})(?:T(\\d{2})(?:\\:(\\d{2})(?:\\:(\\d{2})(?:\\.(\\d+))?)?)?)?)?)?$";
24
25 static {
26 PATTERN = Pattern.compile(REGEX);
27 }
28
29 @Override
30 public String marshal(Partial partial) throws Exception {
31 StringBuilder stringBuilder = new StringBuilder();
32 int[] values = partial.getValues();
33
34 switch(values.length) {
35 case 7:
36 stringBuilder.append("." + values[6]);
37 case 6:
38 stringBuilder.insert(0,":" + values[5]);
39 case 5:
40 stringBuilder.insert(0,":" + values[4]);
41 case 4:
42 stringBuilder.insert(0, "T" + values[3]);
43 case 3:
44 stringBuilder.insert(0, "-" + values[2]);
45 case 2:
46 stringBuilder.insert(0, "-" + values[1]);
47 case 1:
48 stringBuilder.insert(0, values[0]);
49 }
50
51 return stringBuilder.toString();
52 }
53
54 @Override
55 public Partial unmarshal(String string) throws Exception {
56 Matcher matcher = PATTERN.matcher(string);
57 if(matcher.matches()) {
58 int nonNullGroups = 0;
59 for(int i = 1; i <= matcher.groupCount(); i++) {
60 if(matcher.group(i) != null)
61 nonNullGroups++;
62 }
63 DateTimeFieldType[] dateTimeFieldTypes = new DateTimeFieldType[nonNullGroups];
64 int[] values = new int[nonNullGroups];
65 switch(nonNullGroups) {
66 case 7:
67 dateTimeFieldTypes[6] = DateTimeFieldType.millisOfSecond();
68 values[6] = Integer.parseInt(matcher.group(7));
69 case 6:
70 dateTimeFieldTypes[5] = DateTimeFieldType.secondOfMinute();
71 values[5] = Integer.parseInt(matcher.group(6));
72 case 5:
73 dateTimeFieldTypes[4] = DateTimeFieldType.minuteOfHour();
74 values[4] = Integer.parseInt(matcher.group(5));
75 case 4:
76 dateTimeFieldTypes[3] = DateTimeFieldType.hourOfDay();
77 values[3] = Integer.parseInt(matcher.group(4));
78 case 3:
79 dateTimeFieldTypes[2] = DateTimeFieldType.dayOfMonth();
80 values[2] = Integer.parseInt(matcher.group(3));
81 case 2:
82 dateTimeFieldTypes[1] = DateTimeFieldType.monthOfYear();
83 values[1] = Integer.parseInt(matcher.group(2));
84 case 1:
85 dateTimeFieldTypes[0] = DateTimeFieldType.year();
86 values[0] = Integer.parseInt(matcher.group(1));
87 }
88 return new Partial(dateTimeFieldTypes, values);
89 } else {
90 throw new RuntimeException("Could not parse " + string + " with regex " + PartialAdapter.REGEX);
91 }
92 }
93
94 }