001: /*
002: * ModeList.java
003: *
004: * Copyright (C) 1998-2004 Peter Graves
005: * $Id: ModeList.java,v 1.19 2004/06/08 19:20:03 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.util.ArrayList;
025: import java.util.Iterator;
026:
027: public final class ModeList implements Constants {
028: private static ModeList modeList;
029:
030: public static synchronized ModeList getInstance() {
031: if (modeList == null)
032: modeList = new ModeList();
033: return modeList;
034: }
035:
036: private ArrayList list;
037:
038: private ModeList() {
039: list = new ArrayList();
040: addEntry(ARCHIVE_MODE, ARCHIVE_MODE_NAME, "ArchiveMode", false,
041: null);
042: addEntry(ASM_MODE, ASM_MODE_NAME, "AsmMode", true,
043: ".+\\.asm|.+\\inc");
044: addEntry(AUTOCONF_MODE, AUTOCONF_MODE_NAME, "AutoconfMode",
045: true, "configure.ac|configure.in|aclocal.m4");
046: addEntry(BINARY_MODE, BINARY_MODE_NAME, "BinaryMode", true,
047: null);
048: addEntry(CHECKIN_MODE, CHECKIN_MODE_NAME, "CheckinMode", false,
049: null);
050: addEntry(COMPILATION_MODE, COMPILATION_MODE_NAME,
051: "CompilationMode", false, null);
052: addEntry(CPP_MODE, CPP_MODE_NAME, "CppMode", true,
053: "(.+\\.cpp)|(.+\\.cxx)|(.+\\.cc)|(.+\\.hpp)|(.+\\.hxx)|(.+\\.h)");
054: addEntry(CSS_MODE, CSS_MODE_NAME, "CSSMode", true, ".+\\.css");
055: addEntry(C_MODE, C_MODE_NAME, "CMode", true, ".+\\.c");
056: addEntry(DIFF_MODE, DIFF_MODE_NAME, "DiffMode", true,
057: ".+\\.diff|.+\\.patch");
058: addEntry(DIRECTORY_MODE, DIRECTORY_MODE_NAME, "DirectoryMode",
059: false, null);
060: addEntry(HTML_MODE, HTML_MODE_NAME, "HtmlMode", true,
061: ".+\\.html?");
062: addEntry(IMAGE_MODE, IMAGE_MODE_NAME, "ImageMode", false,
063: ".+\\.gif|.+\\.jpe?g|.+\\.png");
064: addEntry(JAVASCRIPT_MODE, JAVASCRIPT_MODE_NAME,
065: "JavaScriptMode", true, ".+\\.js");
066: addEntry(JAVA_MODE, JAVA_MODE_NAME, "JavaMode", true,
067: ".+\\.java|.+\\.jad");
068: addEntry(JDB_MODE, JDB_MODE_NAME, "JdbMode", false, null);
069: addEntry(LISP_MODE, LISP_MODE_NAME, "LispMode", true,
070: ".+\\.[ej]l|.*\\.li?sp|.*\\.cl|.*\\.emacs|.*\\.asd");
071: addEntry(LISP_SHELL_MODE, LISP_SHELL_MODE_NAME,
072: "LispShellMode", false, null);
073: addEntry(LIST_OCCURRENCES_MODE, LIST_OCCURRENCES_MODE_NAME,
074: "ListOccurrencesMode", false, null);
075: addEntry(LIST_REGISTERS_MODE, LIST_REGISTERS_MODE_NAME,
076: "ListRegistersMode", false, null);
077: addEntry(LIST_TAGS_MODE, LIST_TAGS_MODE_NAME, "ListTagsMode",
078: false, null);
079: addEntry(MAILBOX_MODE, MAILBOX_MODE_NAME, "mail.MailboxMode",
080: false, null);
081: addEntry(MAKEFILE_MODE, MAKEFILE_MODE_NAME, "MakefileMode",
082: true, "makefile(\\.in)?");
083: addEntry(MAN_MODE, MAN_MODE_NAME, "ManMode", false, null);
084: addEntry(MESSAGE_MODE, MESSAGE_MODE_NAME, "mail.MessageMode",
085: false, null);
086: addEntry(NEWS_GROUPS_MODE, NEWS_GROUPS_MODE_NAME,
087: "mail.NewsGroupsMode", false, null);
088: addEntry(NEWS_GROUP_SUMMARY_MODE, NEWS_GROUP_SUMMARY_MODE_NAME,
089: "mail.NewsGroupsSummaryMode", false, null);
090: addEntry(OBJC_MODE, OBJC_MODE_NAME, "ObjCMode", true, ".+\\.m");
091: addEntry(PERL_MODE, PERL_MODE_NAME, "PerlMode", true,
092: ".+\\.p[lm]");
093: addEntry(PHP_MODE, PHP_MODE_NAME, "PHPMode", true,
094: ".+\\.php[34]?");
095: addEntry(PLAIN_TEXT_MODE, PLAIN_TEXT_MODE_NAME,
096: "PlainTextMode", true, null);
097: addEntry(PROPERTIES_MODE, PROPERTIES_MODE_NAME,
098: "PropertiesMode", true,
099: "(.+\\.config)|(.+\\.co?nf)|(.+\\.cfg)|(.+\\.ini)|(.+\\.properties)|prefs");
100: addEntry(PYTHON_MODE, PYTHON_MODE_NAME, "PythonMode", true,
101: ".+\\.py");
102: addEntry(RUBY_MODE, RUBY_MODE_NAME, "RubyMode", true, ".+\\.rb");
103: addEntry(SCHEME_MODE, SCHEME_MODE_NAME, "SchemeMode", true,
104: ".+\\.sc[ehm]|.+\\.ss");
105: addEntry(SEND_MAIL_MODE, SEND_MAIL_MODE_NAME,
106: "mail.SendMailMode", false, null);
107: addEntry(SHELL_MODE, SHELL_MODE_NAME, "ShellMode", false, null);
108: addEntry(SHELL_SCRIPT_MODE, SHELL_SCRIPT_MODE_NAME,
109: "ShellScriptMode", true,
110: ".+\\.[ck]?sh|\\.bashrc|\\.bash_profile");
111: addEntry(TCL_MODE, TCL_MODE_NAME, "TclMode", true, ".+\\.tcl");
112: addEntry(VERILOG_MODE, VERILOG_MODE_NAME, "VerilogMode", true,
113: ".+\\.v");
114: addEntry(VHDL_MODE, VHDL_MODE_NAME, "VHDLMode", true,
115: ".+\\.vhdl?");
116: addEntry(WEB_MODE, WEB_MODE_NAME, "WebMode", false, null);
117: addEntry(WORD_MODE, WORD_MODE_NAME, "WordMode", false, null);
118: addEntry(XML_MODE, XML_MODE_NAME, "XmlMode", true,
119: ".+\\.x[msu]l|.+\\.dtd");
120: }
121:
122: public synchronized Mode getMode(int id) {
123: final ModeListEntry entry = getEntry(id);
124: return entry == null ? null : entry.getMode(true);
125: }
126:
127: public synchronized boolean modeAccepts(int id, String filename) {
128: final ModeListEntry entry = getEntry(id);
129: if (entry == null) {
130: Debug.bug("ModeList.modeAccepts() invalid mode id " + id);
131: return false;
132: }
133: return entry.accepts(filename);
134: }
135:
136: public synchronized Mode getModeFromModeName(String modeName) {
137: if (modeName != null) {
138: for (int i = list.size(); i-- > 0;) {
139: ModeListEntry entry = (ModeListEntry) list.get(i);
140: if (modeName.equalsIgnoreCase(entry.getDisplayName()))
141: return entry.getMode(true);
142: }
143: if (modeName.equalsIgnoreCase("asm"))
144: return getMode(ASM_MODE);
145: if (modeName.equalsIgnoreCase("objc"))
146: return getMode(OBJC_MODE);
147: if (modeName.equalsIgnoreCase("text"))
148: return getMode(PLAIN_TEXT_MODE);
149: }
150: return null;
151: }
152:
153: public synchronized int getModeIdFromModeName(String modeName) {
154: if (modeName != null) {
155: for (int i = list.size(); i-- > 0;) {
156: ModeListEntry entry = (ModeListEntry) list.get(i);
157: if (modeName.equalsIgnoreCase(entry.getDisplayName()))
158: return entry.getId();
159: }
160: if (modeName.equalsIgnoreCase("asm"))
161: return ASM_MODE;
162: if (modeName.equalsIgnoreCase("objc"))
163: return OBJC_MODE;
164: if (modeName.equalsIgnoreCase("text"))
165: return PLAIN_TEXT_MODE;
166: }
167: return -1;
168: }
169:
170: public synchronized Mode getModeForFileName(String fileName) {
171: int id = getModeIdForFileName(fileName);
172: return id > 0 ? getMode(id) : null;
173: }
174:
175: public synchronized int getModeIdForFileName(String fileName) {
176: if (fileName != null) {
177: for (int i = list.size(); i-- > 0;) {
178: ModeListEntry entry = (ModeListEntry) list.get(i);
179: if (entry.accepts(fileName))
180: return entry.getId();
181: }
182: }
183: return -1;
184: }
185:
186: // Hard-coded for now.
187: public synchronized Mode getModeForContentType(String contentType) {
188: if (contentType != null) {
189: if (contentType.toLowerCase().startsWith("text/css"))
190: return getMode(CSS_MODE);
191: }
192: return null;
193: }
194:
195: public synchronized final Iterator iterator() {
196: return list.iterator();
197: }
198:
199: // Does not check for duplicate entries.
200: private final void addEntry(int id, String displayName,
201: String className, boolean selectable, String defaultFiles) {
202: list.add(new ModeListEntry(id, displayName, className,
203: selectable, defaultFiles));
204: }
205:
206: private final ModeListEntry getEntry(int id) {
207: for (int i = list.size(); i-- > 0;) {
208: ModeListEntry entry = (ModeListEntry) list.get(i);
209: if (entry.getId() == id) {
210: // Found entry.
211: return entry;
212: }
213: }
214: return null;
215: }
216: }
|