CleanProperties.java revision 3265:b7583d50f67d
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        boolean rc = true;
80        for (String pkgName : pkgSrcs.keySet()) {
81            String pkgNameF = pkgName.replace('.',File.separatorChar);
82            for (URI u : pkgSrcs.get(pkgName)) {
83                File src = new File(u);
84                boolean r = clean(pkgName, pkgNameF, src, new File(destRoot), debugLevel,
85                                  packageArtifacts);
86                if (r == false) {
87                    rc = false;
88                }
89            }
90        }
91        return rc;
92    }
93
94    boolean clean(String pkgName,
95                  String pkgNameF,
96                  File src,
97                  File destRoot,
98                  int debugLevel,
99                  Map<String,Set<URI>> packageArtifacts) {
100        // Load the properties file.
101        Properties p = new Properties();
102        try {
103            p.load(new FileInputStream(src));
104        } catch (IOException e) {
105            Log.error("Error reading file "+src.getPath());
106            return false;
107        }
108
109        // Sort the properties in increasing key order.
110        List<String> sortedKeys = new ArrayList<>();
111        for (Object key : p.keySet()) {
112            sortedKeys.add((String)key);
113        }
114        Collections.sort(sortedKeys);
115
116        // Collect the properties into a string buffer.
117        StringBuilder data = new StringBuilder();
118        for (String key : sortedKeys) {
119            data.append(CompileProperties.escape(key))
120                .append(":")
121                .append(CompileProperties.escape((String) p.get(key)))
122                .append("\n");
123        }
124
125        String destFilename = destRoot.getPath()+File.separator+pkgNameF+File.separator+src.getName();
126        File dest = new File(destFilename);
127
128        // Make sure the dest directories exist.
129        if (!dest.getParentFile().isDirectory()) {
130            if (!dest.getParentFile().mkdirs()) {
131                Log.error("Could not create the directory "+dest.getParentFile().getPath());
132                return false;
133            }
134        }
135
136        Set<URI> as = packageArtifacts.get(pkgName);
137        if (as == null) {
138            as = new HashSet<>();
139            packageArtifacts.put(pkgName, as);
140        }
141        as.add(dest.toURI());
142
143        if (dest.exists() && dest.lastModified() > src.lastModified()) {
144            // A cleaned property file exists, and its timestamp is newer than the source.
145            // Assume that we do not need to clean!
146            // Thus we are done.
147            return true;
148        }
149
150        Log.info("Cleaning property file "+pkgNameF+File.separator+src.getName());
151        try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest)))) {
152            writer.write(data.toString());
153        } catch ( IOException e ) {
154            Log.error("Could not write file "+dest.getPath());
155            return false;
156        }
157        return true;
158    }
159}
160