summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/YoctoJSONHelper.java
blob: 37ce54d96b9c9c7ab11aac9642d5bb137ff1aab8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*******************************************************************************
 * Copyright (c) 2010 Intel Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Intel - initial API and implementation
 *******************************************************************************/
package org.yocto.sdk.remotetools;

import java.io.IOException;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.Set;
import java.util.HashSet;
import java.io.FileReader;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class YoctoJSONHelper {
	private static final String PROPERTIES_FILE = "/tmp/properties.json";
	private static final String PROPERTY_VALUE_FILE = "/tmp/propertyvalues.json";

	private static HashSet<YoctoBspPropertyElement> properties;

	public static HashSet<YoctoBspPropertyElement> getProperties() throws Exception {
		
		properties = new HashSet<YoctoBspPropertyElement>(); 
		try {
			
			JSONObject obj = (JSONObject)JSONValue.parse(new FileReader(PROPERTIES_FILE));
			Set<String> keys = obj.keySet();
			if (!keys.isEmpty()) {
				Iterator<String> iter = keys.iterator();
			    while (iter.hasNext()) {
			      String key = (String)iter.next();
			      if (validKey(key)) {
			    	  JSONObject value_obj = (JSONObject)obj.get(key);
			    	  YoctoBspPropertyElement elem = new YoctoBspPropertyElement();
			    	  elem.setName(key);
			    	  String type = (String)value_obj.get("type");
			    	  elem.setType(type);
			    	  if (type.contentEquals("boolean")) {
			    		  elem.setDefaultValue((String)value_obj.get("default"));
			    	  }
			    	  properties.add(elem);
			      }
			    }
			}
			
		} catch (Exception e) {
			throw e;
		} 
		return properties;
	}

	public static void createBspJSONFile(HashSet<YoctoBspPropertyElement> properties) {
		try {
			JSONObject obj = new JSONObject();
			if (!properties.isEmpty()) {
				Iterator<YoctoBspPropertyElement> it = properties.iterator();
				while (it.hasNext()) {
					// Get property
					YoctoBspPropertyElement propElem = (YoctoBspPropertyElement)it.next();
					obj.put(propElem.getName(), propElem.getValue());
				}
			}
		 
			FileWriter file = new FileWriter(PROPERTY_VALUE_FILE);
			file.write(obj.toJSONString());
			file.flush();
			file.close();
		 
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static boolean validKey(String key) {
		if (key.contains("kernel"))
			return false;
		if (key.contentEquals("qemuarch"))
			return false;
		return true;
	}
}