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.oracle.ipack.resources;
  27 
  28 import com.oracle.ipack.resources.ResourceRules.Exclude;
  29 import com.oracle.ipack.util.Base64;
  30 import java.io.BufferedWriter;
  31 import java.io.IOException;
  32 import java.io.OutputStream;
  33 import java.io.OutputStreamWriter;
  34 import java.io.PrintWriter;
  35 import java.util.ArrayList;
  36 import java.util.List;
  37 
  38 public final class CodeResources {
  39     private final ResourceRules resourceRules;
  40     private final List<HashedResource> hashedResources;
  41 
  42     public CodeResources(final ResourceRules resourceRules) {
  43         this.resourceRules = resourceRules;
  44         this.hashedResources = new ArrayList<HashedResource>();
  45     }
  46 
  47     public void addHashedResource(
  48             final String resourceName,
  49             final byte[] resourceHash) {
  50         hashedResources.add(
  51                 new HashedResource(resourceName,
  52                                    Base64.byteArrayToBase64(resourceHash)));
  53     }
  54 
  55     public void write(final OutputStream os) throws IOException {
  56         final PrintWriter pw =
  57                 new PrintWriter(
  58                     new BufferedWriter(
  59                         new OutputStreamWriter(os, "UTF-8")));
  60 
  61         pw.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  62                       + "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\""
  63                       + " \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
  64                       + "<plist version=\"1.0\">\n"
  65                       + "<dict>\n"
  66                       + "\t<key>files</key>\n"
  67                       + "\t<dict>\n");
  68 
  69         for (final HashedResource hashedResource: hashedResources) {
  70             appendResource(pw, hashedResource);
  71         }
  72 
  73         pw.append("\t</dict>\n"
  74                       + "\t<key>rules</key>\n"
  75                       + "\t<dict>\n"
  76                       + "\t\t<key>.*</key>\n"
  77                       + "\t\t<true/>\n");
  78 
  79         for (final Exclude exclude: resourceRules.getExcludes()) {
  80             if (exclude.getWeight() >= 0) {
  81                 appendExclude(pw, exclude);
  82             }
  83         }
  84 
  85         pw.append("\t</dict>\n"
  86                       + "</dict>\n"
  87                       + "</plist>\n");
  88         pw.flush();
  89     }
  90 
  91     private static void appendResource(final PrintWriter pw,
  92                                        final HashedResource hashedResource) {
  93         pw.append("\t\t<key>").append(hashedResource.getName())
  94                               .append("</key>\n")
  95           .append("\t\t<data>\n")
  96           .append("\t\t").append(hashedResource.getHash()).append('\n')
  97           .append("\t\t</data>\n");
  98     }
  99 
 100     private static void appendExclude(final PrintWriter pw,
 101                                       final Exclude exclude) {
 102         pw.append("\t\t<key>").append(exclude.getName()).append("</key>\n")
 103           .append("\t\t<dict>\n")
 104           .append("\t\t\t<key>omit</key>\n")
 105           .append("\t\t\t<true/>\n")
 106           .append("\t\t\t<key>weight</key>\n")
 107           .append("\t\t\t<real>");
 108         pw.print(exclude.getWeight());
 109         pw.append("</real>\n")
 110           .append("\t\t</dict>\n");
 111     }
 112 
 113     private static final class HashedResource {
 114         private final String name;
 115         private final String hash;
 116 
 117         public HashedResource(final String name, final String hash) {
 118             this.name = name;
 119             this.hash = hash;
 120         }
 121 
 122         public String getName() {
 123             return name;
 124         }
 125 
 126         public String getHash() {
 127             return hash;
 128         }
 129     }
 130 }