001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017:
018: import java.io.FilterOutputStream;
019: import java.io.OutputStream;
020: import java.lang.reflect.Constructor;
021: import java.util.HashMap;
022:
023: /**
024: * Provides FilterOutputStream, that filters incoming files in {@link SFileChooser} components.
025: *
026: * You can use UploadFilters to inspect the stream or rewrite it to some own format.
027: *
028: * @author Holger Engels
029: */
030: public class UploadFilterManager {
031: private final transient static Log log = LogFactory
032: .getLog(UploadFilterManager.class);
033:
034: private static HashMap filterMappings = new HashMap();
035:
036: public static void registerFilter(String name, Class filter,
037: String errorText) {
038: if (!FilterOutputStream.class.isAssignableFrom(filter))
039: throw new IllegalArgumentException(
040: "class is not a FilterOutputStream!");
041:
042: Entry entry = new Entry(filter);
043: entry.errorText = errorText;
044: filterMappings.put(name, entry);
045: }
046:
047: public static void registerFilter(String name, Class filter) {
048: registerFilter(name, filter, null);
049: }
050:
051: public static Class getFilterClass(String name) {
052: int dividerIndex = name.indexOf(SConstants.UID_DIVIDER);
053: name = name.substring(dividerIndex + 1);
054:
055: return ((Entry) filterMappings.get(name)).filterClass;
056: }
057:
058: public static FilterOutputStream createFilterInstance(String name,
059: OutputStream out) {
060: FilterOutputStream filter = null;
061: try {
062: Entry entry = getFilterEntry(name);
063: if (entry == null)
064: filter = new FilterOutputStream(out);
065: else {
066: Class filterClass = entry.filterClass;
067: if (filterClass != null) {
068: log.info("using " + filterClass.getName() + " for "
069: + name);
070: Constructor constructor = filterClass
071: .getConstructor(new Class[] { OutputStream.class });
072: filter = (FilterOutputStream) constructor
073: .newInstance(new Object[] { out });
074: entry.filterInstance = filter;
075: }
076: }
077: } catch (Exception e) {
078: log.fatal("Exception", e);
079: }
080: return filter;
081: }
082:
083: public static FilterOutputStream getFilterInstance(String name) {
084: try {
085: Entry entry = getFilterEntry(name);
086: FilterOutputStream filterInstance = entry.filterInstance;
087: return filterInstance;
088: } catch (Exception e) {
089: log.fatal(null, e);
090: return null;
091: }
092: }
093:
094: private static Entry getFilterEntry(String name) {
095: int dividerIndex = name.indexOf(SConstants.UID_DIVIDER);
096: name = name.substring(dividerIndex + 1);
097:
098: return (Entry) filterMappings.get(name);
099: }
100:
101: private static class Entry {
102: public Class filterClass;
103: public FilterOutputStream filterInstance;
104: public String errorText;
105:
106: public Entry(Class filterClass) {
107: this .filterClass = filterClass;
108: }
109:
110: public void setInstance(FilterOutputStream filterInstance) {
111: this.filterInstance = filterInstance;
112: }
113: }
114: }
|