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.query;
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.ContextQuery;
24  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.Constants;
25  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.Marshaller;
26  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.listener.XMLContextListeners;
27  
28  /**
29   * Command that marshalls a {@link ContextQuery} into an {@link Element}.
30   * 
31   * @author sanjin
32   */
33  public class XMLContextQuery implements Marshaller<ContextQuery> {
34  
35      private final Element element;
36  
37      /**
38       * Creates instance of {@link ContextQuery} marshaller which uses specified
39       * {@link Element} to marshal the {@link ContextQuery}.
40       * 
41       * @param element the {@link Element} used for marshalling.
42       * @throws NullPointerException if specified {@link Element} is
43       *         <code>null</code>.
44       */
45      public XMLContextQuery(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       * Marshalls specified {@link ContextQuery} into specified {@link Element}.
56       * 
57       * @param query the {@link ContextQuery}.
58       * @throws NullPointerException if specified {@link ContextQuery} is
59       *         <code>null</code>.
60       */
61      @Override
62      public void marshal(final ContextQuery query) {
63          if (query == null)
64              throw new NullPointerException("Query cannot be null.");
65  
66          // remove all children
67          while (this.element.hasChildNodes())
68              this.element.removeChild(this.element.getFirstChild());
69  
70          new Name(this.element).marshal(query);
71          new Event(this.element).marshal(query);
72          new Criteria(this.element).marshal(query);
73  
74          final Document document = this.element.getOwnerDocument();
75  
76          final ContextListener[] listeners = query.getListeners();
77          if (listeners.length > 0) {
78              final Element listnersElement = document.createElementNS(
79                      Constants.COPAL_NAMESPACE_URI, Constants.COPAL_PREFIX
80                              + ":Listeners");
81              new XMLContextListeners(listnersElement).marshal(listeners);
82              this.element.appendChild(listnersElement);
83          }
84      }
85  }