Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVeronika Irvine2004-11-23 21:28:22 +0000
committerVeronika Irvine2004-11-23 21:28:22 +0000
commit97cc483badf6657f2e65afe2073636323fc40bdd (patch)
tree6d2f58d3fe838daf886816e996b87bbb4c28ed18 /examples
parent6c47784c5a7b45e769c078aad8b37a11143d5176 (diff)
downloadeclipse.platform.swt-97cc483badf6657f2e65afe2073636323fc40bdd.tar.gz
eclipse.platform.swt-97cc483badf6657f2e65afe2073636323fc40bdd.tar.xz
eclipse.platform.swt-97cc483badf6657f2e65afe2073636323fc40bdd.zip
add validation checking
Diffstat (limited to 'examples')
-rw-r--r--examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/clipboard/MyTypeTransfer.java195
-rw-r--r--examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/dnd/ImageTransfer.java12
-rw-r--r--examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet79.java58
3 files changed, 151 insertions, 114 deletions
diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/clipboard/MyTypeTransfer.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/clipboard/MyTypeTransfer.java
index 306a5de507..e001d5339b 100644
--- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/clipboard/MyTypeTransfer.java
+++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/clipboard/MyTypeTransfer.java
@@ -1,92 +1,103 @@
-/*******************************************************************************
- * Copyright (c) 2000, 2003 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Common Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/cpl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-package org.eclipse.swt.examples.clipboard;
-
-import java.io.*;
-import org.eclipse.swt.dnd.*;
-
-public class MyTypeTransfer extends ByteArrayTransfer {
-
- private static final String MYTYPENAME = "name_list"; //$NON-NLS-1$
- private static final int MYTYPEID = registerType(MYTYPENAME);
- private static MyTypeTransfer _instance = new MyTypeTransfer();
-
-public static MyTypeTransfer getInstance () {
- return _instance;
-}
-public void javaToNative (Object object, TransferData transferData) {
- if (object == null || !(object instanceof MyType[])) return;
-
- if (isSupportedType(transferData)) {
- MyType[] myTypes = (MyType[]) object;
- try {
- // write data to a byte array and then ask super to convert to pMedium
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- DataOutputStream writeOut = new DataOutputStream(out);
- for (int i = 0, length = myTypes.length; i < length; i++){
- byte[] buffer = myTypes[i].firstName.getBytes();
- writeOut.writeInt(buffer.length);
- writeOut.write(buffer);
- buffer = myTypes[i].firstName.getBytes();
- writeOut.writeInt(buffer.length);
- writeOut.write(buffer);
- }
- byte[] buffer = out.toByteArray();
- writeOut.close();
-
- super.javaToNative(buffer, transferData);
-
- } catch (IOException e) {
- }
- }
-}
-public Object nativeToJava(TransferData transferData){
-
- if (isSupportedType(transferData)) {
-
- byte[] buffer = (byte[])super.nativeToJava(transferData);
- if (buffer == null) return null;
-
- MyType[] myData = new MyType[0];
- try {
- ByteArrayInputStream in = new ByteArrayInputStream(buffer);
- DataInputStream readIn = new DataInputStream(in);
- while(readIn.available() > 20) {
- MyType datum = new MyType();
- int size = readIn.readInt();
- byte[] name = new byte[size];
- readIn.read(name);
- datum.firstName = new String(name);
- size = readIn.readInt();
- name = new byte[size];
- readIn.read(name);
- datum.lastName = new String(name);
- MyType[] newMyData = new MyType[myData.length + 1];
- System.arraycopy(myData, 0, newMyData, 0, myData.length);
- newMyData[myData.length] = datum;
- myData = newMyData;
- }
- readIn.close();
- } catch (IOException ex) {
- return null;
- }
- return myData;
- }
-
- return null;
-}
-protected String[] getTypeNames(){
- return new String[]{MYTYPENAME};
-}
-protected int[] getTypeIds(){
- return new int[] {MYTYPEID};
-}
-}
+/*******************************************************************************
+ * Copyright (c) 2000, 2003 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.swt.examples.clipboard;
+
+import java.io.*;
+import org.eclipse.swt.dnd.*;
+
+public class MyTypeTransfer extends ByteArrayTransfer {
+
+ private static final String MYTYPENAME = "name_list"; //$NON-NLS-1$
+ private static final int MYTYPEID = registerType(MYTYPENAME);
+ private static MyTypeTransfer _instance = new MyTypeTransfer();
+
+public static MyTypeTransfer getInstance () {
+ return _instance;
+}
+public void javaToNative (Object object, TransferData transferData) {
+ if (!checkMyType(object) || !isSupportedType(transferData)) {
+ DND.error(DND.ERROR_INVALID_DATA);
+ }
+ MyType[] myTypes = (MyType[]) object;
+ try {
+ // write data to a byte array and then ask super to convert to pMedium
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ DataOutputStream writeOut = new DataOutputStream(out);
+ for (int i = 0, length = myTypes.length; i < length; i++){
+ byte[] buffer = myTypes[i].firstName.getBytes();
+ writeOut.writeInt(buffer.length);
+ writeOut.write(buffer);
+ buffer = myTypes[i].firstName.getBytes();
+ writeOut.writeInt(buffer.length);
+ writeOut.write(buffer);
+ }
+ byte[] buffer = out.toByteArray();
+ writeOut.close();
+ super.javaToNative(buffer, transferData);
+ } catch (IOException e) {
+ }
+}
+public Object nativeToJava(TransferData transferData){
+ if (isSupportedType(transferData)) {
+
+ byte[] buffer = (byte[])super.nativeToJava(transferData);
+ if (buffer == null) return null;
+
+ MyType[] myData = new MyType[0];
+ try {
+ ByteArrayInputStream in = new ByteArrayInputStream(buffer);
+ DataInputStream readIn = new DataInputStream(in);
+ while(readIn.available() > 20) {
+ MyType datum = new MyType();
+ int size = readIn.readInt();
+ byte[] name = new byte[size];
+ readIn.read(name);
+ datum.firstName = new String(name);
+ size = readIn.readInt();
+ name = new byte[size];
+ readIn.read(name);
+ datum.lastName = new String(name);
+ MyType[] newMyData = new MyType[myData.length + 1];
+ System.arraycopy(myData, 0, newMyData, 0, myData.length);
+ newMyData[myData.length] = datum;
+ myData = newMyData;
+ }
+ readIn.close();
+ } catch (IOException ex) {
+ return null;
+ }
+ return myData;
+ }
+
+ return null;
+}
+protected String[] getTypeNames(){
+ return new String[]{MYTYPENAME};
+}
+protected int[] getTypeIds(){
+ return new int[] {MYTYPEID};
+}
+boolean checkMyType(Object object) {
+ if (object == null || !(object instanceof MyType[]) || ((MyType[])object).length == 0) return false;
+ MyType[] myTypes = (MyType[])object;
+ for (int i = 0; i < myTypes.length; i++) {
+ if (myTypes[i] == null ||
+ myTypes[i].firstName == null ||
+ myTypes[i].firstName.length() == 0 ||
+ myTypes[i].lastName == null ||
+ myTypes[i].lastName.length() == 0) return false;
+ }
+ return true;
+}
+protected boolean validate(Object object) {
+ return checkMyType(object);
+}
+}
diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/dnd/ImageTransfer.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/dnd/ImageTransfer.java
index 301b0fd6d5..29aa222800 100644
--- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/dnd/ImageTransfer.java
+++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/dnd/ImageTransfer.java
@@ -31,9 +31,9 @@ public static ImageTransfer getInstance () {
}
public void javaToNative (Object object, TransferData transferData) {
- if (object == null || !(object instanceof ImageData)) return;
- if (!isSupportedType(transferData)) return;
-
+ if (!checkImage(object) || !isSupportedType(transferData)) {
+ DND.error(DND.ERROR_INVALID_DATA);
+ }
ImageData imdata = (ImageData)object;
try {
// write data to a byte array and then ask super to convert to pMedium
@@ -73,4 +73,10 @@ protected String[] getTypeNames(){
protected int[] getTypeIds(){
return new int[] {TYPEID};
}
+boolean checkImage(Object object) {
+ return (object != null && object instanceof ImageData);
+}
+protected boolean validate(Object object) {
+ return checkImage(object);
+}
}
diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet79.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet79.java
index d9fda49163..193510e056 100644
--- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet79.java
+++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet79.java
@@ -43,27 +43,26 @@ public static MyTransfer getInstance () {
}
public void javaToNative (Object object, TransferData transferData) {
- if (object == null || !(object instanceof MyType [])) return;
- if (isSupportedType (transferData)) {
- MyType [] myTypes = (MyType []) object;
- try {
- // write data to a byte array and then ask super to convert to pMedium
- ByteArrayOutputStream out = new ByteArrayOutputStream ();
- DataOutputStream writeOut = new DataOutputStream (out);
- for (int i = 0, length = myTypes.length; i < length; i++) {
- byte [] buffer = myTypes [i].fileName.getBytes ();
- writeOut.writeInt (buffer.length);
- writeOut.write (buffer);
- writeOut.writeLong (myTypes [i].fileLength);
- writeOut.writeLong (myTypes [i].lastModified);
- }
- byte [] buffer = out.toByteArray ();
- writeOut.close ();
-
- super.javaToNative (buffer, transferData);
+ if (!checkMyType(object) || !isSupportedType (transferData)) {
+ DND.error(DND.ERROR_INVALID_DATA);
+ }
+ MyType [] myTypes = (MyType []) object;
+ try {
+ // write data to a byte array and then ask super to convert to pMedium
+ ByteArrayOutputStream out = new ByteArrayOutputStream ();
+ DataOutputStream writeOut = new DataOutputStream (out);
+ for (int i = 0, length = myTypes.length; i < length; i++) {
+ byte [] buffer = myTypes [i].fileName.getBytes ();
+ writeOut.writeInt (buffer.length);
+ writeOut.write (buffer);
+ writeOut.writeLong (myTypes [i].fileLength);
+ writeOut.writeLong (myTypes [i].lastModified);
}
- catch (IOException e) {}
+ byte [] buffer = out.toByteArray ();
+ writeOut.close ();
+ super.javaToNative (buffer, transferData);
}
+ catch (IOException e) {}
}
public Object nativeToJava (TransferData transferData) {
@@ -106,6 +105,27 @@ protected String [] getTypeNames () {
protected int [] getTypeIds () {
return new int [] {MYTYPEID};
}
+
+boolean checkMyType(Object object) {
+ if (object == null ||
+ !(object instanceof MyType[]) ||
+ ((MyType[])object).length == 0) {
+ return false;
+ }
+ MyType[] myTypes = (MyType[])object;
+ for (int i = 0; i < myTypes.length; i++) {
+ if (myTypes[i] == null ||
+ myTypes[i].fileName == null ||
+ myTypes[i].fileName.length() == 0) {
+ return false;
+ }
+ }
+ return true;
+}
+
+protected boolean validate(Object object) {
+ return checkMyType(object);
+}
}
public static void main (String [] args) {

Back to the top