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;
33  
34  import com.cedarsoft.AssertUtils;
35  import org.jdom.Document;
36  import org.jdom.Element;
37  import org.jdom.JDOMException;
38  import org.jdom.Namespace;
39  import org.jdom.input.SAXBuilder;
40  import org.jdom.output.Format;
41  import org.jdom.output.XMLOutputter;
42  
43  import javax.annotation.Nonnull;
44  import java.io.ByteArrayInputStream;
45  import java.io.IOException;
46  
47  /**
48   * Abstract base class for XML based serializers.
49   * <p/>
50   * <p/>
51   * Attention: it is necessary to define at least one DataPoint:
52   * <p/>
53   * <pre>&#064;DataPoint<br/>public static final Entry&lt;?&gt; ENTRY1 = create(<br/> new DomainObject(),<br/> &quot;&lt;xml/&gt;&quot; );</pre>
54   *
55   * @param <T> the type of the serialized object
56   */
57  public abstract class AbstractXmlSerializerTest2<T> extends AbstractSerializerTest2<T> {
58    protected void verify( @Nonnull byte[] current, @Nonnull byte[] exectedXml ) throws Exception {
59      if ( addNameSpace() ) {
60        String expectedWithNamespace = addNameSpace( ( AbstractXmlSerializer<?, ?, ?, ?> ) getSerializer(), exectedXml );
61        AssertUtils.assertXMLEquals( expectedWithNamespace, new String( current ) );
62      } else {
63        AssertUtils.assertXMLEquals( new String( exectedXml ), new String( current ) );
64      }
65    }
66  
67    @Override
68    protected void verifySerialized( @Nonnull Entry<T> entry, @Nonnull byte[] serialized ) throws Exception {
69      verify( serialized, entry.getExpected() );
70    }
71  
72    protected boolean addNameSpace() {
73      return true;
74    }
75  
76  
77    @Nonnull
78  
79    public static String addNameSpace( @Nonnull AbstractXmlSerializer<?, ?, ?, ?> serializer, @Nonnull byte[] xmlBytes ) throws Exception {
80      return addNameSpace( serializer.createNameSpace( serializer.getFormatVersion() ), xmlBytes );
81    }
82  
83    public static String addNameSpace( @Nonnull String nameSpaceUri, @Nonnull byte[] xml ) throws JDOMException, IOException {
84      Document doc = new SAXBuilder().build( new ByteArrayInputStream( xml ) );
85  
86      Element root = doc.getRootElement();
87      if ( root.getNamespaceURI().length() == 0 ) {
88        Namespace namespace = Namespace.getNamespace( nameSpaceUri );
89  
90        addNameSpaceRecursively( root, namespace );
91      }
92  
93      return new XMLOutputter( Format.getPrettyFormat() ).outputString( doc );
94    }
95  
96    public static void addNameSpaceRecursively( @Nonnull Element element, @Nonnull Namespace namespace ) {
97      element.setNamespace( namespace );
98      for ( Element child : ( ( Iterable<? extends Element> ) element.getChildren() ) ) {
99        addNameSpaceRecursively( child, namespace );
100     }
101   }
102 
103   @Nonnull
104   protected static <T> Entry<? extends T> create( @Nonnull T object, @Nonnull String expected ) {
105     return new Entry<T>( object, expected.getBytes() );
106   }
107 }