Debian Codegraph @

demo/src/main/java/DCHTest.java

raw
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 DCHTest {
15 private FileDataSource fds = null;
16 private DataHandler dh = null;
17 private DataContentHandlerFactory dchf = null;
18 /**
19 * main function
20 */
21 public static void main(String args[]) {
22 DCHTest test = new DCHTest();
23
24 if(args.length == 0) {
25 System.out.println("usage: DCHTest file.txt");
26 System.exit(1);
27 }
28
29 // first let's get a DataSource
30
31 test.setFile(args[0]);
32
33 test.doit();
34 }
35
36 private void doit() {
37 ActivationDataFlavor xfer_flavors[];
38 Object content = null;
39
40 // now let's create a DataHandler
41 dh = new DataHandler(fds);
42 System.out.println("DCHTest: DataHandler created");
43
44 // now lets set a DataContentHandlerFactory
45 dchf = new SimpleDCF("text/plain:PlainDCH\n");
46 System.out.println("DCHTest: Simple dchf created");
47
48 // now let's set the dchf in the dh
49 dh.setDataContentHandlerFactory(dchf);
50 System.out.println("DCHTest: DataContentHandlerFactory set in DataHandler");
51
52 // get the dataflavors
53 xfer_flavors = dh.getTransferDataFlavors();
54 System.out.println("DCHTest: dh.getTransferDF returned " +
55 xfer_flavors.length + " data flavors.");
56
57 // get the content:
58 try {
59 content = dh.getContent();
60 } catch (Exception e) { e.printStackTrace(); }
61
62 if(content == null)
63 System.out.println("DCHTest: no content to be had!!!");
64 else
65 System.out.println("DCHTest: got content of the following type: " +
66 content.getClass().getName());
67
68 }
69
70 /**
71 * set the file
72 */
73 public void setFile(String filename) {
74 fds = new FileDataSource(filename);
75 System.out.println("DCHTest: FileDataSource created");
76 if(!fds.getContentType().equals("text/plain")) {
77 System.out.println("Type must be text/plain");
78 System.exit(1);
79 }
80 }
81
82 }
83
84