001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Pavel Dolgov
019: * @version $Revision$
020: */package org.apache.harmony.awt.datatransfer;
021:
022: import java.awt.Graphics;
023: import java.awt.Image;
024: import java.awt.datatransfer.DataFlavor;
025: import java.awt.datatransfer.SystemFlavorMap;
026: import java.awt.datatransfer.Transferable;
027: import java.awt.image.BufferedImage;
028: import java.awt.image.DataBufferInt;
029: import java.io.ByteArrayOutputStream;
030: import java.io.IOException;
031: import java.io.ObjectOutputStream;
032: import java.io.Reader;
033: import java.io.Serializable;
034: import java.net.URL;
035: import java.util.ArrayList;
036: import java.util.Iterator;
037: import java.util.List;
038:
039: /**
040: * Convertor from {@link java.awt.datatransfer.Transferable} to
041: * {@link org.apache.harmony.awt.datatransfer.DataProvider}
042: */
043: public class DataSource implements DataProvider {
044:
045: // Cached data from transferable object
046: private DataFlavor[] flavors;
047: private List<String> nativeFormats;
048:
049: protected final Transferable contents;
050:
051: public DataSource(Transferable contents) {
052: this .contents = contents;
053: }
054:
055: private boolean isHtmlFlavor(DataFlavor f) {
056: return "html".equalsIgnoreCase(f.getSubType()); //$NON-NLS-1$
057: }
058:
059: protected DataFlavor[] getDataFlavors() {
060: if (flavors == null) {
061: flavors = contents.getTransferDataFlavors();
062: }
063: return flavors;
064: }
065:
066: public String[] getNativeFormats() {
067: return getNativeFormatsList().toArray(new String[0]);
068: }
069:
070: public List<String> getNativeFormatsList() {
071: if (nativeFormats == null) {
072: DataFlavor[] flavors = getDataFlavors();
073: nativeFormats = getNativesForFlavors(flavors);
074: }
075:
076: return nativeFormats;
077: }
078:
079: private static List<String> getNativesForFlavors(
080: DataFlavor[] flavors) {
081: ArrayList<String> natives = new ArrayList<String>();
082:
083: SystemFlavorMap flavorMap = (SystemFlavorMap) SystemFlavorMap
084: .getDefaultFlavorMap();
085:
086: for (int i = 0; i < flavors.length; i++) {
087: List<String> list = flavorMap
088: .getNativesForFlavor(flavors[i]);
089: for (Iterator<String> it = list.iterator(); it.hasNext();) {
090: String nativeFormat = it.next();
091: if (!natives.contains(nativeFormat)) {
092: natives.add(nativeFormat);
093: }
094: }
095: }
096: return natives;
097: }
098:
099: private String getTextFromReader(Reader r) throws IOException {
100: StringBuilder buffer = new StringBuilder();
101: char chunk[] = new char[1024];
102: int len;
103: while ((len = r.read(chunk)) > 0) {
104: buffer.append(chunk, 0, len);
105: }
106: return buffer.toString();
107: }
108:
109: private String getText(boolean htmlOnly) {
110: DataFlavor[] flavors = contents.getTransferDataFlavors();
111: for (int i = 0; i < flavors.length; i++) {
112: DataFlavor f = flavors[i];
113: if (!f.isFlavorTextType()) {
114: continue;
115: }
116: if (htmlOnly && !isHtmlFlavor(f)) {
117: continue;
118: }
119: try {
120: if (String.class.isAssignableFrom(f
121: .getRepresentationClass())) {
122: return (String) contents.getTransferData(f);
123: }
124: Reader r = f.getReaderForText(contents);
125: return getTextFromReader(r);
126: } catch (Exception e) {
127: }
128: }
129: return null;
130: }
131:
132: public String getText() {
133: return getText(false);
134: }
135:
136: public String[] getFileList() {
137: try {
138: List<?> list = (List<?>) contents
139: .getTransferData(DataFlavor.javaFileListFlavor);
140: return list.toArray(new String[list.size()]);
141: } catch (Exception e) {
142: return null;
143: }
144: }
145:
146: public String getURL() {
147: try {
148: URL url = (URL) contents.getTransferData(urlFlavor);
149: return url.toString();
150: } catch (Exception e) {
151: }
152: try {
153: URL url = (URL) contents.getTransferData(uriFlavor);
154: return url.toString();
155: } catch (Exception e) {
156: }
157: try {
158: URL url = new URL(getText());
159: return url.toString();
160: } catch (Exception e) {
161: }
162: return null;
163: }
164:
165: public String getHTML() {
166: return getText(true);
167: }
168:
169: public RawBitmap getRawBitmap() {
170: DataFlavor[] flavors = contents.getTransferDataFlavors();
171:
172: for (int i = 0; i < flavors.length; i++) {
173: DataFlavor f = flavors[i];
174: Class<?> c = f.getRepresentationClass();
175: if (c != null
176: && Image.class.isAssignableFrom(c)
177: && (f.isMimeTypeEqual(DataFlavor.imageFlavor) || f
178: .isFlavorSerializedObjectType())) {
179: try {
180: Image im = (Image) contents.getTransferData(f);
181: return getImageBitmap(im);
182: } catch (Throwable ex) {
183: continue;
184: }
185: }
186: }
187: return null;
188: }
189:
190: private RawBitmap getImageBitmap(Image im) {
191: if (im instanceof BufferedImage) {
192: BufferedImage bi = (BufferedImage) im;
193: if (bi.getType() == BufferedImage.TYPE_INT_RGB) {
194: return getImageBitmap32(bi);
195: }
196: }
197: int width = im.getWidth(null);
198: int height = im.getHeight(null);
199: if (width <= 0 || height <= 0) {
200: return null;
201: }
202: BufferedImage bi = new BufferedImage(width, height,
203: BufferedImage.TYPE_INT_RGB);
204: Graphics gr = bi.getGraphics();
205: gr.drawImage(im, 0, 0, null);
206: gr.dispose();
207: return getImageBitmap32(bi);
208: }
209:
210: private RawBitmap getImageBitmap32(BufferedImage bi) {
211: int buffer[] = new int[bi.getWidth() * bi.getHeight()];
212: DataBufferInt data = (DataBufferInt) bi.getRaster()
213: .getDataBuffer();
214: int bufferPos = 0;
215: int bankCount = data.getNumBanks();
216: int offsets[] = data.getOffsets();
217: for (int i = 0; i < bankCount; i++) {
218: int[] fragment = data.getData(i);
219: System.arraycopy(fragment, offsets[i], buffer, bufferPos,
220: fragment.length - offsets[i]);
221: bufferPos += fragment.length - offsets[i];
222: }
223: return new RawBitmap(bi.getWidth(), bi.getHeight(), bi
224: .getWidth(), 32, 0xFF0000, 0xFF00, 0xFF, buffer);
225: }
226:
227: public byte[] getSerializedObject(Class<?> clazz) {
228: try {
229: DataFlavor f = new DataFlavor(clazz, null);
230: Serializable s = (Serializable) contents.getTransferData(f);
231: ByteArrayOutputStream bytes = new ByteArrayOutputStream();
232: new ObjectOutputStream(bytes).writeObject(s);
233: return bytes.toByteArray();
234: } catch (Throwable e) {
235: return null;
236: }
237: }
238:
239: public boolean isNativeFormatAvailable(String nativeFormat) {
240: return getNativeFormatsList().contains(nativeFormat);
241: }
242: }
|