01: /*
02: * Copyright (C) 2007, 2008 XStream Committers.
03: * All rights reserved.
04: *
05: * The software in this package is published under the terms of the BSD
06: * style license a copy of which has been included with this distribution in
07: * the LICENSE.txt file.
08: *
09: * Created on 21.09.2007 by Joerg Schaible
10: */
11: package com.thoughtworks.xstream.converters.extended;
12:
13: import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;
14:
15: import javax.xml.datatype.DatatypeConfigurationException;
16: import javax.xml.datatype.DatatypeFactory;
17: import javax.xml.datatype.Duration;
18:
19: /**
20: * A Converter for the XML Schema datatype <a
21: * href="http://www.w3.org/TR/xmlschema-2/#duration">duration</a> and the Java type
22: * {@link javax.xml.datatype.Duration Duration}.
23: *
24: * @author John Kristian
25: * @author Jörg Schaible
26: * @since 1.3
27: */
28: public class DurationConverter extends AbstractSingleValueConverter {
29: private final DatatypeFactory factory;
30:
31: public DurationConverter() throws DatatypeConfigurationException {
32: this (DatatypeFactory.newInstance());
33: }
34:
35: public DurationConverter(DatatypeFactory factory) {
36: this .factory = factory;
37: }
38:
39: public boolean canConvert(Class c) {
40: return factory != null && Duration.class.isAssignableFrom(c);
41: }
42:
43: public Object fromString(String s) {
44: return factory.newDuration(s);
45: }
46: }
|