DataHandler.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /* DataHandler.java -- Handler for data available in multiple formats.
  2. Copyright (C) 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.activation;
  32. import java.awt.datatransfer.DataFlavor;
  33. import java.awt.datatransfer.Transferable;
  34. import java.awt.datatransfer.UnsupportedFlavorException;
  35. import java.io.InputStream;
  36. import java.io.IOException;
  37. import java.io.OutputStream;
  38. import java.io.PipedInputStream;
  39. import java.io.PipedOutputStream;
  40. import java.net.URL;
  41. /**
  42. * Handler for data available in multiple sources and formats.
  43. *
  44. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  45. * @version 1.1
  46. */
  47. public class DataHandler
  48. implements Transferable
  49. {
  50. private static final DataFlavor[] NO_FLAVORS = new DataFlavor[0];
  51. private static DataContentHandlerFactory factory = null;
  52. private final DataSource dataSource;
  53. private DataSource objDataSource;
  54. private Object object;
  55. private String objectMimeType;
  56. private CommandMap currentCommandMap;
  57. private DataFlavor[] transferFlavors = NO_FLAVORS;
  58. private DataContentHandler dataContentHandler;
  59. private DataContentHandler factoryDCH;
  60. private DataContentHandlerFactory oldFactory;
  61. private String shortType;
  62. /**
  63. * Constructor in which the data is read from a data source.
  64. * @param ds the data source
  65. */
  66. public DataHandler(DataSource ds)
  67. {
  68. dataSource = ds;
  69. oldFactory = factory;
  70. }
  71. /**
  72. * Constructor using a reified object representation.
  73. * @param obj the object representation of the data
  74. * @param mimeType the MIME type of the object
  75. */
  76. public DataHandler(Object obj, String mimeType)
  77. {
  78. dataSource = null;
  79. object = obj;
  80. objectMimeType = mimeType;
  81. oldFactory = factory;
  82. }
  83. /**
  84. * Constructor in which the data is read from a URL.
  85. * @param url the URL
  86. */
  87. public DataHandler(URL url)
  88. {
  89. dataSource = new URLDataSource(url);
  90. oldFactory = factory;
  91. }
  92. /**
  93. * Returns the data source from which data is read.
  94. */
  95. public DataSource getDataSource()
  96. {
  97. if (dataSource != null)
  98. {
  99. return dataSource;
  100. }
  101. if (objDataSource == null)
  102. {
  103. objDataSource = new DataHandlerDataSource(this);
  104. }
  105. return objDataSource;
  106. }
  107. /**
  108. * Returns the name of the data object if created with a DataSource.
  109. */
  110. public String getName()
  111. {
  112. if (dataSource != null)
  113. {
  114. return dataSource.getName();
  115. }
  116. return null;
  117. }
  118. /**
  119. * Returns the MIME type of the data (with parameters).
  120. */
  121. public String getContentType()
  122. {
  123. if (dataSource != null)
  124. {
  125. return dataSource.getContentType();
  126. }
  127. return objectMimeType;
  128. }
  129. /**
  130. * Returns an input stream from which the data can be read.
  131. */
  132. public InputStream getInputStream()
  133. throws IOException
  134. {
  135. if (dataSource != null)
  136. {
  137. return dataSource.getInputStream();
  138. }
  139. DataContentHandler dch = getDataContentHandler();
  140. if (dch == null)
  141. {
  142. throw new UnsupportedDataTypeException("no DCH for MIME type " +
  143. getShortType());
  144. }
  145. if ((dch instanceof ObjectDataContentHandler) &&
  146. ((ObjectDataContentHandler)dch).getDCH() == null)
  147. {
  148. throw new UnsupportedDataTypeException("no object DCH " +
  149. "for MIME type " +
  150. getShortType());
  151. }
  152. PipedOutputStream pos = new PipedOutputStream();
  153. DataContentHandlerWriter dchw =
  154. new DataContentHandlerWriter(dch, object, objectMimeType, pos);
  155. Thread thread = new Thread(dchw, "DataHandler.getInputStream");
  156. thread.start();
  157. return new PipedInputStream(pos);
  158. }
  159. static class DataContentHandlerWriter
  160. implements Runnable
  161. {
  162. DataContentHandler dch;
  163. Object object;
  164. String mimeType;
  165. OutputStream out;
  166. DataContentHandlerWriter(DataContentHandler dch, Object object,
  167. String mimeType, OutputStream out)
  168. {
  169. this.dch = dch;
  170. this.object = object;
  171. this.mimeType = mimeType;
  172. this.out = out;
  173. }
  174. public void run()
  175. {
  176. try
  177. {
  178. dch.writeTo(object, mimeType, out);
  179. }
  180. catch(IOException e)
  181. {
  182. }
  183. finally
  184. {
  185. try
  186. {
  187. out.close();
  188. }
  189. catch(IOException e)
  190. {
  191. }
  192. }
  193. }
  194. }
  195. /**
  196. * Writes the data as a byte stream.
  197. * @param os the stream to write to
  198. */
  199. public void writeTo(OutputStream os)
  200. throws IOException
  201. {
  202. if (dataSource != null)
  203. {
  204. InputStream in = dataSource.getInputStream();
  205. byte[] buf = new byte[8192];
  206. for (int len = in.read(buf); len != -1; len = in.read(buf))
  207. {
  208. os.write(buf, 0, len);
  209. }
  210. in.close();
  211. }
  212. else
  213. {
  214. DataContentHandler dch = getDataContentHandler();
  215. dch.writeTo(object, objectMimeType, os);
  216. }
  217. }
  218. /**
  219. * Returns an output stream that can be used to overwrite the underlying
  220. * data, if the DataSource constructor was used.
  221. */
  222. public OutputStream getOutputStream()
  223. throws IOException
  224. {
  225. if (dataSource != null)
  226. {
  227. return dataSource.getOutputStream();
  228. }
  229. return null;
  230. }
  231. /**
  232. * Returns the data flavors in which this data is available.
  233. */
  234. public synchronized DataFlavor[] getTransferDataFlavors()
  235. {
  236. if (factory != oldFactory || transferFlavors == NO_FLAVORS)
  237. {
  238. DataContentHandler dch = getDataContentHandler();
  239. transferFlavors = dch.getTransferDataFlavors();
  240. }
  241. return transferFlavors;
  242. }
  243. /**
  244. * Indicates whether the specified data flavor is supported for this
  245. * data.
  246. */
  247. public boolean isDataFlavorSupported(DataFlavor flavor)
  248. {
  249. DataFlavor[] flavors = getTransferDataFlavors();
  250. for (int i = 0; i < flavors.length; i++)
  251. {
  252. if (flavors[i].equals(flavor))
  253. {
  254. return true;
  255. }
  256. }
  257. return false;
  258. }
  259. /**
  260. * Returns an object representing the data to be transferred.
  261. * @param flavor the requested data flavor
  262. */
  263. public Object getTransferData(DataFlavor flavor)
  264. throws UnsupportedFlavorException, IOException
  265. {
  266. DataContentHandler dch = getDataContentHandler();
  267. return dch.getTransferData(flavor, dataSource);
  268. }
  269. /**
  270. * Sets the command map to be used by this data handler.
  271. * Setting to null uses the default command map.
  272. * @param commandMap the command map to use
  273. */
  274. public synchronized void setCommandMap(CommandMap commandMap)
  275. {
  276. if (commandMap != currentCommandMap || commandMap == null)
  277. {
  278. transferFlavors = NO_FLAVORS;
  279. dataContentHandler = null;
  280. currentCommandMap = commandMap;
  281. }
  282. }
  283. /**
  284. * Returns the preferred commands for this type of data.
  285. */
  286. public CommandInfo[] getPreferredCommands()
  287. {
  288. CommandMap commandMap = getCommandMap();
  289. return commandMap.getPreferredCommands(getShortType());
  290. }
  291. /**
  292. * Returns the complete list of commands for this type of data.
  293. */
  294. public CommandInfo[] getAllCommands()
  295. {
  296. CommandMap commandMap = getCommandMap();
  297. return commandMap.getAllCommands(getShortType());
  298. }
  299. /**
  300. * Returns the specified command.
  301. * @param cmdName the command name
  302. */
  303. public CommandInfo getCommand(String cmdName)
  304. {
  305. CommandMap commandMap = getCommandMap();
  306. return commandMap.getCommand(getShortType(), cmdName);
  307. }
  308. /**
  309. * Returns the data as a reified object.
  310. */
  311. public Object getContent()
  312. throws IOException
  313. {
  314. DataContentHandler dch = getDataContentHandler();
  315. return dch.getContent(getDataSource());
  316. }
  317. /**
  318. * Returns the instantiated bean using the specified command.
  319. * @param cmdInfo the command to instantiate the bean with
  320. */
  321. public Object getBean(CommandInfo cmdInfo)
  322. {
  323. try
  324. {
  325. return cmdInfo.getCommandObject(this, getClass().getClassLoader());
  326. }
  327. catch (IOException e)
  328. {
  329. e.printStackTrace(System.err);
  330. return null;
  331. }
  332. catch (ClassNotFoundException e)
  333. {
  334. e.printStackTrace(System.err);
  335. return null;
  336. }
  337. }
  338. /**
  339. * Sets the data content handler factory.
  340. * If the factory has already been set, throws an Error.
  341. * @param newFactory the factory to set
  342. */
  343. public static synchronized void
  344. setDataContentHandlerFactory(DataContentHandlerFactory newFactory)
  345. {
  346. if (factory != null)
  347. {
  348. throw new Error("DataContentHandlerFactory already defined");
  349. }
  350. SecurityManager security = System.getSecurityManager();
  351. if (security != null)
  352. {
  353. try
  354. {
  355. security.checkSetFactory();
  356. }
  357. catch (SecurityException e)
  358. {
  359. if (newFactory != null && DataHandler.class.getClassLoader()
  360. != newFactory.getClass().getClassLoader())
  361. {
  362. throw e;
  363. }
  364. }
  365. }
  366. factory = newFactory;
  367. }
  368. /*
  369. * Returns just the base part of the data's content-type, with no
  370. * parameters.
  371. */
  372. private synchronized String getShortType()
  373. {
  374. if (shortType == null)
  375. {
  376. String contentType = getContentType();
  377. try
  378. {
  379. MimeType mimeType = new MimeType(contentType);
  380. shortType = mimeType.getBaseType();
  381. }
  382. catch (MimeTypeParseException e)
  383. {
  384. shortType = contentType;
  385. }
  386. }
  387. return shortType;
  388. }
  389. /*
  390. * Returns the command map for this handler.
  391. */
  392. private synchronized CommandMap getCommandMap()
  393. {
  394. if (currentCommandMap != null)
  395. {
  396. return currentCommandMap;
  397. }
  398. return CommandMap.getDefaultCommandMap();
  399. }
  400. /*
  401. * Returns the DCH for this handler.
  402. */
  403. private synchronized DataContentHandler getDataContentHandler()
  404. {
  405. if (factory != oldFactory)
  406. {
  407. oldFactory = factory;
  408. factoryDCH = null;
  409. dataContentHandler = null;
  410. transferFlavors = NO_FLAVORS;
  411. }
  412. if (dataContentHandler != null)
  413. {
  414. return dataContentHandler;
  415. }
  416. String mimeType = getShortType();
  417. if (factoryDCH == null && factory != null)
  418. {
  419. factoryDCH = factory.createDataContentHandler(mimeType);
  420. }
  421. if (factoryDCH != null)
  422. {
  423. dataContentHandler = factoryDCH;
  424. }
  425. if (dataContentHandler == null)
  426. {
  427. CommandMap commandMap = getCommandMap();
  428. dataContentHandler = commandMap.createDataContentHandler(mimeType);
  429. }
  430. if (dataSource != null)
  431. {
  432. dataContentHandler =
  433. new DataSourceDataContentHandler(dataContentHandler, dataSource);
  434. }
  435. else
  436. {
  437. dataContentHandler =
  438. new ObjectDataContentHandler(dataContentHandler, object,
  439. objectMimeType);
  440. }
  441. return dataContentHandler;
  442. }
  443. }