/** * Clone a configuration and put it on the tmp list if it is not already a saved configuration and * not already on the tmp list. * * @param p project * @param oldId the id of the old configuration to clone * @param cfgd the configuration descriptor for the clone * @return true if the configuration is already saved, false otherwise */ public synchronized boolean cloneCfg(IProject p, String oldId, ICConfigurationDescription cfgd) { if (isConfigurationAlreadySaved(p, cfgd)) return true; Map<String, IAConfiguration> tmpList = getTmpConfigs(p); String newId = cfgd.getId(); // Don't bother if the new configuration is already on the tmp list IAConfiguration cfg = tmpList.get(newId); if (cfg != null) return false; // Otherwise, try and find the old id to copy the configuration from // or punt if not found IAConfiguration oldCfg = null; Map<String, IAConfiguration> savedList = getSavedConfigs(p); if (savedList != null) oldCfg = savedList.get(oldId); if (oldCfg != null) { IAConfiguration newCfg = oldCfg.copy(cfgd.getId()); tmpList.put(cfgd.getId(), newCfg); // Check to see if the new configuration is already stored as part of the project description. // If yes, it should already be saved. This can occur if the configuration was added as part // of // another CDT Property page and the Autotools Property page was never opened. if (CoreModel.getDefault().getProjectDescription(p).getConfigurationById(newId) != null) { addConfiguration(p, newCfg); return true; } } return false; }
public synchronized IAConfiguration getConfiguration(IProject p, String cfgId, boolean persist) { IAConfiguration cfg = findCfg(p, cfgId); if (cfg == null) { cfg = createDefaultConfiguration(cfgId); if (persist) { addConfiguration(p, cfg); } } else { if (!persist) { cfg = cfg.copy(); } } return cfg; }
public synchronized void addConfiguration(IProject project, IAConfiguration cfg) { String projectName = project.getName(); Map<String, IAConfiguration> cfgs = getSavedConfigs(project); if (cfgs == null) { cfgs = new HashMap<>(); configs.put(projectName, cfgs); } cfgs.put(cfg.getId(), cfg); saveConfigs(project); }
/** @since 1.2 */ public synchronized Map<String, IAutotoolsOption> getAutotoolsCfgOptions( IProject project, String cfgId) throws CoreException { // Verify project is valid Autotools project if (project == null || !project.hasNature(AutotoolsNewProjectNature.AUTOTOOLS_NATURE_ID)) { throw new CoreException( new Status( IStatus.ERROR, AutotoolsPlugin.PLUGIN_ID, ConfigureMessages.getString(INVALID_AUTOTOOLS_PROJECT))); } // Verify configuration id is valid ICConfigurationDescription cfgd = CoreModel.getDefault().getProjectDescription(project).getConfigurationById(cfgId); IConfiguration icfg = ManagedBuildManager.getConfigurationForDescription(cfgd); if (icfg == null) { throw new CoreException( new Status( IStatus.ERROR, AutotoolsPlugin.PLUGIN_ID, ConfigureMessages.getString(INVALID_AUTOTOOLS_CONFIG_ID))); } IAConfiguration cfg = getConfiguration(project, cfgId); HashMap<String, IAutotoolsOption> options = new HashMap<>(); // Get set of configuration options and convert to set of IAutotoolOptions Map<String, IConfigureOption> cfgOptions = cfg.getOptions(); IAConfiguration dummyCfg = createDefaultConfiguration(createDummyId()); for (Iterator<Entry<String, IConfigureOption>> i = cfgOptions.entrySet().iterator(); i.hasNext(); ) { Map.Entry<String, IConfigureOption> entry = i.next(); String name = entry.getKey(); IAutotoolsOption configOption = new AutotoolsOption(entry.getValue().copy((AutotoolsConfiguration) dummyCfg)); options.put(name, configOption); } return options; }
/** @since 1.2 */ public synchronized void updateAutotoolCfgOptions( IProject project, String cfgId, Map<String, IAutotoolsOption> options) throws CoreException { // Verify project is valid Autotools project if (project == null || !project.hasNature(AutotoolsNewProjectNature.AUTOTOOLS_NATURE_ID)) { throw new CoreException( new Status( IStatus.ERROR, AutotoolsPlugin.PLUGIN_ID, ConfigureMessages.getString(INVALID_AUTOTOOLS_PROJECT))); } // Verify configuration id is valid IAConfiguration cfg = findCfg(project, cfgId); if (cfg == null) { throw new CoreException( new Status( IStatus.ERROR, AutotoolsPlugin.PLUGIN_ID, ConfigureMessages.getString(INVALID_AUTOTOOLS_CONFIG_ID))); } // Get set of configuration options and convert to set of IAutotoolOptions for (Iterator<Entry<String, IAutotoolsOption>> i = options.entrySet().iterator(); i.hasNext(); ) { Map.Entry<String, IAutotoolsOption> entry = i.next(); String name = entry.getKey(); IAutotoolsOption option = entry.getValue(); IConfigureOption cfgOption = cfg.getOption(name); if (cfgOption != null) { cfgOption.setValue(option.getValue()); } } // Save changes saveConfigs(project); }
// Perform apply of configuration changes. This rewrites out the current known list of // configurations // with any changes currently that have been made to them. If a configuration has been renamed, // but this // has not yet been confirmed by the end-user, then only the changes to the configuration are // made. The // name currently remains the same in the output file. public synchronized void applyConfigs(String projectName, ICConfigurationDescription[] cfgds) { try { IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); IResource res = root.findMember(projectName, false); if (res == null || res.getType() != IResource.PROJECT) { AutotoolsPlugin.logErrorMessage( ConfigureMessages.getFormattedString(CFG_CANT_SAVE, new String[] {projectName})); return; } IProject project = (IProject) res; IPath output = project.getLocation().append(CFG_FILE_NAME); File f = output.toFile(); if (!f.exists()) f.createNewFile(); if (f.exists()) { try (PrintWriter p = new PrintWriter(new BufferedWriter(new FileWriter(f)))) { Map<String, IAConfiguration> cfgs = getSavedConfigs(project); if (cfgs == null) return; p.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); // $NON-NLS-1$ p.println("<configurations>"); // $NON-NLS-1$ Option[] optionList = AutotoolsConfiguration.getOptionList(); HashSet<String> savedIds = new HashSet<>(); setSyncing(true); for (int x = 0; x < cfgds.length; ++x) { ICConfigurationDescription cfgd = cfgds[x]; @SuppressWarnings("unused") CConfigurationData data = cfgd.getConfigurationData(); String id = cfgd.getId(); savedIds.add(id); IAConfiguration cfg = getTmpConfiguration(project, cfgd); cfgs.put( id, cfg); // add to list in case we have a new configuration not yet added to Project // Description p.println("<configuration id=\"" + id + "\">"); // $NON-NLS-1$ //$NON-NLS-2$ for (int j = 0; j < optionList.length; ++j) { Option option = optionList[j]; IConfigureOption opt = cfg.getOption(option.getName()); if (!opt.isCategory()) p.println( "<option id=\"" + option.getName() + "\" value=\"" + opt.getValue() + "\"/>"); //$NON-NLS-1$ //$NON-NLS-2$ // $NON-NLS-3$ } p.println("</configuration>"); // $NON-NLS-1$ syncNameField(cfgd); } setSyncing(false); // Put all the remaining configurations already saved back into the file. // These represent deleted configurations, but confirmation has not occurred. for (Entry<String, IAConfiguration> i : cfgs.entrySet()) { String id = i.getKey(); // A remaining id won't appear in our savedIds list. if (!savedIds.contains(id)) { IAConfiguration cfg = i.getValue(); p.println("<configuration id=\"" + id + "\">"); // $NON-NLS-1$ //$NON-NLS-2$ for (int j = 0; j < optionList.length; ++j) { Option option = optionList[j]; IConfigureOption opt = cfg.getOption(option.getName()); if (!opt.isCategory()) p.println( "<option id=\"" + option.getName() + "\" value=\"" + opt.getValue() + "\"/>"); //$NON-NLS-1$ //$NON-NLS-2$ // $NON-NLS-3$ } p.println("</configuration>"); // $NON-NLS-1$ } } p.println("</configurations>"); } } } catch (IOException e) { AutotoolsPlugin.log(e); } }
private void saveConfigs(IProject project, ICConfigurationDescription[] cfgds) { try { String projectName = project.getName(); IPath output = project.getLocation().append(CFG_FILE_NAME); File f = output.toFile(); if (!f.exists()) f.createNewFile(); if (f.exists()) { try (PrintWriter p = new PrintWriter(new BufferedWriter(new FileWriter(f)))) { Map<String, IAConfiguration> cfgs = configs.get(projectName); p.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); // $NON-NLS-1$ p.println("<configurations>"); // $NON-NLS-1$ Option[] optionList = AutotoolsConfiguration.getOptionList(); // Before saving, force any cloning to occur via the option // value handler. setSyncing(true); for (int i = 0; i < cfgds.length; ++i) { @SuppressWarnings("unused") CConfigurationData data = cfgds[i].getConfigurationData(); } setSyncing(false); for (int i = 0; i < cfgds.length; ++i) { ICConfigurationDescription cfgd = cfgds[i]; String id = cfgd.getId(); IAConfiguration cfg = cfgs.get(id); if (cfg == null) { cfg = createDefaultConfiguration(id); } p.println("<configuration id=\"" + cfg.getId() + "\">"); // $NON-NLS-1$ //$NON-NLS-2$ for (int j = 0; j < optionList.length; ++j) { Option option = optionList[j]; IConfigureOption opt = cfg.getOption(option.getName()); if (opt.isFlag()) { p.println( "<flag id=\"" + option.getName() + "\" value=\"" //$NON-NLS-1$ //$NON-NLS-2$ + xmlEscape(option.getDefaultValue()) + "\">"); //$NON-NLS-1$ FlagConfigureOption fco = (FlagConfigureOption) opt; List<String> children = fco.getChildren(); for (int k = 0; k < children.size(); ++k) { String childName = children.get(k); IConfigureOption childopt = cfg.getOption(childName); p.println( "<flagvalue id=\"" + childopt.getName() + "\" value=\"" //$NON-NLS-1$ //$NON-NLS-2$ + xmlEscape(childopt.getValue()) + "\"/>"); // $NON-NLS-3$ } p.println("</flag>"); // $NON-NLS-1$ } else if (!opt.isCategory() && !opt.isFlagValue()) p.println( "<option id=\"" + option.getName() + "\" value=\"" + xmlEscape(opt.getValue()) // $NON-NLS-1$ //$NON-NLS-2$ + "\"/>"); // $NON-NLS-3$ } p.println("</configuration>"); // $NON-NLS-1$ // Sync name field as this configuration is now // officially saved syncNameField(cfgd); } p.println("</configurations>"); } } } catch (IOException e) { AutotoolsPlugin.log(e); } }
private Map<String, IAConfiguration> getSavedConfigs(IProject project) { String projectName = project.getName(); Map<String, IAConfiguration> list = configs.get(projectName); if (list == null) { try { IPath fileLocation = project.getLocation().append(CFG_FILE_NAME); File dirFile = fileLocation.toFile(); Map<String, IAConfiguration> cfgList = new HashMap<>(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); if (dirFile.exists()) { Document d = db.parse(dirFile); Element e = d.getDocumentElement(); // Get the stored configuration data NodeList cfgs = e.getElementsByTagName("configuration"); // $NON-NLS-1$ for (int x = 0; x < cfgs.getLength(); ++x) { Node n = cfgs.item(x); NamedNodeMap attrs = n.getAttributes(); // Originally we used the configuration name, but now we use // the ConfigurationDescription id which is unique. Check for // id first, but fall back to name for older .autotools files. Node nameNode = attrs.getNamedItem("name"); // $NON-NLS-1$ Node cfgIdNode = attrs.getNamedItem("id"); // $NON-NLS-1$ String cfgId = null; if (cfgIdNode != null) cfgId = cfgIdNode.getNodeValue(); else if (nameNode != null) { String cfgName = nameNode.getNodeValue(); ICConfigurationDescription cfgd = CoreModel.getDefault() .getProjectDescription(project) .getConfigurationByName(cfgName); if (cfgd != null) cfgId = cfgd.getId(); else continue; // have to punt, this doesn't map to real cfg } IAConfiguration cfg = new AutotoolsConfiguration(cfgId); NodeList l = n.getChildNodes(); for (int y = 0; y < l.getLength(); ++y) { Node child = l.item(y); if (child.getNodeName().equals("option")) { // $NON-NLS-1$ NamedNodeMap optionAttrs = child.getAttributes(); Node id = optionAttrs.getNamedItem("id"); // $NON-NLS-1$ Node value = optionAttrs.getNamedItem("value"); // $NON-NLS-1$ if (id != null && value != null) cfg.setOption(id.getNodeValue(), value.getNodeValue()); } else if (child.getNodeName().equals("flag")) { // $NON-NLS-1$ // read in flag values NamedNodeMap optionAttrs = child.getAttributes(); Node id = optionAttrs.getNamedItem("id"); // $NON-NLS-1$ String idValue = id.getNodeValue(); IConfigureOption opt = cfg.getOption(idValue); if (opt instanceof FlagConfigureOption) { NodeList l2 = child.getChildNodes(); for (int z = 0; z < l2.getLength(); ++z) { Node flagChild = l2.item(z); if (flagChild.getNodeName().equals("flagvalue")) { // $NON-NLS-1$ NamedNodeMap optionAttrs2 = flagChild.getAttributes(); Node id2 = optionAttrs2.getNamedItem("id"); // $NON-NLS-1$ Node value = optionAttrs2.getNamedItem("value"); // $NON-NLS-1$ cfg.setOption(id2.getNodeValue(), value.getNodeValue()); } } } } } cfg.setDirty(false); cfgList.put(cfg.getId(), cfg); } if (cfgList.size() > 0) { configs.put(projectName, cfgList); list = cfgList; } } } catch (ParserConfigurationException | SAXException | IOException e) { e.printStackTrace(); } } return list; }