01: /*
02: * Copyright (C) 2004, 2005 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 31. July 2004 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.converters.extended;
13:
14: import com.thoughtworks.xstream.converters.Converter;
15: import com.thoughtworks.xstream.converters.MarshallingContext;
16: import com.thoughtworks.xstream.converters.UnmarshallingContext;
17: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
18: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
19:
20: import java.util.regex.Pattern;
21:
22: /**
23: * Ensures java.util.regex.Pattern is compiled upon deserialization.
24: */
25: public class RegexPatternConverter implements Converter {
26:
27: private Converter defaultConverter;
28:
29: public RegexPatternConverter(Converter defaultConverter) {
30: this .defaultConverter = defaultConverter;
31: }
32:
33: public boolean canConvert(final Class type) {
34: return type.equals(Pattern.class);
35: }
36:
37: public void marshal(Object source, HierarchicalStreamWriter writer,
38: MarshallingContext context) {
39: defaultConverter.marshal(source, writer, context);
40: }
41:
42: public Object unmarshal(HierarchicalStreamReader reader,
43: UnmarshallingContext context) {
44: Pattern notCompiled = (Pattern) defaultConverter.unmarshal(
45: reader, context);
46: return Pattern.compile(notCompiled.pattern(), notCompiled
47: .flags());
48: }
49:
50: }
|