package grammar.input.refdata;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import grammar.input.Configuration;
import grammar.model.Language;
import grammar.output.tts.SpeechSynthesiser;
public class LanguageHandler extends DataHandler<List<Language>> {
private List<Language> languages = new ArrayList<Language>();
private String identifier;
private String nativeName;
private String reflexiveVerbInfinitivePattern;
private String reflexiveCliticText;
private String elidedReflexiveCliticText;
private SpeechSynthesiser voiceSynthesiser;
private boolean nativeContext;
private boolean cliticContext;
private boolean elidedContext;
public void startElement(String uri, String localName, String qName, Attributes attributes) {
if (qName.equals("identifier")) {
enableParsing(ParseMode.NAME);
nativeContext = false;
cliticContext = false;
elidedContext = false;
}
else if (qName.equals("native-name")) {
enableParsing(ParseMode.NAME);
nativeContext = true;
cliticContext = false;
elidedContext = false;
}
else if (qName.equals("pattern")) {
enableParsing(ParseMode.PATTERN);
}
else if (qName.equals("clitic-text")) {
enableParsing(ParseMode.NAME);
nativeContext = false;
cliticContext = true;
elidedContext = false;
}
else if (qName.equals("clitic-elided-text")) {
enableParsing(ParseMode.NAME);
nativeContext = false;
cliticContext = true;
elidedContext = true;
}
else if (qName.equals("synthesiser-class")) {
enableParsing(ParseMode.CLASS_NAME);
}
}
public void endElement(String uri, String localName, String qName) {
String str = getParsedString();
ParseMode mode = getParseMode();
if (mode.equals(ParseMode.NAME) && cliticContext && !elidedContext) {
reflexiveCliticText = str;
}
else if (qName.equals("synthesiser-class")) {
try {
Class<SpeechSynthesiser> synthesiserClass = (Class<SpeechSynthesiser>) Class.forName(str);
voiceSynthesiser = synthesiserClass.newInstance();
}
catch (Exception e) {
if (Configuration.getInstance().warning())
System.err.println("Warning: failed to load speech synthesis engine for language '"+
nativeName+"'; text-to-speech functionality disabled for this language.");
// throw new RuntimeException("Unable to load VoiceSynthesiser class: "+str, e);
}
}
else if (mode.equals(ParseMode.NAME) && cliticContext && elidedContext) {
elidedReflexiveCliticText = str;
}
else if (mode.equals(ParseMode.NAME) && !nativeContext) {
identifier = str;
}
else if (mode.equals(ParseMode.NAME) && nativeContext) {
nativeName = str;
}
else if (mode.equals(ParseMode.PATTERN)) {
reflexiveVerbInfinitivePattern = str;
}
else if (qName.equals("language")) {
if (identifier == null)
throw new IllegalStateException("Language's identifier not defined.");
if (nativeName == null)
throw new IllegalStateException("Language's native name not defined.");
languages.add(new Language(identifier, nativeName, reflexiveVerbInfinitivePattern,
reflexiveCliticText, elidedReflexiveCliticText, voiceSynthesiser));
}
disableParsing();
}
public List<Language> getParsedData() {
return languages;
}
}