示例#1
0
 /**
  * Use the fully qualified name of a MimeDetector and try to instantiate it if it's not already
  * registered. If it's already registered then log a warning and return the already registered
  * MimeDetector
  *
  * @param mimeDetector
  * @return MimeDetector registered under this name. Returns null if an exception occurs
  */
 MimeDetector registerMimeDetector(final String mimeDetector) {
   if (mimeDetectors.containsKey(mimeDetector)) {
     log.warn(
         "MimeDetector ["
             + mimeDetector
             + "] will not be registered as a MimeDetector with this name is already registered.");
     return (MimeDetector) mimeDetectors.get(mimeDetector);
   }
   // Create the mime detector if we can
   try {
     MimeDetector md = (MimeDetector) Class.forName(mimeDetector).newInstance();
     md.init();
     if (log.isDebugEnabled()) {
       log.debug(
           "Registering MimeDetector with name ["
               + md.getName()
               + "] and description ["
               + md.getDescription()
               + "]");
     }
     mimeDetectors.put(mimeDetector, md);
     return md;
   } catch (Exception e) {
     log.error("Exception while registering MimeDetector [" + mimeDetector + "].", e);
   }
   // Failed to create an instance
   return null;
 }
示例#2
0
  MimeDetector unregisterMimeDetector(final String mimeDetector) {
    if (mimeDetector == null) {
      return null;
    }
    if (log.isDebugEnabled()) {
      log.debug("Unregistering MimeDetector [" + mimeDetector + "] from registry.");
    }
    try {
      MimeDetector md = (MimeDetector) mimeDetectors.get(mimeDetector);
      if (md != null) {
        md.delete();
        return (MimeDetector) mimeDetectors.remove(mimeDetector);
      }
    } catch (Exception e) {
      log.error("Exception while un-registering MimeDetector [" + mimeDetector + "].", e);
    }

    // Shouldn't get here
    return null;
  }
示例#3
0
 Collection getMimeTypes(final File file) throws MimeException {
   Collection mimeTypes = new ArrayList();
   try {
     if (!EncodingGuesser.getSupportedEncodings().isEmpty()) {
       mimeTypes = TextMimeDetector.getMimeTypes(file);
     }
   } catch (UnsupportedOperationException ignore) {
     // The TextMimeDetector will throw this if it decides
     // the content is not text
   }
   for (Iterator it = mimeDetectors.values().iterator(); it.hasNext(); ) {
     try {
       MimeDetector md = (MimeDetector) it.next();
       mimeTypes.addAll(md.getMimeTypes(file));
     } catch (UnsupportedOperationException usoe) {
       // We ignore this as it indicates that this MimeDetector does not support
       // Getting mime types from streams
     } catch (Exception e) {
       log.error(e.getLocalizedMessage(), e);
     }
   }
   return mimeTypes;
 }
示例#4
0
 /**
  * unregister the MimeDetector from the list.
  *
  * @param mimeDetector the MimeDetector to unregister
  * @return MimeDetector unregistered or null if it was not registered
  */
 MimeDetector unregisterMimeDetector(final MimeDetector mimeDetector) {
   if (mimeDetector == null) {
     return null;
   }
   return unregisterMimeDetector(mimeDetector.getName());
 }