/wonderland/core/src/classes/org/jdesktop/wonderland/client/content/ContentImportManager.java

https://github.com/jehc/MondocosmOS · Java · 172 lines · 59 code · 17 blank · 96 comment · 19 complexity · 536cfa9e74fae058979d98b49e5c9236 MD5 · raw file

  1. /**
  2. * Open Wonderland
  3. *
  4. * Copyright (c) 2011, Open Wonderland Foundation, All Rights Reserved
  5. *
  6. * Redistributions in source code form must reproduce the above
  7. * copyright and this condition.
  8. *
  9. * The contents of this file are subject to the GNU General Public
  10. * License, Version 2 (the "License"); you may not use this file
  11. * except in compliance with the License. A copy of the License is
  12. * available at http://www.opensource.org/licenses/gpl-license.php.
  13. *
  14. * The Open Wonderland Foundation designates this particular file as
  15. * subject to the "Classpath" exception as provided by the Open Wonderland
  16. * Foundation in the License file that accompanied this code.
  17. */
  18. /**
  19. * Project Wonderland
  20. *
  21. * Copyright (c) 2004-2009, Sun Microsystems, Inc., All Rights Reserved
  22. *
  23. * Redistributions in source code form must reproduce the above
  24. * copyright and this condition.
  25. *
  26. * The contents of this file are subject to the GNU General Public
  27. * License, Version 2 (the "License"); you may not use this file
  28. * except in compliance with the License. A copy of the License is
  29. * available at http://www.opensource.org/licenses/gpl-license.php.
  30. *
  31. * Sun designates this particular file as subject to the "Classpath"
  32. * exception as provided by Sun in the License file that accompanied
  33. * this code.
  34. */
  35. package org.jdesktop.wonderland.client.content;
  36. import java.util.HashMap;
  37. import java.util.Map;
  38. import org.jdesktop.wonderland.client.content.spi.ContentImporterSPI;
  39. import org.jdesktop.wonderland.common.ExperimentalAPI;
  40. /**
  41. * Manages the collection import handlers for different kinds of content. Each
  42. * content type is given by the file extension, as returned by the class that
  43. * implements the ContentImportSPI. There can also be a "default" content
  44. * handler for all file extensions not registered.
  45. *
  46. * @author Jordan Slott <jslott@dev.java.net>
  47. */
  48. @ExperimentalAPI
  49. public class ContentImportManager {
  50. /* A map of content extensions and their import handlers */
  51. private Map<String, ContentImporterSPI> contentImportMap = new HashMap();
  52. /* The default content handler for imports, can be null */
  53. private ContentImporterSPI defaultContentImporter = null;
  54. /** Default constructor */
  55. private ContentImportManager() {
  56. }
  57. /**
  58. * Singleton to hold instance of DragAndDropMananger. This holder class is
  59. * loader on the first execution of DragAndDropManager.getDragAndDropManager().
  60. */
  61. private static class ContentImportManagerHolder {
  62. private final static ContentImportManager dndManager = new ContentImportManager();
  63. }
  64. /**
  65. * Returns a single instance of this class
  66. * <p>
  67. * @return Single instance of this class.
  68. */
  69. public static final ContentImportManager getContentImportManager() {
  70. return ContentImportManagerHolder.dndManager;
  71. }
  72. /**
  73. * Sets the default content importer, overrides any previously set.
  74. *
  75. * @param importer The content import handler
  76. */
  77. public void setDefaultContentImporter(ContentImporterSPI importer) {
  78. defaultContentImporter = importer;
  79. }
  80. /**
  81. * Get the current default content importer.
  82. *
  83. * @return importer the current default content importer, or null if there
  84. * is no default content importer.
  85. */
  86. public ContentImporterSPI getDefaultContentImporter() {
  87. return defaultContentImporter;
  88. }
  89. /**
  90. * Registers a handler for content import. A content import handler handles
  91. * when an items is to be imported into the world with a specific file
  92. * extension. Only one import handler is permitted per file extension
  93. *
  94. * @param importer The content importer
  95. */
  96. public void registerContentImporter(ContentImporterSPI importer) {
  97. // For each of the extensions that are supported by the handler, add
  98. // then to the map. If the extension type is already registered, then
  99. // overwrite.
  100. String extensions[] = importer.getExtensions();
  101. if (extensions != null) {
  102. for (String extension : extensions) {
  103. if (extension == null) {
  104. continue;
  105. }
  106. contentImportMap.put(extension.toLowerCase(), importer);
  107. }
  108. }
  109. }
  110. /**
  111. * Unregisters a handler for content import.
  112. * @param importer The content importer
  113. */
  114. public void unregisterContentImporter(ContentImporterSPI importer) {
  115. // For each of the extensions that are supported by the handler, add
  116. // then to the map. If the extension type is already registered, then
  117. // overwrite.
  118. String extensions[] = importer.getExtensions();
  119. if (extensions != null) {
  120. for (String extension : extensions) {
  121. if (extension == null) {
  122. continue;
  123. }
  124. ContentImporterSPI curImporter = contentImportMap.get(extension.toLowerCase());
  125. // XXX the use of .equals() here is problematic -- it
  126. // means we can easily overwrite valid values with null
  127. // if unregister is called after register XXX
  128. if (curImporter != null && curImporter.equals(importer)) {
  129. contentImportMap.remove(extension.toLowerCase());
  130. }
  131. }
  132. }
  133. }
  134. /**
  135. * Returns the content importer given the extension name, possibly returning
  136. * the default content importer if the extension is not found and the
  137. * useDefault argument is set to true. If useDefault is false and no importer
  138. * is found for the given extension, this method returns null.
  139. *
  140. * @param extension The file extension to search for a content import
  141. * @param useDefault If true and no importer is found for the extension,
  142. * return the default content importer if available
  143. * @return A ContentImportSPI object
  144. */
  145. public ContentImporterSPI getContentImporter(String extension, boolean useDefault) {
  146. if (extension == null) {
  147. return null;
  148. }
  149. ContentImporterSPI importer = contentImportMap.get(extension.toLowerCase());
  150. if (importer == null && useDefault == true) {
  151. return defaultContentImporter;
  152. }
  153. return importer;
  154. }
  155. }