Alternative zu switch case in Java

Programmierung unter AOO/LO (StarBasic, Python, Java, ...)

Moderator: Moderatoren

AlemannenLady
*
Beiträge: 14
Registriert: Do, 25.01.2007 15:37

Alternative zu switch case in Java

Beitrag von AlemannenLady »

Hi Leute,

ich habe folgendes gebaut:

ich habe in einer Textdatei verschiedene Dateipfade stehen. Diese lese ich in Java aus und splitte sie, indem ich Dateinamen ermittel und die Dateiendung. Die Dateiendung ist für mich deshalb wichtig zu wissen, damit ich damit weiterarbeiten kann und den richtigen DocumentType wie SpreadsheetDocument oder TextDocument öffnen kann.

Fakt ist, ich habe Endungen wie txt, doc, xls, ppt, odt, ods, csv, odp, etc...
Nun möchte - da ich verschiedene Prozeduren für jeden DocumentType geschrieben habe - vorher eine Abfrage starten, die ich mir eigentlich als switch-case vorgestellt habe. "ist die Endung ppt, dann öffne die Prozedur für PresentationDocument". Die Dateiendungen werden beizeiten mehr werden, ich habe mir nur die haupten Endungen herausgepickt. Frage ist, wie ich nun vorgehen kann.

Hier mein Code (wenn's hilft ;))

public class ClsMain {

public static void main(String[] args) throws IOException {

String officeHome = "C:\\Programme\\OpenOffice.org 2.1\\";
String Pfade = "C:\\Dokumente und Einstellungen\\name.WY.000\\Desktop\\Pfade.txt";

// Datei auslesen
FileReader fr = new FileReader(Pfade);
BufferedReader br = new BufferedReader(fr);
String record = null;
try {
while ( (record=br.readLine()) != null ) {
//Dateinamen ermitteln
String dEndung = "";
String dName = "";
char[] chars = new char[ 3 ];
record.getChars(record.length()-4, record.length()-1, chars, 0);
dEndung = new String(chars);
//System.out.println(dEndung); // --> Dateiendung ausgeben
int indBSlash = record.lastIndexOf("\\");
int indDot = record.lastIndexOf(".");
//Berechne die Länge des Dateinamens
int rechne = indDot - (indBSlash+1);
char[] filename = new char [rechne];
record.getChars(indBSlash+1, indDot, filename, 0);
dName = new String(filename);
System.out.println(dName);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

und dann die Prozeduren für die Dokumente zu öffnen und unter anderem Format abzuspeichern:

public static void wrap(String offHome, String indexLis, String htmlExport, IFilter ifil) {
HashMap hashMap = new HashMap(3);
hashMap.put(IOfficeApplication.APPLICATION_TYPE_KEY, IOfficeApplication.LOCAL_APPLICATION);
hashMap.put(IOfficeApplication.APPLICATION_HOME_KEY, offHome);

try {
IOfficeApplication officeApp = OfficeApplicationRuntime.getApplication(hashMap);
officeApp.activate();
ITextDocument txtDoc = (ITextDocument) officeApp.getDocumentService().loadDocument(indexLis);
txtDoc.getPersistenceService().export(htmlExport, ifil);
txtDoc.close();
//officeApp.deactivate();
//officeApp.getDesktopService().terminate();
}
catch (Throwable ta) {
ta.printStackTrace();
}
}

public static void wrap_format(String offHome, String indexLis, String htmlExport, IFilter ifil) {
HashMap hashMap = new HashMap(3);
hashMap.put(IOfficeApplication.APPLICATION_TYPE_KEY, IOfficeApplication.LOCAL_APPLICATION);
hashMap.put(IOfficeApplication.APPLICATION_HOME_KEY, offHome);

try {
IOfficeApplication officeApp = OfficeApplicationRuntime.getApplication(hashMap);
officeApp.activate();
SpreadsheetDocument ssd = (SpreadsheetDocument) officeApp.getDocumentService().loadDocument(indexLis);
ssd.getPersistenceService().export(htmlExport, ifil);
ssd.close();
//officeApp.deactivate();
//officeApp.getDesktopService().terminate();
}
catch (Throwable ta) {
ta.printStackTrace();
}
}

usw.

Any idea?
AlemannenLady
*
Beiträge: 14
Registriert: Do, 25.01.2007 15:37

vorläufige Lösung

Beitrag von AlemannenLady »

also ich habs jetzt so gelöst, begeistert bin ich aber davon nicht...

//Pfad zum Abspeichern
String savePath = "C:\\Dokumente und Einstellungen\\name.WY.000\\Desktop\\" + dName + ".html";
//handelt es sich um ein TextDocument, dann im Writer öffnen
if (dEndung.compareTo("txt") == 0 || dEndung.compareTo("doc") == 0 || dEndung.compareTo("odt") == 0) {
wrap(officeHome, record, savePath, HTMLFilter.FILTER);
}

//handelt es sich um ein Sheet, dann im Calc öffnen
if (dEndung.compareTo("xls") == 0 || dEndung.compareTo("ods") == 0) {
wrap_format(officeHome, record, savePath, HTMLFilter.FILTER);
}

//handelt es sich um eine Präsi, dann im Impress öffnen
if (dEndung.compareTo("ppt") == 0 || dEndung.compareTo("odp") == 0) {
wrap_pres(officeHome, record, savePath, HTMLFilter.FILTER);
}
Antworten