1 /*
   2  * Copyright (c) 2011, 2013, 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 abstract class DetailedResourceTraversal implements ResourceTraversal {
  35     private String lastRelativePath;
  36 
  37     private boolean lastIsDirectory;
  38 
  39     public DetailedResourceTraversal() {
  40         lastRelativePath = "";
  41         lastIsDirectory = true;
  42     }
  43 
  44     public final boolean traverse(final PackagerResource rootResource,
  45                                   final File file,
  46                                   final String relativePath) {
  47         final boolean isDirectory = file.isDirectory();
  48         if (!traverseBetween(lastRelativePath, lastIsDirectory,
  49                              relativePath, isDirectory)) {
  50             return false;
  51         }
  52 
  53         lastRelativePath = relativePath;
  54         lastIsDirectory = isDirectory;
  55 
  56         return isDirectory ? true
  57                            : traverseFile(rootResource, file, relativePath);
  58     }
  59 
  60     public final void finish() {
  61         traverseBetween(lastRelativePath, lastIsDirectory, "", true);
  62 
  63         lastRelativePath = "";
  64         lastIsDirectory = true;
  65     }
  66 
  67     protected abstract boolean enterDirectory(String relativePath);
  68 
  69     protected abstract boolean exitDirectory(String relativePath);
  70 
  71     protected abstract boolean traverseFile(PackagerResource rootResource,
  72                                             File file,
  73                                             String relativePath);
  74 
  75     private boolean traverseBetween(final String relPath1,
  76                                     final boolean isDirectory1,
  77                                     final String relPath2,
  78                                     final boolean isDirectory2) {
  79         final int commonPathLength = getCommonPathLength(relPath1, relPath2);
  80 
  81         return traverseDown(relPath1, isDirectory1, commonPathLength)
  82                    && traverseUp(relPath2, isDirectory2, commonPathLength);
  83     }
  84 
  85     private boolean traverseDown(final String relPath,
  86                                  final boolean isDirectory,
  87                                  final int commonPathLength) {
  88         if (relPath.length() == commonPathLength) {
  89             return true;
  90         }
  91 
  92         if (isDirectory && !exitDirectory(relPath)) {
  93             return false;
  94         }
  95 
  96         int prevSeparator = findPrevSeparator(relPath, relPath.length() - 1,
  97                                               commonPathLength);
  98 
  99         while (prevSeparator > commonPathLength) {
 100             if (!exitDirectory(relPath.substring(0, prevSeparator))) {
 101                 return false;
 102             }
 103 
 104             prevSeparator = findPrevSeparator(relPath, prevSeparator - 1,
 105                                               commonPathLength);
 106         }
 107 
 108         return true;
 109     }
 110 
 111     private boolean traverseUp(final String relPath,
 112                                final boolean isDirectory,
 113                                final int commonPathLength) {
 114         if (relPath.length() == commonPathLength) {
 115             return true;
 116         }
 117 
 118         final int pathLength = relPath.length();
 119 
 120         int nextSeparator = findNextSeparator(relPath, commonPathLength + 1,
 121                                               pathLength);
 122 
 123         while (nextSeparator < pathLength) {
 124             if (!enterDirectory(relPath.substring(0, nextSeparator))) {
 125                 return false;
 126             }
 127 
 128             nextSeparator = findNextSeparator(relPath, nextSeparator + 1,
 129                                               pathLength);
 130         }
 131 
 132         if (isDirectory && !enterDirectory(relPath)) {
 133             return false;
 134         }
 135 
 136         return true;
 137     }
 138 
 139     private static int findPrevSeparator(final String relPath,
 140                                          final int fromIndex,
 141                                          final int minIndex) {
 142         final int prevSeparator = relPath.lastIndexOf('/', fromIndex);
 143         return (prevSeparator < minIndex) ? minIndex : prevSeparator;
 144     }
 145 
 146     private static int findNextSeparator(final String relPath,
 147                                          final int fromIndex,
 148                                          final int maxIndex) {
 149         final int nextSeparator = relPath.indexOf('/', fromIndex);
 150         return ((nextSeparator == -1) || (nextSeparator > maxIndex))
 151                        ? maxIndex : nextSeparator;
 152     }
 153 
 154     private static int getCommonPathLength(final String relPath1,
 155                                            final String relPath2) {
 156         final char[] path1Chars = relPath1.toCharArray();
 157         final char[] path2Chars = relPath2.toCharArray();
 158 
 159         int lastMatchIndex = 0;
 160         int i;
 161         for (i = 0; (i < path1Chars.length)
 162                         && (i < path2Chars.length)
 163                         && (path1Chars[i] == path2Chars[i]); ++i) {
 164             if (path1Chars[i] == '/') {
 165                 lastMatchIndex = i;
 166             }
 167         }
 168 
 169         if (i == path1Chars.length) {
 170             if ((i == path2Chars.length) || (path2Chars[i] == '/')) {
 171                 lastMatchIndex = i;
 172             }
 173         } else if ((i == path2Chars.length) && (path1Chars[i] == '/')) {
 174             lastMatchIndex = i;
 175         }
 176 
 177         return lastMatchIndex;
 178     }
 179 }