| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * 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: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| */ |
| package org.eclipse.osbp.preferences.ui.utils; |
| |
| import java.io.File; |
| import java.io.IOException; |
| import java.util.Map; |
| |
| import javax.xml.parsers.DocumentBuilder; |
| import javax.xml.parsers.DocumentBuilderFactory; |
| import javax.xml.parsers.ParserConfigurationException; |
| |
| import org.eclipse.core.resources.IProject; |
| import org.eclipse.core.runtime.IPath; |
| import org.eclipse.osbp.preferences.ProductFileBase; |
| import org.eclipse.osbp.preferences.ui.component.APreferencePage; |
| import org.w3c.dom.Document; |
| import org.xml.sax.SAXException; |
| |
| /** |
| * Abstract class for each block of information inside a <code>*.product</code> configuration file. |
| * <br> |
| * The file will be loaded via <code>javax.xml.parser</code>. |
| */ |
| abstract public class ProductFileVerifier extends ConfigurationFileVerifier { |
| |
| protected String productFileName; |
| protected ProductFileBase base; |
| protected String nodeName; |
| protected Document document; |
| |
| public ProductFileVerifier(IProject project, String productFileName, ProductFileBase base, String nodeName) { |
| super(project); |
| this.productFileName = productFileName; |
| this.base = base; |
| this.nodeName = nodeName; |
| iconName = APreferencePage.STATUS_OK; |
| Document document = null; |
| try { |
| IPath relativeFilePath = project.getFile(productFileName).getProjectRelativePath(); |
| IPath locationFilePath = project.getFile(productFileName).getLocation(); |
| if (project.exists(relativeFilePath)) { |
| DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| DocumentBuilder builder = factory.newDocumentBuilder(); |
| document = builder.parse(new File(locationFilePath.toOSString())); |
| } |
| } |
| catch (IOException | ParserConfigurationException | SAXException e) { |
| e.printStackTrace(); |
| } |
| this.document = document; |
| Map<String, Map<String, String>> actual = getPluginBundles(); |
| verifyNecessary("required", actual, base.getRequiredAsMap()); |
| verifyNecessary("recommended", actual, base.getRecommendedAsMap()); |
| verifyDeprecated("deprecated", actual, base.getDeprecatedAsMap()); |
| } |
| |
| abstract public Map<String,Map<String,String>> getPluginBundles(); |
| |
| /** |
| * For every violation found a subitem will be generated. |
| * @param type type of necessity as string |
| * @param actual map with information about all actual configured bundles |
| * @param target map with information about all necessary bundles to be configured |
| */ |
| protected void verifyNecessary(String type, Map<String, Map<String, String>> actual, Map<String, Map<String, String>> target) { |
| for (String id : target.keySet()) { |
| if (!actual.containsKey(id)) { |
| iconName = APreferencePage.STATUS_ERROR; |
| subItems.add(new SubItem("add missing "+type, base.serialize(id, target.get(id)), APreferencePage.ITEM_TO_ADD)); |
| } |
| else if (!actual.get(id).equals(target.get(id))) { |
| iconName = APreferencePage.STATUS_ERROR; |
| subItems.add(new SubItem("replace existing "+type, base.serialize(id, target.get(id)), APreferencePage.ITEM_TO_UPDATE)); |
| } |
| } |
| } |
| |
| /** |
| * For every violation found a subitem will be generated. |
| * @param type type of unnecessity as string |
| * @param actual map with information about all actual configured bundles |
| * @param deprecated map with information about all deprecated bundles to be de-configured |
| */ |
| protected void verifyDeprecated(String type, Map<String, Map<String, String>> actual, Map<String, Map<String, String>> deprecated) { |
| for (String id : actual.keySet()) { |
| if (deprecated.containsKey(id)) { |
| iconName = APreferencePage.STATUS_ERROR; |
| subItems.add(new SubItem("remove existing "+type, base.serialize(id, actual.get(id)), APreferencePage.ITEM_TO_REMOVE)); |
| } |
| } |
| } |
| |
| @Override |
| public String getName() { |
| return nodeName; |
| } |
| |
| @Override |
| public SubItem[] getSubItems() { |
| return subItems.toArray(new SubItem[0]); |
| } |
| } |