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.Element;
21  import at.ac.tuwien.infosys.sm4all.copal.api.ContextQuery;
22  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.Marshaller;
23  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.OptionalAttributeUnmarshaller;
24  
25  /**
26   * Command that marshalls and unmarshalls criteria of a {@link ContextQuery}
27   * into/from an {@link Element}.
28   * 
29   * @author sanjin
30   */
31  public class Criteria extends OptionalAttributeUnmarshaller<String> implements
32          Marshaller<ContextQuery> {
33  
34      private static final String CRITERIA_ATTRIBUTE = "criteria";
35  
36      private final Element element;
37  
38      /**
39       * Creates instance of {@link ContextQuery} criteria marshaller and
40       * unmarshaller which uses specified {@link Element} to marshal and/or
41       * unmarshal the criteria.
42       * 
43       * @param element the {@link Element} used for marshalling and
44       *        unmarshalling.
45       * @throws NullPointerException if specified {@link Element} is
46       *         <code>null</code>.
47       */
48      public Criteria(final Element element) {
49          super(element, CRITERIA_ATTRIBUTE);
50  
51          this.element = element;
52      }
53  
54      @Override
55      protected String valueOf(final String value) {
56          return value;
57      }
58  
59      /**
60       * Sets the <code>criteria</code> attribute of specified {@link Element} to
61       * criteria of specified {@link ContextQuery} or removes the
62       * <code>criteria</code> attribute if specified {@link ContextQuery} does
63       * not have a criteria.
64       * 
65       * @param query the {@link ContextQuery}.
66       * @throws NullPointerException if specified {@link ContextQuery} is
67       *         <code>null</code>.
68       */
69      @Override
70      public void marshal(final ContextQuery query) {
71          if (query == null)
72              throw new NullPointerException("Query cannot be null.");
73  
74          if (query.hasCriteria())
75              this.element.setAttribute(CRITERIA_ATTRIBUTE, query.getCriteria());
76          else
77              this.element.removeAttribute(CRITERIA_ATTRIBUTE);
78      }
79  }