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.event.xml.listener;
19  
20  import org.w3c.dom.Document;
21  import org.w3c.dom.Element;
22  import at.ac.tuwien.infosys.sm4all.copal.api.ContextListener;
23  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.Constants;
24  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.Marshaller;
25  
26  /**
27   * Command that marshalls an array of {@link ContextListener}s into an
28   * {@link Element}.
29   * 
30   * @author sanjin
31   */
32  public class Listeners implements Marshaller<ContextListener[]> {
33  
34      private final Element element;
35  
36      /**
37       * Constructs instance of {@link ContextListener}s marshaller which uses
38       * specified {@link Element} to marshal the {@link ContextListener}s.
39       * 
40       * @param element the {@link Element} used for marshalling and
41       *        unmarshalling.
42       * @throws NullPointerException if specified {@link Element} is
43       *         <code>null</code>.
44       */
45      public Listeners(final Element element) {
46          super();
47  
48          if (element == null)
49              throw new NullPointerException("XML DOM element cannot be null.");
50  
51          this.element = element;
52      }
53  
54      /**
55       * Removes all child {@link Element}s of specified {@link Element} and
56       * appends marshalled {@link ContextListener}s from specified array of
57       * {@link ContextListener}s using the
58       * {@link XMLContextListener#marshal(ContextListener)} method.
59       * 
60       * @param listeners the {@link ContextListener}s.
61       * @throws NullPointerException if specified array of
62       *         {@link ContextListener}s is <code>null</code>.
63       */
64      @Override
65      public void marshal(final ContextListener[] listeners) {
66          if (listeners == null)
67              throw new NullPointerException("Listeners cannot be null.");
68  
69          // remove all children
70          while (this.element.hasChildNodes())
71              this.element.removeChild(this.element.getFirstChild());
72  
73          final Document document = this.element.getOwnerDocument();
74  
75          for (final ContextListener listener : listeners) {
76              final Element listnerElement = document.createElementNS(
77                      Constants.COPAL_NAMESPACE_URI, Constants.COPAL_PREFIX
78                              + ":Listener");
79              new XMLContextListener(listnerElement).marshal(listener);
80              this.element.appendChild(listnerElement);
81          }
82      }
83  }