1 /*
   2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 
  33 package com.oracle.javafx.scenebuilder.kit.metadata.util;
  34 
  35 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMArchive;
  36 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMDocument;
  37 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMNodes;
  38 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMObject;
  39 import java.io.File;
  40 import java.io.IOException;
  41 import java.net.URL;
  42 import java.util.ArrayList;
  43 import java.util.Arrays;
  44 import java.util.Collections;
  45 import java.util.List;
  46 import java.util.ResourceBundle;
  47 import javafx.scene.input.Clipboard;
  48 
  49 /**
  50  *
  51  */
  52 public class ClipboardDecoder {
  53 
  54     final Clipboard clipboard;
  55 
  56     public ClipboardDecoder(Clipboard clipboard) {
  57         this.clipboard = clipboard;
  58     }
  59 
  60     public List<FXOMObject> decode(FXOMDocument targetDocument) {
  61         assert targetDocument != null;
  62 
  63         List<FXOMObject> result = null;
  64 
  65         // SB_DATA_FORMAT
  66         if (clipboard.hasContent(ClipboardEncoder.SB_DATA_FORMAT)) {
  67             final Object content = clipboard.getContent(ClipboardEncoder.SB_DATA_FORMAT);
  68             if (content instanceof FXOMArchive) {
  69                 final FXOMArchive archive = (FXOMArchive) content;
  70                 try {
  71                     result = archive.decode(targetDocument);
  72                 } catch(IOException x) {
  73                     result = null;
  74                 }
  75             }
  76         }
  77 
  78         // FXML_DATA_FORMAT
  79         if ((result == null)
  80                 && clipboard.hasContent(ClipboardEncoder.FXML_DATA_FORMAT)) {
  81             final Object content = clipboard.getContent(ClipboardEncoder.FXML_DATA_FORMAT);
  82             if (content instanceof String) {
  83                 final String fxmlText = (String) content;
  84                 try {
  85                     final URL location = targetDocument.getLocation();
  86                     final ClassLoader classLoader = targetDocument.getClassLoader();
  87                     final ResourceBundle resources = targetDocument.getResources();
  88                     final FXOMDocument transientDoc
  89                             = new FXOMDocument(fxmlText, location, classLoader, resources);
  90                     result = Arrays.asList(transientDoc.getFxomRoot());
  91                 } catch(IOException x) {
  92                     result = null;
  93                 }
  94             }
  95         }
  96 
  97         // DataFormat.FILES
  98         if ((result == null) && clipboard.hasFiles()) {
  99             result = new ArrayList<>();
 100             for (File file : clipboard.getFiles()) {
 101                 try {
 102                     final FXOMObject newObject
 103                             = FXOMNodes.newObject(targetDocument, file);
 104                     // newObject is null when file is empty
 105                     if (newObject != null) {
 106                         result.add(newObject);
 107                     }
 108                 } catch (IOException x) {
 109                     // Then we silently ignore this file
 110                 }
 111             }
 112         }
 113 
 114         // If nothing is exploitable, we return a list.
 115         if (result == null) {
 116             result = Collections.emptyList();
 117         }
 118 
 119         return result;
 120     }
 121 }