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.ant;
  27 
  28 import java.util.Iterator;
  29 import java.util.LinkedList;
  30 import java.util.List;
  31 import com.sun.javafx.tools.resource.PackagerResource;
  32 import org.apache.tools.ant.types.DataType;
  33 import org.apache.tools.ant.types.resources.FileResource;
  34 
  35 /**
  36  * Collection of resources used by application.
  37  * Defined as set of JavaFX FileSet's. Could be reused using id/refid.
  38  *
  39  * In the example below both rx:resource elements define collection consisting
  40  * of s.jar in the dist directory:
  41  * <pre>
  42  *    &lt;fx:resource id="aaa"&gt;
  43  *       &lt;fx:fileset dir="dist" includes="s.jar"/&gt;
  44  *    &lt;/fx:resource&gt;
  45  *
  46  *    &lt;fx:resource refid="aaa"/&gt;
  47  * </pre>
  48  *
  49  * @ant.type name="resources" category="javafx"
  50  */
  51 public class Resources extends DataType {
  52    private List<com.sun.javafx.tools.ant.FileSet> rsets =
  53            new LinkedList<com.sun.javafx.tools.ant.FileSet>();
  54 
  55     private Resources get() {
  56         if (isReference()) {
  57             return (Resources) getRefid().getReferencedObject();
  58         }
  59         return this;
  60     }
  61 
  62     public List<com.sun.javafx.tools.ant.FileSet> getResources() {
  63         return get().rsets;
  64     }
  65 
  66     public com.sun.javafx.tools.ant.FileSet createFileSet() {
  67         com.sun.javafx.tools.ant.FileSet r = new com.sun.javafx.tools.ant.FileSet();
  68         rsets.add(r);
  69         return r;
  70     }
  71 
  72     //not really accurate as platfrom specific resources are not handled
  73     public String exportAsClassPath() {
  74         StringBuffer sb = new StringBuffer();
  75 
  76         List<String> jars = new LinkedList<String>();
  77         getJars("progress", jars);
  78         getJars("eager", jars);
  79         getJars("lazy", jars);
  80 
  81         for (String s : jars) {
  82             if (sb.length() != 0) {
  83                 sb.append(" "); //jars to be separated by space
  84             }
  85             sb.append(s);
  86         }
  87         return sb.toString();
  88     }
  89 
  90     private void getJars(String mode, List<String> lst) {
  91         for (com.sun.javafx.tools.ant.FileSet r : getResources()) {
  92             if (mode.equals(r.getMode())) {
  93                 for (final Iterator i = r.iterator(); i.hasNext();) {
  94                         FileResource fr = (FileResource) i.next();
  95                         PackagerResource p = new PackagerResource(fr.getBaseDir(),
  96                                 fr.getFile());
  97                         lst.add(p.getRelativePath());
  98                 }
  99             }
 100         }
 101     }
 102 }