alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 * ASPTokenMarker.java 
 * Copyright (c) 1999 André Kaplan
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
  
package org.gjt.sp.jedit.syntax;

import java.util.Enumeration;
import java.util.Stack;
import javax.swing.text.Segment;

import gnu.regexp.*;


/**
 * An utility class to save some relevant infos (language and client/server side)
 * found in SCRIPT Tags or <%@ like Tags
 *
 * @author  Andre Kaplan
 * @version 0.6
 */
class ASPStateInfo
{
	ASPStateInfo() {}

	ASPStateInfo(boolean client, String language)
	{
		this.client   = client;
		this.language = language;
	}

	void init(boolean client, String language)
	{
		this.client   = client;
		this.language = language;
	}

	public boolean equals(Object o)
	{
		if (o == null || !(o instanceof ASPStateInfo))
		{
			return false;
		}

		ASPStateInfo other = (ASPStateInfo)o;

		return ((this.client == other.client) && this.language.equals(other.language));
	}
 
	byte toASPMode()
	{
		for (int i = 0; i < modes.length; i++)
		{
			if (this.equals(modes[i][0]))
			{
				return ((Byte)modes[i][1]).byteValue();
			}
		}

		return ASPMode.HTML;
	}

	void display(java.io.PrintStream o)
	{
		o.println("LANGUAGE: [" + this.language + "]");
		o.println("CLIENT:   [" + this.client   + "]");
	}

	boolean client   = true;
	String  language = "javascript";

	private static Object[][] modes = new Object[][] {
		 new Object[] { new ASPStateInfo(true,  "html"),       new Byte(ASPMode.HTML) }
		,new Object[] { new ASPStateInfo(false, "html"),       new Byte(ASPMode.HTML) }
		,new Object[] { new ASPStateInfo(true,  "javascript"), new Byte(ASPMode.CSJS) }
		,new Object[] { new ASPStateInfo(true,  "jscript"),    new Byte(ASPMode.CSJS) }
		,new Object[] { new ASPStateInfo(false, "javascript"), new Byte(ASPMode.SSJS) }
		,new Object[] { new ASPStateInfo(false, "jscript"),    new Byte(ASPMode.SSJS) }
		,new Object[] { new ASPStateInfo(true,  "vbscript"),   new Byte(ASPMode.CSVB) }
		,new Object[] { new ASPStateInfo(false, "vbscript"),   new Byte(ASPMode.SSVB) }
		,new Object[] { new ASPStateInfo(true,  "perlscript"), new Byte(ASPMode.CSPS) }
		,new Object[] { new ASPStateInfo(false, "perlscript"), new Byte(ASPMode.SSPS) }
	};
}
	
/**
 * ASP Token Marker.
 * TO DO: CSS support, HTML Entities, HTML Attributes & Values colorizing
 *
 * @author Andre Kaplan
 * @version 0.6
 */
public class ASPTokenMarker 
	extends    TokenMarker
	implements TokenMarkerWithAddToken
{
	// Token to request next line parsing when the mode has changed
	public static final byte MODE_CHANGE = Token.INTERNAL_FIRST;

	public ASPTokenMarker()
	{
	}

	public void addToken(int length, byte id)
	{
		super.addToken(length, id);
	}

	// **************
	// markTokensImpl
	// **************
	protected byte markTokensImpl(byte token, Segment line, int lineIndex)
	{
		TokenMarkerContext tokenContext = new TokenMarkerContext(line, lineIndex, this, this.lineInfo);
		
		// We store the old defaultASPMode
		byte defaultASPMode = this.defaultASPMode;

		MultiModeToken prevLineToken = MultiModeToken.NULL;

		if (    tokenContext.prevLineInfo != null 
			 && tokenContext.prevLineInfo.obj != null 
			 && tokenContext.prevLineInfo.obj instanceof MultiModeToken
		)
		{
			prevLineToken = (MultiModeToken)tokenContext.prevLineInfo.obj;
		}

		MultiModeToken currLineToken = MultiModeToken.NULL;
		if (    tokenContext.currLineInfo != null 
			 && tokenContext.currLineInfo.obj != null 
			 && tokenContext.currLineInfo.obj instanceof MultiModeToken
		)
		{
			currLineToken = (MultiModeToken)tokenContext.currLineInfo.obj;
		}

		MultiModeToken res = this.markTokensImpl(prevLineToken, tokenContext);

		byte retval = res.token;

		// We check if mode has changed
		if (
				(defaultASPMode != this.defaultASPMode || currLineToken.mode != res.mode)
			 && (currLineToken.token == res.token)
		)
		{
			// We inform markTokens that the mode has changed
			retval = MODE_CHANGE;
		}

		tokenContext.currLineInfo.obj = res;

		return retval;
	}

	// **************
	// markTokensImpl
	// **************
	MultiModeToken markTokensImpl(final MultiModeToken token, TokenMarkerContext tokenContext)
	{
		MultiModeToken res = new MultiModeToken(token);

loop:   for (this.debug.reset(); tokenContext.hasMoreChars(); )
		{
			char c = tokenContext.getChar();

			if (!this.debug.isOK(tokenContext)) {
				// We got stuck here at some point
				// Log this and increment tokenContext.pos to escape this
				tokenContext.pos++;
			}

			// Switch to ASP Mode if <% found at pos
			// Except if already in a server-side mode
			if (	(res.mode != ASPMode.HTML_SCRIPT)
				 &&	(res.mode != ASPMode.SSI)
				 &&	(res.mode != ASPMode.ASP)
				 &&	(res.mode != ASPMode.ASP_CFG)
				 &&	(res.mode != ASPMode.SSVB)
				 &&	(res.mode != ASPMode.SSJS)
				 &&	(res.mode != ASPMode.SSPS)
			   )
			{
				if (this.doASP(res, tokenContext)) { 
					continue; }
			}

			// Switch to HTML_SCRIPT Mode if "))
		{
			tokenContext.addTokenToPos(mmt.token);

			byte b = Token.KEYWORD1;

			if (this.modes.empty()) {
				mmt.reset();
			} else {
				mmt.assign((MultiModeToken)(this.modes.pop()));
				if (mmt.mode == ASPMode.HTML_COMMENT) {
					b = mmt.token;
				}
			}
			tokenContext.pos += 9;
			tokenContext.addTokenToPos(b);
				
			return true;
		}

		return false;
	}

	// private members
	private KeywordMap keywords;

	private ASPStateInfo stateInfo = new ASPStateInfo();
	private byte defaultASPMode    = ASPMode.SSVB;

	private TokenMarkerDebugger debug = new TokenMarkerDebugger();

	private MultiModeTokenMarkerWithContext vbs = new ASPVBScriptTokenMarker(false);
	private MultiModeTokenMarkerWithContext js  = new ASPJavascriptTokenMarker(false);
	private MultiModeTokenMarkerWithContext ps  = new ASPPerlscriptTokenMarker(false);

	private Stack modes = new Stack();

	private static RE language = null;
	private static RE runat    = null;

	static {
		try {
			language = new RE(
				"^language\\s*=\\s*[\"']?(jscript|javascript|perlscript|vbscript)([0-9]*|[0-9]+(?:\\.[0-9]+){0,2})[\"']?", RE.REG_ICASE);
		}
		catch (REException ree)
		{
		}
	
		try {
			runat = new RE("^runat\\s*=\\s*[\"']?(server)[\"']?", RE.REG_ICASE);
		}
		catch (REException ree)
		{
		}
	}
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.