1 11 package org.eclipse.swt.dnd; 12 13 import org.eclipse.swt.internal.ole.win32.*; 14 import org.eclipse.swt.internal.win32.*; 15 16 29 public class HTMLTransfer extends ByteArrayTransfer { 30 31 static HTMLTransfer _instance = new HTMLTransfer(); 32 static final String HTML_FORMAT = "HTML Format"; static final int HTML_FORMATID = registerType(HTML_FORMAT); 34 static final String NUMBER = "00000000"; static final String HEADER = "Version:0.9\r\nStartHTML:"+NUMBER+"\r\nEndHTML:"+NUMBER+"\r\nStartFragment:"+NUMBER+"\r\nEndFragment:"+NUMBER+"\r\n"; 36 static final String PREFIX = "<html><body><!--StartFragment-->"; static final String SUFFIX = "<!--EndFragment--></body></html>"; static final String StartFragment = "StartFragment:"; static final String EndFragment = "EndFragment:"; 41 private HTMLTransfer() {} 42 43 48 public static HTMLTransfer getInstance () { 49 return _instance; 50 } 51 52 61 public void javaToNative (Object object, TransferData transferData){ 62 if (!checkHTML(object) || !isSupportedType(transferData)) { 63 DND.error(DND.ERROR_INVALID_DATA); 64 } 65 String string = (String )object; 66 int count = string.length(); 67 char[] chars = new char[count + 1]; 68 string.getChars(0, count, chars, 0); 69 int codePage = OS.GetACP(); 70 int cchMultiByte = OS.WideCharToMultiByte(codePage, 0, chars, -1, null, 0, null, null); 71 if (cchMultiByte == 0) { 72 transferData.stgmedium = new STGMEDIUM(); 73 transferData.result = COM.DV_E_STGMEDIUM; 74 return; 75 } 76 int startHTML = HEADER.length(); 77 int startFragment = startHTML + PREFIX.length(); 78 int endFragment = startFragment + cchMultiByte - 1; 79 int endHTML = endFragment + SUFFIX.length(); 80 81 StringBuffer buffer = new StringBuffer (HEADER); 82 int maxLength = NUMBER.length(); 83 int start = buffer.toString().indexOf(NUMBER); 85 String temp = Integer.toString(startHTML); 86 buffer.replace(start + maxLength-temp.length(), start + maxLength, temp); 87 start = buffer.toString().indexOf(NUMBER, start); 89 temp = Integer.toString(endHTML); 90 buffer.replace(start + maxLength-temp.length(), start + maxLength, temp); 91 start = buffer.toString().indexOf(NUMBER, start); 93 temp = Integer.toString(startFragment); 94 buffer.replace(start + maxLength-temp.length(), start + maxLength, temp); 95 start = buffer.toString().indexOf(NUMBER, start); 97 temp = Integer.toString(endFragment); 98 buffer.replace(start + maxLength-temp.length(), start + maxLength, temp); 99 100 buffer.append(PREFIX); 101 buffer.append(string); 102 buffer.append(SUFFIX); 103 104 count = buffer.length(); 105 chars = new char[count + 1]; 106 buffer.getChars(0, count, chars, 0); 107 cchMultiByte = OS.WideCharToMultiByte(codePage, 0, chars, -1, null, 0, null, null); 108 int lpMultiByteStr = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, cchMultiByte); 109 OS.WideCharToMultiByte(codePage, 0, chars, -1, lpMultiByteStr, cchMultiByte, null, null); 110 transferData.stgmedium = new STGMEDIUM(); 111 transferData.stgmedium.tymed = COM.TYMED_HGLOBAL; 112 transferData.stgmedium.unionField = lpMultiByteStr; 113 transferData.stgmedium.pUnkForRelease = 0; 114 transferData.result = COM.S_OK; 115 return; 116 } 117 118 128 public Object nativeToJava(TransferData transferData){ 129 if (!isSupportedType(transferData) || transferData.pIDataObject == 0) return null; 130 IDataObject data = new IDataObject(transferData.pIDataObject); 131 data.AddRef(); 132 STGMEDIUM stgmedium = new STGMEDIUM(); 133 FORMATETC formatetc = transferData.formatetc; 134 stgmedium.tymed = COM.TYMED_HGLOBAL; 135 transferData.result = data.GetData(formatetc, stgmedium); 136 data.Release(); 137 if (transferData.result != COM.S_OK) return null; 138 int hMem = stgmedium.unionField; 139 140 try { 141 int lpMultiByteStr = OS.GlobalLock(hMem); 142 if (lpMultiByteStr == 0) return null; 143 try { 144 int codePage = OS.GetACP(); 145 int cchWideChar = OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, null, 0); 146 if (cchWideChar == 0) return null; 147 char[] lpWideCharStr = new char [cchWideChar - 1]; 148 OS.MultiByteToWideChar (codePage, OS.MB_PRECOMPOSED, lpMultiByteStr, -1, lpWideCharStr, lpWideCharStr.length); 149 String string = new String (lpWideCharStr); 150 int fragmentStart = 0, fragmentEnd = 0; 151 int start = string.indexOf(StartFragment) + StartFragment.length(); 152 int end = start + 1; 153 while (end < string.length()) { 154 String s = string.substring(start, end); 155 try { 156 fragmentStart = Integer.parseInt(s); 157 end++; 158 } catch (NumberFormatException e) { 159 break; 160 } 161 } 162 start = string.indexOf(EndFragment) + EndFragment.length(); 163 end = start + 1; 164 while (end < string.length()) { 165 String s = string.substring(start, end); 166 try { 167 fragmentEnd = Integer.parseInt(s); 168 end++; 169 } catch (NumberFormatException e) { 170 break; 171 } 172 } 173 if (fragmentEnd <= fragmentStart || fragmentEnd > lpWideCharStr.length) return null; 174 178 String s = string.substring(fragmentStart, fragmentEnd); 179 182 String foxStart = "<!--StartFragment -->\r\n"; int prefix = s.indexOf(foxStart); 184 if (prefix != -1) { 185 prefix += foxStart.length(); 186 s = s.substring(prefix); 187 } 188 return s; 189 } finally { 190 OS.GlobalUnlock(hMem); 191 } 192 } finally { 193 OS.GlobalFree(hMem); 194 } 195 } 196 protected int[] getTypeIds(){ 197 return new int[] {HTML_FORMATID}; 198 } 199 protected String [] getTypeNames(){ 200 return new String [] {HTML_FORMAT}; 201 } 202 boolean checkHTML(Object object) { 203 return (object != null && object instanceof String && ((String )object).length() > 0); 204 } 205 protected boolean validate(Object object) { 206 return checkHTML(object); 207 } 208 } 209 | Popular Tags |