PageRenderTime 18ms CodeModel.GetById 10ms app.highlight 7ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/ModeCatalogHandler.java

#
Java | 142 lines | 82 code | 20 blank | 40 comment | 25 complexity | 3a6e09d24a4518b20afbc7db3652e8ca MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1/*
  2 * ModeCatalogHandler.java - XML handler for mode catalog files
  3 * Copyright (C) 2000, 2001 Slava Pestov
  4 *
  5 * This program is free software; you can redistribute it and/or
  6 * modify it under the terms of the GNU General Public License
  7 * as published by the Free Software Foundation; either version 2
  8 * of the License, or any later version.
  9 *
 10 * This program is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 * GNU General Public License for more details.
 14 *
 15 * You should have received a copy of the GNU General Public License
 16 * along with this program; if not, write to the Free Software
 17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 18 */
 19
 20package org.gjt.sp.jedit;
 21
 22import com.microstar.xml.*;
 23import java.io.*;
 24import org.gjt.sp.util.Log;
 25
 26class ModeCatalogHandler extends HandlerBase
 27{
 28	ModeCatalogHandler(String directory, boolean resource)
 29	{
 30		this.directory = directory;
 31		this.resource = resource;
 32	}
 33
 34	public Object resolveEntity(String publicId, String systemId)
 35	{
 36		if("catalog.dtd".equals(systemId))
 37		{
 38			// this will result in a slight speed up, since we
 39			// don't need to read the DTD anyway, as AElfred is
 40			// non-validating
 41			return new StringReader("<!-- -->");
 42
 43			/* try
 44			{
 45				return new BufferedReader(new InputStreamReader(
 46					getClass().getResourceAsStream("catalog.dtd")));
 47			}
 48			catch(Exception e)
 49			{
 50				Log.log(Log.ERROR,this,"Error while opening"
 51					+ " catalog.dtd:");
 52				Log.log(Log.ERROR,this,e);
 53			} */
 54		}
 55
 56		return null;
 57	}
 58
 59	public void attribute(String aname, String value, boolean isSpecified)
 60	{
 61		aname = (aname == null) ? null : aname.intern();
 62
 63		if(aname == "NAME")
 64			modeName = value;
 65		else if(aname == "FILE")
 66		{
 67			if(value == null)
 68			{
 69				Log.log(Log.ERROR,this,directory + "catalog:"
 70					+ " mode " + modeName + " doesn't have"
 71					+ " a FILE attribute");
 72			}
 73			else
 74				file = value;
 75		}
 76		else if(aname == "FILE_NAME_GLOB")
 77			filenameGlob = value;
 78		else if(aname == "FIRST_LINE_GLOB")
 79			firstlineGlob = value;
 80	}
 81
 82	public void doctypeDecl(String name, String publicId,
 83		String systemId) throws Exception
 84	{
 85		// older jEdit versions used a DOCTYPE of CATALOG, which
 86		// is incorrect since the DOCTYPE must be the name of the
 87		// root element, which is MODES.
 88
 89		// so you the avid code reader should use MODES as the
 90		// DOCTYPE instead, but we still let old catalogs through
 91		// to avoid annoying users.
 92		if("CATALOG".equals(name) || "MODES".equals(name))
 93			return;
 94
 95		Log.log(Log.ERROR,this,directory + "catalog: DOCTYPE must be CATALOG");
 96	}
 97
 98	public void endElement(String name)
 99	{
100		if(name.equals("MODE"))
101		{
102			Mode mode = jEdit.getMode(modeName);
103			if(mode == null)
104			{
105				mode = new Mode(modeName);
106				jEdit.addMode(mode);
107			}
108
109			Object path;
110			if(resource)
111				path = jEdit.class.getResource(directory + file);
112			else
113				path = MiscUtilities.constructPath(directory,file);
114			mode.setProperty("file",path);
115
116			if(filenameGlob != null)
117				mode.setProperty("filenameGlob",filenameGlob);
118			else
119				mode.unsetProperty("filenameGlob");
120
121			if(firstlineGlob != null)
122				mode.setProperty("firstlineGlob",firstlineGlob);
123			else
124				mode.unsetProperty("firstlineGlob");
125
126			mode.init();
127
128			modeName = file = filenameGlob = firstlineGlob = null;
129		}
130	}
131
132	// end HandlerBase implementation
133
134	// private members
135	private String directory;
136	private boolean resource;
137
138	private String modeName;
139	private String file;
140	private String filenameGlob;
141	private String firstlineGlob;
142}