1 19 package org.netbeans.modules.ruby; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 24 import org.openide.filesystems.FileObject; 25 import org.openide.filesystems.MIMEResolver; 26 27 28 33 public class RubyMimeResolver extends MIMEResolver { 34 38 public static final String RUBY_MIME_TYPE = "text/x-ruby"; 40 41 private static final int HEADER_LENGTH = 20; 42 43 public RubyMimeResolver() { 44 } 45 46 public String findMIMEType(FileObject fo) { 47 String ext = fo.getExt(); 48 49 if (ext.equalsIgnoreCase("rb") || ext.equalsIgnoreCase("mab") || ext.equalsIgnoreCase("gemspec") || ext.equalsIgnoreCase("rake")) { return RUBY_MIME_TYPE; 52 } 53 54 59 String name = fo.getName(); 60 61 if ("Rakefile".equals(name) || "rakefile".equals(name)) { 62 return RUBY_MIME_TYPE; 63 } 64 65 if (ext.length() == 0 || ext.equals("cgi")) { 68 byte[] header = readHeader(fo); 69 70 if (header != null) { 71 if (isRubyHeader(header)) { 72 return RUBY_MIME_TYPE; 73 } 74 } 75 } 76 77 return null; 78 } 79 80 public static boolean isRubyHeader(byte[] header) { 81 int max = header.length; 82 83 if ((max < 2) || (header[0] != '#') || (header[1] != '!')) { 84 return false; 85 } 86 87 95 int index = 2; 97 98 while ((index < max) && (header[index] == ' ')) { 99 index++; 100 } 101 102 while ((index < max) && (header[index] != '\n') && (header[index] != ' ')) { 104 index++; 105 } 106 107 index--; 108 109 while ((index >= 2) && (header[index] != '/') && (header[index] != '\\') && 111 (header[index] != ' ')) { 112 index--; 113 } 114 115 index++; 116 117 if ((((index + 3) < max) && (header[index] == 'r') && (header[index + 1] == 'u') && 119 (header[index + 2] == 'b') && (header[index + 3] == 'y')) || 120 (((index + 4) < max) && (header[index] == 'j') && (header[index + 1] == 'r') && 121 (header[index + 2] == 'u') && (header[index + 3] == 'b') && 122 (header[index + 4] == 'y'))) { 123 if (header[index] == 'j') { 126 index += 5; 127 } else { 128 index += 4; 129 } 130 131 if ((index >= max) || (header[index] == '\n') || (header[index] == ' ')) { 132 return true; 133 } 134 135 if ((header[index] == '.') && ((index + 3) < max) && (header[index + 1] == 'e') && 136 (header[index + 2] == 'x') && (header[index + 3] == 'e')) { 137 return true; 138 } 139 140 return false; 141 } else if (((index + 2) < max) && (header[index] == 'e') && (header[index + 1] == 'n') && 142 (header[index + 2] == 'v')) { 143 index += 3; 144 145 if ((header[index] == ' ') || 147 ((header[index] == '.') && ((index + 4) < max) && (header[index + 1] == 'e') && 148 (header[index + 2] == 'x') && (header[index + 3] == 'e') && 149 (header[index + 4] == ' '))) { 150 if (header[index] == '.') { 152 index += 4; 153 } 154 155 while ((index < max) && (header[index] == ' ')) { 156 index++; 157 } 158 159 if ((((index + 3) < max) && (header[index] == 'r') && (header[index + 1] == 'u') && 161 (header[index + 2] == 'b') && (header[index + 3] == 'y')) || 162 (((index + 4) < max) && (header[index] == 'j') && 163 (header[index + 1] == 'r') && (header[index + 2] == 'u') && 164 (header[index + 3] == 'b') && (header[index + 4] == 'y'))) { 165 if (header[index] == 'j') { 167 index += 5; 168 } else { 169 index += 4; 170 } 171 172 if ((index == max) || (header[index] == '\n') || (header[index] == ' ') || 173 ((header[index] == '.') && ((index + 3) < max) && 174 (header[index + 1] == 'e') && (header[index + 2] == 'x') && 175 (header[index + 3] == 'e'))) { 176 return true; 177 } 178 } 179 } 180 } 181 182 return false; 183 } 184 185 private byte[] readHeader(FileObject fo) { 186 byte[] header = new byte[HEADER_LENGTH]; 188 189 InputStream in = null; 190 191 try { 192 in = fo.getInputStream(); 193 194 for (int i = 0; i < HEADER_LENGTH;) { 195 try { 196 int read = in.read(header, i, HEADER_LENGTH - i); 197 198 if (read < 0) { 199 return null; } 201 202 i += read; 203 } catch (IOException ex) { 204 return null; } 206 } 207 } catch (IOException openex) { 208 return null; } finally { 210 try { 211 if (in != null) { 212 in.close(); 213 } 214 } catch (IOException ioe) { 215 } 217 } 218 219 return header; 220 } 221 } 222 | Popular Tags |