1 /*******************************************************************************
2 * Copyright (c) 2000, 20007 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.dnd;
16 import org.eclipse.swt.internal.ole.win32.*;
17 import org.eclipse.swt.internal.win32.*;
20 * The class <code>URLTransfer</code> provides a platform specific mechanism
21 * for converting text in URL format represented as a java <code>String</code>
22 * to a platform specific representation of the data and vice versa. The string
23 * must contain a fully specified url.
25 * <p>An example of a java <code>String</code> containing a URL is shown below:</p>
28 * String url = "http://www.eclipse.org";
34 public class URLTransfer extends ByteArrayTransfer {
36 static URLTransfer _instance = new URLTransfer();
37 static final String CFSTR_INETURLW = "UniformResourceLocatorW"; //$NON-NLS-1$
38 static final int CFSTR_INETURLIDW = registerType(CFSTR_INETURLW);
39 static final String CFSTR_INETURL = "UniformResourceLocator"; //$NON-NLS-1$
40 static final int CFSTR_INETURLID = registerType(CFSTR_INETURL);
42 private URLTransfer() {}
45 * Returns the singleton instance of the URLTransfer class.
47 * @return the singleton instance of the URLTransfer class
49 public static URLTransfer getInstance () {
54 * This implementation of <code>javaToNative</code> converts a URL
55 * represented by a java <code>String</code> to a platform specific representation.
57 * @param object a java <code>String</code> containing a URL
58 * @param transferData an empty <code>TransferData</code> object that will
59 * be filled in on return with the platform specific format of the data
61 * @see Transfer#nativeToJava
64 public void javaToNative (Object object, TransferData transferData){
65 if (!checkURL(object) || !isSupportedType(transferData)) {
66 DND.error(DND.ERROR_INVALID_DATA);
68 transferData.result = COM.E_FAIL;
69 // URL is stored as a null terminated byte array
70 String url = ((String)object);
71 if (transferData.type == CFSTR_INETURLIDW) {
72 int charCount = url.length ();
73 char[] chars = new char[charCount+1];
74 url.getChars (0, charCount, chars, 0);
75 int byteCount = chars.length * 2;
76 long newPtr = OS.GlobalAlloc(COM.GMEM_FIXED | COM.GMEM_ZEROINIT, byteCount);
77 OS.MoveMemory(newPtr, chars, byteCount);
78 transferData.stgmedium = new STGMEDIUM();
79 transferData.stgmedium.tymed = COM.TYMED_HGLOBAL;
80 transferData.stgmedium.unionField = newPtr;
81 transferData.stgmedium.pUnkForRelease = 0;
82 transferData.result = COM.S_OK;
83 } else if (transferData.type == CFSTR_INETURLID) {
84 int count = url.length();
85 char[] chars = new char[count + 1];
86 url.getChars(0, count, chars, 0);
87 int codePage = OS.GetACP();
88 int cchMultiByte = OS.WideCharToMultiByte(codePage, 0, chars, -1, null, 0, null, null);
89 if (cchMultiByte == 0) {
90 transferData.stgmedium = new STGMEDIUM();
91 transferData.result = COM.DV_E_STGMEDIUM;
94 long lpMultiByteStr = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, cchMultiByte);
95 OS.WideCharToMultiByte(codePage, 0, chars, -1, lpMultiByteStr, cchMultiByte, null, null);
96 transferData.stgmedium = new STGMEDIUM();
97 transferData.stgmedium.tymed = COM.TYMED_HGLOBAL;
98 transferData.stgmedium.unionField = lpMultiByteStr;
99 transferData.stgmedium.pUnkForRelease = 0;
100 transferData.result = COM.S_OK;
105 * This implementation of <code>nativeToJava</code> converts a platform
106 * specific representation of a URL to a java <code>String</code>.
108 * @param transferData the platform specific representation of the data to be converted
109 * @return a java <code>String</code> containing a URL if the conversion was successful;
112 * @see Transfer#javaToNative
115 public Object nativeToJava(TransferData transferData){
116 if (!isSupportedType(transferData) || transferData.pIDataObject == 0) return null;
117 IDataObject data = new IDataObject(transferData.pIDataObject);
119 STGMEDIUM stgmedium = new STGMEDIUM();
120 FORMATETC formatetc = transferData.formatetc;
121 stgmedium.tymed = COM.TYMED_HGLOBAL;
122 transferData.result = getData(data, formatetc, stgmedium);
124 if (transferData.result != COM.S_OK) return null;
125 long hMem = stgmedium.unionField;
127 if (transferData.type == CFSTR_INETURLIDW) {
128 /* Ensure byteCount is a multiple of 2 bytes */
129 int size = OS.GlobalSize(hMem) / 2 * 2;
130 if (size == 0) return null;
131 char[] chars = new char[size/2];
132 long ptr = OS.GlobalLock(hMem);
133 if (ptr == 0) return null;
135 OS.MoveMemory(chars, ptr, size);
136 int length = chars.length;
137 for (int i=0; i<chars.length; i++) {
138 if (chars [i] == '\0') {
143 return new String (chars, 0, length);
145 OS.GlobalUnlock(hMem);
147 } else if (transferData.type == CFSTR_INETURLID) {
148 long lpMultiByteStr = OS.GlobalLock(hMem);
149 if (lpMultiByteStr == 0) return null;
151 int codePage = OS.GetACP();
152 int cchWideChar = OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, null, 0);
153 if (cchWideChar == 0) return null;
154 char[] lpWideCharStr = new char [cchWideChar - 1];
155 OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, lpWideCharStr, lpWideCharStr.length);
156 return new String(lpWideCharStr);
158 OS.GlobalUnlock(hMem);
168 protected int[] getTypeIds(){
169 return new int[] {CFSTR_INETURLIDW, CFSTR_INETURLID};
173 protected String[] getTypeNames(){
174 return new String[] {CFSTR_INETURLW, CFSTR_INETURL};
177 boolean checkURL(Object object) {
178 return object != null && (object instanceof String) && ((String)object).length() > 0;
182 protected boolean validate(Object object) {
183 return checkURL(object);