KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > dnd > HTMLTransfer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.dnd;
12
13 import org.eclipse.swt.internal.ole.win32.*;
14 import org.eclipse.swt.internal.win32.*;
15
16 /**
17  * The class <code>HTMLTransfer</code> provides a platform specific mechanism
18  * for converting text in HTML format represented as a java <code>String</code>
19  * to a platform specific representation of the data and vice versa. See
20  * <code>Transfer</code> for additional information.
21  *
22  * <p>An example of a java <code>String</code> containing HTML text is shown
23  * below:</p>
24  *
25  * <code><pre>
26  * String htmlData = "<p>This is a paragraph of text.</p>";
27  * </code></pre>
28  */

29 public class HTMLTransfer extends ByteArrayTransfer {
30
31     static HTMLTransfer _instance = new HTMLTransfer();
32     static final String JavaDoc HTML_FORMAT = "HTML Format"; //$NON-NLS-1$
33
static final int HTML_FORMATID = registerType(HTML_FORMAT);
34     static final String JavaDoc NUMBER = "00000000"; //$NON-NLS-1$
35
static final String JavaDoc HEADER = "Version:0.9\r\nStartHTML:"+NUMBER+"\r\nEndHTML:"+NUMBER+"\r\nStartFragment:"+NUMBER+"\r\nEndFragment:"+NUMBER+"\r\n";
36     static final String JavaDoc PREFIX = "<html><body><!--StartFragment-->"; //$NON-NLS-1$
37
static final String JavaDoc SUFFIX = "<!--EndFragment--></body></html>"; //$NON-NLS-1$
38
static final String JavaDoc StartFragment = "StartFragment:"; //$NON-NLS-1$
39
static final String JavaDoc EndFragment = "EndFragment:"; //$NON-NLS-1$
40

41 private HTMLTransfer() {}
42
43 /**
44  * Returns the singleton instance of the HTMLTransfer class.
45  *
46  * @return the singleton instance of the HTMLTransfer class
47  */

48 public static HTMLTransfer getInstance () {
49     return _instance;
50 }
51
52 /**
53  * This implementation of <code>javaToNative</code> converts HTML-formatted text
54  * represented by a java <code>String</code> to a platform specific representation.
55  * For additional information see <code>Transfer#javaToNative</code>.
56  *
57  * @param object a java <code>String</code> containing HTML text
58  * @param transferData an empty <code>TransferData</code> object; this
59  * object will be filled in on return with the platform specific format of the data
60  */

61 public void javaToNative (Object JavaDoc object, TransferData transferData){
62     if (!checkHTML(object) || !isSupportedType(transferData)) {
63         DND.error(DND.ERROR_INVALID_DATA);
64     }
65     String JavaDoc string = (String JavaDoc)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 JavaDoc buffer = new StringBuffer JavaDoc(HEADER);
82     int maxLength = NUMBER.length();
83     //startHTML
84
int start = buffer.toString().indexOf(NUMBER);
85     String JavaDoc temp = Integer.toString(startHTML);
86     buffer.replace(start + maxLength-temp.length(), start + maxLength, temp);
87     //endHTML
88
start = buffer.toString().indexOf(NUMBER, start);
89     temp = Integer.toString(endHTML);
90     buffer.replace(start + maxLength-temp.length(), start + maxLength, temp);
91     //startFragment
92
start = buffer.toString().indexOf(NUMBER, start);
93     temp = Integer.toString(startFragment);
94     buffer.replace(start + maxLength-temp.length(), start + maxLength, temp);
95     //endFragment
96
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 /**
119  * This implementation of <code>nativeToJava</code> converts a platform specific
120  * representation of HTML text to a java <code>String</code>.
121  * For additional information see <code>Transfer#nativeToJava</code>.
122  *
123  * @param transferData the platform specific representation of the data to be
124  * been converted
125  * @return a java <code>String</code> containing HTML text if the
126  * conversion was successful; otherwise null
127  */

128 public Object JavaDoc 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 JavaDoc string = new String JavaDoc(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 JavaDoc s = string.substring(start, end);
155                 try {
156                     fragmentStart = Integer.parseInt(s);
157                     end++;
158                 } catch (NumberFormatException JavaDoc e) {
159                     break;
160                 }
161             }
162             start = string.indexOf(EndFragment) + EndFragment.length();
163             end = start + 1;
164             while (end < string.length()) {
165                 String JavaDoc s = string.substring(start, end);
166                 try {
167                     fragmentEnd = Integer.parseInt(s);
168                     end++;
169                 } catch (NumberFormatException JavaDoc e) {
170                     break;
171                 }
172             }
173             if (fragmentEnd <= fragmentStart || fragmentEnd > lpWideCharStr.length) return null;
174             /* TO DO:
175              * FragmentStart and FragmentEnd are offsets in original byte stream, not
176              * the wide char version of the byte stream.
177              */

178             String JavaDoc s = string.substring(fragmentStart, fragmentEnd);
179             /*
180              * Firefox includes <!--StartFragment --> in the fragment, so remove it.
181              */

182             String JavaDoc foxStart = "<!--StartFragment -->\r\n"; //$NON-NLS-1$
183
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 JavaDoc[] getTypeNames(){
200     return new String JavaDoc[] {HTML_FORMAT};
201 }
202 boolean checkHTML(Object JavaDoc object) {
203     return (object != null && object instanceof String JavaDoc && ((String JavaDoc)object).length() > 0);
204 }
205 protected boolean validate(Object JavaDoc object) {
206     return checkHTML(object);
207 }
208 }
209
Popular Tags