1 19 20 package org.netbeans.modules.lexer.nbbridge; 21 22 import java.io.BufferedReader ; 23 import java.io.IOException ; 24 import java.io.InputStreamReader ; 25 import java.text.MessageFormat ; 26 import java.util.HashMap ; 27 import java.util.List ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 import org.netbeans.api.lexer.Language; 31 import org.netbeans.api.lexer.TokenId; 32 import org.netbeans.lib.lexer.LanguageManager; 33 import org.netbeans.spi.editor.mimelookup.Class2LayerFolder; 34 import org.netbeans.spi.editor.mimelookup.InstanceProvider; 35 import org.netbeans.spi.lexer.LanguageEmbedding; 36 import org.openide.filesystems.FileObject; 37 38 42 public class MimeLookupFolderInfo implements Class2LayerFolder, InstanceProvider { 43 44 private static final Logger LOG = Logger.getLogger(MimeLookupFolderInfo.class.getName()); 45 46 47 public MimeLookupFolderInfo() { 48 } 49 50 public Class getClazz() { 51 return LanguagesEmbeddingMap.class; 52 } 53 54 public String getLayerFolderName() { 55 return "languagesEmbeddingMap"; } 57 58 public InstanceProvider getInstanceProvider() { 59 return this; 60 } 61 62 public Object createInstance(List fileObjectList) { 63 HashMap <String , LanguageEmbedding<? extends TokenId>> map 64 = new HashMap <String , LanguageEmbedding<? extends TokenId>>(); 65 66 for(Object o : fileObjectList) { 67 assert o instanceof FileObject : "fileObjectList should contain FileObjects and not " + o; 69 FileObject f = (FileObject) o; 70 try { 71 Object [] info = parseFile(f); 72 String mimeType = (String ) info[0]; 73 int startSkipLength = (Integer ) info[1]; 74 int endSkipLength = (Integer ) info[2]; 75 76 if (isMimeTypeValid(mimeType)) { 77 Language<? extends TokenId> language = LanguageManager.getInstance().findLanguage(mimeType); 78 if (language != null) { 79 map.put(f.getName(), LanguageEmbedding.create(language, startSkipLength, endSkipLength)); 80 } else { 81 LOG.warning("Can't find Language for mime type '" + mimeType + "', ignoring."); } 83 } else { 84 LOG.log(Level.WARNING, "Ignoring invalid mime type '" + mimeType + "' from: " + f.getPath()); } 86 } catch (IOException ioe) { 87 LOG.log(Level.WARNING, "Can't read language embedding definition from: " + f.getPath()); } 89 } 90 91 return new LanguagesEmbeddingMap(map); 92 } 93 94 private boolean isMimeTypeValid(String mimeType) { 95 if (mimeType == null) { 96 return false; 97 } 98 int slashIndex = mimeType.indexOf('/'); if (slashIndex == -1) { return false; 101 } 102 if (mimeType.indexOf('/', slashIndex + 1) != -1) { return false; 104 } 105 return true; 106 } 107 108 private Object [] parseFile(FileObject f) throws IOException { 109 BufferedReader r = new BufferedReader (new InputStreamReader (f.getInputStream())); 110 try { 111 String line; 112 113 while (null != (line = r.readLine())) { 114 line.trim(); 115 if (line.length() != 0) { 116 String [] parts = line.split(","); return new Object [] { 118 parts[0], 119 parts.length > 1 ? toInt(parts[1], "Ignoring invalid start-skip-length '{0}' in " + f.getPath()) : 0, parts.length > 2 ? toInt(parts[2], "Ignoring invalid end-skip-length '{0}' in " + f.getPath()) : 0 }; 122 } 123 } 124 125 return null; 126 } finally { 127 r.close(); 128 } 129 } 130 131 private int toInt(String s, String errorMsg) { 132 try { 133 return Integer.parseInt(s); 134 } catch (NumberFormatException e) { 135 LOG.log(Level.WARNING, MessageFormat.format(errorMsg, s), e); 136 return 0; 137 } 138 } 139 140 } 141 | Popular Tags |