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 at.ac.tuwien.infosys.sm4all.copal.api.util.Constants;
26  import at.ac.tuwien.infosys.sm4all.copal.api.util.FailedUnmarshallingException;
27  import at.ac.tuwien.infosys.sm4all.copal.api.util.MissingFieldException;
28  
29  /**
30   * The virtual COPAL element which checks if all actions in the XML DOM document
31   * are processed.
32   * 
33   * @author sanjin
34   */
35  public class ActionsFinished extends VirtualElement<Boolean> {
36  
37      private static final String X_PATH = MessageFormat.format(
38              "count(//{0}/{1}:ProcessedAction) = count(//{0}/*)",
39              Actions.INSTANCE.getQualifiedName(), Constants.COPAL_PREFIX);
40  
41      @Override
42      public String getXPath() {
43          return X_PATH;
44      }
45  
46      @Override
47      public Boolean retrieve(final Document document) throws FailedUnmarshallingException {
48          if (document == null)
49              throw new NullPointerException("XML DOM document cannot be null.");
50  
51          final boolean result;
52  
53          try {
54              final Element actions = Actions.INSTANCE.getElement(document);
55              if (actions == null)
56                  throw new MissingFieldException(MessageFormat.format(
57                          "''{0}'' XML DOM element",
58                          Actions.INSTANCE.getQualifiedName()));
59  
60              result = (Boolean) getExpression().evaluate(actions,
61                      XPathConstants.BOOLEAN);
62          } catch (final XPathExpressionException ex) {
63              throw new FailedUnmarshallingException(
64                      "Could not parse if actions are finished.", ex);
65          }
66  
67          return result;
68      }
69  
70      /**
71       * Singleton instance.
72       */
73      public static final ActionsFinished INSTANCE = new ActionsFinished();
74  
75      private ActionsFinished() {
76          super(boolean.class);
77      }
78  }