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 javax.xml.xpath.XPathConstants;
22  import javax.xml.xpath.XPathExpressionException;
23  import org.w3c.dom.Document;
24  import org.w3c.dom.Element;
25  import org.w3c.dom.Node;
26  import at.ac.tuwien.infosys.sm4all.copal.api.util.Constants;
27  import at.ac.tuwien.infosys.sm4all.copal.api.util.FailedUnmarshallingException;
28  import at.ac.tuwien.infosys.sm4all.copal.api.util.InvalidFieldValueException;
29  import at.ac.tuwien.infosys.sm4all.copal.api.util.MissingFieldException;
30  
31  /**
32   * The virtual COPAL element which returns the name of the current action from
33   * the XML DOM document.
34   * 
35   * @author sanjin
36   */
37  public class CurrentActionName extends VirtualElement<String> {
38  
39      private static final String X_PATH = MessageFormat.format(
40              "//{0}/{1}:CurrentAction/@name",
41              Actions.INSTANCE.getQualifiedName(), Constants.COPAL_PREFIX);
42  
43      @Override
44      public String getXPath() {
45          return X_PATH;
46      }
47  
48      @Override
49      public String retrieve(final Document document) throws FailedUnmarshallingException {
50          if (document == null)
51              throw new NullPointerException("XML DOM document cannot be null.");
52  
53          final String result;
54  
55          try {
56              final Element actions = Actions.INSTANCE.getElement(document);
57              if (actions == null)
58                  throw new MissingFieldException(MessageFormat.format(
59                          "''{0}'' XML DOM element",
60                          Actions.INSTANCE.getQualifiedName()));
61  
62              final Node node = (Node) getExpression().evaluate(actions,
63                      XPathConstants.NODE);
64              if (node != null) {
65                  final String textContent = node.getTextContent().trim();
66                  if (textContent.isEmpty())
67                      throw new InvalidFieldValueException(
68                              "Name of the current action is an empty or blank string.");
69  
70                  result = textContent;
71              } else
72                  result = null;
73          } catch (final XPathExpressionException ex) {
74              throw new FailedUnmarshallingException(
75                      "Could not parse name of the current action.", ex);
76          }
77  
78          return result;
79      }
80  
81      /**
82       * Singleton instance.
83       */
84      public static final CurrentActionName INSTANCE = new CurrentActionName();
85  
86      private CurrentActionName() {
87          super(String.class);
88      }
89  }