001: /*
002: * Copyright (c) 1998-2007 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Emil Ong
028: */
029:
030: package com.caucho.jaxb.property;
031:
032: import java.awt.Image;
033: import java.awt.image.*;
034: import javax.imageio.*;
035: import javax.imageio.stream.*;
036:
037: import javax.xml.XMLConstants;
038: import javax.xml.bind.DatatypeConverter;
039: import javax.xml.bind.JAXBException;
040: import javax.xml.bind.Marshaller;
041: import javax.xml.namespace.QName;
042: import javax.xml.stream.XMLStreamException;
043: import javax.xml.stream.XMLStreamWriter;
044:
045: import java.io.ByteArrayInputStream;
046: import java.io.ByteArrayOutputStream;
047: import java.io.IOException;
048: import java.io.InputStream;
049: import java.io.OutputStream;
050:
051: import java.util.HashMap;
052: import java.util.Iterator;
053:
054: import com.caucho.util.Attachment;
055: import com.caucho.util.Base64;
056: import com.caucho.util.L10N;
057:
058: /**
059: * an Image Property
060: */
061: public class ImageProperty extends CDataProperty implements
062: AttachmentProperty {
063: public static final QName SCHEMA_TYPE = new QName(
064: XMLConstants.W3C_XML_SCHEMA_NS_URI, "base64Binary", "xsd");
065:
066: private static final L10N L = new L10N(ImageProperty.class);
067: private static final String DEFAULT_IMAGE_MIME_TYPE = "image/png";
068: private static final HashMap<String, ImageProperty> _mimeTypePropertyMap = new HashMap<String, ImageProperty>();
069:
070: private String _mimeType;
071: private ImageReader _imageReader;
072: private ImageWriter _imageWriter;
073:
074: public static ImageProperty getImageProperty(String mimeType)
075: throws JAXBException {
076: ImageProperty property = _mimeTypePropertyMap.get(mimeType);
077:
078: if (property == null) {
079: if (mimeType == null) {
080: property = new ImageProperty(DEFAULT_IMAGE_MIME_TYPE);
081: _mimeTypePropertyMap.put(DEFAULT_IMAGE_MIME_TYPE,
082: property);
083: } else
084: property = new ImageProperty(mimeType);
085:
086: _mimeTypePropertyMap.put(mimeType, property);
087: }
088:
089: return property;
090: }
091:
092: private ImageProperty(String mimeType) throws JAXBException {
093: int slash = mimeType.indexOf('/');
094:
095: if (slash < 0)
096: throw new JAXBException(L.l("Unrecognized MIME type : {0}",
097: mimeType));
098:
099: String formatName = mimeType.substring(slash + 1);
100:
101: Iterator writers = ImageIO
102: .getImageWritersByFormatName(formatName);
103:
104: if (!writers.hasNext())
105: throw new JAXBException(L.l("Unrecognized MIME type : {0}",
106: mimeType));
107:
108: _mimeType = mimeType;
109: _imageWriter = (ImageWriter) writers.next();
110: _imageReader = ImageIO.getImageReader(_imageWriter);
111:
112: if (_imageReader == null)
113: throw new JAXBException(L.l("Unrecognized MIME type : {0}",
114: mimeType));
115: }
116:
117: public String write(Object in) throws IOException {
118: ByteArrayOutputStream baos = new ByteArrayOutputStream();
119: MemoryCacheImageOutputStream out = new MemoryCacheImageOutputStream(
120: baos);
121:
122: _imageWriter.setOutput(out);
123: _imageWriter.write((RenderedImage) in);
124:
125: out.flush();
126:
127: return Base64.encodeFromByteArray(baos.toByteArray());
128: }
129:
130: protected Object read(String in) throws IOException {
131: byte[] buffer = Base64.decodeToByteArray(in);
132: ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
133: MemoryCacheImageInputStream is = new MemoryCacheImageInputStream(
134: bais);
135:
136: _imageReader.setInput(is);
137:
138: return _imageReader.read(0);
139: }
140:
141: public QName getSchemaType() {
142: return SCHEMA_TYPE;
143: }
144:
145: public String getMimeType(Object obj) {
146: return _mimeType;
147: }
148:
149: public void writeAsAttachment(Object obj, OutputStream out)
150: throws IOException {
151: MemoryCacheImageOutputStream iout = new MemoryCacheImageOutputStream(
152: out);
153:
154: _imageWriter.setOutput(iout);
155: _imageWriter.write((RenderedImage) obj);
156:
157: iout.flush();
158: }
159:
160: public Object readFromAttachment(Attachment attachment)
161: throws IOException {
162: InputStream is = new ByteArrayInputStream(attachment
163: .getRawContents());
164: MemoryCacheImageInputStream iis = new MemoryCacheImageInputStream(
165: is);
166:
167: _imageReader.setInput(iis);
168:
169: return _imageReader.read(0);
170: }
171: }
|