from
configInputParser
by Luca Balbi
configures an inputParser object with the configuration contained in an XML file
|
| configInputParser(xmlConfigFile)
|
function configuredParser = configInputParser(xmlConfigFile)
%CONFIGINPUTPARSER configures InputParser based on XML file
% ip = CONFIGINPUTPARSER(xmlFilePath) loads the specified XML file,
% and returns an inputParser object configured according to the
% settings contained in it.
% See the comments in the sample XML file sampleConfig.xml and readme.html
% for explanation of the mapping between the XML structures
% and the inputParser properties and arguments
%
% Example
% myIP = configInputParser('sampleConfig.xml');
%
% See also inputParser
%
% Copyright 2007 Luca Balbi
%% Default initialisation
% Default inputParser option set is used unless an explicit
% setting is found in the XML file
% Default inputParser object
configuredParser = inputParser;
%% Loading and parsing of XML file
% Read the XML file into a DOM object
l_xDOM = xmlread(xmlConfigFile);
% Extract the ParseInputConfiguration tag
l_xConf = l_xDOM.getElementsByTagName('ParseInputConfiguration');
% List of attributes of the Properties element, representing the inputParser
% properties which will be set
l_xProperties = l_xConf.item(0).getElementsByTagName('Properties').item(0).getChildNodes;
%% Loop for assigning the inputParser properties
for k=0:l_xProperties.getLength-1
if l_xProperties.item(k).getNodeType == l_xDOM.ELEMENT_NODE
% Store the name of the property
l_sNodeName = char(l_xProperties.item(k).getNodeName);
% Store the value of the property
l_sNodeValue = char(l_xProperties.item(k).getFirstChild.getNodeValue);
configuredParser.(l_sNodeName) = eval(l_sNodeValue);
end % if
end % for
%% Loop for defining the inputParser arguments
% List of Argument nodes
l_xParams = l_xConf.item(0).getElementsByTagName('Arguments').item(0).getElementsByTagName('Argument');
% Array of structures holding the argument proprties
l_ArgumentsSettings = [];
for k=0:l_xParams.getLength-1
l_xElementList = l_xParams.item(k).getChildNodes;
for m=0:l_xElementList.getLength-1
if l_xElementList.item(m).getNodeType == l_xDOM.ELEMENT_NODE
l_ArgumentsSettings{1+k}.(char(l_xElementList.item(m).getNodeName)) = eval(char(l_xElementList.item(m).getFirstChild.getNodeValue)); %#ok<AGROW> % no need for speed concerns, few elements will be parsed
end % if
end % for
end % for
%% Actual definition of the inputParser arguments
for k=1:length(l_ArgumentsSettings)
l_ArgumentSettings = l_ArgumentsSettings{k};
% Check if a validator definition is needed
switch l_ArgumentSettings.Type
case 'Required'
if isfield(l_ArgumentSettings,'Validator') % validator present
configuredParser.addRequired(l_ArgumentSettings.Name,l_ArgumentSettings.Validator)
else % validator not needed
configuredParser.addRequired(l_ArgumentSettings.Name)
end % if
case 'Optional'
if isfield(l_ArgumentSettings,'Validator') % validator present
configuredParser.addOptional(l_ArgumentSettings.Name,l_ArgumentSettings.DefaultValue,l_ArgumentSettings.Validator)
else % validator not needed
configuredParser.addOptional(l_ArgumentSettings.Name,l_ArgumentSettings.DefaultValue)
end % if
case 'ParameterValue'
if isfield(l_ArgumentSettings,'Validator') % validator present
configuredParser.addParamValue(l_ArgumentSettings.Name,l_ArgumentSettings.DefaultValue,l_ArgumentSettings.Validator)
else % validator not needed
configuredParser.addParamValue(l_ArgumentSettings.Name,l_ArgumentSettings.DefaultValue)
end % if
end % switch
end % for
end % function
|
|
Contact us at files@mathworks.com