| 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 jakarta.activation.*; |
| 12 |
import java.util.StringTokenizer; |
| 13 |
import java.util.Hashtable; |
| 14 |
|
| 15 |
public class SimpleDCF implements DataContentHandlerFactory { |
| 16 |
Hashtable entry_hash = new Hashtable(); |
| 17 |
/** |
| 18 |
* the constructor, takes a list of classes as an argument in the |
| 19 |
* form: |
| 20 |
* <mimetype>:<class name>\n |
| 21 |
* |
| 22 |
* For Example: |
| 23 |
* |
| 24 |
* application/x-wombat:com.womco.WombatDCH |
| 25 |
* text/plain:com.textco.TextDCH |
| 26 |
* |
| 27 |
*/ |
| 28 |
public SimpleDCF(String entries) { |
| 29 |
StringTokenizer tok = new StringTokenizer(entries); |
| 30 |
|
| 31 |
String entry; |
| 32 |
System.out.println("SimpleDCH: new SimpleDCF being created"); |
| 33 |
|
| 34 |
// parse the string |
| 35 |
while(tok.hasMoreTokens()) { |
| 36 |
int colon; |
| 37 |
|
| 38 |
entry = tok.nextToken(); |
| 39 |
System.out.println("full entry = " + entry); |
| 40 |
|
| 41 |
// parse out the fields |
| 42 |
colon = entry.indexOf(':'); |
| 43 |
VectorEntry ve = new VectorEntry(entry.substring(0,colon), |
| 44 |
entry.substring(colon + 1, |
| 45 |
entry.length())); |
| 46 |
System.out.println("adding element = " + ve); |
| 47 |
entry_hash.put(ve.getMimeType(),ve); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* implement the factor interface |
| 53 |
*/ |
| 54 |
public DataContentHandler createDataContentHandler(String mimeType){ |
| 55 |
DataContentHandler dch = null; |
| 56 |
|
| 57 |
System.out.print("SimpleDCF: trying to create a DCH"); |
| 58 |
|
| 59 |
VectorEntry ve = (VectorEntry)entry_hash.get(mimeType); |
| 60 |
if(ve != null) { |
| 61 |
System.out.print("...found token"); |
| 62 |
try { |
| 63 |
|
| 64 |
dch = (DataContentHandler)Class.forName( |
| 65 |
ve.getClassName()).newInstance(); |
| 66 |
if(dch == null) |
| 67 |
System.out.println("...FAILED!!!"); |
| 68 |
else |
| 69 |
System.out.println("...SUCCESS!!!"); |
| 70 |
|
| 71 |
} catch(Exception e) { |
| 72 |
System.out.println(e); |
| 73 |
} |
| 74 |
} |
| 75 |
return dch; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
class VectorEntry { |
| 80 |
private String mimeType; |
| 81 |
private String className; |
| 82 |
|
| 83 |
public VectorEntry(String mimeType, String className) { |
| 84 |
this.mimeType = mimeType; |
| 85 |
this.className = className; |
| 86 |
} |
| 87 |
|
| 88 |
public String getMimeType() { return mimeType; } |
| 89 |
public String getClassName() { return className; } |
| 90 |
public String toString() { |
| 91 |
return new String("type: " + mimeType + " class name: " + className); |
| 92 |
} |
| 93 |
|
| 94 |
} |