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