View Javadoc

1   /* Copyright (c) 2008 Sascha Kohlmann
2    *
3    * This program is free software: you can redistribute it and/or modify
4    * it under the terms of the GNU Affero General Public License as published by
5    * the Free Software Foundation, either version 3 of the License, or
6    * (at your option) any later version.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Affero General Public License for more details.
12   *
13   * You should have received a copy of the GNU Affero General Public License
14   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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   * Implementations must serialize and deserialize an {@link EosDocument}.
36   * An implementation which serialized a document thru th 
37   * {@link #serialize(EosDocument, Writer)} implementation must deserialize it
38   * thru the corresponding {@link #deserialize(Reader)} method.
39   * @author Sascha Kohlmann
40   */
41  public abstract class Serializer extends Configured {
42  
43      /** For logging. */
44      private static final Log LOG =
45          LogFactory.getLog(Serializer.class.getName());
46  
47      /** The configuration key name for the classname of the serializer.
48       * @see #newInstance(Configuration) */
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       * Implementations serializes the content of an {@code EosDocument}
100      * thru the given <em>writer</em>
101      * @param doc the document to serialize
102      * @param out the sink to write the content thru
103      * @throws IOException if something goes wrong during serialization
104      */
105     public abstract void serialize(final EosDocument doc,
106                                    final Writer out) throws IOException;
107 
108     /**
109      * Implementations must deserialize a {@code EosDocument} which are
110      * serialized by {@link #serialize(EosDocument,Writer)}.
111      * @param in the stream to read the {@code EosDocument} content from
112      * @return a {@code EosDocument} constructed form the content
113      * @throws Exception if an error occurs during deserilization
114      * @throws IOException if <em>in</em> occurs an error
115      */
116     public abstract EosDocument deserialize(final Reader in)
117         throws Exception, IOException;
118 }