📄 treebuild.java
字号:
m_st.slashStarComments(true);
m_st.slashSlashComments(true);
//System.out.println("slash");
m_st.whitespaceChars(0,' ');
m_st.wordChars(' '+1,'\u00ff');
m_st.ordinaryChar('[');
m_st.ordinaryChar(']');
m_st.ordinaryChar('{');
m_st.ordinaryChar('}');
m_st.ordinaryChar('-');
m_st.ordinaryChar('>');
m_st.ordinaryChar('/');
m_st.ordinaryChar('*');
m_st.quoteChar('"');
m_st.whitespaceChars(';',';');
m_st.ordinaryChar('=');
}
/**
* This is the alternative syntax for the tokenizer.
*/
private void alterSyntax() {
m_st.resetSyntax();
m_st.wordChars('\u0000', '\u00ff');
m_st.slashStarComments(false);
m_st.slashSlashComments(false);
m_st.ordinaryChar('\n');
m_st.ordinaryChar('\r');
}
/**
* This will parse the next token out of the stream and check for certain
* conditions.
*
* @param r The error string to print out if something goes wrong.
*/
private void nextToken(String r) {
int t = 0;
try {
t = m_st.nextToken();
} catch(IOException e) {
}
if (t == StreamTokenizer.TT_EOF) {
System.out.println("eof , " + r);
}
else if (t == StreamTokenizer.TT_NUMBER) {
System.out.println("got a number , " + r);
}
}
/**
* Parses the top of the dotty stream that has the graph information.
*
*/
private void graph() {
nextToken("expected 'digraph'");
if (m_st.sval.equalsIgnoreCase("digraph")) {
}
else {
System.out.println("expected 'digraph'");
}
nextToken("expected a Graph Name");
if (m_st.sval != null) {
m_graphName = m_st.sval;
}
else {
System.out.println("expected a Graph Name");
}
nextToken("expected '{'");
if (m_st.ttype == '{') {
stmtList();
}
else {
System.out.println("expected '{'");
}
}
/**
* This is one of the states, this one is where new items can be defined
* or the structure can end.
*
*/
private void stmtList() {
boolean flag = true;
while(flag) {
nextToken("expects a STMT_LIST item or '}'");
if (m_st.ttype == '}') {
flag = false;
}
else if (m_st.sval.equalsIgnoreCase("graph") ||
m_st.sval.equalsIgnoreCase("node") ||
m_st.sval.equalsIgnoreCase("edge")) {
m_st.pushBack();
attrStmt();
}
else if (m_st.sval != null) {
nodeId(m_st.sval,0);
}
else {
System.out.println("expects a STMT_LIST item or '}'");
}
}
}
/**
* This will deal specifically with a new object such as graph , node , edge.
*
*/
private void attrStmt() {
nextToken("expected 'graph' or 'node' or 'edge'");
if (m_st.sval.equalsIgnoreCase("graph")) {
nextToken("expected a '['");
if (m_st.ttype == '[') {
attrList(m_grObj);
}
else {
System.out.println("expected a '['");
}
}
else if (m_st.sval.equalsIgnoreCase("node")) {
nextToken("expected a '['");
if (m_st.ttype == '[') {
attrList(m_noObj);
}
else {
System.out.println("expected a '['");
}
}
else if (m_st.sval.equalsIgnoreCase("edge")) {
nextToken("expected a '['");
if (m_st.ttype == '[') {
attrList(m_edObj);
}
else {
System.out.println("expected a '['");
}
}
else {
System.out.println("expected 'graph' or 'node' or 'edge'");
}
}
/**
* Generates a new InfoObject with the specified name and either does
* further processing if applicable
* Otherwise it is an edge and will deal with that.
*
* @param s The ID string.
* @param t Not sure!.
*/
private void nodeId(String s,int t) {
nextToken("error occurred in node_id");
if (m_st.ttype == '}') {
//creates a node if t is zero
if (t == 0) {
m_nodes.addElement(new InfoObject(s));
}
m_st.pushBack();
}
else if (m_st.ttype == '-') {
nextToken("error occurred checking for an edge");
if (m_st.ttype == '>') {
edgeStmt(s);
}
else {
System.out.println("error occurred checking for an edge");
}
}
else if (m_st.ttype == '[') {
//creates a node if t is zero and sends it to attr
if (t == 0) {
m_nodes.addElement(new InfoObject(s));
attrList(m_nodes.lastElement());
}
else {
attrList(m_edges.lastElement());
}
}
else if (m_st.sval != null) {
//creates a node if t is zero
if (t == 0) {
m_nodes.addElement(new InfoObject(s));
}
m_st.pushBack();
}
else {
System.out.println("error occurred in node_id");
}
}
/**
* This will get the target of the edge.
*
* @param i The source of the edge.
*/
private void edgeStmt(String i) {
nextToken("error getting target of edge");
if (m_st.sval != null) {
m_edges.addElement(new InfoObject("an edge ,no id"));
m_edges.lastElement().m_source = i;
m_edges.lastElement().m_target = m_st.sval;
nodeId(m_st.sval,1);
}
else {
System.out.println("error getting target of edge");
}
}
/**
* This will parse all the items in the attrib list for an object.
*
* @param a The object that the attribs apply to.
*/
private void attrList(InfoObject a) {
boolean flag = true;
while (flag) {
nextToken("error in attr_list");
//System.out.println(st.sval);
if (m_st.ttype == ']') {
flag = false;
}
else if (m_st.sval.equalsIgnoreCase("color")) {
nextToken("error getting color");
if (m_st.ttype == '=') {
nextToken("error getting color");
if (m_st.sval != null) {
a.m_color = m_st.sval;
}
else {
System.out.println("error getting color");
}
}
else {
System.out.println("error getting color");
}
}
else if (m_st.sval.equalsIgnoreCase("fontcolor")) {
nextToken("error getting font color");
if (m_st.ttype == '=') {
nextToken("error getting font color");
if (m_st.sval != null) {
a.m_fontColor = m_st.sval;
}
else {
System.out.println("error getting font color");
}
}
else {
System.out.println("error getting font color");
}
}
else if (m_st.sval.equalsIgnoreCase("fontsize")) {
nextToken("error getting font size");
if (m_st.ttype == '=') {
nextToken("error getting font size");
if (m_st.sval != null) {
a.m_fontSize = m_st.sval;
}
else {
System.out.println("error getting font size");
}
}
else {
System.out.println("error getting font size");
}
}
else if (m_st.sval.equalsIgnoreCase("label")) {
nextToken("error getting label");
if (m_st.ttype == '=') {
nextToken("error getting label");
if (m_st.sval != null) {
a.m_label = m_st.sval;
}
else {
System.out.println("error getting label");
}
}
else {
System.out.println("error getting label");
}
}
else if (m_st.sval.equalsIgnoreCase("shape")) {
nextToken("error getting shape");
if (m_st.ttype == '=') {
nextToken("error getting shape");
if (m_st.sval != null) {
a.m_shape = m_st.sval;
}
else {
System.out.println("error getting shape");
}
}
else {
System.out.println("error getting shape");
}
}
else if (m_st.sval.equalsIgnoreCase("style")) {
nextToken("error getting style");
if (m_st.ttype == '=') {
nextToken("error getting style");
if (m_st.sval != null) {
a.m_style = m_st.sval;
}
else {
System.out.println("error getting style");
}
}
else {
System.out.println("error getting style");
}
}
else if (m_st.sval.equalsIgnoreCase("data")) {
nextToken("error getting data");
if (m_st.ttype == '=') {
//data has a special data string that can have anything
//this is delimited by a single comma on an otherwise empty line
alterSyntax();
a.m_data = new String("");
while (true) {
nextToken("error getting data");
if (m_st.sval != null && a.m_data
!= null && m_st.sval.equals(",")) {
break;
}
else if (m_st.sval != null) {
a.m_data = a.m_data.concat(m_st.sval);
}
else if (m_st.ttype == '\r') {
a.m_data = a.m_data.concat("\r");
}
else if (m_st.ttype == '\n') {
a.m_data = a.m_data.concat("\n");
}
else {
System.out.println("error getting data");
}
}
setSyntax();
}
else {
System.out.println("error getting data");
}
}
}
}
//special class for use in creating the tree
/**
* This is an internal class used to keep track of the info for the objects
* before they are
* actually created.
*/
private class InfoObject {
/** The ID string for th object. */
public String m_id;
/** The color name for the object. */
public String m_color;
/** The font color for the object. not in use. */
public String m_fontColor;
/** The fontsize for the object. not in use. */
public String m_fontSize;
/** The label for the object. */
public String m_label;
/** The shape name of for the object. */
public String m_shape;
/** The backstyle name for the object. */
public String m_style;
/** The source ID of the object. */
public String m_source;
/** The target ID of the object. */
public String m_target;
/** The data for this object. */
public String m_data;
/**
* This will construct a new InfoObject with the specified ID string.
*/
public InfoObject(String i) {
m_id = i;
m_color = null;
m_fontColor = null;
m_fontSize = null;
m_label = null;
m_shape = null;
m_style = null;
m_source = null;
m_target = null;
m_data = null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -