package org.xins.common.spec;
import java.io.IOException;
import java.io.Reader;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.w3c.dom.Element;
import org.xins.common.MandatoryArgumentChecker;
import org.xins.common.xml.ElementList;
import org.xins.common.xml.ElementFormatter;
import org.xml.sax.SAXException;
public final class ErrorCodeSpec {
public static final Type FUNCTIONAL = new Type();
public static final Type TECHNICAL = new Type();
private final String _errorCodeName;
private String _description;
private Map<String, ParameterSpec> _outputParameters = new LinkedHashMap<String, ParameterSpec>();
private Map _outputDataSectionElements;
private Type _type;
public ErrorCodeSpec(String name, Class reference, String baseURL)
throws IllegalArgumentException, InvalidSpecificationException {
MandatoryArgumentChecker.check("name", name, "reference", reference, "baseURL", baseURL);
_errorCodeName = name;
try {
Reader reader = APISpec.getReader(baseURL, name + ".rcd");
parseErrorCode(reader, reference);
} catch (IOException ioe) {
throw new InvalidSpecificationException("[ErrorCode: " + name + "] Cannot read error code.", ioe);
}
}
public String getName() {
return _errorCodeName;
}
public String getDescription() {
return _description;
}
public ParameterSpec getOutputParameter(String parameterName)
throws EntityNotFoundException, IllegalArgumentException {
MandatoryArgumentChecker.check("parameterName", parameterName);
ParameterSpec parameter = _outputParameters.get(parameterName);
if (parameter == null) {
throw new EntityNotFoundException("Output parameter \"" + parameterName
+ "\" not found in the error code \"" + _errorCodeName +"\".");
}
return parameter;
}
public Map<String, ParameterSpec> getOutputParameters() {
return _outputParameters;
}
public DataSectionElementSpec getOutputDataSectionElement(String elementName)
throws EntityNotFoundException, IllegalArgumentException {
MandatoryArgumentChecker.check("elementName", elementName);
DataSectionElementSpec element = (DataSectionElementSpec) _outputDataSectionElements.get(elementName);
if (element == null) {
throw new EntityNotFoundException("Output data section element \"" + elementName
+ "\" not found in the error code \"" + _errorCodeName +"\".");
}
return element;
}
public Map getOutputDataSectionElements() {
return _outputDataSectionElements;
}
public Type getType() {
return _type;
}
private void parseErrorCode(Reader reader, Class reference)
throws IllegalArgumentException, IOException, InvalidSpecificationException {
MandatoryArgumentChecker.check("reader", reader, "reference", reference);
Element errorCode;
try {
errorCode = ElementFormatter.parse(reader);
} catch (SAXException pe) {
throw new InvalidSpecificationException("[ErrorCode: " + _errorCodeName + "] Cannot parse error code.", pe);
}
String typeAsString = errorCode.getAttribute("type");
if (typeAsString.equals("") || typeAsString.equals("technical")) {
_type = TECHNICAL;
} else if (typeAsString.equals("functional")) {
_type = FUNCTIONAL;
} else {
throw new InvalidSpecificationException("[ErrorCode: " + _errorCodeName
+ "] Incorrect type specified.");
}
ElementList descriptionElementList = new ElementList(errorCode, "description");
if (descriptionElementList.isEmpty()) {
throw new InvalidSpecificationException("[ErrorCode: " + _errorCodeName
+ "] No definition specified.");
}
Element descriptionElement = descriptionElementList.get(0);
_description = descriptionElement.getTextContent();
if (_description == null) {
_description = "";
}
ElementList output = new ElementList(errorCode, "output");
if (!output.isEmpty()) {
Element outputElement = output.get(0);
_outputParameters = FunctionSpec.parseParameters(reference, outputElement);
ElementList dataSections = new ElementList(outputElement, "data");
if (!dataSections.isEmpty()) {
Element dataSection = dataSections.get(0);
_outputDataSectionElements = FunctionSpec.parseDataSectionElements(reference, dataSection, dataSection);
}
}
}
public static class Type {
private Type() {
}
}
}