1 /*
   2  * Copyright (c) 2011, 2015, 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 
  26 package com.sun.javafx.tools.resource;
  27 
  28 import java.io.File;
  29 
  30 /**
  31  * @deprecated use {@link ToolProvider} to locate the {@code "javapackager"} tool instead.
  32  */
  33 @Deprecated(since="10", forRemoval=true)
  34 public class PackagerResource {
  35     private static final ResourceFilter ACCEPT_ALL_FILTER =
  36             new ResourceFilter() {
  37                 @Override
  38                 public boolean descent(final File file,
  39                                        final String relativePath) {
  40                     return true;
  41                 }
  42 
  43                 @Override
  44                 public boolean accept(final File file,
  45                                       final String relativePath) {
  46                     return true;
  47                 }
  48             };
  49 
  50     private final File baseDir;
  51     private final File file;
  52     private final String relativePath;
  53 
  54     public PackagerResource(final File baseDir, final String path) {
  55         this(baseDir, createFile(baseDir, path));
  56     }
  57 
  58     public PackagerResource(final File baseDir, final File file) {
  59         final File nrmFile = normalizeFile(file);
  60         if (nrmFile == null) {
  61             throw new IllegalArgumentException("Invalid file specified");
  62         }
  63 
  64         if (baseDir != null) {
  65             final File nrmBaseDir = normalizeFile(baseDir);
  66             if (nrmBaseDir == null) {
  67                 throw new IllegalArgumentException("Invalid basedir specified");
  68             }
  69 
  70             if (nrmFile.equals(nrmBaseDir)) {
  71                 this.file = nrmFile;
  72                 this.baseDir = nrmFile;
  73                 this.relativePath = "";
  74                 return;
  75             }
  76 
  77             final StringBuilder relativePathBuilder =
  78                     new StringBuilder(nrmFile.getName());
  79 
  80             File tempFile = nrmFile.getParentFile();
  81             while (tempFile != null) {
  82                 if (tempFile.equals(nrmBaseDir)) {
  83                     this.file = nrmFile;
  84                     this.baseDir = nrmBaseDir;
  85                     this.relativePath = relativePathBuilder.toString();
  86                     return;
  87                 }
  88 
  89                 relativePathBuilder.insert(0, '/');
  90                 relativePathBuilder.insert(0, tempFile.getName());
  91                 tempFile = tempFile.getParentFile();
  92             }
  93         }
  94 
  95         final File nrmParentFile = nrmFile.getParentFile();
  96 
  97         this.file = nrmFile;
  98         this.baseDir = (nrmParentFile != null) ? nrmParentFile : nrmFile;
  99         this.relativePath = nrmFile.getName();
 100     }
 101 
 102     public final File getBaseDir() {
 103         return baseDir;
 104     }
 105 
 106     public final File getFile() {
 107         return file;
 108     }
 109 
 110     public final String getRelativePath() {
 111         return relativePath;
 112     }
 113 
 114     public final boolean traverse(final ResourceTraversal resourceTraversal) {
 115         return traverse(resourceTraversal, null);
 116     }
 117 
 118     public final boolean traverse(final ResourceTraversal resourceTraversal,
 119                                   final ResourceFilter resourceFilter) {
 120         return new TraversalOperation((resourceFilter != null)
 121                                               ? resourceFilter
 122                                               : ACCEPT_ALL_FILTER,
 123                                       resourceTraversal, this).execute();
 124     }
 125 
 126     private static File normalizeFile(final File inputFile) {
 127         return normalizeFileImpl(inputFile.getAbsoluteFile());
 128     }
 129 
 130     private static File normalizeFileImpl(final File inputFile) {
 131         if (inputFile.getParentFile() == null) {
 132             return inputFile;
 133         }
 134 
 135         final File partiallyNormalizedFile =
 136                 normalizeFileImpl(inputFile.getParentFile());
 137 
 138         if (partiallyNormalizedFile == null) {
 139             // error
 140             return null;
 141         }
 142 
 143         final String fileName = inputFile.getName();
 144 
 145         if (fileName.equals(".")) {
 146             // ignore this path element
 147             return partiallyNormalizedFile;
 148         }
 149 
 150         if (fileName.equals("..")) {
 151             // remove the last path element
 152             return partiallyNormalizedFile.getParentFile();
 153         }
 154 
 155         return new File(partiallyNormalizedFile, fileName);
 156     }
 157 
 158     private static File createFile(final File baseDir, final String path) {
 159         final File testFile = new File(path);
 160         return testFile.isAbsolute()
 161                    ? testFile
 162                    : new File(baseDir == null
 163                                   ? null
 164                                   : baseDir.getAbsolutePath(),
 165                               path);
 166     }
 167 
 168     private static final class TraversalOperation {
 169         private final ResourceFilter resourceFilter;
 170         private final ResourceTraversal resourceTraversal;
 171         private final PackagerResource rootResource;
 172         private final StringBuilder relativePathBuilder;
 173 
 174         public TraversalOperation(final ResourceFilter resourceFilter,
 175                                   final ResourceTraversal resourceTraversal,
 176                                   final PackagerResource rootResource) {
 177             this.resourceFilter = resourceFilter;
 178             this.resourceTraversal = resourceTraversal;
 179             this.rootResource = rootResource;
 180             this.relativePathBuilder =
 181                     new StringBuilder(rootResource.relativePath);
 182         }
 183 
 184         public boolean execute() {
 185             return traverse(rootResource.file);
 186         }
 187 
 188         private boolean traverse(final File file) {
 189             final String relativePath = relativePathBuilder.toString();
 190             if (resourceFilter.accept(file, relativePath)
 191                     && !resourceTraversal.traverse(rootResource, file,
 192                                                    relativePath)) {
 193                 return false;
 194             }
 195 
 196             if (!file.isDirectory()
 197                     || !resourceFilter.descent(file, relativePath)) {
 198                 return true;
 199             }
 200 
 201             final int resetLength = relativePathBuilder.length();
 202             File[] children = file.listFiles();
 203             if (children != null) {
 204                 for (final File nextFile : children) {
 205                     if (resetLength > 0) {
 206                         relativePathBuilder.append('/');
 207                     }
 208                     relativePathBuilder.append(nextFile.getName());
 209                     if (!traverse(nextFile)) {
 210                         return false;
 211                     }
 212                     relativePathBuilder.setLength(resetLength);
 213                 }
 214             }
 215 
 216             return true;
 217         }
 218     }
 219 }
--- EOF ---