core/com.nokia.carbide.cpp.sdk.core/src/com/nokia/carbide/cpp/internal/api/sdk/SBSv2Utils.java
changeset 0 fb279309251b
child 176 11eeeeeb1733
equal deleted inserted replaced
-1:000000000000 0:fb279309251b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 */
       
    13 package com.nokia.carbide.cpp.internal.api.sdk;
       
    14 
       
    15 import java.io.File;
       
    16 import java.io.FileFilter;
       
    17 import java.util.ArrayList;
       
    18 import java.util.Collections;
       
    19 import java.util.Comparator;
       
    20 import java.util.List;
       
    21 
       
    22 import javax.xml.parsers.DocumentBuilder;
       
    23 import javax.xml.parsers.DocumentBuilderFactory;
       
    24 
       
    25 import org.eclipse.cdt.utils.spawner.EnvironmentReader;
       
    26 import org.eclipse.core.filesystem.URIUtil;
       
    27 import org.eclipse.core.runtime.IPath;
       
    28 import org.eclipse.core.runtime.Path;
       
    29 import org.eclipse.core.runtime.Preferences;
       
    30 import org.osgi.framework.Version;
       
    31 import org.w3c.dom.Element;
       
    32 import org.w3c.dom.Node;
       
    33 import org.w3c.dom.NodeList;
       
    34 import org.xml.sax.InputSource;
       
    35 import org.xml.sax.helpers.DefaultHandler;
       
    36 
       
    37 import com.nokia.carbide.cpp.sdk.core.ISymbianBuildContext;
       
    38 import com.nokia.carbide.cpp.sdk.core.ISymbianSDK;
       
    39 import com.nokia.carbide.cpp.sdk.core.SDKCorePlugin;
       
    40 import com.nokia.cpp.internal.api.utils.core.*;
       
    41 import com.nokia.cpp.internal.api.utils.core.*;
       
    42 
       
    43 /**
       
    44  * Utility class for SBSv2
       
    45  * @since 2.0
       
    46  */
       
    47 public class SBSv2Utils {
       
    48 
       
    49 	private static final String SBSV2_FILTERED_CONFIGS_STORE = "sbsv2FilteredConfigs"; //$NON-NLS-1$
       
    50 	private static final String SBSV2_FILTERED_CONFIGS_DELIMETER = ";"; //$NON-NLS-1$
       
    51 
       
    52 	private static List<String> unfilteredSBSv2ConfigNames;
       
    53 
       
    54 	
       
    55 	/**
       
    56      * Get the path to the SBSv2 bin directory.  This is based on the SBS_HOME environment variable
       
    57      * and may or may not actually exist.
       
    58      * @return absolute path to the bin directory, or null if SBS_HOME is not set
       
    59      */
       
    60     public static IPath getSBSBinDirectory() {
       
    61     	String sbsHome = EnvironmentReader.getEnvVar("SBS_HOME"); //$NON-NLS-1$
       
    62     	if (sbsHome != null) {
       
    63     		return new Path(sbsHome).append("bin"); //$NON-NLS-1$
       
    64     	}
       
    65     	return null;
       
    66     }
       
    67 
       
    68     /**
       
    69      * Get the build configurations supported by SBSv2
       
    70      * @param refreshList whether or not to parse the configuration xml files again
       
    71      * @return list of configuration names, never null
       
    72      */
       
    73     public static List<String> getUnfilteredSBSv2BuildConfigurations(boolean refreshList) {
       
    74     	
       
    75     	if (unfilteredSBSv2ConfigNames == null || refreshList) {
       
    76     		unfilteredSBSv2ConfigNames = new ArrayList<String>();
       
    77     		
       
    78         	// parse the xml files in SBS_HOME/lib/config/ to get SBSv2 configs
       
    79     		try {
       
    80 
       
    81     			IPath configPath = getSBSBinDirectory();
       
    82     			if (configPath != null) {
       
    83     				configPath = configPath.removeLastSegments(1).append("lib/config"); //$NON-NLS-1$
       
    84     				File configDir = configPath.toFile();
       
    85     				if (configDir.exists() && configDir.isDirectory()) {
       
    86     					File[] configFiles = FileUtils.listFilesInTree(configDir, new FileFilter() {
       
    87 
       
    88     						public boolean accept(File arg0) {
       
    89     							if (arg0.isDirectory()) {
       
    90     								return true;
       
    91     							}
       
    92     							return arg0.getName().toLowerCase().endsWith("xml"); //$NON-NLS-1$
       
    93     						}
       
    94     						
       
    95     					}, false);
       
    96     					
       
    97     					for (File file : configFiles) {
       
    98     						getConfigsForFile(file);
       
    99     					}
       
   100     				}
       
   101     			}
       
   102 
       
   103     		} catch (Exception e) {
       
   104         		e.printStackTrace();
       
   105         		Logging.log(SDKCorePlugin.getDefault(), Logging.newStatus(SDKCorePlugin.getDefault(), e));
       
   106     		}
       
   107     	}
       
   108     	
       
   109     	return unfilteredSBSv2ConfigNames;
       
   110 	}
       
   111 
       
   112     /**
       
   113      * Given a list of SDKs, returns the list of the SDK's supported by SBSv2
       
   114      * @param sdks list of SDK's to check
       
   115      * @return list of SBSv2 supported SDK's, may be empty
       
   116      */
       
   117     public static List<ISymbianSDK> getSupportedSDKs(List<ISymbianSDK> sdks) {
       
   118     	List<ISymbianSDK> supportedSDKs = new ArrayList<ISymbianSDK>();
       
   119     	
       
   120     	//TODO need a better way to do this from Symbian.
       
   121     	// For now, just filter out anything older than 9.4
       
   122     	for (ISymbianSDK sdk : sdks) {
       
   123     		Version osVersion = sdk.getOSVersion();
       
   124     		if (osVersion.getMajor() > 8 && osVersion.getMinor() > 3) {
       
   125     			supportedSDKs.add(sdk);
       
   126     		}
       
   127     	}
       
   128     	
       
   129     	return supportedSDKs;
       
   130     }
       
   131     
       
   132 	/**
       
   133 	 * Returns the list of SBSv2 build configuration names that should
       
   134 	 * be filtered out of any UI
       
   135 	 */
       
   136 	public static String[] getSBSv2ConfigurationsToFilter() {
       
   137 		Preferences prefs = SDKCorePlugin.getDefault().getPluginPreferences();
       
   138 		if (prefs != null) {
       
   139 			String configs = prefs.getString(SBSV2_FILTERED_CONFIGS_STORE);
       
   140 			return configs.split(SBSV2_FILTERED_CONFIGS_DELIMETER);
       
   141 		}
       
   142 		return new String[0];
       
   143 	}
       
   144 
       
   145 	/**
       
   146 	 * Set the list of SBSv2 build configurations that should be filtered
       
   147 	 * out of any UI
       
   148 	 * @param configs configs to be filtered
       
   149 	 */
       
   150 	public static void setSBSv2ConfigurationsToFilter(String[] configs) {
       
   151 		Preferences prefs = SDKCorePlugin.getDefault().getPluginPreferences();
       
   152 		if (prefs != null) {
       
   153 			String store = ""; //$NON-NLS-1$
       
   154 			for (String config : configs) {
       
   155 				store = store + SBSV2_FILTERED_CONFIGS_DELIMETER + config;
       
   156 			}
       
   157 			
       
   158 			// remove the leading delimeter
       
   159 			if (store.length() > 0) {
       
   160 				store = store.substring(1);
       
   161 				prefs.setValue(SBSV2_FILTERED_CONFIGS_STORE, store);
       
   162 				SDKCorePlugin.getDefault().savePluginPreferences();
       
   163 
       
   164 			}
       
   165 		}
       
   166 	}
       
   167 
       
   168 	/**
       
   169 	 * Gets the list of SBSv2 build contexts for the given SDK
       
   170 	 * @param sdk the SDK to get the build contexts for
       
   171 	 * @return the list of SBSv2 build contexts.  the list may be empty
       
   172 	 */
       
   173 	public static List<ISymbianBuildContext> getFilteredSBSv2BuildContexts(ISymbianSDK sdk) {
       
   174 		List<ISymbianBuildContext> contexts = new ArrayList<ISymbianBuildContext>();
       
   175 		
       
   176 		for (String name : getUnfilteredSBSv2BuildConfigurations(false)) {
       
   177 
       
   178 			boolean addConfig = true;
       
   179 			for (String filteredConfig : getSBSv2ConfigurationsToFilter()) {
       
   180 				if (filteredConfig.compareTo(name) == 0) {
       
   181 					addConfig = false;
       
   182 					break;
       
   183 				}
       
   184 			}
       
   185 
       
   186 			if (addConfig) {
       
   187 				// only support configs that fall into something we can make a build context
       
   188 				// out of.  They must have a platform and a target.
       
   189 				String targetString = null;
       
   190 		    	if (name.toLowerCase().endsWith("_udeb") || name.toLowerCase().endsWith("_deb")) { //$NON-NLS-1$ //$NON-NLS-2$
       
   191 		    		targetString = ISymbianBuildContext.DEBUG_TARGET;
       
   192 		    	} else if (name.toLowerCase().endsWith("_urel") || name.toLowerCase().endsWith("_rel")) { //$NON-NLS-1$ //$NON-NLS-2$
       
   193 		    		targetString = ISymbianBuildContext.RELEASE_TARGET;
       
   194 		    	}
       
   195 		    	
       
   196 		    	if (targetString != null) {
       
   197 		    		String[] parts = name.split("_"); //$NON-NLS-1$
       
   198 		    		if (parts.length == 2) {
       
   199 		    			SymbianBuildContext context = new SymbianBuildContext(sdk, parts[0].toUpperCase(), targetString);
       
   200 		    			contexts.add(context);
       
   201 		    		}
       
   202 		    	}
       
   203 			}
       
   204 		}
       
   205 		
       
   206 		Collections.sort(contexts, new Comparator<ISymbianBuildContext>() {
       
   207 
       
   208 			public int compare(ISymbianBuildContext o1, ISymbianBuildContext o2) {
       
   209 				String platform1 = o1.getPlatformString();
       
   210 				String platform2 = o2.getPlatformString();
       
   211 				if (platform1.equals(platform2)) {
       
   212 					return o1.getTargetString().compareTo(o2.getTargetString());
       
   213 				} else {
       
   214 					if (platform1.equals(ISymbianBuildContext.EMULATOR_PLATFORM)) {
       
   215 						return -1;
       
   216 					}
       
   217 					else if (platform2.equals(ISymbianBuildContext.EMULATOR_PLATFORM)) {
       
   218 						return 1;
       
   219 					}
       
   220 				}
       
   221 				return 0;
       
   222 			}
       
   223 			
       
   224 		});
       
   225 
       
   226 		return contexts;
       
   227 	}
       
   228 
       
   229 	/**
       
   230 	 * Whether or not to display SBSv2 builder UI
       
   231 	 * @return true if SBSv2 is installed, false otherwise
       
   232 	 */
       
   233 	public static boolean enableSBSv2Support() {
       
   234 		IPath sbsBinPath = getSBSBinDirectory();
       
   235 		if (sbsBinPath != null && sbsBinPath.toFile().exists()) {
       
   236 			return true;
       
   237 		}
       
   238 		return false;
       
   239 	}
       
   240 
       
   241 	private static void getConfigsForFile(File file) {
       
   242     	
       
   243     	try {
       
   244     		Element root = null;
       
   245     		DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
       
   246     		parser.setErrorHandler(new DefaultHandler());
       
   247 
       
   248     		InputSource source = new InputSource(URIUtil.toURI(file.getAbsolutePath()).getPath());
       
   249     		root = parser.parse(source).getDocumentElement();
       
   250     		
       
   251     		NodeList children = root.getChildNodes();
       
   252     		for (int i=0; i< children.getLength(); i++) {
       
   253     			getConfigsForNode(children.item(i));
       
   254     		}
       
   255     		
       
   256     	} catch (Exception e) {
       
   257     		e.printStackTrace();
       
   258     		Logging.log(SDKCorePlugin.getDefault(), Logging.newStatus(SDKCorePlugin.getDefault(), e));
       
   259     	}
       
   260     }
       
   261     
       
   262     private static void getConfigsForNode(Node node) {
       
   263 		if (node.getNodeName().equals("config")) { //$NON-NLS-1$
       
   264 			Node abstractNode = node.getAttributes().getNamedItem("abstract");  //$NON-NLS-1$
       
   265 			if (abstractNode == null || abstractNode.getNodeValue().equals("false")) { //$NON-NLS-1$
       
   266 				Node namedNode = node.getAttributes().getNamedItem("name"); //$NON-NLS-1$
       
   267 				if (namedNode != null) {
       
   268 					// only support configs that fall into something we can make a build context
       
   269 					// out of.  They must have a platform and a target.
       
   270 					String configName = namedNode.getNodeValue();
       
   271 			    	if (configName.toLowerCase().endsWith("_udeb") || configName.toLowerCase().endsWith("_deb") || //$NON-NLS-1$ //$NON-NLS-2$
       
   272 		    			configName.toLowerCase().endsWith("_urel") || configName.toLowerCase().endsWith("_rel")) { //$NON-NLS-1$ //$NON-NLS-2$
       
   273 			    		if (configName.split("_").length == 2) { //$NON-NLS-1$
       
   274 							unfilteredSBSv2ConfigNames.add(configName);
       
   275 			    		}
       
   276 			    	}
       
   277 				}
       
   278 			}
       
   279 
       
   280 			NodeList children = node.getChildNodes();
       
   281 			for (int i=0; i< children.getLength(); i++) {
       
   282 				getConfigsForNode(children.item(i));
       
   283 			}
       
   284 		}
       
   285     }
       
   286 }