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