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 java.text.MessageFormat;
21  import java.util.LinkedList;
22  import java.util.List;
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.ContextEventType;
28  import at.ac.tuwien.infosys.sm4all.copal.api.event.DefaultAction;
29  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.Constants;
30  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.InvalidFieldValueException;
31  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.Marshaller;
32  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.ParsingException;
33  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.Unmarshaller;
34  
35  /**
36   * Command that marshalls and unmarshalls {@link DefaultAction}s of a
37   * {@link ContextEventType} into/from an {@link Element}.
38   * 
39   * @author sanjin
40   */
41  public class DefaultActions implements Unmarshaller<DefaultAction[]>,
42          Marshaller<ContextEventType> {
43  
44      private final Element element;
45  
46      /**
47       * Constructs instance of {@link ContextEventType} {@link DefaultAction}s
48       * marshaller and unmarshaller which uses specified {@link Element} to
49       * marshal and/or unmarshal the {@link DefaultAction}s.
50       * 
51       * @param element the {@link Element} used for marshalling and
52       *        unmarshalling.
53       * @throws NullPointerException if specified {@link Element} is
54       *         <code>null</code>.
55       */
56      public DefaultActions(final Element element) {
57          super();
58  
59          if (element == null)
60              throw new NullPointerException("XML DOM element cannot be null.");
61  
62          this.element = element;
63      }
64  
65      /**
66       * Returns the unmarshalled <code>DefaultAtion</code> child {@link Element}s
67       * of specified {@link Element} using the
68       * {@link XMLDefaultAction#unmarshal()} method.
69       * 
70       * @return the unmarshalled <code>DefaultAtion</code> child {@link Element}
71       *         s.
72       * @throws InvalidFieldValueException if specified {@link Element} has a
73       *         non-<code>DefaultAtion</code> child {@link Element}.
74       */
75      @Override
76      public DefaultAction[] unmarshal() throws ParsingException {
77          final NodeList children = this.element.getChildNodes();
78          final List<DefaultAction> result = new LinkedList<DefaultAction>();
79  
80          for (int i = 0; i < children.getLength(); i++) {
81              final Node child = children.item(i);
82              if ((child.getNodeType() == Node.ELEMENT_NODE)
83                      && Constants.COPAL_NAMESPACE_URI.equals(child.getNamespaceURI())
84                      && "DefaultAction".equals(child.getLocalName()))
85                  result.add(new XMLDefaultAction((Element) child).unmarshal());
86              else if ((child.getNodeType() == Node.TEXT_NODE)
87                      && (!child.getTextContent().trim().isEmpty()))
88                  throw new InvalidFieldValueException(MessageFormat.format(
89                          "Unknown action ''{0}''.", child.getTextContent()));
90              else if (child.getNodeType() == Node.ELEMENT_NODE)
91                  throw new InvalidFieldValueException(MessageFormat.format(
92                          "Unknown action ''{0}''.", child.getLocalName()));
93          }
94  
95          return result.toArray(new DefaultAction[result.size()]);
96      }
97  
98      /**
99       * Removes all child {@link Element}s of specified {@link Element} and
100      * appends marshalled {@link DefaultAction}s from specified
101      * {@link ContextEventType}.
102      * 
103      * @param eventType the {@link ContextEventType}.
104      * @throws NullPointerException if specified {@link ContextEventType} is
105      *         <code>null</code>.
106      */
107     @Override
108     public void marshal(final ContextEventType eventType) {
109         if (eventType == null)
110             throw new NullPointerException("Event type cannot be null.");
111 
112         // remove all children
113         while (this.element.hasChildNodes())
114             this.element.removeChild(this.element.getFirstChild());
115 
116         final Document document = this.element.getOwnerDocument();
117 
118         for (final DefaultAction action : eventType.getActions()) {
119             final Element actionElement = document.createElementNS(
120                     Constants.COPAL_NAMESPACE_URI, Constants.COPAL_PREFIX
121                             + ":DefaultAction");
122             new XMLDefaultAction(actionElement).marshal(action);
123             this.element.appendChild(actionElement);
124         }
125     }
126 }