1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.eos.document;
17
18
19 import static net.sf.eos.config.ConfigurationKey.Type.CLASSNAME;
20 import net.sf.eos.EosException;
21 import net.sf.eos.analyzer.TokenizerException;
22 import net.sf.eos.config.Configuration;
23 import net.sf.eos.config.ConfigurationKey;
24 import net.sf.eos.config.Configured;
25 import net.sf.eos.config.FactoryMethod;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import java.io.IOException;
31 import java.io.Reader;
32 import java.io.Writer;
33
34
35
36
37
38
39
40
41 public abstract class Serializer extends Configured {
42
43
44 private static final Log LOG =
45 LogFactory.getLog(Serializer.class.getName());
46
47
48
49 @SuppressWarnings("nls")
50 @ConfigurationKey(type=CLASSNAME,
51 description="Implementation supports serialization "
52 + "and deserialization of EosDocuments.")
53 public final static String SERIALIZER_IMPL_CONFIG_NAME =
54 "net.sf.eos.document.Serializer.impl";
55
56 @FactoryMethod(key=SERIALIZER_IMPL_CONFIG_NAME,
57 implementation=XmlSerializer.class)
58 public final static Serializer newInstance(final Configuration config)
59 throws EosException {
60
61 final Thread t = Thread.currentThread();
62 ClassLoader classLoader = t.getContextClassLoader();
63 if (classLoader == null) {
64 classLoader = XmlSerializer.class.getClassLoader();
65 }
66
67 final String clazzName = config.get(SERIALIZER_IMPL_CONFIG_NAME,
68 XmlSerializer.class.getName());
69
70 try {
71 final Class<? extends Serializer> clazz =
72 (Class<? extends Serializer>)
73 Class.forName(clazzName, true, classLoader);
74 try {
75
76 final Serializer serializer = clazz.newInstance();
77 serializer.configure(config);
78 if (LOG.isDebugEnabled()) {
79 LOG.debug("Serializer instance: "
80 + serializer.getClass().getName());
81 }
82 return serializer;
83
84 } catch (final InstantiationException e) {
85 throw new TokenizerException(e);
86 } catch (final IllegalAccessException e) {
87 throw new TokenizerException(e);
88 }
89 } catch (final ClassNotFoundException e) {
90 throw new TokenizerException(e);
91 }
92 }
93
94 protected Serializer() {
95 super();
96 }
97
98
99
100
101
102
103
104
105 public abstract void serialize(final EosDocument doc,
106 final Writer out) throws IOException;
107
108
109
110
111
112
113
114
115
116 public abstract EosDocument deserialize(final Reader in)
117 throws Exception, IOException;
118 }