Exemple #1
0
 /**
  * Returns the <code>Transferable</code>'s data in the requested <code>DataFlavor</code> if
  * possible. If the desired flavor is <code>DataFlavor.stringFlavor</code>, or an equivalent
  * flavor, the <code>String</code> representing the selection is returned. If the desired flavor
  * is <code>DataFlavor.plainTextFlavor</code>, or an equivalent flavor, a <code>Reader</code> is
  * returned. <b>Note:</b> The behavior of this method for <code>DataFlavor.plainTextFlavor</code>
  * and equivalent <code>DataFlavor</code>s is inconsistent with the definition of <code>
  * DataFlavor.plainTextFlavor</code>.
  *
  * @param flavor the requested flavor for the data
  * @return the data in the requested flavor, as outlined above
  * @throws UnsupportedFlavorException if the requested data flavor is not equivalent to either
  *     <code>DataFlavor.stringFlavor</code> or <code>DataFlavor.plainTextFlavor</code>
  * @throws IOException if an IOException occurs while retrieving the data. By default,
  *     StringSelection never throws this exception, but a subclass may.
  * @throws NullPointerException if flavor is <code>null</code>
  * @see java.io.Reader
  */
 public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
   // JCK Test StringSelection0007: if 'flavor' is null, throw NPE
   if (flavor.equals(flavors[STRING])) {
     return (Object) data;
   } else if (flavor.equals(flavors[PLAIN_TEXT])) {
     return new StringReader(data == null ? "" : data);
   } else {
     throw new UnsupportedFlavorException(flavor);
   }
 }
