KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > extended > RegexPatternConverter


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 JavaDoc;
10
11 /**
12  * Ensures java.util.regex.Pattern is compiled upon deserialization.
13  */

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 JavaDoc type) {
23         return type.equals(Pattern JavaDoc.class);
24     }
25
26     public void marshal(Object JavaDoc source, HierarchicalStreamWriter writer, MarshallingContext context) {
27         defaultConverter.marshal(source, writer, context);
28     }
29
30     public Object JavaDoc unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
31         Pattern JavaDoc notCompiled = (Pattern JavaDoc) defaultConverter.unmarshal(reader, context);
32         Pattern JavaDoc compiled = Pattern.compile(notCompiled.pattern(), notCompiled.flags());
33         return compiled;
34     }
35
36 }
37
Popular Tags