160da63a831b726581449d6d3b9c6a72be90f972
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / utils / Utility.java
1 package oic.simulator.serviceprovider.utils;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Set;
7
8 public class Utility {
9
10     public static String uriToDisplayName(String uri) {
11         String result = null;
12         if (null != uri) {
13             String tokens[] = uri.split(Constants.FORWARD_SLASH);
14             if (Constants.PROPER_RESOURCE_URI_TOKEN_COUNT == tokens.length) {
15                 // Proper URI
16                 result = tokens[2] + Constants.UNDERSCORE + tokens[4];
17             }
18         }
19         return result;
20     }
21
22     public static String displayNameToUri(String displayName) {
23         String result = null;
24         if (null != displayName) {
25             String tokens[] = displayName.split(Constants.UNDERSCORE);
26             if (Constants.DISPLAY_RESOURCE_URI_TOKEN_COUNT == tokens.length) {
27                 // Proper Display Name
28                 result = Constants.FORWARD_SLASH + Constants.OIC
29                         + Constants.FORWARD_SLASH + tokens[0]
30                         + Constants.FORWARD_SLASH + Constants.SIMULATOR
31                         + Constants.FORWARD_SLASH + tokens[1];
32             }
33         }
34         return result;
35     }
36
37     public static String fileNameToDisplay(String fileName) {
38         if (null == fileName || fileName.length() < 1) {
39             return null;
40         }
41         // Remove the RAML file standard prefix
42         int len = Constants.RAML_FILE_PREFIX.length();
43         if (len > 0) {
44             if (fileName.startsWith(Constants.RAML_FILE_PREFIX)) {
45                 fileName = fileName.substring(len);
46             }
47         }
48
49         // Removing the file extension
50         String[] token = fileName.split(Constants.SPLIT_BY_DOT_PATTERN);
51         int tokLen = token.length;
52         String result = null;
53         if (tokLen - 2 >= 0) {
54             result = token[tokLen - 2];
55         }
56         return result;
57     }
58
59     public static String displayToFileName(String displayName) {
60         if (null == displayName || displayName.length() < 1) {
61             return null;
62         }
63         String fileName;
64         // Adding the prefix
65         fileName = Constants.RAML_FILE_PREFIX + displayName;
66
67         // Adding the file extension
68         fileName = fileName + Constants.RAML_FILE_EXTENSION;
69
70         return fileName;
71     }
72
73     public static boolean isUriComplete(String uri) {
74         boolean uriComplete = false;
75         if (null != uri) {
76             String tokens[] = uri.split(Constants.FORWARD_SLASH);
77             if (Constants.PROPER_RESOURCE_URI_TOKEN_COUNT == tokens.length) {
78                 uriComplete = true;
79             }
80         }
81         return uriComplete;
82     }
83
84     public static String getAutomationStatus(boolean status) {
85         if (status) {
86             return Constants.ENABLED;
87         } else {
88             return Constants.DISABLED;
89         }
90     }
91
92     public static String getAutomationString(boolean status) {
93         if (status) {
94             return Constants.ENABLE;
95         } else {
96             return Constants.DISABLE;
97         }
98     }
99
100     public static boolean getAutomationBoolean(String status) {
101         if (null != status) {
102             if (status.equals(Constants.ENABLE)) {
103                 return true;
104             }
105         }
106         return false;
107     }
108
109     public static int getUpdateIntervalFromString(String value) {
110         int result = Constants.DEFAULT_AUTOMATION_INTERVAL;
111         if (null != value) {
112             try {
113                 result = Integer.parseInt(value);
114             } catch (NumberFormatException nfe) {
115                 // Do nothing
116             }
117         }
118         return result;
119     }
120
121     public static List<String> convertSetToList(Set<String> typeSet) {
122         if (null == typeSet) {
123             return null;
124         }
125         List<String> list = new ArrayList<String>();
126         Iterator<String> typeItr = typeSet.iterator();
127         while (typeItr.hasNext()) {
128             list.add(typeItr.next());
129         }
130         return list;
131     }
132 }