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.type;
19  
20  import org.w3c.dom.Element;
21  import at.ac.tuwien.infosys.sm4all.copal.api.event.Attribute;
22  import at.ac.tuwien.infosys.sm4all.copal.api.xml.InvalidFieldValueException;
23  import at.ac.tuwien.infosys.sm4all.copal.api.xml.Marshaller;
24  import at.ac.tuwien.infosys.sm4all.copal.api.xml.MissingFieldException;
25  import at.ac.tuwien.infosys.sm4all.copal.api.xml.ParsingException;
26  import at.ac.tuwien.infosys.sm4all.copal.api.xml.Unmarshaller;
27  
28  /**
29   * Command that marshalls and unmarshalls an {@link Attribute} into/from an
30   * {@link Element}.
31   * 
32   * @author sanjin
33   */
34  public class XMLAttribute implements Unmarshaller<Attribute>,
35          Marshaller<Attribute> {
36  
37      private final Element element;
38  
39      /**
40       * Creates instance of {@link Attribute} marshaller and unmarshaller which
41       * uses specified {@link Element} to marshal and/or unmarshal an
42       * {@link Attribute}.
43       * 
44       * @param element the {@link Element} used for marshalling and
45       *        unmarshalling.
46       * @throws NullPointerException if specified {@link Element} is
47       *         <code>null</code>.
48       */
49      public XMLAttribute(final Element element) {
50          super();
51  
52          if (element == null)
53              throw new NullPointerException("XML DOM element cannot be null.");
54  
55          this.element = element;
56      }
57  
58      /**
59       * Returns an {@link Attribute} with its name equal to the <code>name</code>
60       * attribute of specified {@link Element} and value equal to the
61       * <code>value</code> attribute.
62       * 
63       * @return the {@link Attribute}
64       * @throws MissingFieldException if specified {@link Element} does not have
65       *         <code>name</code> or <code>value</code> attribute.
66       * @throws InvalidFieldValueException if either the <code>name</code> or
67       *         <code>value</code> attribute is empty or blank string.
68       */
69      @Override
70      public Attribute unmarshal() throws ParsingException {
71          final String name = new AttributeName(this.element).unmarshal();
72          final String value = new AttributeValue(this.element).unmarshal();
73  
74          return new Attribute(name, value);
75      }
76  
77      /**
78       * Sets the <code>name</code> attribute and <code>value</code> attribute of
79       * specified {@link Element} to name and value of specified
80       * {@link Attribute}.
81       * 
82       * @param attribute the {@link Attribute}.
83       * @throws NullPointerException if specified {@link Attribute} is
84       *         <code>null</code>.
85       */
86      @Override
87      public void marshal(final Attribute attribute) {
88          if (attribute == null)
89              throw new NullPointerException("Attribute cannot be null.");
90  
91          new AttributeName(this.element).marshal(attribute);
92          new AttributeValue(this.element).marshal(attribute);
93      }
94  }