Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / things-manager / sdk / java / src / org / iotivity / service / tm / ActionSet.java
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 /**
22  * @file
23  * This file contains class which provides functions to retrieve ActionSet from plain text.
24  */
25 package org.iotivity.service.tm;
26
27 import java.util.StringTokenizer;
28 import java.util.Vector;
29
30 import android.util.Log;
31
32 /**
33  * This class provides functions to retrieve ActionSet from plain text. An
34  * ActionSet is a set of descriptions of actions needed by remote devices as
35  * members of a specific group. To create an ActionSet, one needs to know the
36  * Delimeter serialization.
37  */
38 public class ActionSet extends Time {
39     /**
40      * String Tag for Logging
41      */
42     private static final String LOG_TAG      = "ActionSet";
43     /**
44      * A first segment before the first asterisk(*) in ActionSet string is an
45      * actionset name
46      */
47     public String               actionsetName;
48     /**
49      * Action instance stored in listOfAction vector variable.
50      */
51     public Vector<Action>       listOfAction = new Vector<Action>();
52
53     /**
54      * This function generates an ActionSet String which contains ActionSet
55      * name, delay in seconds, ActionSetType which can be 0(NONE), 1(SCHEDULED)
56      * or 2(RECURSIVE) and a list of actions. (Example: movieTime*10
57      * 1*uri=coap://10.251.44.228:49858/a/light|power=
58      * OFF*uri=coap://10.251.44.228:49858). The first segment before the first
59      * asterisk(*) is an ActionSet name. The second segment goes before the next
60      * asterisk. In the above example, 10 is the second segment which signifies
61      * time delay in seconds followed by a space and ActionSetType which
62      * signifies whether an ActionSet has to be triggered Recursively(2) or
63      * Scheduled manner(1) or immediately(0).
64      * "uri=coap://10.251.44.228:49858/a/light|power=10" is the third segment.
65      * This can be also divided into two sub segments by a vertical bar(|): URI
66      * and a pair of attribute key and value.
67      *
68      * @return String for a specific action.
69      */
70     @Override
71     public String toString() {
72         StringBuilder result = new StringBuilder();
73
74         // Append action name
75         result.append(actionsetName + "*");
76
77         // Append delay and type
78         result.append(getDelay() + " " + getType().ordinal() + "*");
79
80         // Append list of actions
81         for (int i = 0; i < listOfAction.size(); i++) {
82             if (i != 0)
83                 result.append('*');
84             result.append(listOfAction.elementAt(i).toString());
85         }
86
87         return result.toString();
88     }
89
90     /**
91      * This function extracts a list of Actions from plain text and constructs
92      * an ActionSet.
93      *
94      * @param actionsetString
95      *            ActionSet in String format. (Example: movieTime*10
96      *            1*uri=coap://10.251.44.228:49858/a/light|power=
97      *            OFF*uri=coap://10.251.44.228:49858). The first segment before
98      *            the first asterisk(*) is an ActionSet name. The second segment
99      *            goes before the next asterisk. In the above example, 10 is the
100      *            second segment which signifies time delay in seconds followed
101      *            by a space and ActionSetType which signifies whether an
102      *            ActionSet has to be triggered Recursively(2) or Scheduled
103      *            manner(1) or immediately(0).
104      *            "uri=coap://10.251.44.228:49858/a/light|power=10" is the third
105      *            segment. This can be also divided into two sub segments by a
106      *            vertical bar(|): URI and a pair of attribute key and value).
107      *
108      * @return ActionSet which is a set of descriptions of actions needed by
109      *         remote devices as members of a specific group.
110      */
111     public static ActionSet toActionSet(String actionsetString) {
112         if (0 == actionsetString.length()) {
113             return null;
114         }
115
116         ActionSet result = new ActionSet();
117         StringTokenizer tokenizer = new StringTokenizer(actionsetString, "*");
118         boolean actionNameFlag = false;
119         while (tokenizer.hasMoreTokens()) {
120             String segment = tokenizer.nextToken();
121             if (false == actionNameFlag) {
122                 if (true == segment.contains("|")
123                         || true == segment.contains("=")) {
124                     Log.e(LOG_TAG, "Invalid actionset name string!");
125                     return null;
126                 }
127
128                 result.actionsetName = segment;
129                 actionNameFlag = true;
130             } else {
131                 Action action = Action.toAction(segment);
132                 if (null == action) {
133                     Log.e(LOG_TAG, "Failed to convert string to Action class!");
134                     return null;
135                 }
136
137                 result.listOfAction.add(action);
138             }
139         }
140         return result;
141     }
142 }