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 import java.util.Map;
  30 import java.util.SortedMap;
  31 import java.util.TreeMap;
  32 
  33 /**
  34  * @deprecated use {@link ToolProvider} to locate the {@code "javapackager"} tool instead.
  35  */
  36 @Deprecated(since="10", forRemoval=true)
  37 public final class ConsolidatedResources {
  38     private final SortedMap<ResourceKey, ResourceRecord> resourceMap;
  39 
  40     public ConsolidatedResources() {
  41         resourceMap = new TreeMap<ResourceKey, ResourceRecord>();
  42     }
  43 
  44     public void addResource(final PackagerResource resource) {
  45         addResource(resource, null);
  46     }
  47 
  48     public void addResource(final PackagerResource resource,
  49                             final ResourceFilter resourceFilter) {
  50         resource.traverse(new ResourceRegistration(resourceMap),
  51                           resourceFilter);
  52     }
  53 
  54     public boolean traverse(final ResourceTraversal resourceTraversal) {
  55         for (final Map.Entry<ResourceKey, ResourceRecord> mapEntry:
  56                 resourceMap.entrySet()) {
  57             final ResourceRecord resourceRecord = mapEntry.getValue();
  58             if (!resourceTraversal.traverse(resourceRecord.getRootResource(),
  59                                             resourceRecord.getFile(),
  60                                             resourceRecord.getRelativePath())) {
  61                 return false;
  62             }
  63         }
  64 
  65         return true;
  66     }
  67 
  68     private static final class ResourceRegistration
  69             implements ResourceTraversal {
  70         private final Map<ResourceKey, ResourceRecord> resourceMap;
  71 
  72         public ResourceRegistration(
  73                 final Map<ResourceKey, ResourceRecord> resourceMap) {
  74             this.resourceMap = resourceMap;
  75         }
  76 
  77         public boolean traverse(final PackagerResource rootResource,
  78                                 final File file,
  79                                 final String relativePath) {
  80             resourceMap.put(file.isDirectory()
  81                                     ? ResourceKey.forDirectory(relativePath)
  82                                     : ResourceKey.forFile(relativePath),
  83                             new ResourceRecord(rootResource,
  84                                                file,
  85                                                relativePath));
  86 
  87             return true;
  88         }
  89     }
  90 
  91     private static final class ResourceRecord {
  92         private final PackagerResource rootResource;
  93         private final File file;
  94         private final String relativePath;
  95 
  96         public ResourceRecord(final PackagerResource rootResource,
  97                               final File file,
  98                               final String relativePath) {
  99             this.rootResource = rootResource;
 100             this.file = file;
 101             this.relativePath = relativePath;
 102         }
 103 
 104         public PackagerResource getRootResource() {
 105             return rootResource;
 106         }
 107 
 108         public File getFile() {
 109             return file;
 110         }
 111 
 112         public String getRelativePath() {
 113             return relativePath;
 114         }
 115     }
 116 
 117     private static final class ResourceKey implements Comparable<ResourceKey> {
 118         private final String directory;
 119         private final String fileName;
 120 
 121         private ResourceKey(final String directory,
 122                             final String fileName) {
 123             this.directory = directory;
 124             this.fileName = fileName;
 125         }
 126 
 127         public static ResourceKey forFile(final String relPath) {
 128             final int lastSlashIndex = relPath.lastIndexOf('/');
 129             if (lastSlashIndex == -1) {
 130                 return new ResourceKey("", relPath);
 131             }
 132 
 133             return new ResourceKey(relPath.substring(0, lastSlashIndex),
 134                                    relPath.substring(lastSlashIndex + 1));
 135         }
 136 
 137         public static ResourceKey forDirectory(final String relPath) {
 138             return new ResourceKey(relPath, "");
 139         }
 140 
 141         public String getDirectory() {
 142             return directory;
 143         }
 144 
 145         public String getFileName() {
 146             return fileName;
 147         }
 148 
 149         public int compareTo(final ResourceKey otherKey) {
 150             final int dirResult = directory.compareTo(otherKey.directory);
 151             return (dirResult != 0) ? dirResult
 152                                     : fileName.compareTo(otherKey.fileName);
 153         }
 154 
 155         @Override
 156         public boolean equals(final Object other) {
 157             if (other == this) {
 158                 return true;
 159             }
 160 
 161             if (!(other instanceof ResourceKey)) {
 162                 return false;
 163             }
 164 
 165             return compareTo((ResourceKey) other) == 0;
 166         }
 167 
 168         @Override
 169         public int hashCode() {
 170             int hash = 7;
 171 
 172             hash = 97 * hash + directory.hashCode();
 173             hash = 97 * hash + fileName.hashCode();
 174 
 175             return hash;
 176         }
 177     }
 178 }