| 1 |
/* |
| 2 |
* Copyright (c) 1997, 2019 Oracle and/or its affiliates. All rights reserved. |
| 3 |
* |
| 4 |
* This program and the accompanying materials are made available under the |
| 5 |
* terms of the Eclipse Distribution License v. 1.0, which is available at |
| 6 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 7 |
* |
| 8 |
* SPDX-License-Identifier: BSD-3-Clause |
| 9 |
*/ |
| 10 |
|
| 11 |
import java.io.*; |
| 12 |
import jakarta.activation.*; |
| 13 |
|
| 14 |
public class DCHTest2 { |
| 15 |
private FileDataSource fds = null; |
| 16 |
private MailcapCommandMap cmdMap = null; |
| 17 |
private DataHandler dh = null; |
| 18 |
private DataContentHandlerFactory dchf = null; |
| 19 |
/** |
| 20 |
* main function |
| 21 |
*/ |
| 22 |
public static void main(String args[]) { |
| 23 |
DCHTest2 test = new DCHTest2(); |
| 24 |
|
| 25 |
if(args.length < 2) { |
| 26 |
System.out.println("usage: DCHTest2 file.txt mailcap"); |
| 27 |
System.exit(1); |
| 28 |
} |
| 29 |
|
| 30 |
// first let's get a DataSource |
| 31 |
|
| 32 |
test.setFile(args[0]); |
| 33 |
|
| 34 |
test.setMailcap(args[1]); |
| 35 |
test.doit(); |
| 36 |
} |
| 37 |
|
| 38 |
private void doit() { |
| 39 |
ActivationDataFlavor xfer_flavors[]; |
| 40 |
Object content = null; |
| 41 |
|
| 42 |
// now let's create a DataHandler |
| 43 |
dh = new DataHandler(fds); |
| 44 |
dh.setCommandMap(cmdMap); |
| 45 |
System.out.println("DCHTest2: DataHandler created"); |
| 46 |
|
| 47 |
// get the dataflavors |
| 48 |
xfer_flavors = dh.getTransferDataFlavors(); |
| 49 |
System.out.println("DCHTest2: dh.getTransferDF returned " + |
| 50 |
xfer_flavors.length + " data flavors."); |
| 51 |
|
| 52 |
// get the content: |
| 53 |
try { |
| 54 |
content = dh.getContent(); |
| 55 |
} catch (Exception e) { e.printStackTrace(); } |
| 56 |
if(content == null) |
| 57 |
System.out.println("DCHTest2: no content to be had!!!"); |
| 58 |
else |
| 59 |
System.out.println("DCHTest2: got content of the following type: " + |
| 60 |
content.getClass().getName()); |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* set the file |
| 66 |
*/ |
| 67 |
public void setFile(String filename) { |
| 68 |
fds = new FileDataSource(filename); |
| 69 |
System.out.println("DCHTest2: FileDataSource created"); |
| 70 |
if(!fds.getContentType().equals("text/plain")) { |
| 71 |
System.out.println("Type must be text/plain"); |
| 72 |
System.exit(1); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* set the mailcap file in the CMD Map |
| 78 |
*/ |
| 79 |
public void setMailcap(String mailcap) { |
| 80 |
|
| 81 |
try { |
| 82 |
cmdMap = new MailcapCommandMap(mailcap); |
| 83 |
} catch(Exception e){ |
| 84 |
e.printStackTrace(); |
| 85 |
System.exit(1); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
|