View Javadoc

1   /* This file is part of COPAL (COntext Provisioning for All).
2    *
3    * COPAL is a part of SM4All (Smart hoMes for All) project.
4    *
5    * COPAL is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * COPAL is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with COPAL. If not, see <http://www.gnu.org/licenses/>.
17   */
18  package at.ac.tuwien.infosys.sm4all.copal.api.xml;
19  
20  import org.w3c.dom.Document;
21  import org.w3c.dom.Element;
22  import org.w3c.dom.Node;
23  import org.w3c.dom.NodeList;
24  
25  /**
26   * Command that marshals an array.
27   * 
28   * @param <T> the type of array elements.
29   * @author sanjin
30   */
31  public abstract class ArrayMarshaller<T> extends ElementUtil implements
32          Marshaller<T[]> {
33  
34      private final Element parentElement;
35      private final String namespaceURI;
36      private final String qualifiedName;
37      private final String localName;
38  
39      /**
40       * Creates an instance of array marshaller which uses specified parent
41       * {@link Element} to marshal array elements.
42       * 
43       * @param parentElement the {@link Element} that holds all marshalled
44       *        {@link Element}s.
45       * @param namespaceURI the namespace URI of marshalled {@link Element}s.
46       * @param prefix the prefix of marshalled {@link Element}s.
47       * @param localName the local name of marshalled {@link Element}s.
48       * @throws NullPointerException if specified parent {@link Element} is
49       *         <code>null</code>.
50       */
51      public ArrayMarshaller(final Element parentElement,
52              final String namespaceURI, final String prefix,
53              final String localName) {
54          super(parentElement);
55  
56          this.parentElement = parentElement;
57          this.namespaceURI = namespaceURI;
58          this.qualifiedName = prefix + ":" + localName;
59          this.localName = localName;
60      }
61  
62      /**
63       * Marshal specified object into specified {@link Element}.
64       * 
65       * @param object the object to marshal.
66       * @param element the {@link Element} into which specified object should be
67       *        marshalled.
68       */
69      protected abstract void marshal(T object, Element element);
70  
71      /**
72       * Removes all array {@link Element}s from specified parent {@link Element}
73       * and appends marshalled array {@link Element}s from specified array.
74       * 
75       * @param array the array.
76       * @throws NullPointerException if specified array is <code>null</code>.
77       */
78      @Override
79      public void marshal(final T[] array) {
80          if (array == null)
81              throw new NullPointerException("Array cannot be null.");
82  
83          removeArrayElements();
84  
85          final Document document = this.parentElement.getOwnerDocument();
86          for (final T object : array)
87              addArrayElement(object, document);
88      }
89  
90      private void removeArrayElements() {
91          final NodeList elements = this.parentElement.getElementsByTagNameNS(
92                  this.namespaceURI, this.localName);
93          for (int i = 0; i < elements.getLength(); i++) {
94              final Node node = elements.item(i);
95              if ((node.getParentNode() == this.parentElement)
96                      && (node instanceof Element))
97                  this.parentElement.removeChild(node);
98          }
99      }
100 
101     private void addArrayElement(final T object, final Document document) {
102         final Element element = document.createElementNS(this.namespaceURI,
103                 this.qualifiedName);
104         marshal(object, element);
105         this.parentElement.appendChild(element);
106     }
107 }