1 /*
   2  * Copyright (c) 2011, 2016, 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 org.apache.tools.ant.BuildException;
  29 import org.apache.tools.ant.DynamicAttribute;
  30 import org.apache.tools.ant.types.DataType;
  31 
  32 import java.io.File;
  33 import java.util.ArrayList;
  34 import java.util.Collection;
  35 import java.util.HashMap;
  36 import java.util.List;
  37 import java.util.Map;
  38 
  39 import static com.oracle.tools.packager.StandardBundlerParam.*;
  40 
  41 
  42 public class FileAssociation extends DataType implements DynamicAttribute {
  43 
  44     String extension;
  45     String mimeType;
  46     String description;
  47     File icon;
  48 
  49     List<DeployFXTask.BundleArgument> bundleArgumentList = new ArrayList<>();
  50 
  51     public DeployFXTask.BundleArgument createBundleArgument() {
  52         DeployFXTask.BundleArgument ba = new DeployFXTask.BundleArgument();
  53         bundleArgumentList.add(ba);
  54         return ba;
  55     }
  56 
  57     @Override
  58     public void setDynamicAttribute(String name, String value) throws BuildException {
  59         //Use qName and value - can't really validate anything until we know which bundlers we have, so this has
  60         //to done (way) downstream
  61         bundleArgumentList.add(new DeployFXTask.BundleArgument(name, value));
  62     }
  63 
  64     public Map<String, ? super Object> createLauncherMap() {
  65         Map<String, ? super Object> fileAssociations = new HashMap<>();
  66 
  67         putUnlessNull(fileAssociations, FA_EXTENSIONS.getID(), extension);
  68         putUnlessNull(fileAssociations, FA_CONTENT_TYPE.getID(), mimeType);
  69         putUnlessNull(fileAssociations, FA_DESCRIPTION.getID(), description);
  70         putUnlessNull(fileAssociations, FA_ICON.getID(), icon);
  71 
  72         for (DeployFXTask.BundleArgument ba : bundleArgumentList) {
  73             // TODO check and complain about collisions
  74             putUnlessNull(fileAssociations, ba.arg, ba.value);
  75         }
  76 
  77         return fileAssociations;
  78     }
  79 
  80     public void putUnlessNull(Map<String, ? super Object> params, String param, Object value) {
  81         if (value != null) {
  82             params.put(param, value);
  83         }
  84     }
  85 
  86     public void putUnlessNullOrEmpty(Map<String, ? super Object> params, String param, Collection value) {
  87         if (value != null && !value.isEmpty()) {
  88             params.put(param, value);
  89         }
  90     }
  91 
  92     public void putUnlessNullOrEmpty(Map<String, ? super Object> params, String param, Map value) {
  93         if (value != null && !value.isEmpty()) {
  94             params.put(param, value);
  95         }
  96     }
  97 
  98     /**
  99      * The file extension or extensions (separated by spaces) that the application requests it be registered to handle
 100      *
 101      * @ant.not-required
 102      */
 103     public void setExtension(String extension) {
 104         this.extension = extension;
 105     }
 106 
 107     /**
 108      * The mime-type that the application requests it be registered to handle.
 109      *
 110      * @ant.not-required
 111      */
 112     public void setMimeType(String mimeType) {
 113         this.mimeType = mimeType;
 114     }
 115 
 116     /**
 117      * The description the Operation System may show for files of the associated extension and mime-type.
 118      *
 119      * @ant.optional
 120      */
 121     public void setDescription(String description) {
 122         this.description = description;
 123     }
 124 
 125     /**
 126      * The icon the Operation System may show for files of the associated extension and mime-type.
 127      *
 128      * @ant.optional
 129      */
 130     public void setIcon(File icon) {
 131         this.icon = icon;
 132     }
 133 
 134     /**
 135      * Extra arguments that the bundler may interpret to provide for better
 136      * integration with specific operating systems.
 137      *
 138      * @ant.optional
 139      */
 140     public DeployFXTask.BundleArgument createArg() {
 141         DeployFXTask.BundleArgument ba = new DeployFXTask.BundleArgument();
 142         bundleArgumentList.add(ba);
 143         return ba;
 144     }
 145 }