1 package com.thoughtworks.xstream.converters.extended; 2 3 import com.thoughtworks.xstream.converters.Converter; 4 import com.thoughtworks.xstream.converters.MarshallingContext; 5 import com.thoughtworks.xstream.converters.UnmarshallingContext; 6 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 7 import com.thoughtworks.xstream.io.HierarchicalStreamReader; 8 9 import java.util.regex.Pattern ; 10 11 14 public class RegexPatternConverter implements Converter { 15 16 private Converter defaultConverter; 17 18 public RegexPatternConverter(Converter defaultConverter) { 19 this.defaultConverter = defaultConverter; 20 } 21 22 public boolean canConvert(final Class type) { 23 return type.equals(Pattern .class); 24 } 25 26 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { 27 defaultConverter.marshal(source, writer, context); 28 } 29 30 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { 31 Pattern notCompiled = (Pattern ) defaultConverter.unmarshal(reader, context); 32 Pattern compiled = Pattern.compile(notCompiled.pattern(), notCompiled.flags()); 33 return compiled; 34 } 35 36 } 37 | Popular Tags |