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 the Action
24 * details from a Segment.
27 package org.iotivity.service.tm;
29 import java.util.StringTokenizer;
30 import java.util.Vector;
32 import android.util.Log;
35 * This class provides functions to retrieve the Action details from a Segment.
38 private static final String LOG_TAG = "Action";
45 * Capability instance stored in listOfCapability vector variable.
47 public Vector<Capability> listOfCapability = new Vector<Capability>();
50 * This function generates an Action String (Example:
51 * uri=coap://10.251.44.228:49858/a/light|power=10)
53 * @return String for a specific action.
55 public String toString() {
56 StringBuilder result = new StringBuilder();
58 result.append("uri=" + target + "|");
59 for (int i = 0; i < listOfCapability.size(); i++) {
62 result.append(listOfCapability.elementAt(i).toString());
65 return result.toString();
69 * This function parses the Segment value to retrieve sub segments separated
70 * by a vertical bar(|): URI and a pair of attribute key and value.
75 * @return Action needed by remote devices as members of a specific group
77 public static Action toAction(String actionString) {
78 Action result = new Action();
79 StringTokenizer tokenizer = new StringTokenizer(actionString, "|");
81 boolean actionFlag = false;
82 while (tokenizer.hasMoreTokens()) {
83 String segment = tokenizer.nextToken();
84 if (false == actionFlag) {
85 // Parse the action string
86 StringTokenizer targetTokenizer = new StringTokenizer(segment,
88 if (2 != targetTokenizer.countTokens()
89 || false == targetTokenizer.nextToken()
90 .equalsIgnoreCase("uri")) {
91 Log.e(LOG_TAG, "Invalid action segment = " + segment);
95 result.target = targetTokenizer.nextToken();
98 // Parse the capability string
99 Capability capability = Capability.toCapability(segment);
100 if (null == capability) {
102 "Failed to convert string to Capability class!");
106 // Add the parsed capability to list
107 result.listOfCapability.add(capability);