Exemple #2
0
  /**
   * returns whether the given flavor is a color flavor
   *
   * @param flavor a flavor
   * @return whether the given flavor is a color flavor
   */
  public boolean isColorDataFlavor(DataFlavor flavor) {

    boolean isColorDataFlavor = false;

    if (flavor != null) {

      isColorDataFlavor =
          (flavor.isMimeTypeEqual(colorFlavor) || flavor.isMimeTypeEqual(w3cSVGColorFlavor));
    }

    return isColorDataFlavor;
  }
  /**
   * This method has the same behavior as {@link #equals(Object)}. The only difference being that it
   * takes a {@code DataFlavor} instance as a parameter.
   *
   * @param that the <code>DataFlavor</code> to compare with <code>this</code>
   * @return <code>true</code> if <code>that</code> is equivalent to this <code>DataFlavor</code>;
   *     <code>false</code> otherwise
   * @see #selectBestTextFlavor
   */
  public boolean equals(DataFlavor that) {
    if (that == null) {
      return false;
    }
    if (this == that) {
      return true;
    }

    if (representationClass == null) {
      if (that.getRepresentationClass() != null) {
        return false;
      }
    } else {
      if (!representationClass.equals(that.getRepresentationClass())) {
        return false;
      }
    }

    if (mimeType == null) {
      if (that.mimeType != null) {
        return false;
      }
    } else {
      if (!mimeType.match(that.mimeType)) {
        return false;
      }

      if ("text".equals(getPrimaryType())
          && DataTransferer.doesSubtypeSupportCharset(this)
          && representationClass != null
          && !(isRepresentationClassReader()
              || String.class.equals(representationClass)
              || isRepresentationClassCharBuffer()
              || DataTransferer.charArrayClass.equals(representationClass))) {
        String thisCharset = DataTransferer.canonicalName(getParameter("charset"));
        String thatCharset = DataTransferer.canonicalName(that.getParameter("charset"));
        if (thisCharset == null) {
          if (thatCharset != null) {
            return false;
          }
        } else {
          if (!thisCharset.equals(thatCharset)) {
            return false;
          }
        }
      }
    }

    return true;
  }
    /**
     * Compares two <code>DataFlavor</code> objects. Returns a negative integer, zero, or a positive
     * integer as the first <code>DataFlavor</code> is worse than, equal to, or better than the
     * second.
     *
     * <p><code>DataFlavor</code>s are ordered according to the rules outlined for <code>
     * selectBestTextFlavor</code>.
     *
     * @param obj1 the first <code>DataFlavor</code> to be compared
     * @param obj2 the second <code>DataFlavor</code> to be compared
     * @return a negative integer, zero, or a positive integer as the first argument is worse, equal
     *     to, or better than the second
     * @throws ClassCastException if either of the arguments is not an instance of <code>DataFlavor
     *     </code>
     * @throws NullPointerException if either of the arguments is <code>null</code>
     * @see #selectBestTextFlavor
     */
    public int compare(Object obj1, Object obj2) {
      DataFlavor flavor1 = (DataFlavor) obj1;
      DataFlavor flavor2 = (DataFlavor) obj2;

      if (flavor1.isFlavorTextType()) {
        if (flavor2.isFlavorTextType()) {
          return super.compare(obj1, obj2);
        } else {
          return 1;
        }
      } else if (flavor2.isFlavorTextType()) {
        return -1;
      } else {
        return 0;
      }
    }
  /**
   * Common initialization code called from various constructors.
   *
   * @param mimeType the MIME Content Type (must have a class= param)
   * @param humanPresentableName the human Presentable Name or <code>null</code>
   * @param classLoader the fallback class loader to resolve against
   * @throws MimeTypeParseException
   * @throws ClassNotFoundException
   * @throws NullPointerException if <code>mimeType</code> is null
   * @see tryToLoadClass
   */
  private void initialize(String mimeType, String humanPresentableName, ClassLoader classLoader)
      throws MimeTypeParseException, ClassNotFoundException {
    if (mimeType == null) {
      throw new NullPointerException("mimeType");
    }

    this.mimeType = new MimeType(mimeType); // throws

    String rcn = getParameter("class");

    if (rcn == null) {
      if ("application/x-java-serialized-object".equals(this.mimeType.getBaseType()))
        throw new IllegalArgumentException("no representation class specified for:" + mimeType);
      else representationClass = java.io.InputStream.class; // default
    } else { // got a class name
      representationClass = DataFlavor.tryToLoadClass(rcn, classLoader);
    }

    this.mimeType.setParameter("class", representationClass.getName());

    if (humanPresentableName == null) {
      humanPresentableName = this.mimeType.getParameter("humanPresentableName");
      if (humanPresentableName == null)
        humanPresentableName = this.mimeType.getPrimaryType() + "/" + this.mimeType.getSubType();
    }

    this.humanPresentableName = humanPresentableName; // set it.

    this.mimeType.removeParameter("humanPresentableName"); // just in case
  }
  /** Restores this <code>DataFlavor</code> from a Serialized state. */
  public synchronized void readExternal(ObjectInput is) throws IOException, ClassNotFoundException {
    String rcn = null;
    mimeType = (MimeType) is.readObject();

    if (mimeType != null) {
      humanPresentableName = mimeType.getParameter("humanPresentableName");
      mimeType.removeParameter("humanPresentableName");
      rcn = mimeType.getParameter("class");
      if (rcn == null) {
        throw new IOException("no class parameter specified in: " + mimeType);
      }
    }

    try {
      representationClass = (Class) is.readObject();
    } catch (OptionalDataException ode) {
      if (!ode.eof || ode.length != 0) {
        throw ode;
      }
      // Ensure backward compatibility.
      // Old versions didn't write the representation class to the stream.
      if (rcn != null) {
        representationClass = DataFlavor.tryToLoadClass(rcn, getClass().getClassLoader());
      }
    }
  }
 public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
   if (flavor.equals(DEFAULT_MUTABLE_TREENODE_FLAVOR)) {
     //        System.out.println("my getTransferData invoked");
     return data;
   } else {
     throw new UnsupportedFlavorException(flavor);
   }
 }
