//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
*
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.framework.menu;
import com.nexirius.util.TextToken;
import com.nexirius.util.XFile;
import java.io.PushbackInputStream;
public class MenuParser {
public final static String TOOLBAR = "TOOLBAR";
public final static String MENUBAR = "MENUBAR";
public final static String MENU = "MENU";
public final static String TOGGLE = "TOGGLE";
public final static String ITEM = "ITEM";
public final static String SEPARATOR = "SEPARATOR";
public final static String END = "END";
protected String menuFileName;
public MenuParser(String menuFileName) throws Exception {
this.menuFileName = menuFileName;
}
public void parse(MenuCreator creator) throws Exception {
parse(createStream(), creator);
}
private PushbackInputStream createStream() {
XFile xf = new XFile(menuFileName);
try {
return new PushbackInputStream(xf.getBufferedInputStream());
} catch (Exception ex) {
throw new RuntimeException("Can't open menu file '" + menuFileName + "'");
}
}
private void parse(PushbackInputStream in, MenuCreator creator) throws Exception {
TextToken textToken = nextToken(in);
while (textToken != null) {
if (textToken.isIdentifier(MENUBAR)) {
creator.newMenuBar();
parseMenuBar(in, creator);
} else if (textToken.isIdentifier(TOOLBAR)) {
creator.newToolBar();
parseToolBar(in, creator);
} else {
throw new Exception("Expecting MENUBAR or TOOLBAR but have: " + textToken);
}
textToken = nextToken(in);
}
in.close();
}
private void parseMenuBar(PushbackInputStream in, MenuCreator creator) throws Exception {
TextToken textToken = nextToken(in);
while (textToken != null) {
if (textToken.isIdentifier(MENU)) {
parseMenu(in, creator);
} else if (textToken.isIdentifier(END)) {
creator.end();
return;
} else {
throw new Exception("Expecting MENU or END but have: " + textToken);
}
textToken = nextToken(in);
}
}
private void parseToolBar(PushbackInputStream in, MenuCreator creator) throws Exception {
parseItems(in, creator);
}
private void parseMenu(PushbackInputStream in, MenuCreator creator) throws Exception {
TextToken textToken = nextToken(in);
if (textToken == null || !textToken.isString()) {
throw new Exception("Expecting string literal (menu name) but have: " + textToken);
}
creator.addMenu(textToken.getString());
parseItems(in, creator);
}
private void parseToggle(PushbackInputStream in, MenuCreator creator) throws Exception {
TextToken textToken = nextToken(in);
if (textToken == null || !textToken.isString()) {
throw new Exception("Expecting string literal (toggle menu name) but have: " + textToken);
}
creator.addToggle(textToken.getString());
parseItems(in, creator);
}
private void parseItems(PushbackInputStream in, MenuCreator creator) throws Exception {
TextToken textToken = nextToken(in);
while (textToken != null) {
if (textToken.isIdentifier(ITEM)) {
textToken = nextToken(in);
if (textToken == null || !textToken.isString()) {
throw new Exception("Expecting string literal (menu item name) but have: " + textToken);
}
creator.addMenuItem(textToken.getString());
} else if (textToken.isIdentifier(END)) {
creator.end();
return;
} else if (textToken.isIdentifier(SEPARATOR)) {
creator.addSeparator();
} else if (textToken.isIdentifier(MENU)) {
parseMenu(in, creator);
} else if (textToken.isIdentifier(TOGGLE)) {
parseToggle(in, creator);
} else {
throw new Exception("Expecting ITEM or SEPARATOR or END but have: " + textToken);
}
textToken = nextToken(in);
}
}
private TextToken nextToken(PushbackInputStream in) throws Exception {
TextToken ret = TextToken.nextToken(in);
while (ret != null && ret.isChar('#')) {
TextToken.skipLine(in);
ret = TextToken.nextToken(in);
}
return ret;
}
}