/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package clips.delegate.doctor.checkup;
import cli_fmw.delegate.directory.complex.DirectoryIntelliWriter;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import cli_fmw.main.ClipsException;
import cli_fmw.utils.Base64;
import clips.delegate.directory.simple.iwtype.DirectoryIWType;
import clips.delegate.directory.simple.iwtype.DirectoryIWTypeItem;
import com.keypoint.PngEncoder;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
*
* @author vip
*/
public class FieldForXML implements Cloneable {
public int id;
public String title;
private DirectoryIWType directory;
private final Type type;
private String data;
private Object object;
public enum Type {
text, image;
}
public FieldForXML(Type type) throws ClipsException {
this.directory = DirectoryLocator.getDirectory(DirectoryIWType.class);
this.type = type;
}
public FieldForXML() throws ClipsException {
this(Type.text);
}
public Type getType() {
return type;
}
public void setData(String data) throws ClipsException {
this.data = data;
if (type == Type.image) {
try {
// BASE64Decoder e64Decoder = new BASE64Decoder();
// byte[] decodeBuffer = e64Decoder.decodeBuffer(data);
byte[] decodeBuffer = Base64.decode(data);
object = ImageIO.read(new ByteArrayInputStream(decodeBuffer));
} catch (IOException ex) {
throw new ClipsException("Невозможно декодировать изображение", ex);
}
} else if (type == Type.text) {
object = data;
}
}
public String getData() {
return data;
}
public void setObject(Object object) throws ClipsException {
System.out.println("setObject: " + object);
if (type == Type.text) {
if (!(object instanceof String)) {
throw new ClipsException("Объект не является строкой");
} else {
data = (String) object;
}
}
if (type == Type.image) {
System.out.println("IMAGE");
if (!(object instanceof Image)) {
throw new ClipsException("Объект не является изображением");
} else {
try {
BufferedImage image;
if (object instanceof BufferedImage) {
image = (BufferedImage) object;
} else {
int width = ((Image) object).getWidth(null);
int height = ((Image) object).getHeight(null);
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics gd = image.getGraphics();
gd.drawImage(((Image) object), 0, 0, null);
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
encoder.encode(image);
byte[] byteArray = os.toByteArray();
// BASE64Encoder e64Encoder = new BASE64Encoder();
// data = e64Encoder.encode(byteArray);
data = Base64.encodeBytes(byteArray);
} catch (ImageFormatException ex) {
throw new ClipsException("Неверный формат изображения", ex);
} catch (Exception ex) {
throw new ClipsException("Ошибка чтения", ex);
}
}
}
this.object = object;
}
public <T> T getObject() {
return (T) object;
}
/**
*
* @return may be null (if id == 0)
* @throws cli_fmw.main.ClipsException
*/
public Set<String> getIntelliItems() throws ClipsException {
if (id == 0) {
return null;
}
DirectoryIWTypeItem type = directory.getItemFromID(id);
if (type == null) {
throw new ClipsException("no such IntelliWriter directory: " + id);
}
DirectoryIntelliWriter dir = DirectoryLocator.getDirectory(DirectoryIntelliWriter.class);
return dir.getItems(type);
}
public DirectoryIWTypeItem getIWType() throws ClipsException {
return directory.getItemFromID(id);
}
@Override
public FieldForXML clone() {
try {
FieldForXML ffxml = (FieldForXML) super.clone();
ffxml.id = id;
ffxml.title = new String(title);
return ffxml;
} catch (CloneNotSupportedException ex) {
ex.printStackTrace();
return null;
}
}
}