%this file reads the line scratch info from an xml file
%   Input :
%   1/ fileName : name of the file (inlcude the .xml in the fileName)
%   2/ Maximum number of frames in the video sequence

%   Output :
%   scratchesOut : [x1 y1 x2 y2] %% (first and last points of the scratch segment)

function[scratchesOut] = read_xml_line_scratches(fileName)
    
    if (~exist(fileName,'file'))
        disp 'Error. The XML file does not exist in the current path.';
        return;
    end

    scratchDoc = xmlread(fileName);
    rootElement = scratchDoc.getDocumentElement;      %get the root element

    currentFrame = rootElement.getFirstChild;      %get the first child

    newScratches = cell(1);    %create a cell structure with the necessary amount of cells (number of frames)
    ii=1;

    while (~isempty(currentFrame) && ~isempty(currentFrame.getNextSibling))
       jj=1;
       %move forward a sibling
       currentFrame = currentFrame.getNextSibling;    %note : we move forward two siblings (one at the beginning of the 
       %loop, and one at the end, because for some reason empty siblings
       %are given every other node when reading the XML file

       currentScratch = currentFrame.getFirstChild;
       currentScratchOut = [];
       while (~isempty(currentScratch) &&~isempty(currentScratch.getNextSibling))
           %move forward a sibling
           currentScratch = currentScratch.getNextSibling;    %same note as for the level above (skip one sibling)

           %get the scratch information
           currentScratchOut(jj,1) = str2num(currentScratch.getAttribute('x1'));
           currentScratchOut(jj,2) = str2num(currentScratch.getAttribute('y1'));
           currentScratchOut(jj,3) = str2num(currentScratch.getAttribute('x2'));
           currentScratchOut(jj,4) = str2num(currentScratch.getAttribute('y2'));

           jj=jj+1;
           currentScratch = currentScratch.getNextSibling;
       end
       newScratches{ii} = currentScratchOut;
       currentFrame = currentFrame.getNextSibling;
       ii=ii+1;
    end

    scratchesOut = newScratches;
end