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