Exemple #8
0
 private boolean testFlavor(DataFlavor inFlavor, Transferable t) {
   if (inFlavor != null) {
     if (t.isDataFlavorSupported(inFlavor)) {
       JConfig.log().logVerboseDebug("Accepting(2): " + inFlavor.getMimeType());
       return true;
     }
   }
   return false;
 }
  /**
   * Selects the best text <code>DataFlavor</code> from an array of <code>
   * DataFlavor</code>s. Only <code>DataFlavor.stringFlavor</code>, and equivalent flavors, and
   * flavors that have a primary MIME type of "text", are considered for selection.
   *
   * <p>Flavors are first sorted by their MIME types in the following order:
   *
   * <ul>
   *   <li>"text/sgml"
   *   <li>"text/xml"
   *   <li>"text/html"
   *   <li>"text/rtf"
   *   <li>"text/enriched"
   *   <li>"text/richtext"
   *   <li>"text/uri-list"
   *   <li>"text/tab-separated-values"
   *   <li>"text/t140"
   *   <li>"text/rfc822-headers"
   *   <li>"text/parityfec"
   *   <li>"text/directory"
   *   <li>"text/css"
   *   <li>"text/calendar"
   *   <li>"application/x-java-serialized-object"
   *   <li>"text/plain"
   *   <li>"text/&lt;other&gt;"
   * </ul>
   *
   * <p>For example, "text/sgml" will be selected over "text/html", and <code>
   * DataFlavor.stringFlavor</code> will be chosen over <code>DataFlavor.plainTextFlavor</code>.
   *
   * <p>If two or more flavors share the best MIME type in the array, then that MIME type will be
   * checked to see if it supports the charset parameter.
   *
   * <p>The following MIME types support, or are treated as though they support, the charset
   * parameter:
   *
   * <ul>
   *   <li>"text/sgml"
   *   <li>"text/xml"
   *   <li>"text/html"
   *   <li>"text/enriched"
   *   <li>"text/richtext"
   *   <li>"text/uri-list"
   *   <li>"text/directory"
   *   <li>"text/css"
   *   <li>"text/calendar"
   *   <li>"application/x-java-serialized-object"
   *   <li>"text/plain"
   * </ul>
   *
   * The following MIME types do not support, or are treated as though they do not support, the
   * charset parameter:
   *
   * <ul>
   *   <li>"text/rtf"
   *   <li>"text/tab-separated-values"
   *   <li>"text/t140"
   *   <li>"text/rfc822-headers"
   *   <li>"text/parityfec"
   * </ul>
   *
   * For "text/&lt;other&gt;" MIME types, the first time the JRE needs to determine whether the MIME
   * type supports the charset parameter, it will check whether the parameter is explicitly listed
   * in an arbitrarily chosen <code>DataFlavor</code> which uses that MIME type. If so, the JRE will
   * assume from that point on that the MIME type supports the charset parameter and will not check
   * again. If the parameter is not explicitly listed, the JRE will assume from that point on that
   * the MIME type does not support the charset parameter and will not check again. Because this
   * check is performed on an arbitrarily chosen <code>DataFlavor</code>, developers must ensure
   * that all <code>DataFlavor</code>s with a "text/&lt;other&gt;" MIME type specify the charset
   * parameter if it is supported by that MIME type. Developers should never rely on the JRE to
   * substitute the platform's default charset for a "text/&lt;other&gt;" DataFlavor. Failure to
   * adhere to this restriction will lead to undefined behavior.
   *
   * <p>If the best MIME type in the array does not support the charset parameter, the flavors which
   * share that MIME type will then be sorted by their representation classes in the following
   * order: <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>, <code>[B</code>,
   * &lt;all others&gt;.
   *
   * <p>If two or more flavors share the best representation class, or if no flavor has one of the
   * three specified representations, then one of those flavors will be chosen
   * non-deterministically.
   *
   * <p>If the best MIME type in the array does support the charset parameter, the flavors which
   * share that MIME type will then be sorted by their representation classes in the following
   * order: <code>java.io.Reader</code>, <code>java.lang.String</code>, <code>java.nio.CharBuffer
   * </code>, <code>[C</code>, &lt;all others&gt;.
   *
   * <p>If two or more flavors share the best representation class, and that representation is one
   * of the four explicitly listed, then one of those flavors will be chosen non-deterministically.
   * If, however, no flavor has one of the four specified representations, the flavors will then be
   * sorted by their charsets. Unicode charsets, such as "UTF-16", "UTF-8", "UTF-16BE", "UTF-16LE",
   * and their aliases, are considered best. After them, the platform default charset and its
   * aliases are selected. "US-ASCII" and its aliases are worst. All other charsets are chosen in
   * alphabetical order, but only charsets supported by this implementation of the Java platform
   * will be considered.
   *
   * <p>If two or more flavors share the best charset, the flavors will then again be sorted by
   * their representation classes in the following order: <code>java.io.InputStream</code>, <code>
   * java.nio.ByteBuffer</code>, <code>[B</code>, &lt;all others&gt;.
   *
   * <p>If two or more flavors share the best representation class, or if no flavor has one of the
   * three specified representations, then one of those flavors will be chosen
   * non-deterministically.
   *
   * @param availableFlavors an array of available <code>DataFlavor</code>s
   * @return the best (highest fidelity) flavor according to the rules specified above, or <code>
   *     null</code>, if <code>availableFlavors</code> is <code>null</code>, has zero length, or
   *     contains no text flavors
   * @since 1.3
   */
  public static final DataFlavor selectBestTextFlavor(DataFlavor[] availableFlavors) {
    if (availableFlavors == null || availableFlavors.length == 0) {
      return null;
    }

    if (textFlavorComparator == null) {
      textFlavorComparator = new TextFlavorComparator();
    }

    DataFlavor bestFlavor =
        (DataFlavor) Collections.max(Arrays.asList(availableFlavors), textFlavorComparator);

    if (!bestFlavor.isFlavorTextType()) {
      return null;
    }

    return bestFlavor;
  }
