📄 syntaxparser.java
字号:
package org.jr.jzj.editor;
/**
* <p>Copyright: Copyright (c) 2002-2003</p>
* <p>Company: JavaResearch(http://www.javaresearch.org)</p>
* <p>最后更新日期:2003年3月20日
* @author Barney,Cherami,Brain
* @version 0.8
*/
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import java.awt.*;
import java.awt.font.*;
import org.jr.util.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class SyntaxParser {
//XML's element names
public static final String STYLE = "style";
public static final String FONT = "font";
public static final String CONTENTTYPE = "contentType";
public static final String TEXT = "text";
public static final String STRING = "string";
public static final String COMMENT = "comment";
public static final String LINE_COMMENT = "line-comment";
public static final String MULTI_COMMENT = "multi-comment";
public static final String KEYWORDS = "keywords";
public static final String KEYWORD = "keyword";
public static final String CLASSES = "classes";
public static final String CLASS = "class";
public static final String PLAIN_TEXT = "plain-text";
public static final String TAG = "tag";
public static final String CONTENT = "content";
public static final String DOC_COMMENT = "doc-comment";
public static final String SCRIPT = "script";
public static final String NAME = "name";
public static final String VALUE = "value";
public static final String SIZE = "size";
public static final String FAMILYNAME = "familyname";
public static final String COLOR = "color";
public static final String ESCAPE = "escape";
public static final String QUOTATIUON = "quotatiuon";
public static final String DELIMITER = "delimiter";
public static final String START = "start";
//default values
public static final String def_Familyname = "SansSerif";
public static final Float def_Size = new Float(12);
public static final Color def_Background = Color.white;
public static final Color def_Foreground = Color.black;
//vars
protected Document doc;
private Element element;
private String contentType = "";
private static final HashMap clrMap;
private File syntaxFile;
static {
/**
* 13种基本颜色
*/
clrMap = new HashMap(13);
clrMap.put("black", Color.black);
clrMap.put("blue", Color.blue);
clrMap.put("cyan", Color.cyan);
clrMap.put("darkGray", Color.darkGray);
clrMap.put("gray", Color.gray);
clrMap.put("green", Color.green);
clrMap.put("lightGray", Color.lightGray);
clrMap.put("magenta", Color.magenta);
clrMap.put("orange", Color.orange);
clrMap.put("pink", Color.pink);
clrMap.put("red", Color.red);
clrMap.put("white", Color.white);
clrMap.put("yellow", Color.yellow);
}
public SyntaxParser(File syntaxFile, String contentType) {
try {
this.contentType = contentType;
openFile(syntaxFile);
getStyle();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public SyntaxParser(File syntaxFile) {
try {
this.contentType = "";
openFile(syntaxFile);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void setContentType(String contentType) {
this.contentType = contentType;
this.getStyle();
}
public String getContentType() {
return contentType;
}
/**
* 返回加亮字体
* @return
*/
public Font[] getFonts() {
HashMap attr = new HashMap();
resetFontAttrs(attr);
Font[] tokenFonts = new Font[SyntaxScanner.NUM_TOKEN_TYPES];
///////////////////////////////////////////////////////
this.getFontAttrs(attr, this.TEXT);
Font font = new Font(attr);
tokenFonts[SyntaxScanner.TEXT] = font;
// resetFontAttrs(attr);
//////////////////////////////////////////////////////
this.getFontAttrs(attr, this.PLAIN_TEXT);
font = new Font(attr);
tokenFonts[SyntaxScanner.PLAIN_TEXT] = font;
// resetFontAttrs(attr);
///////////////////////////////////////////////////////
this.getFontAttrs(attr, this.CONTENT);
font = new Font(attr);
tokenFonts[SyntaxScanner.CONTENT] = font;
// resetFontAttrs(attr);
//////////////////////////////////////////////////////
this.getFontAttrs(attr, this.STRING);
font = new Font(attr);
tokenFonts[SyntaxScanner.STRING] = font;
// resetFontAttrs(attr);
//////////////////////////////////////////////////////
this.getFontAttrs(attr, this.KEYWORDS);
font = new Font(attr);
tokenFonts[SyntaxScanner.KEY_WORD] = font;
// resetFontAttrs(attr);
//////////////////////////////////////////////////////
this.getFontAttrs(attr, this.TAG);
font = new Font(attr);
tokenFonts[SyntaxScanner.TAG] = font;
// resetFontAttrs(attr);
/////////////////////////////////////////////////////
this.getFontAttrs(attr, this.CLASSES);
font = new Font(attr);
tokenFonts[SyntaxScanner.CLASS] = font;
// resetFontAttrs(attr);
/////////////////////////////////////////////////////
this.getFontAttrs(attr, this.LINE_COMMENT);
attr.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
font = new Font(attr);
tokenFonts[SyntaxScanner.LINE_COMMENT] = font;
// resetFontAttrs(attr);
////////////////////////////////////////////////////
this.getFontAttrs(attr, this.MULTI_COMMENT);
font = new Font(attr);
tokenFonts[SyntaxScanner.MULTI_COMMENT] = font;
// resetFontAttrs(attr);
/////////////////////////////////////////////////////
this.getFontAttrs(attr, this.COMMENT);
font = new Font(attr);
tokenFonts[SyntaxScanner.COMMENT] = font;
// resetFontAttrs(attr);
/////////////////////////////////////////////////////
this.getFontAttrs(attr, this.DOC_COMMENT);
font = new Font(attr);
attr.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
font = new Font(attr);
tokenFonts[SyntaxScanner.DOC_COMMENT] = font;
// resetFontAttrs(attr);
/////////////////////////////////////////////////////
this.getFontAttrs(attr, this.SCRIPT);
font = new Font(attr);
tokenFonts[SyntaxScanner.SCRIPT] = font;
///////////////////////////////////////////////////
return tokenFonts;
// return fakeGetFonts();
}
/**
* 测试用的
* @return
*/
private Font[] fakeGetFonts() {
Hashtable attr = new Hashtable();
attr.put(TextAttribute.FAMILY, "SansSerif");
attr.put(TextAttribute.SIZE, new Float(12));
attr.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
attr.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_REGULAR);
attr.put(TextAttribute.FOREGROUND, Color.black);
Font font = new Font(attr);
Font[] tokenFonts = new Font[SyntaxScanner.NUM_TOKEN_TYPES];
tokenFonts[SyntaxScanner.TEXT] = font;
tokenFonts[SyntaxScanner.PLAIN_TEXT] = font;
tokenFonts[SyntaxScanner.CONTENT] = font;
attr.put(TextAttribute.FOREGROUND, new Color(153, 153, 255));
font = new Font(attr);
tokenFonts[SyntaxScanner.STRING] = font;
attr.put(TextAttribute.FOREGROUND, new Color(0, 0, 255));
font = new Font(attr);
tokenFonts[SyntaxScanner.KEY_WORD] = font;
tokenFonts[SyntaxScanner.TAG] = font;
attr.put(TextAttribute.FOREGROUND, Color.red);
font = new Font(attr);
tokenFonts[SyntaxScanner.CLASS] = font;
attr.put(TextAttribute.FOREGROUND, new Color(0, 153, 0));
attr.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
font = new Font(attr);
tokenFonts[SyntaxScanner.LINE_COMMENT] = font;
tokenFonts[SyntaxScanner.MULTI_COMMENT] = font;
tokenFonts[SyntaxScanner.COMMENT] = font;
attr.put(TextAttribute.FOREGROUND, new Color(102, 153, 102));
attr.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
font = new Font(attr);
tokenFonts[SyntaxScanner.DOC_COMMENT] = font;
attr.put(TextAttribute.FOREGROUND, new Color(153, 102, 102));
font = new Font(attr);
tokenFonts[SyntaxScanner.SCRIPT] = font;
return tokenFonts;
}
/**
* 辅助函数,找寻font的属性的
* @param attr
* @param tagName
*/
private void getFontAttrs(HashMap attr, String tagName) {
if (element != null) {
Element e = this.getChildElement(element, tagName);
Element font = this.getChildElement(e, this.FONT);
String str;
str = this.getAttrValue(font, this.FAMILYNAME);
if (str != null && !str.equals("")) {
attr.put(TextAttribute.FAMILY, str);
}
str = this.getAttrValue(font, this.COLOR);
if (str != null && !str.equals("")) {
attr.put(TextAttribute.FOREGROUND,
this.getColor(str, this.def_Foreground));
}
str = this.getAttrValue(font, this.SIZE);
if (str != null && !str.equals("")) {
attr.put(TextAttribute.SIZE, new Float(str));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -