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.query.xml;
19  
20  import org.apache.log4j.Logger;
21  import org.w3c.dom.Document;
22  import org.w3c.dom.Element;
23  import at.ac.tuwien.infosys.sm4all.copal.api.listener.ContextListener;
24  import at.ac.tuwien.infosys.sm4all.copal.api.listener.xml.XMLContextListeners;
25  import at.ac.tuwien.infosys.sm4all.copal.api.query.ContextQuery;
26  import at.ac.tuwien.infosys.sm4all.copal.api.xml.Constants;
27  import at.ac.tuwien.infosys.sm4all.copal.api.xml.ElementUtil;
28  import at.ac.tuwien.infosys.sm4all.copal.api.xml.Marshaller;
29  import at.ac.tuwien.infosys.sm4all.copal.api.xml.MissingFieldException;
30  
31  /**
32   * Command that marshalls a {@link ContextQuery} into an {@link Element}.
33   * 
34   * @author sanjin
35   */
36  public class XMLContextQuery extends ElementUtil implements
37          Marshaller<ContextQuery> {
38  
39      private static final String LISTENERS_ELEMENT_NAME = "Listeners";
40  
41      private static final Logger LOGGER = Logger.getLogger(XMLContextQuery.class);
42  
43      private final Element element;
44  
45      /**
46       * Creates instance of {@link ContextQuery} marshaller which uses specified
47       * {@link Element} to marshal the {@link ContextQuery}.
48       * 
49       * @param element the {@link Element} used for marshalling.
50       * @throws NullPointerException if specified {@link Element} is
51       *         <code>null</code>.
52       */
53      public XMLContextQuery(final Element element) {
54          super(element);
55  
56          this.element = element;
57      }
58  
59      /**
60       * Sets the <code>name</code> attribute of specified {@link Element} to name
61       * of specified {@link ContextQuery}, the <code>event</code> attribute to
62       * listened type of specified {@link ContextQuery}, possibly
63       * <code>criteria</code> attribute to criteria of specified
64       * {@link ContextQuery} if there is one. If specified {@link ContextQuery}
65       * does not have any listeners the <code>Listeners</code> child
66       * {@link Element} is removed from specified {@link Element}; otherwise
67       * listeners are marshalled into the <code>Listeners</code> child
68       * {@link Element}.
69       * 
70       * @param query the {@link ContextQuery}.
71       * @throws NullPointerException if specified {@link ContextQuery} is
72       *         <code>null</code>.
73       */
74      @Override
75      public void marshal(final ContextQuery query) {
76          if (query == null)
77              throw new NullPointerException("Query cannot be null.");
78  
79          new Name(this.element).marshal(query);
80          new Event(this.element).marshal(query);
81          new Criteria(this.element).marshal(query);
82  
83          final Document document = this.element.getOwnerDocument();
84  
85          final ContextListener[] listeners = query.getAll();
86          if (listeners.length > 0) {
87              Element listenersElement = null;
88              try {
89                  listenersElement = getChildElement(
90                          Constants.COPAL_NAMESPACE_URI, LISTENERS_ELEMENT_NAME);
91              } catch (final MissingFieldException ex) {
92                  if (LOGGER.isDebugEnabled())
93                      LOGGER.debug(
94                              "Query listeners element is missing! Appending new one.",
95                              ex);
96                  listenersElement = document.createElementNS(
97                          Constants.COPAL_NAMESPACE_URI, Constants.COPAL_PREFIX
98                                  + ":" + LISTENERS_ELEMENT_NAME);
99                  this.element.appendChild(listenersElement);
100             }
101             new XMLContextListeners(listenersElement).marshal(listeners);
102         } else
103             try {
104                 this.element.removeChild(getChildElement(
105                         Constants.COPAL_NAMESPACE_URI, LISTENERS_ELEMENT_NAME));
106             } catch (final MissingFieldException ex) {
107                 if (LOGGER.isDebugEnabled())
108                     LOGGER.debug(
109                             "Query listeners element is missing! Ignoring.", ex);
110             }
111     }
112 }