001: /*
002: * ModeProvider.java - An edit mode provider.
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2003 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022: package org.gjt.sp.jedit.syntax;
023:
024: //{{{ Imports
025:
026: import org.gjt.sp.jedit.GUIUtilities;
027: import org.gjt.sp.jedit.Mode;
028: import org.gjt.sp.jedit.jEdit;
029: import org.gjt.sp.util.IOUtilities;
030: import org.gjt.sp.util.Log;
031: import org.xml.sax.InputSource;
032: import org.xml.sax.SAXException;
033: import org.xml.sax.SAXParseException;
034: import org.xml.sax.XMLReader;
035: import org.xml.sax.helpers.XMLReaderFactory;
036:
037: import java.io.BufferedInputStream;
038: import java.io.FileInputStream;
039: import java.io.InputStream;
040: import java.util.ArrayList;
041: import java.util.List;
042:
043: //}}}
044:
045: /**
046: * @author Matthieu Casanova
047: * @version $Id: Buffer.java 8190 2006-12-07 07:58:34Z kpouer $
048: * @since jEdit 4.3pre10
049: */
050: public class ModeProvider {
051: public static final ModeProvider instance = new ModeProvider();
052:
053: private List<Mode> modes = new ArrayList<Mode>(160);
054:
055: //{{{ ModeProvider constructor
056: private ModeProvider() {
057: } //}}}
058:
059: //{{{ removeAll() method
060: public void removeAll() {
061: modes = new ArrayList<Mode>(160);
062: } //}}}
063:
064: //{{{ getMode() method
065: /**
066: * Returns the edit mode with the specified name.
067: * @param name The edit mode
068: * @since jEdit 4.3pre10
069: */
070: public Mode getMode(String name) {
071: for (int i = 0; i < modes.size(); i++) {
072: Mode mode = modes.get(i);
073: if (mode.getName().equals(name))
074: return mode;
075: }
076: return null;
077: } //}}}
078:
079: //{{{ getModeForFile() method
080: /**
081: * Get the appropriate mode that must be used for the file
082: * @param filename the filename
083: * @param firstLine the first line of the file
084: * @return the edit mode, or null if no mode match the file
085: * @since jEdit 4.3pre12
086: */
087: public Mode getModeForFile(String filename, String firstLine) {
088: String nogzName = filename.substring(0, filename.length()
089: - (filename.endsWith(".gz") ? 3 : 0));
090: Mode[] modes = getModes();
091:
092: // this must be in reverse order so that modes from the user
093: // catalog get checked first!
094: for (int i = modes.length - 1; i >= 0; i--) {
095: if (modes[i].accept(nogzName, firstLine)) {
096: return modes[i];
097: }
098: }
099: return null;
100: } //}}}
101:
102: //{{{ getModes() method
103: /**
104: * Returns an array of installed edit modes.
105: * @since jEdit 4.3pre10
106: */
107: public Mode[] getModes() {
108: Mode[] array = new Mode[modes.size()];
109: modes.toArray(array);
110: return array;
111: } //}}}
112:
113: //{{{ addMode() method
114: /**
115: * Do not call this method. It is only public so that classes
116: * in the org.gjt.sp.jedit.syntax package can access it.
117: * @since jEdit 4.3pre10
118: * @param mode The edit mode
119: */
120: public void addMode(Mode mode) {
121: modes.add(mode);
122: } //}}}
123:
124: //{{{ loadMode() method
125: public void loadMode(Mode mode, XModeHandler xmh) {
126: String fileName = (String) mode.getProperty("file");
127:
128: Log.log(Log.NOTICE, jEdit.class, "Loading edit mode "
129: + fileName);
130:
131: XMLReader parser = null;
132: try {
133: parser = XMLReaderFactory.createXMLReader();
134: } catch (SAXException saxe) {
135: Log.log(Log.ERROR, jEdit.class, saxe);
136: return;
137: }
138: mode.setTokenMarker(xmh.getTokenMarker());
139:
140: InputStream grammar = null;
141:
142: try {
143: grammar = new BufferedInputStream(new FileInputStream(
144: fileName));
145:
146: InputSource isrc = new InputSource(grammar);
147: isrc.setSystemId("jedit.jar");
148: parser.setContentHandler(xmh);
149: parser.setDTDHandler(xmh);
150: parser.setEntityResolver(xmh);
151: parser.setErrorHandler(xmh);
152: parser.parse(isrc);
153:
154: mode.setProperties(xmh.getModeProperties());
155: } catch (Throwable e) {
156: Log.log(Log.ERROR, jEdit.class, e);
157:
158: if (e instanceof SAXParseException) {
159: String message = e.getMessage();
160: int line = ((SAXParseException) e).getLineNumber();
161: int col = ((SAXParseException) e).getColumnNumber();
162:
163: Object[] args = { fileName, line, col, message };
164: GUIUtilities.error(null, "xmode-error", args);
165: }
166: } finally {
167: IOUtilities.closeQuietly(grammar);
168: }
169: } //}}}
170:
171: //{{{ loadMode() method
172: public void loadMode(Mode mode) {
173: XModeHandler xmh = new XModeHandler(mode.getName()) {
174: public void error(String what, Object subst) {
175: Log.log(Log.ERROR, this , subst);
176: }
177:
178: public TokenMarker getTokenMarker(String modeName) {
179: Mode mode = getMode(modeName);
180: if (mode == null)
181: return null;
182: else
183: return mode.getTokenMarker();
184: }
185: };
186: loadMode(mode, xmh);
187: } //}}}
188:
189: }
|