CompletionXml.dtd DTD found in
* this package.
*
* @author Robert Futrell
* @version 1.0
*/
public class CompletionXMLParser extends DefaultHandler {
/**
* The completions found after parsing the XML.
*/
private Listnull, then a
* default class loader is used. This field will usually be null.
*/
private static ClassLoader DEFAULT_COMPLETION_CLASS_LOADER;
/**
* Constructor.
*
* @param provider The provider to get completions for.
* @see #reset(CompletionProvider)
*/
public CompletionXMLParser(CompletionProvider provider) {
this(provider, null);
}
/**
* Constructor.
*
* @param provider The provider to get completions for.
* @param cl The class loader to use, if necessary, when loading classes
* from the XML (custom {@link FunctionCompletion}s, for
* example). This may be null if the default is to
* be used, or if the XML does not define specific classes for
* completion types.
* @see #reset(CompletionProvider)
*/
public CompletionXMLParser(CompletionProvider provider, ClassLoader cl) {
this.provider = provider;
this.completionCL = cl;
if (completionCL == null) {
// May also be null, but that's okay.
completionCL = DEFAULT_COMPLETION_CLASS_LOADER;
}
completions = new ArrayListnull if none was specified.
*/
public String getParamSeparator() {
return paramSeparator;
}
/**
* Returns the parameter start character specified.
*
* @return The character, or 0 if none was specified.
*/
public char getParamStartChar() {
return paramStartChar;
}
private static final char getSingleChar(String str) {
return str.length() == 1 ? str.charAt(0) : 0;
}
/**
* Resets this parser to grab more completions.
*
* @param provider The new provider to get completions for.
*/
public void reset(CompletionProvider provider) {
this.provider = provider;
completions.clear();
doingKeywords = inKeyword = gettingDesc = gettingParams = inParam = gettingParamDesc = false;
paramStartChar = paramEndChar = 0;
paramSeparator = null;
}
@Override
public InputSource resolveEntity(String publicID, String systemID) throws SAXException {
return new InputSource(getClass().getResourceAsStream("CompletionXml.dtd"));
}
/**
* Sets the class loader to use when loading custom classes to use for various
* {@link Completion} types, such as {@link FunctionCompletion}s, from XML.
*
*
* Users should very rarely have a need to use this method.
*
* @param cl The class loader to use. If this is null, then a
* default is used.
*/
public static void setDefaultCompletionClassLoader(ClassLoader cl) {
DEFAULT_COMPLETION_CLASS_LOADER = cl;
}
/**
* Called when an element starts.
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attrs) {
if ("keywords".equals(qName)) {
doingKeywords = true;
} else if (doingKeywords) {
if ("keyword".equals(qName)) {
name = attrs.getValue("name");
type = attrs.getValue("type");
returnType = attrs.getValue("returnType");
params.clear();
definedIn = attrs.getValue("definedIn");
inKeyword = true;
} else if (inKeyword) {
if ("returnValDesc".equals(qName)) {
gettingReturnValDesc = true;
} else if ("params".equals(qName)) {
gettingParams = true;
} else if (gettingParams) {
if ("param".equals(qName)) {
paramName = attrs.getValue("name");
paramType = attrs.getValue("type");
inParam = true;
}
if (inParam) {
if ("desc".equals(qName)) {
gettingParamDesc = true;
}
}
} else if ("desc".equals(qName)) {
gettingDesc = true;
}
}
} else if ("environment".equals(qName)) {
paramStartChar = getSingleChar(attrs.getValue("paramStartChar"));
paramEndChar = getSingleChar(attrs.getValue("paramEndChar"));
paramSeparator = attrs.getValue("paramSeparator");
// paramTerminal = attrs.getValua("terminal");
} else if ("completionTypes".equals(qName)) {
inCompletionTypes = true;
} else if (inCompletionTypes) {
if ("functionCompletionType".equals(qName)) {
funcCompletionType = attrs.getValue("type");
}
}
}
@Override
public void warning(SAXParseException e) throws SAXException {
throw e;
}
}