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