src/share/classes/com/sun/tools/sjavac/CleanProperties.java

changeset 1570
f91144b7da75
parent 1504
22e417cdddee
child 2227
998b10c43157
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/sjavac/CleanProperties.java	Mon Feb 04 18:08:53 2013 -0500
     1.3 @@ -0,0 +1,148 @@
     1.4 +/*
     1.5 + * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.sjavac;
    1.30 +
    1.31 +import java.io.*;
    1.32 +import java.net.URI;
    1.33 +import java.util.ArrayList;
    1.34 +import java.util.Collections;
    1.35 +import java.util.Iterator;
    1.36 +import java.util.List;
    1.37 +import java.util.Set;
    1.38 +import java.util.HashSet;
    1.39 +import java.util.Map;
    1.40 +import java.util.Properties;
    1.41 +
    1.42 +/**
    1.43 + * The clean properties transform should not be necessary.
    1.44 + * Eventually we will cleanup the property file sources in the OpenJDK instead.
    1.45 + *
    1.46 + * <p><b>This is NOT part of any supported API.
    1.47 + * If you write code that depends on this, you do so at your own
    1.48 + * risk.  This code and its internal interfaces are subject to change
    1.49 + * or deletion without notice.</b></p>
    1.50 + */
    1.51 +public class CleanProperties implements Transformer
    1.52 +{
    1.53 +    public void setExtra(String e) {
    1.54 +        // Any extra information is ignored for clean properties.
    1.55 +    }
    1.56 +
    1.57 +    public void setExtra(String[] a) {
    1.58 +        // Any extra information is ignored for clean properties.
    1.59 +    }
    1.60 +
    1.61 +    public boolean transform(Map<String,Set<URI>> pkgSrcs,
    1.62 +                             Set<URI>             visibleSrcs,
    1.63 +                             Map<URI,Set<String>> visibleClasses,
    1.64 +                             Map<String,Set<String>> oldPackageDependencies,
    1.65 +                             URI destRoot,
    1.66 +                             Map<String,Set<URI>>    packageArtifacts,
    1.67 +                             Map<String,Set<String>> packageDependencies,
    1.68 +                             Map<String,String>      packagePublicApis,
    1.69 +                             int debugLevel,
    1.70 +                             boolean incremental,
    1.71 +                             int numCores,
    1.72 +                             PrintStream out,
    1.73 +                             PrintStream err)
    1.74 +    {
    1.75 +        boolean rc = true;
    1.76 +        for (String pkgName : pkgSrcs.keySet()) {
    1.77 +            String pkgNameF = pkgName.replace('.',File.separatorChar);
    1.78 +            for (URI u : pkgSrcs.get(pkgName)) {
    1.79 +                File src = new File(u);
    1.80 +                boolean r = clean(pkgName, pkgNameF, src, new File(destRoot), debugLevel,
    1.81 +                                  packageArtifacts);
    1.82 +                if (r == false) {
    1.83 +                    rc = false;
    1.84 +                }
    1.85 +            }
    1.86 +        }
    1.87 +        return rc;
    1.88 +    }
    1.89 +
    1.90 +    boolean clean(String pkgName, String pkgNameF, File src, File destRoot, int debugLevel,
    1.91 +                  Map<String,Set<URI>> packageArtifacts)
    1.92 +    {
    1.93 +        // Load the properties file.
    1.94 +        Properties p = new Properties();
    1.95 +        try {
    1.96 +            p.load(new FileInputStream(src));
    1.97 +        } catch (IOException e) {
    1.98 +            Log.error("Error reading file "+src.getPath());
    1.99 +            return false;
   1.100 +        }
   1.101 +
   1.102 +        // Sort the properties in increasing key order.
   1.103 +        List<String> sortedKeys = new ArrayList<String>();
   1.104 +        for (Object key : p.keySet()) {
   1.105 +            sortedKeys.add((String)key);
   1.106 +        }
   1.107 +        Collections.sort(sortedKeys);
   1.108 +        Iterator<String> keys = sortedKeys.iterator();
   1.109 +
   1.110 +        // Collect the properties into a string buffer.
   1.111 +        StringBuilder data = new StringBuilder();
   1.112 +        while (keys.hasNext()) {
   1.113 +            String key = keys.next();
   1.114 +            data.append(CompileProperties.escape(key)+":"+CompileProperties.escape((String)p.get(key))+"\n");
   1.115 +        }
   1.116 +
   1.117 +        String destFilename = destRoot.getPath()+File.separator+pkgNameF+File.separator+src.getName();
   1.118 +        File dest = new File(destFilename);
   1.119 +
   1.120 +        // Make sure the dest directories exist.
   1.121 +        if (!dest.getParentFile().isDirectory()) {
   1.122 +            if (!dest.getParentFile().mkdirs()) {
   1.123 +                Log.error("Could not create the directory "+dest.getParentFile().getPath());
   1.124 +                return false;
   1.125 +            }
   1.126 +        }
   1.127 +
   1.128 +        Set<URI> as = packageArtifacts.get(pkgName);
   1.129 +        if (as == null) {
   1.130 +            as = new HashSet<URI>();
   1.131 +            packageArtifacts.put(pkgName, as);
   1.132 +        }
   1.133 +        as.add(dest.toURI());
   1.134 +
   1.135 +        if (dest.exists() && dest.lastModified() > src.lastModified()) {
   1.136 +            // A cleaned property file exists, and its timestamp is newer than the source.
   1.137 +            // Assume that we do not need to clean!
   1.138 +            // Thus we are done.
   1.139 +            return true;
   1.140 +        }
   1.141 +
   1.142 +        Log.info("Cleaning property file "+pkgNameF+File.separator+src.getName());
   1.143 +        try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest)))) {
   1.144 +            writer.write(data.toString());
   1.145 +        } catch ( IOException e ) {
   1.146 +            Log.error("Could not write file "+dest.getPath());
   1.147 +            return false;
   1.148 +        }
   1.149 +        return true;
   1.150 +    }
   1.151 +}

mercurial