| /** |
| * |
| * 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 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| */ |
| package org.eclipse.osbp.preferences; |
| |
| import java.io.IOException; |
| import java.io.StringReader; |
| import java.util.ArrayList; |
| import java.util.Map; |
| import java.util.Set; |
| import java.util.TreeMap; |
| |
| import javax.xml.parsers.DocumentBuilder; |
| import javax.xml.parsers.DocumentBuilderFactory; |
| import javax.xml.parsers.ParserConfigurationException; |
| |
| import org.w3c.dom.Attr; |
| import org.w3c.dom.Document; |
| import org.w3c.dom.Element; |
| import org.w3c.dom.NamedNodeMap; |
| import org.w3c.dom.NodeList; |
| import org.xml.sax.InputSource; |
| import org.xml.sax.SAXException; |
| |
| /** |
| * Dependencies for the OSBP product bundle <code>*.product</code> file. |
| */ |
| public abstract class ProductFileBase extends AbstractConfigurationFileBase { |
| |
| /** |
| * @param document document containing the attributes to be extracted |
| * @param path root xml path to be extracted |
| * @param nodes nodes name inside the path to be extracted |
| * @param idkey name of the node attribute, which should be used as id |
| * @return map of values with the id as map key and attributes map as map value |
| */ |
| public static Map<String,Map<String,String>> getValues(Document document, String path, String nodes, String idkey) { |
| Map<String,Map<String,String>> results = new TreeMap<String,Map<String,String>>(); |
| if (document != null) { |
| String[] paths = path.split("/"); |
| Element parent = (Element)document.getElementsByTagName(paths[0]).item(0); |
| for (int i=1; i<paths.length && (parent != null); i++) { |
| parent = (Element)parent.getElementsByTagName(paths[i]).item(0); |
| } |
| if (parent != null) { |
| NodeList children = parent.getElementsByTagName(nodes); |
| for (int i=0; i<children.getLength(); i++) { |
| Element child = (Element)children.item(i); |
| String id = child.getAttribute(idkey); |
| if ((id != null) && !id.isEmpty()) { |
| NamedNodeMap childAttributes = child.getAttributes(); |
| Map<String,String> attributes = new TreeMap<String,String>(); |
| for (int a=0; a<childAttributes.getLength(); a++) { |
| try { |
| Attr childAttribute = (Attr)childAttributes.item(a); |
| String key = childAttribute.getName(); |
| String value = childAttribute.getValue(); |
| attributes.put(key, value); |
| } |
| catch (Exception e) { |
| } |
| } |
| results.put(id, attributes); |
| } |
| } |
| } |
| } |
| return results; |
| } |
| |
| /** set of necessary data base vendors defined by the product preferences */ |
| protected final Set<EnumDatabaseVendor> dataBaseVendors; |
| |
| public ProductFileBase(ProductConfigurationPrefs prefs) { |
| dataBaseVendors = prefs.getDataBaseVendors(); |
| } |
| |
| public ProductFileBase(ProductConfigurationPrefs prefs, String... jndiNames) { |
| dataBaseVendors = prefs.getDataBaseVendors(jndiNames); |
| } |
| |
| @Override |
| public final Map<String, Map<String, String>> getRequiredAsMap() { |
| return deserialize(getRequiredAsString()); |
| } |
| |
| @Override |
| public final Map<String, Map<String, String>> getRecommendedAsMap() { |
| return deserialize(getRecommendedAsString()); |
| } |
| |
| @Override |
| public final Map<String, Map<String, String>> getDeprecatedAsMap() { |
| Map<String, Map<String, String>> deprecated = deserialize(getDeprecatedAsString()); |
| for (String id : getRequiredAsMap().keySet()) { |
| if (deprecated.containsKey(id)) { |
| deprecated.remove(id); |
| } |
| } |
| for (String id : getRecommendedAsMap().keySet()) { |
| if (deprecated.containsKey(id)) { |
| deprecated.remove(id); |
| } |
| } |
| return deprecated; |
| } |
| |
| @Override |
| public final String serialize(String id, Map<String,String> attributes) { |
| ArrayList<String> result = new ArrayList<String>(); |
| for (String key : attributes.keySet()) { |
| if (!"id".equals(key)) { |
| result.add(" "+key+"=\""+attributes.get(key)+"\""); |
| } |
| } |
| return "<plugin id=\""+id+"\""+String.join("", result)+"/>"; |
| } |
| |
| @Override |
| public Map<String, Map<String, String>> deserialize(String asString) { |
| DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| try { |
| DocumentBuilder builder = factory.newDocumentBuilder(); |
| InputSource source = new InputSource(new StringReader("<root>"+asString+"</root>")); |
| Document document = builder.parse(source); |
| return getValues(document, "root", "plugin", "id"); |
| } |
| catch (ParserConfigurationException | SAXException | IOException e) { |
| e.printStackTrace(); |
| } |
| return null; |
| } |
| } |