PageRenderTime 92ms CodeModel.GetById 70ms app.highlight 9ms RepoModel.GetById 11ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/org/gjt/sp/jedit/ModeCatalogHandler.java

#
Java | 138 lines | 93 code | 19 blank | 26 comment | 25 complexity | 346c809670187bbb1ec4adca4121f584 MD5 | raw file
  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 java.util.Stack;
 25import org.gjt.sp.util.Log;
 26
 27class ModeCatalogHandler extends HandlerBase
 28{
 29	ModeCatalogHandler(String directory, boolean resource)
 30	{
 31		this.directory = directory;
 32		this.resource = resource;
 33	}
 34
 35	public Object resolveEntity(String publicId, String systemId)
 36	{
 37		if("catalog.dtd".equals(systemId))
 38		{
 39			try
 40			{
 41				return new BufferedReader(new InputStreamReader(
 42					getClass().getResourceAsStream("catalog.dtd")));
 43			}
 44			catch(Exception e)
 45			{
 46				Log.log(Log.ERROR,this,"Error while opening"
 47					+ " catalog.dtd:");
 48				Log.log(Log.ERROR,this,e);
 49			}
 50		}
 51
 52		return null;
 53	}
 54
 55	public void attribute(String aname, String value, boolean isSpecified)
 56	{
 57		aname = (aname == null) ? null : aname.intern();
 58
 59		if(aname == "NAME")
 60			modeName = value;
 61		else if(aname == "FILE")
 62		{
 63			if(value == null)
 64			{
 65				Log.log(Log.ERROR,this,directory + "catalog:"
 66					+ " mode " + modeName + " doesn't have"
 67					+ " a FILE attribute");
 68			}
 69			else
 70				file = value;
 71		}
 72		else if(aname == "FILE_NAME_GLOB")
 73			filenameGlob = value;
 74		else if(aname == "FIRST_LINE_GLOB")
 75			firstlineGlob = value;
 76	}
 77
 78	public void doctypeDecl(String name, String publicId,
 79		String systemId) throws Exception
 80	{
 81		// older jEdit versions used a DOCTYPE of CATALOG, which
 82		// is incorrect since the DOCTYPE must be the name of the
 83		// root element, which is MODES.
 84
 85		// so you the avid code reader should use MODES as the
 86		// DOCTYPE instead, but we still let old catalogs through
 87		// to avoid annoying users.
 88		if("CATALOG".equals(name) || "MODES".equals(name))
 89			return;
 90
 91		Log.log(Log.ERROR,this,directory + "catalog: DOCTYPE must be CATALOG");
 92	}
 93
 94	public void endElement(String name)
 95	{
 96		if(name.equals("MODE"))
 97		{
 98			Mode mode = jEdit.getMode(modeName);
 99			if(mode == null)
100			{
101				mode = new Mode(modeName);
102				jEdit.addMode(mode);
103			}
104
105			Object path;
106			if(resource)
107				path = jEdit.class.getResource(directory + file);
108			else
109				path = MiscUtilities.constructPath(directory,file);
110			mode.setProperty("file",path);
111
112			if(filenameGlob != null)
113				mode.setProperty("filenameGlob",filenameGlob);
114			else
115				mode.unsetProperty("filenameGlob");
116
117			if(firstlineGlob != null)
118				mode.setProperty("firstlineGlob",firstlineGlob);
119			else
120				mode.unsetProperty("firstlineGlob");
121
122			mode.init();
123
124			modeName = file = filenameGlob = firstlineGlob = null;
125		}
126	}
127
128	// end HandlerBase implementation
129
130	// private members
131	private String directory;
132	private boolean resource;
133
134	private String modeName;
135	private String file;
136	private String filenameGlob;
137	private String firstlineGlob;
138}