Path: blob/master/sourcetools/com.ibm.jpp.preprocessor/com/ibm/jpp/om/JxeRulesExtension.java
6004 views
/*******************************************************************************1* Copyright (c) 1999, 2017 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package com.ibm.jpp.om;2223import java.io.File;24import java.io.FileOutputStream;25import java.io.IOException;26import java.io.PrintWriter;27import java.util.Properties;28import java.util.Set;29import java.util.TreeSet;3031/**32* JXE Rules Extension33*/34public class JxeRulesExtension extends BuilderExtension {3536/**37* The JXE rules found during the build.38*/39private final Set<String> jxeRules = new TreeSet<>();4041/**42* Determines whether API classes should be preserved with jxe rules.43*/44private boolean jxePreserveApi = false;4546/**47* The output path.48*/49private String jxeRulesPath = null;5051/**52* The output filename.53*/54private static final String jxeRulesFilename = "jxeLink.rules";5556/**57* Constructor for JxeRuleExtension.58*/59public JxeRulesExtension() {60super("jxerules");61}6263/**64* @see com.ibm.jpp.om.BuilderExtension#notifyBuildEnd()65*/66@Override67public void notifyBuildEnd() {68if (jxeRules.size() > 0) {69if (this.jxeRulesPath != null) {70// add the generated jxerules to the file71File outputFile = getOutputRulesFile();72File parentDir = outputFile.getParentFile();7374parentDir.mkdirs();7576if (parentDir.exists()) {77try (FileOutputStream fout = new FileOutputStream(outputFile, true);78PrintWriter writer = new PrintWriter(fout)) {79for (String jxeRule : jxeRules) {80writer.println(jxeRule);81}82writer.flush();83builder.getLogger().log("Jxelink rules written to \"" + outputFile.getAbsolutePath() + "\"", Logger.SEVERITY_INFO);84} catch (IOException e) {85builder.getLogger().log("An exception occured while writing the jxelink rules", Logger.SEVERITY_ERROR, e);86}87}88} else {89builder.getLogger().log("No output path specified for jxe rules", Logger.SEVERITY_ERROR);90}91}92}9394/**95* @see com.ibm.jpp.om.BuilderExtension#notifyConfigurePreprocessor(JavaPreprocessor)96*/97@Override98public void notifyConfigurePreprocessor(JavaPreprocessor preprocessor) {99preprocessor.setJxeRules(this.jxeRules);100preprocessor.setJxePreserveApi(this.jxePreserveApi);101}102103/**104* Validates the JXE rule options105*106* @param options jxerules options107*108* @throws BuilderConfigurationException if the options are invalid109*110* @see com.ibm.jpp.om.BuilderExtension#validateOptions(Properties)111*/112@Override113public void validateOptions(Properties options) throws BuilderConfigurationException {114if (options.containsKey("jxerules:preserveapi")) {115this.jxePreserveApi = options.getProperty("jxerules:preserveapi").equalsIgnoreCase("true");116}117if (options.containsKey("jxerules:outputdir")) {118this.jxeRulesPath = options.getProperty("jxerules:outputdir");119}120}121122private File getOutputRulesFile() {123return new File(new File(builder.getOutputDir(), jxeRulesPath), jxeRulesFilename);124}125126}127128129