function [metadata,dom] = readMetadata(id, componentDir)
metadata = struct;
metadata.id = id;

tokens = regexp(id,'(\w+)-(\w+)','tokens','once');
metadata.component = tokens{1};
metadata.filename = tokens{2};
metadata.componentDir = componentDir;

examplesXml = fullfile(metadata.componentDir,'examples.xml');
dom = exampleUtils.getExampleDom(metadata.filename, examplesXml);
% Main file
mainNode = dom.getElementsByTagName('source').item(0);
metadata.main = getChar(mainNode);
metadata.extension = getChar(dom.getElementsByTagName('extension').item(0));

% Supporting files and subfolders.
fileNodes = dom.getElementsByTagName('file');
dirNodes = dom.getElementsByTagName('dir');
files = cell(0,1);
dirs = cell(dirNodes.getLength(),1);
for i = 1:fileNodes.getLength()
    fileNode = fileNodes.item(i-1);
    fileName = getChar(fileNode);
    if startsWith(fileName, 'html/')
        continue;
    end
    files{end+1,1} = struct( ...
        'filename', getChar(fileNode), ...
        'component', metadata.component, ...
        'componentDir', exampleUtils.componentExamplesDir(metadata.component), ...
        'timestamp', char(fileNode.getAttribute('timestamp')), ...
        'open', char(fileNode.getAttribute('open')));
end

for i = 1:dirNodes.getLength()
    dirNode = dirNodes.item(i-1);
    dirs{i,1} = struct( ...
        'dirname', getChar(dirNode), ...
        'component', metadata.component, ...
        'componentDir', exampleUtils.componentExamplesDir(metadata.component), ...
        'timestamp', char(dirNode.getAttribute('timestamp')));
end

metadata.files = files;
metadata.dirs = dirs;

productNodes = dom.getElementsByTagName('product');
metadata.products = '';
products = [];
for i = 1:productNodes.getLength()
    productNode = productNodes.item(i-1);
    products{end+1} = getChar(productNode);
end
if ~isempty(products)
    metadata.products = strjoin(products, ',');
end

% Callback.
cb = getNodeValue(dom,'callback','char');
if ~isempty(cb)
    metadata.callback = cb;
end

% Sandbox-published
sp = getNodeValue(dom,'sandboxPublished','logical');
if ~isempty(sp)
    metadata.sandboxPublished = true;
end

% Hide code
hc = getNodeValue(dom,'hideCode','logical');
if ~isempty(hc)
    metadata.hideCode = true;
end

% Thumbnail
metadata.thumbnail = '';
tn = getNodeValue(dom,'thumbnail','char');
if ~isempty(tn)
    metadata.thumbnailIsCheckedIn = ~startsWith(tn, 'html/');
    metadata.thumbnail = regexprep(tn, '(?:data|html)/', '');
end

% Project
prNode = dom.getElementsByTagName('project').item(0);
if ~isempty(prNode)
    metadata.project = struct(...
        'path', getChar(prNode), ...
        'open', char(prNode.getAttribute('open')), ... 
        'cmSystem', strcmp(char(prNode.getAttribute('cmSystem')),'true'));
end

end

function s = getNodeValue(dom,nodeName,nodeType)
s = [];
nodes = dom.getElementsByTagName(nodeName);
if nodes.getLength() ~= 1
    % TODO error?
    return;
else
    node = nodes.item(0);
    switch nodeType
        case 'char'
            s = getChar(node);
        case 'logical'
            % Don't actually check the node value, its presence indicates
            % the value was set.
            s = true;
    end
end
    
    
end

function s = getChar(n)
textNode = n.getFirstChild();
s = '';
if ~isempty(textNode)    
    s = char(textNode.getData);
end
end

