Path: blob/master/src/jdk.compiler/share/classes/com/sun/tools/sjavac/CleanProperties.java
41175 views
/*1* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.sjavac;2627import java.io.BufferedWriter;28import java.io.File;29import java.io.FileInputStream;30import java.io.FileOutputStream;31import java.io.IOException;32import java.io.OutputStreamWriter;33import java.io.Writer;34import java.net.URI;35import java.util.ArrayList;36import java.util.Collections;37import java.util.HashSet;38import java.util.List;39import java.util.Map;40import java.util.Properties;41import java.util.Set;4243import com.sun.tools.sjavac.comp.CompilationService;44import com.sun.tools.sjavac.options.Options;45import com.sun.tools.sjavac.pubapi.PubApi;4647/**48* The clean properties transform should not be necessary.49* Eventually we will cleanup the property file sources in the OpenJDK instead.50*51* <p><b>This is NOT part of any supported API.52* If you write code that depends on this, you do so at your own risk.53* This code and its internal interfaces are subject to change or54* deletion without notice.</b>55*/56public class CleanProperties implements Transformer {57public void setExtra(String e) {58// Any extra information is ignored for clean properties.59}6061public void setExtra(Options a) {62// Any extra information is ignored for clean properties.63}6465public boolean transform(CompilationService sjavac,66Map<String,Set<URI>> pkgSrcs,67Set<URI> visibleSrcs,68Map<String,Set<String>> oldPackageDependencies,69URI destRoot,70Map<String,Set<URI>> packageArtifacts,71Map<String, Map<String, Set<String>>> packageDependencies,72Map<String, Map<String, Set<String>>> packageCpDependencies,73Map<String, PubApi> packagePublicApis,74Map<String, PubApi> dependencyPublicApis,75int debugLevel,76boolean incremental,77int numCores) {78boolean rc = true;79for (String pkgName : pkgSrcs.keySet()) {80String pkgNameF = pkgName.replace('.',File.separatorChar);81for (URI u : pkgSrcs.get(pkgName)) {82File src = new File(u);83boolean r = clean(pkgName, pkgNameF, src, new File(destRoot), debugLevel,84packageArtifacts);85if (r == false) {86rc = false;87}88}89}90return rc;91}9293boolean clean(String pkgName,94String pkgNameF,95File src,96File destRoot,97int debugLevel,98Map<String,Set<URI>> packageArtifacts) {99// Load the properties file.100Properties p = new Properties();101try {102p.load(new FileInputStream(src));103} catch (IOException e) {104Log.error("Error reading file "+src.getPath());105return false;106}107108// Sort the properties in increasing key order.109List<String> sortedKeys = new ArrayList<>();110for (Object key : p.keySet()) {111sortedKeys.add((String)key);112}113Collections.sort(sortedKeys);114115// Collect the properties into a string buffer.116StringBuilder data = new StringBuilder();117for (String key : sortedKeys) {118data.append(CompileProperties.escape(key))119.append(":")120.append(CompileProperties.escape((String) p.get(key)))121.append("\n");122}123124String destFilename = destRoot.getPath()+File.separator+pkgNameF+File.separator+src.getName();125File dest = new File(destFilename);126127// Make sure the dest directories exist.128if (!dest.getParentFile().isDirectory()) {129if (!dest.getParentFile().mkdirs()) {130Log.error("Could not create the directory "+dest.getParentFile().getPath());131return false;132}133}134135Set<URI> as = packageArtifacts.get(pkgName);136if (as == null) {137as = new HashSet<>();138packageArtifacts.put(pkgName, as);139}140as.add(dest.toURI());141142if (dest.exists() && dest.lastModified() > src.lastModified()) {143// A cleaned property file exists, and its timestamp is newer than the source.144// Assume that we do not need to clean!145// Thus we are done.146return true;147}148149Log.info("Cleaning property file "+pkgNameF+File.separator+src.getName());150try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest)))) {151writer.write(data.toString());152} catch ( IOException e ) {153Log.error("Could not write file "+dest.getPath());154return false;155}156return true;157}158}159160161