//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
*
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.framework.htmlview;
import com.nexirius.framework.datamodel.*;
import com.nexirius.framework.htmlview.translator.*;
import com.nexirius.util.XFile;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Hashtable;
/**
* This class maps a template to an InputStream.
*/
public class HTMLStreamMap implements Serializable {
private Hashtable templateTable = new Hashtable();
private Hashtable editorMap = new Hashtable();
private Hashtable viewerMap = new Hashtable();
public HTMLStreamMap() {
init();
}
private void init() {
addModelTranslator(StructModel.class, new StructModelViewerTranslator());
addModelTranslator(StructModel.class, new StructModelEditorTranslator());
addModelTranslator(SimpleModel.class, new SimpleModelViewerTranslator());
addModelTranslator(SimpleModel.class, new SimpleModelEditorTranslator());
addModelTranslator(ComboBoxModel.class, new ComboBoxModelEditorTranslator());
addModelTranslator(ArrayModel.class, new ArrayModelViewerTranslator());
addModelTranslator(ArrayModel.class, new ArrayModelEditorTranslator());
addModelTranslator(BooleanModel.class, new BooleanModelEditorTranslator());
addModelTranslator(TextModel.class, new TextModelEditorTranslator());
addModelTranslator(TextModel.class, new TextModelViewerTranslator());
}
/**
* translates a template into an input stream. If the template is null then the
* model instance will be directly translated into an input stream
*
* @param model
* @param template
* @return
* @throws Exception
*/
public InputStream getInputStreamFor(DataModel model, String template, boolean isEditor)
throws Exception {
if (template == null) {
return getInputStreamFor(model, isEditor);
}
Object o = templateTable.get(template);
if (o instanceof XFile) {
XFile f = (XFile) o;
return f.getBufferedInputStream();
} else if (o instanceof String) {
return new ByteArrayInputStream(o.toString().getBytes());
}
throw new Exception("Cannot translate template '" + template + "' into an InputStream");
}
public InputStream getInputStreamFor(DataModel model, boolean isEditor) {
ModelToStreamTranslator modelToStreamTranslator = getModelToStreamTranslator(model.getClass(), isEditor);
if (modelToStreamTranslator == null) {
return new ByteArrayInputStream(new String("No model to stream translator for " + model.getClass()).getBytes());
}
return modelToStreamTranslator.translate(model);
}
protected ModelToStreamTranslator getModelToStreamTranslator(Class modelClass, boolean isEditor) {
ModelToStreamTranslator ret = null;
while (modelClass != null) {
if (isEditor) {
ret = (ModelToStreamTranslator) editorMap.get(modelClass);
} else {
ret = (ModelToStreamTranslator) viewerMap.get(modelClass);
}
if (ret != null) {
return ret;
}
modelClass = modelClass.getSuperclass();
}
return ret;
}
public void addFile(String template, XFile file) {
templateTable.put(template, file);
}
public void addString(String template, String s) {
templateTable.put(template, s);
}
public void addEntry(HTMLStreamMapEntry entry) {
if (entry.getFileStream() == null) {
addString(entry.getName(), entry.getStringStream());
} else {
addFile(entry.getName(), entry.getFileStream());
}
}
public void addModelTranslator(Class modelClass, ModelToStreamTranslator translator) {
if (translator.isEditor()) {
editorMap.put(modelClass, translator);
} else {
viewerMap.put(modelClass, translator);
}
}
}