KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > lexer > nbbridge > MimeLookupFolderInfo


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.lexer.nbbridge;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
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 /**
39  *
40  * @author vita
41  */

42 public class MimeLookupFolderInfo implements Class2LayerFolder, InstanceProvider {
43     
44     private static final Logger JavaDoc LOG = Logger.getLogger(MimeLookupFolderInfo.class.getName());
45     
46     /** Creates a new instance of MimeLookupFolderInfo */
47     public MimeLookupFolderInfo() {
48     }
49
50     public Class JavaDoc getClazz() {
51         return LanguagesEmbeddingMap.class;
52     }
53
54     public String JavaDoc getLayerFolderName() {
55         return "languagesEmbeddingMap"; //NOI18N
56
}
57
58     public InstanceProvider getInstanceProvider() {
59         return this;
60     }
61
62     public Object JavaDoc createInstance(List JavaDoc fileObjectList) {
63         HashMap JavaDoc<String JavaDoc, LanguageEmbedding<? extends TokenId>> map
64                 = new HashMap JavaDoc<String JavaDoc, LanguageEmbedding<? extends TokenId>>();
65         
66         for(Object JavaDoc o : fileObjectList) {
67             assert o instanceof FileObject : "fileObjectList should contain FileObjects and not " + o; //NOI18N
68

69             FileObject f = (FileObject) o;
70             try {
71                 Object JavaDoc [] info = parseFile(f);
72                 String JavaDoc mimeType = (String JavaDoc) info[0];
73                 int startSkipLength = (Integer JavaDoc) info[1];
74                 int endSkipLength = (Integer JavaDoc) 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."); //NOI18N
82
}
83                 } else {
84                     LOG.log(Level.WARNING, "Ignoring invalid mime type '" + mimeType + "' from: " + f.getPath()); //NOI18N
85
}
86             } catch (IOException JavaDoc ioe) {
87                 LOG.log(Level.WARNING, "Can't read language embedding definition from: " + f.getPath()); //NOI18N
88
}
89         }
90         
91         return new LanguagesEmbeddingMap(map);
92     }
93     
94     private boolean isMimeTypeValid(String JavaDoc mimeType) {
95         if (mimeType == null) {
96             return false;
97         }
98         int slashIndex = mimeType.indexOf('/'); //NOI18N
99
if (slashIndex == -1) { // no slash
100
return false;
101         }
102         if (mimeType.indexOf('/', slashIndex + 1) != -1) { //NOI18N
103
return false;
104         }
105         return true;
106     }
107     
108     private Object JavaDoc [] parseFile(FileObject f) throws IOException JavaDoc {
109         BufferedReader JavaDoc r = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(f.getInputStream()));
110         try {
111             String JavaDoc line;
112             
113             while (null != (line = r.readLine())) {
114                 line.trim();
115                 if (line.length() != 0) {
116                     String JavaDoc [] parts = line.split(","); //NOI18N
117
return new Object JavaDoc [] {
118                         parts[0],
119                         parts.length > 1 ? toInt(parts[1], "Ignoring invalid start-skip-length '{0}' in " + f.getPath()) : 0, //NOI18N
120
parts.length > 2 ? toInt(parts[2], "Ignoring invalid end-skip-length '{0}' in " + f.getPath()) : 0 //NOI18N
121
};
122                 }
123             }
124             
125             return null;
126         } finally {
127             r.close();
128         }
129     }
130     
131     private int toInt(String JavaDoc s, String JavaDoc errorMsg) {
132         try {
133             return Integer.parseInt(s);
134         } catch (NumberFormatException JavaDoc e) {
135             LOG.log(Level.WARNING, MessageFormat.format(errorMsg, s), e);
136             return 0;
137         }
138     }
139     
140 }
141
Popular Tags