Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / things-manager / sdk / java / src / org / iotivity / service / tm / Action.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 the Action
24  * details from a Segment.
25  */
26
27 package org.iotivity.service.tm;
28
29 import java.util.StringTokenizer;
30 import java.util.Vector;
31
32 import android.util.Log;
33
34 /**
35  * This class provides functions to retrieve the Action details from a Segment.
36  */
37 public class Action {
38     private static final String LOG_TAG          = "Action";
39     /**
40      * Sub Segment Value
41      */
42     public String               target;
43
44     /**
45      * Capability instance stored in listOfCapability vector variable.
46      */
47     public Vector<Capability>   listOfCapability = new Vector<Capability>();
48
49     /**
50      * This function generates an Action String (Example:
51      * uri=coap://10.251.44.228:49858/a/light|power=10)
52      *
53      * @return String for a specific action.
54      */
55     public String toString() {
56         StringBuilder result = new StringBuilder();
57
58         result.append("uri=" + target + "|");
59         for (int i = 0; i < listOfCapability.size(); i++) {
60             if (i != 0)
61                 result.append('|');
62             result.append(listOfCapability.elementAt(i).toString());
63         }
64
65         return result.toString();
66     }
67
68     /**
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.
71      *
72      * @param actionString
73      *            Segment String
74      *
75      * @return Action needed by remote devices as members of a specific group
76      */
77     public static Action toAction(String actionString) {
78         Action result = new Action();
79         StringTokenizer tokenizer = new StringTokenizer(actionString, "|");
80
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,
87                         "=");
88                 if (2 != targetTokenizer.countTokens()
89                         || false == targetTokenizer.nextToken()
90                                 .equalsIgnoreCase("uri")) {
91                     Log.e(LOG_TAG, "Invalid action segment = " + segment);
92                     return null;
93                 }
94
95                 result.target = targetTokenizer.nextToken();
96                 actionFlag = true;
97             } else {
98                 // Parse the capability string
99                 Capability capability = Capability.toCapability(segment);
100                 if (null == capability) {
101                     Log.e(LOG_TAG,
102                             "Failed to convert string to Capability class!");
103                     return null;
104                 }
105
106                 // Add the parsed capability to list
107                 result.listOfCapability.add(capability);
108             }
109         }
110         return result;
111     }
112 }