コード例 #1
0
 private static IContentType getContentType(ITypedElement element) {
   if (element == null) return null;
   String name = element.getName();
   IContentType ct = null;
   if (element instanceof IStreamContentAccessor) {
     IStreamContentAccessor isa = (IStreamContentAccessor) element;
     try {
       InputStream is = isa.getContents();
       if (is != null) {
         InputStream bis = new BufferedInputStream(is);
         try {
           ct = fgContentTypeManager.findContentTypeFor(is, name);
         } catch (IOException e) {
           // silently ignored
         } finally {
           try {
             bis.close();
           } catch (IOException e2) {
             // silently ignored
           }
         }
       }
     } catch (CoreException e1) {
       // silently ignored
     }
   }
   if (ct == null) ct = fgContentTypeManager.findContentTypeFor(name);
   return ct;
 }
コード例 #2
0
 public static String readString(IStreamContentAccessor sa) throws CoreException {
   InputStream is = sa.getContents();
   if (is != null) {
     String encoding = null;
     if (sa instanceof IEncodedStreamContentAccessor) {
       try {
         encoding = ((IEncodedStreamContentAccessor) sa).getCharset();
       } catch (Exception e) {
       }
     }
     if (encoding == null) encoding = ResourcesPlugin.getEncoding();
     return readString(is, encoding);
   }
   return null;
 }
コード例 #3
0
 /*
  * Guesses the file type of the given input.
  * Returns ITypedElement.TEXT_TYPE if none of the first 10 lines is longer than 1000 bytes.
  * Returns ITypedElement.UNKNOWN_TYPE otherwise.
  * Returns <code>null</code> if the input isn't an <code>IStreamContentAccessor</code>.
  */
 private static String guessType(ITypedElement input) {
   if (input instanceof IStreamContentAccessor) {
     IStreamContentAccessor sca = (IStreamContentAccessor) input;
     InputStream is = null;
     try {
       is = sca.getContents();
       if (is == null) return null;
       int lineLength = 0;
       int lines = 0;
       while (lines < 10) {
         int c = is.read();
         if (c == -1) // EOF
         break;
         if (c == '\n' || c == '\r') { // reset line length
           lineLength = 0;
           lines++;
         } else lineLength++;
         if (lineLength > 1000) return ITypedElement.UNKNOWN_TYPE;
       }
       return ITypedElement.TEXT_TYPE;
     } catch (CoreException ex) {
       // be silent and return UNKNOWN_TYPE
     } catch (IOException ex) {
       // be silent and return UNKNOWN_TYPE
     } finally {
       if (is != null) {
         try {
           is.close();
         } catch (IOException ex) {
           // silently ignored
         }
       }
     }
     return ITypedElement.UNKNOWN_TYPE;
   }
   return null;
 }