View Javadoc

1   /**
2    * Copyright (C) cedarsoft GmbH.
3    *
4    * Licensed under the GNU General Public License version 3 (the "License")
5    * with Classpath Exception; you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at
7    *
8    *         http://www.cedarsoft.org/gpl3ce
9    *         (GPL 3 with Classpath Exception)
10   *
11   * This code is free software; you can redistribute it and/or modify it
12   * under the terms of the GNU General Public License version 3 only, as
13   * published by the Free Software Foundation. cedarsoft GmbH designates this
14   * particular file as subject to the "Classpath" exception as provided
15   * by cedarsoft GmbH in the LICENSE file that accompanied this code.
16   *
17   * This code is distributed in the hope that it will be useful, but WITHOUT
18   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20   * version 3 for more details (a copy is included in the LICENSE file that
21   * accompanied this code).
22   *
23   * You should have received a copy of the GNU General Public License version
24   * 3 along with this work; if not, write to the Free Software Foundation,
25   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26   *
27   * Please contact cedarsoft GmbH, 72810 Gomaringen, Germany,
28   * or visit www.cedarsoft.com if you need additional information or
29   * have any questions.
30   */
31  
32  package com.cedarsoft.serialization.jdom;
33  
34  import com.cedarsoft.Version;
35  import com.cedarsoft.VersionException;
36  import com.cedarsoft.VersionRange;
37  import com.cedarsoft.serialization.AbstractXmlSerializer;
38  import org.jdom.Document;
39  import org.jdom.Element;
40  import org.jdom.JDOMException;
41  import org.jdom.Namespace;
42  import org.jdom.input.SAXBuilder;
43  import org.jdom.output.Format;
44  import org.jdom.output.XMLOutputter;
45  import org.jetbrains.annotations.NonNls;
46  import org.jetbrains.annotations.NotNull;
47  
48  import java.io.IOException;
49  import java.io.InputStream;
50  import java.io.OutputStream;
51  
52  /**
53   * Abstract serializer based on JDom
54   *
55   * @param <T> the type
56   */
57  public abstract class AbstractJDomSerializer<T> extends AbstractXmlSerializer<T, Element, Element, IOException> {
58    @NotNull
59    @NonNls
60    protected static final String LINE_SEPARATOR = "\n";
61  
62    protected AbstractJDomSerializer( @NotNull @NonNls String defaultElementName, @NonNls @NotNull String nameSpaceUriBase, @NotNull VersionRange formatVersionRange ) {
63      super( defaultElementName, nameSpaceUriBase, formatVersionRange );
64    }
65  
66    @NotNull
67    public Element serializeToElement( @NotNull T object ) throws IOException {
68      Element element = new Element( getDefaultElementName() );
69      serialize( element, object );
70      return element;
71    }
72  
73    @Override
74    public void serialize( @NotNull T object, @NotNull OutputStream out ) throws IOException {
75      Document document = new Document();
76  
77      //The name space
78      Namespace namespace = Namespace.getNamespace( getNameSpaceUri() );
79  
80      //Create the root
81      Element root = new Element( getDefaultElementName(), namespace );
82      document.setRootElement( root );
83  
84      serialize( root, object );
85      new XMLOutputter( Format.getPrettyFormat().setLineSeparator( LINE_SEPARATOR ) ).output( document, out );
86    }
87  
88    @Override
89    @NotNull
90    public T deserialize( @NotNull InputStream in ) throws IOException, VersionException {
91      try {
92        Document document = new SAXBuilder().build( in );
93  
94        String namespaceURI = document.getRootElement().getNamespaceURI();
95        Version formatVersion = parseVersionFromNamespaceUri( namespaceURI );
96  
97        Version.verifyMatch( getFormatVersion(), formatVersion );
98  
99        return deserialize( document.getRootElement(), formatVersion );
100     } catch ( JDOMException e ) {
101       throw new IOException( "Could not parse stream due to " + e.getMessage(), e );
102     }
103   }
104 }