Exemple #10
0
 /**
  * Returns whether the requested flavor is supported by this <code>Transferable</code>.
  *
  * @param flavor the requested flavor for the data
  * @return true if <code>flavor</code> is equal to <code>DataFlavor.stringFlavor</code> or <code>
  *     DataFlavor.plainTextFlavor</code>; false if <code>flavor</code> is not one of the above
  *     flavors
  * @throws NullPointerException if flavor is <code>null</code>
  */
 public boolean isDataFlavorSupported(DataFlavor flavor) {
   // JCK Test StringSelection0003: if 'flavor' is null, throw NPE
   for (int i = 0; i < flavors.length; i++) {
     if (flavor.equals(flavors[i])) {
       return true;
     }
   }
   return false;
 }
Exemple #11
0
  public StringBuffer getTransferData(Transferable t) {
    StringBuffer xferData = null;
    Object dropped;
    DataFlavor dtf;

    dtf = testAllFlavors(t);

    JConfig.log().logVerboseDebug("dtf == " + dtf);

    try {
      if (dtf == _htmlFlavor || dtf == _utf8HtmlFlavor || dtf == _thtmlFlavor) {
        /*
         * Annoying.
         */
        if (JConfig.queryConfiguration("debug.uber", "false").equals("true") && JConfig.debugging)
          System.out.println("Ick: " + t.getTransferData(DataFlavor.getTextPlainUnicodeFlavor()));
      }
      dropped = t.getTransferData(dtf);
    } catch (IOException ioe) {
      try {
        dropped = t.getTransferData(DataFlavor.stringFlavor);
      } catch (Exception e) {
        JConfig.log().logDebug("I/O Exception: " + ioe);
        return null;
      }
    } catch (UnsupportedFlavorException ufe) {
      try {
        dropped = t.getTransferData(DataFlavor.stringFlavor);
      } catch (Exception e) {
        JConfig.log().logDebug("Unsupported flavor: " + dtf);
        return null;
      }
    }

    if (dropped != null) {
      if (dropped instanceof InputStream) {
        JConfig.log().logVerboseDebug("Dropped an InputStream");
        xferData = getInputStreamData(t, dtf, (InputStream) dropped);
      } else if (dropped instanceof Reader) {
        JConfig.log().logVerboseDebug("Dropped a Reader");
        xferData = getDataFromReader(new BufferedReader((Reader) dropped));
      } else if (dropped instanceof java.net.URL) {
        JConfig.log().logVerboseDebug("Dropped a URL");
        JConfig.log().logVerboseDebug("Got: " + dropped.toString());

        xferData = new StringBuffer(dropped.toString());
      } else if (dropped instanceof String) {
        JConfig.log().logVerboseDebug("Dropped a String");
        xferData = new StringBuffer((String) dropped);
      }

      return (xferData);
    }
    return null;
  }
Exemple #12
0
 private boolean testFlavor(DataFlavor inFlavor, DropTargetDragEvent t) {
   if (inFlavor != null) {
     if (t.isDataFlavorSupported(inFlavor)) {
       /*
        * I think this has been debugged enough.  This gets annoying.
        */
       JConfig.log().logVerboseDebug("Accepting(1): " + inFlavor.getMimeType());
       return true;
     }
   }
   return false;
 }
Exemple #13
0
  private void setupFlavors() {
    //  If it's NOT Windows
    _windows = System.getProperty("os.name").indexOf("indows") != -1;

    //  Deprecated, generally unused, but deprecation didn't provide
    // a useful alternative w/o rewriting a lot of code, so I'm keeping it for now.
    if (_plainFlavor == null) {
      //      _plainFlavor = DataFlavor.plainTextFlavor;
      _plainFlavor = DataFlavor.getTextPlainUnicodeFlavor();
    }

    _isoFlavor = getDataFlavor(_isoFlavor, "isoFlavor");
    _ascFlavor = getDataFlavor(_ascFlavor, "ascFlavor");
    _pl2Flavor = getDataFlavor(_pl2Flavor, "pl2Flavor");
    _htmlFlavor = getDataFlavor(_htmlFlavor, "htmlFlavor");
    _utf8HtmlFlavor = getDataFlavor(_htmlFlavor, "UTF8Html");
    _thtmlFlavor = getDataFlavor(_htmlFlavor, "thtmlFlavor");
    _urlFlavor = getDataFlavor(_urlFlavor, "urlFlavor");
  }
 public boolean isDataFlavorSupported(DataFlavor flavor) {
   for (int i = 0; i < flavors.length; i++) if (flavor.equals(flavors[i])) return true;
   return false;
 }