1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
23 * This file contains class which provides functions to retrieve ActionSet from plain text.
25 package org.iotivity.service.tm;
27 import java.util.StringTokenizer;
28 import java.util.Vector;
30 import android.util.Log;
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.
38 public class ActionSet extends Time {
40 * String Tag for Logging
42 private static final String LOG_TAG = "ActionSet";
44 * A first segment before the first asterisk(*) in ActionSet string is an
47 public String actionsetName;
49 * Action instance stored in listOfAction vector variable.
51 public Vector<Action> listOfAction = new Vector<Action>();
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.
68 * @return String for a specific action.
71 public String toString() {
72 StringBuilder result = new StringBuilder();
75 result.append(actionsetName + "*");
77 // Append delay and type
78 result.append(getDelay() + " " + getType().ordinal() + "*");
80 // Append list of actions
81 for (int i = 0; i < listOfAction.size(); i++) {
84 result.append(listOfAction.elementAt(i).toString());
87 return result.toString();
91 * This function extracts a list of Actions from plain text and constructs
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).
108 * @return ActionSet which is a set of descriptions of actions needed by
109 * remote devices as members of a specific group.
111 public static ActionSet toActionSet(String actionsetString) {
112 if (0 == actionsetString.length()) {
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!");
128 result.actionsetName = segment;
129 actionNameFlag = true;
131 Action action = Action.toAction(segment);
132 if (null == action) {
133 Log.e(LOG_TAG, "Failed to convert string to Action class!");
137 result.listOfAction.add(action);