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;
19  
20  import java.text.MessageFormat;
21  import java.util.HashMap;
22  import java.util.Map;
23  import org.w3c.dom.Document;
24  import org.w3c.dom.Element;
25  import org.w3c.dom.Node;
26  import org.w3c.dom.NodeList;
27  import at.ac.tuwien.infosys.sm4all.copal.api.event.ContextEvent;
28  import at.ac.tuwien.infosys.sm4all.copal.api.util.Attribute;
29  import at.ac.tuwien.infosys.sm4all.copal.api.util.Constants;
30  import at.ac.tuwien.infosys.sm4all.copal.api.util.FailedUnmarshallingException;
31  import at.ac.tuwien.infosys.sm4all.copal.api.util.InvalidFieldValueException;
32  import at.ac.tuwien.infosys.sm4all.copal.api.util.MissingFieldException;
33  
34  /**
35   * The COPAL element for the event's attributes,
36   * 
37   * @author sanjin
38   */
39  public class Attributes extends ConcreteElement<Map<String, String>> {
40  
41      @Override
42      protected Map<String, String> retrieve(final Element element)
43              throws FailedUnmarshallingException {
44          if (element == null)
45              throw new NullPointerException("XML DOM element cannot be null.");
46  
47          final Map<String, String> result = new HashMap<String, String>();
48          final NodeList children = element.getChildNodes();
49  
50          for (int i = 0; i < children.getLength(); i++) {
51              final Node child = children.item(i);
52              if ((child.getNodeType() == Node.ELEMENT_NODE)
53                      && (Constants.COPAL_NAMESPACE.equals(child.getNamespaceURI()))
54                      && ("Attribute".equals(child.getLocalName()))) {
55                  final Element attribute = (Element) child;
56  
57                  if (!attribute.hasAttribute("name"))
58                      throw new MissingFieldException("Name of the attribute");
59                  if (!attribute.hasAttribute("value"))
60                      throw new MissingFieldException("Value of the attribute");
61  
62                  final String name = attribute.getAttribute("name");
63  
64                  if (name.trim().isEmpty())
65                      throw new InvalidFieldValueException(
66                              "Name of the attribute is empty or blank.");
67  
68                  result.put(name, attribute.getAttribute("value"));
69              } else if (child.getNodeType() == Node.TEXT_NODE)
70                  throw new InvalidFieldValueException(MessageFormat.format(
71                          "Unknown attribute ''{0}''.", child.getTextContent()));
72              else if (child.getNodeType() == Node.ELEMENT_NODE)
73                  throw new InvalidFieldValueException(MessageFormat.format(
74                          "Unknown attribute ''{0}''.", child.getLocalName()));
75          }
76  
77          return result;
78      }
79  
80      @Override
81      public void update(final Element element, final ContextEvent event) {
82          if (element == null)
83              throw new NullPointerException("XML DOM element cannot be null.");
84          if (event == null)
85              throw new NullPointerException("Context event cannot be null.");
86  
87          // remove all children
88          while (element.hasChildNodes())
89              element.removeChild(element.getFirstChild());
90  
91          final Document document = element.getOwnerDocument();
92  
93          for (final Attribute attribute : event.getAttributes()) {
94              final Element attributeElement = document.createElementNS(
95                      Constants.COPAL_NAMESPACE, Constants.COPAL_PREFIX
96                              + ":Attribute");
97  
98              attributeElement.setAttribute("name", attribute.getName());
99              attributeElement.setAttribute("value", attribute.getValue());
100 
101             element.appendChild(attributeElement);
102         }
103     }
104 
105     /**
106      * Singleton instance.
107      */
108     public static final Attributes INSTANCE = new Attributes();
109 
110     private Attributes() {
111         super(String[].class);
112     }
113 }