|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Feedzai |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.feedzai.openml.python.modules; |
| 18 | + |
| 19 | +import com.google.common.collect.ImmutableSet; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | +import org.w3c.dom.Document; |
| 23 | +import org.w3c.dom.NodeList; |
| 24 | + |
| 25 | +import javax.xml.parsers.DocumentBuilder; |
| 26 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 27 | +import java.io.InputStream; |
| 28 | +import java.util.Set; |
| 29 | + |
| 30 | +/** |
| 31 | + * Class responsible for parsing a XML file in order to retrieve the name of the Python modules to be shared across |
| 32 | + * sub-interpreters. |
| 33 | + * |
| 34 | + * @author Paulo Pereira (paulo.pereira@feedzai.com) |
| 35 | + * @since 0.1.5 |
| 36 | + */ |
| 37 | +public class SharedModulesParser { |
| 38 | + |
| 39 | + /** |
| 40 | + * Logger. |
| 41 | + */ |
| 42 | + private static final Logger logger = LoggerFactory.getLogger(SharedModulesParser.class); |
| 43 | + |
| 44 | + /** |
| 45 | + * Default value for {@link #xmlFile}. |
| 46 | + */ |
| 47 | + private static final String DEFAULT_XML_FILE = "python-packages.xml"; |
| 48 | + |
| 49 | + /** |
| 50 | + * The filename of a XML file with the name of the Python modules to be shared across sub-interpreters. |
| 51 | + */ |
| 52 | + private final String xmlFile; |
| 53 | + |
| 54 | + /** |
| 55 | + * Constructor. |
| 56 | + * |
| 57 | + * @param xmlFileName The name of a XML file. |
| 58 | + */ |
| 59 | + public SharedModulesParser(final String xmlFileName) { |
| 60 | + this.xmlFile = xmlFileName; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Constructor. |
| 65 | + */ |
| 66 | + public SharedModulesParser() { |
| 67 | + this(DEFAULT_XML_FILE); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Gets the {@link InputStream} of {@link #xmlFile} that exists in the current classpath. |
| 72 | + * |
| 73 | + * @return The {@link InputStream} of {@link #xmlFile}. |
| 74 | + */ |
| 75 | + private InputStream getXMLInputStream() { |
| 76 | + final ClassLoader classLoader = getClass().getClassLoader(); |
| 77 | + return classLoader.getResourceAsStream(this.xmlFile); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Parses the {@link #xmlFile} to retrieve the name of the Python modules to be shared across sub-interpreters. |
| 82 | + * |
| 83 | + * @return A {@link Set} with the name of the Python modules to be shared across sub-interpreters. |
| 84 | + * @throws Exception If there is an error while parsing {@link #xmlFile}. |
| 85 | + */ |
| 86 | + private Set<String> parseXMLFile() throws Exception { |
| 87 | + final ImmutableSet.Builder<String> sharedModulesBuilder = ImmutableSet.builder(); |
| 88 | + final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
| 89 | + final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
| 90 | + |
| 91 | + try (final InputStream xmlFile = getXMLInputStream()) { |
| 92 | + final Document doc = dBuilder.parse(xmlFile); |
| 93 | + doc.getDocumentElement().normalize(); |
| 94 | + |
| 95 | + final NodeList nList = doc.getElementsByTagName("package"); |
| 96 | + for (int i = 0; i < nList.getLength(); i++) { |
| 97 | + sharedModulesBuilder.add(nList.item(i).getFirstChild().getNodeValue()); |
| 98 | + } |
| 99 | + } |
| 100 | + return sharedModulesBuilder.build(); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Retrieves a {@link Set} with the name of the Python modules to be shared across sub-interpreters. |
| 105 | + * |
| 106 | + * @return The name of the Python modules to be shared across sub-interpreters. |
| 107 | + */ |
| 108 | + public Set<String> getSharedModules() { |
| 109 | + Set<String> sharedModules = ImmutableSet.of(); |
| 110 | + try { |
| 111 | + sharedModules = parseXMLFile(); |
| 112 | + } catch (final Exception e) { |
| 113 | + logger.warn("Problem while getting the XML file with the Python modules to be shared.", e); |
| 114 | + } |
| 115 | + return sharedModules; |
| 116 | + } |
| 117 | +} |
0 commit comments