source: trunk/src/editxml.m @ 54

Last change on this file since 54 was 54, checked in by sommeria, 14 years ago

sub_background modified to accept various movie and image inputs
geometry_calib, editxml, civ modified to read a unique PARAM.xml parameter file (instead of PARAM_LINUX...)

File size: 63.0 KB
Line 
1%'editxml': function for editing xml files using a xml schema (associated with the GUI editxml.fig)
2%------------------------------------------------------------------------
3% function heditxml=editxml(inputfile)
4%
5%OUTPUT: heditxml: graphic handle of the GUI
6%
7%INPUT: inputfile:  name of an xml file
8%
9%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
10%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
11%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
12%     This file is part of the toolbox UVMAT.
13%
14%     UVMAT is free software; you can redistribute it and/or modify
15%     it under the terms of the GNU General Public License as published by
16%     the Free Software Foundation; either version 2 of the License, or
17%     (at your option) any later version.
18%
19%     UVMAT is distributed in the hope that it will be useful,
20%     but WITHOUT ANY WARRANTY; without even the implied warranty of
21%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
23%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
24
25function varargout = editxml(varargin)
26
27% Begin initialization code - DO NOT EDIT
28gui_Singleton = 1;
29gui_State = struct('gui_Name',       mfilename, ...
30                   'gui_Singleton',  gui_Singleton, ...
31                   'gui_OpeningFcn', @editxml_OpeningFcn, ...
32                   'gui_OutputFcn',  @editxml_OutputFcn, ...
33                   'gui_LayoutFcn',  [] , ...
34                   'gui_Callback',   []);
35if nargin & isstr(varargin{1})
36    gui_State.gui_Callback = str2func(varargin{1});
37end
38
39if nargout
40    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
41else
42    gui_mainfcn(gui_State, varargin{:});
43end
44% End initialization code - DO NOT EDIT
45
46
47% --- Executes just before editxml is made visible.
48function editxml_OpeningFcn(hObject, eventdata, handles, varargin)
49% This function has no output args, see OutputFcn.
50% hObject    handle to figure
51% eventdata  reserved - to be defined in a future version of MATLAB
52% handles    structure with handles and user data (see GUIDATA)
53% varargin   command line arguments to editxml (see VARARGIN)
54
55% set(handles.replicate,'String',['copy';'<---'])
56if nargin
57    CurrentFile=varargin{1}
58else
59    CurrentFile=[];
60end
61% if exist('varargin') & length(varargin)>=1
62%     CurrentFile=cell2mat(varargin{1});
63% else
64%     CurrentFile=[];
65% end
66% Choose default command line output for editxml
67handles.output = hObject;
68% set(hObject,'Units','pixel')
69if exist(CurrentFile,'file')
70    [PathName,Nme,FileExt]=fileparts(CurrentFile);
71    if isequal(FileExt,'.xls')
72        DataIn.hfig_xls=read_xls(CurrentFile);% DataIn.hfig_xls=handle of the Excel display figure
73        DataIn.CurrentUid=1;
74        figpos=get(hObject,'Position');%position of the editxml interface
75        figposunit=get(hObject,'Units');%unity used to indicate position
76        newfigpos=[figpos(1)-0.5*figpos(3) figpos(2) figpos(3) figpos(4)];
77        set(DataIn.hfig_xls,'Units',figposunit)
78        set(DataIn.hfig_xls,'Position',newfigpos); %set position of the Excel display figure
79        set(hObject,'UserData',DataIn)
80    else
81        set(handles.CurrentFile,'String',CurrentFile)
82        CurrentFile_Callback(hObject, eventdata, handles)
83    end
84end
85% Update handles structure
86guidata(hObject, handles);
87
88%----------------------------------------------------------------
89% --- Outputs from this function are returned to the command line.
90function varargout = editxml_OutputFcn(hObject, eventdata, handles)
91% varargout  cell array for returning output args (see VARARGOUT);
92% hObject    handle to figure
93% eventdata  reserved - to be defined in a future version of MATLAB
94% handles    structure with handles and user data (see GUIDATA)
95
96% Get default command line output from handles structure
97varargout{1} = handles.output;
98
99
100% --- Executes on selection change in list_element.
101function list_element_Callback(hObject, eventdata, handles)
102global t xs t_ref
103CurrentFile=get(handles.CurrentFile,'String');
104bla=get(hObject,'String');
105ind=get(hObject,'Value');
106list=get(hObject,'UserData');
107NewRootUid=list.uid(ind);
108heditxml=get(hObject,'Parent');
109DataIn=get(heditxml,'UserData');
110if ~isempty(xs)
111        xs_node=list.xs_uid(ind);%xs_node of the subelement #ind
112        [nodeup,path,xs_element,xs_subelem]=scan_schema(xs,xs_node);
113        [element,subelem]=get_xml(t,path,xs_element,NewRootUid,xs_subelem);
114        update_list(handles,path,xs_element,element,NewRootUid,xs_subelem,subelem);
115        if xs_element.subtest     
116        DataIn.CurrentUid=[DataIn.CurrentUid NewRootUid];%record new current uid
117        DataIn.xs_CurrentUid=[DataIn.xs_CurrentUid xs_node];%record the new curent schema uid
118        end
119   
120%     %update the import file display
121%     if isfield(DataIn,'h_ref')&ishandle(DataIn.h_ref)
122%         tag0_ref=find(t_ref,['/' path '/' xs_element.key]);
123%         node_ref=list.index(ind);
124%         if length(tag0_ref)<node_ref
125%             node_ref=length(tag0_ref);
126%         end
127%         [ref_element,ref_subelem]=get_xml(t_ref,path,xs_element,node_ref,xs_subelem);
128%         update_ref_list(DataIn.h_ref,xs_element,ref_element,node_ref,xs_subelem,ref_subelem);
129%     end 
130set(get(hObject,'Parent'),'UserData',DataIn);
131else%no schema
132    [DataIn,testsimple]=displ_xml(handles,t,NewRootUid,DataIn,get(hObject,'parent'));
133    if ~testsimple
134        DataIn.CurrentUid=[DataIn.CurrentUid NewRootUid];%record new current uid
135        set(get(hObject,'Parent'),'UserData',DataIn);
136    end
137end
138
139%-------------------------------------------------
140% --- Executes on button press in move_up.
141function move_up_Callback(hObject, eventdata, handles)
142global t xs t_ref
143set(handles.export_list,'Value',1);%
144set(handles.export_list,'String','');% empty the export list
145CurrentFile=get(handles.CurrentFile,'String');
146CurrentElement=get(handles.CurrentElement,'String');
147heditxml=get(handles.move_up,'parent');
148test_root=0;
149DataIn=get(heditxml,'UserData');
150if isfield(DataIn,'CurrentUid')&length(DataIn.CurrentUid)>1
151    nodeup=DataIn.CurrentUid(end-1);
152    DataIn.CurrentUid(end)=[];
153else
154    nodeup=[];
155end
156if isempty(xs)   
157    if isempty(nodeup)
158        test_root=1;
159    else
160        DataIn=displ_xml(handles,t,nodeup,DataIn,heditxml);
161    end
162else
163    xs_nodeup=[];
164%     if isfield(DataIn,'xs_UpUid')
165    if isfield(DataIn,'xs_CurrentUid')&length(DataIn.xs_CurrentUid)>1
166%         xs_nodeup=DataIn.xs_UpUid
167        xs_nodeup=DataIn.xs_CurrentUid(end-1);
168        DataIn.xs_CurrentUid(end)=[];%uid of the root element in the schema
169    end
170    if isempty(xs_nodeup)
171        test_root=1;
172    else
173        [xs_nodeup,path,xs_element,xs_subelem]=scan_schema(xs,xs_nodeup);
174                [element,subelem]=get_xml(t,path,xs_element,nodeup,xs_subelem);
175                update_list(handles,path,xs_element,element,nodeup,xs_subelem,subelem);
176        %update the import file display
177        if isfield(DataIn,'h_ref')&ishandle(DataIn.h_ref)
178            [ref_element,ref_subelem]=get_xml(t_ref,path,xs_element,nodeup,xs_subelem);
179            update_ref_list(DataIn.h_ref,xs_element,ref_element,nodeup,xs_subelem,ref_subelem);
180        end
181    end
182    set(get(hObject,'parent'),'UserData',DataIn);
183end
184if test_root% we are a the root,
185    testupfile=0;
186    DataIn=get(get(hObject,'parent'),'UserData');
187    if isfield(DataIn,'UpFile')&~isempty(DataIn.UpFile)
188        [UpPath,UpName,UpExt]=fileparts(DataIn.UpFile{1});
189        if isequal(UpExt,'.xml')
190            set(handles.CurrentFile,'String',DataIn.UpFile{1})
191            CurrentFile_Callback(handles.CurrentFile,[],handles)
192            testupfile=1;
193            DataIn.UpFile{1}={};
194        end
195    end
196    if ~testupfile  %open the browser
197        RootPath=fileparts(CurrentFile);
198            [FileName, PathName]=uigetfile( ...
199               {'*.xml', '(*.xml)';
200                '*.xml',  '.xml files '; ...
201                '*.*',  'All Files (*.*)'}, ...
202                'Pick a file',RootPath); %file browser
203            fileinput_new=fullfile(PathName,FileName);
204            set(handles.CurrentFile,'String',fileinput_new)
205            CurrentFile_Callback(handles.CurrentFile,[],handles)
206     end
207end
208set(heditxml,'UserData',DataIn);
209%---------------------------------------------------------
210%edit element value
211function element_value_Callback(hObject, eventdata, handles)
212%----------------------------------------------------------
213global t xs
214if isequal(get(handles.element_value,'ForegroundColor'),[0.7 0.7 0.7])
215    return% edit element desactivated (grey display)
216end
217list_enum=get(handles.element_value,'String');
218list_index=get(handles.element_value,'Value');
219if iscell(list_enum)
220    value=list_enum{list_index};
221else
222    value=list_enum;
223end
224heditxml=get(handles.element_value,'Parent');
225DataIn=get(heditxml,'UserData');
226%create the current root element if needed
227LengthElement=length(DataIn.CurrentUid);
228FilledUid=find(DataIn.CurrentUid~=0);
229LengthFilled=FilledUid(end);
230for irank=LengthFilled+1:LengthElement
231    attrib=attributes(xs,'get',DataIn.xs_CurrentUid(irank),1);
232    [t,DataIn.CurrentUid(irank)]=add(t,DataIn.CurrentUid(irank-1),'element',attrib.val);
233end
234node_element=get(handles.element_value,'UserData');
235element_key=get(handles.element_key,'String');
236t=set_element(t,DataIn.CurrentUid(end),node_element,element_key,value);
237 
238set(heditxml,'UserData',DataIn)
239%update the current listing
240[nodeup,path,xs_element,xs_subelem]=scan_schema(xs,DataIn.xs_CurrentUid(end));
241[element,subelem]=get_xml(t,path,xs_element,DataIn.CurrentUid(end),xs_subelem);
242element_index=get(handles.list_element,'Value');
243update_list(handles,path,xs_element,element,DataIn.CurrentUid(end),xs_subelem,subelem);
244set(handles.list_element,'Value',element_index);
245
246%
247% % --- Executes on button press in inport_file.
248% function inport_file_Callback(hObject, eventdata, handles)
249% CurrentFile=get(handles.RefFile,'String');
250% if isempty(CurrentFile)|isequal(CurrentFile,'')
251%     CurrentFile=get(handles.CurrentFile,'String')
252% end
253% [FileName, PathName]=uigetfile( ...
254%        {'*.xml', '(*.xml)';
255%         '*.xml',  '.xml files '; ...
256%         '*.*',  'All Files (*.*)'}, ...
257%         'Pick a file',CurrentFile); %file browser
258% fileinput=fullfile(PathName,FileName);
259% sizf=size(fileinput);
260% if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end% keep only character strings as input file name
261% if exist(fileinput,'file')
262%    set(handles.RefFile,'Visible','on')
263%    set(handles.replicate,'Visible','on')
264%    set(handles.RefFile,'String',fileinput)
265%    RefFile_Callback(handles.RefFile, eventdata, handles)
266% end
267
268
269%------------------------------------------------------
270% --- Executes on button press in browser.
271function browser_Callback(hObject, eventdata, handles)
272%-------------------------------------------------------
273heditxml=get(hObject,'parent');%handle of the interface figure
274DataIn=get(heditxml,'UserData');%get the current input xml file
275CurrentFile=get(handles.CurrentFile,'String');
276DataIn.Schema=[];%schema input file put to [] by default
277[FileName, PathName]=uigetfile( ...
278       {'*.xml;*.xls','(*.xml,*.xls)';
279        '*.xml',  '.xml files '; ...
280        '*.xls',  '.xls files '; ...
281        '*.*',  'All Files (*.*)'}, ...
282        'Pick a file',CurrentFile); %file browser
283CurrentFile=fullfile(PathName,FileName);
284sizf=size(CurrentFile);
285if (~ischar(CurrentFile)|~isequal(sizf(1),1)),return;end% keep only character strings as input file name
286if exist(CurrentFile,'file')
287%     set(handles.CurrentAttributes,'UserDataIn',PathName); %store the path to the xml file
288    [CurPath,CurName,CurExt]=fileparts(CurrentFile);
289    if isequal(CurExt,'.xls')   
290        if isfield(DataIn,'hfig_xls')&ishandle(DataIn.hfig_xls)
291            [hfig_xls]=read_xls(CurrentFile,DataIn.hfig_xls);
292        else
293            [hfig_xls]=read_xls(CurrentFile);
294        end
295        figpos=get(heditxml,'Position');
296        newfigpos=[figpos(1)-0.25*figpos(3) figpos(2) 0.5*figpos(3) 0.5*figpos(4)];
297        set(hfig_xls,'Position',newfigpos)
298    else
299        set(handles.CurrentFile,'String',CurrentFile)
300        CurrentFile_Callback(hObject, eventdata, handles)
301     end
302end
303
304%------------------------------------
305function CurrentFile_Callback(hObject, eventdata, handles)
306global t xs
307CurrentFile=get(handles.CurrentFile,'String');
308heditxml=get(handles.CurrentFile,'parent');%handles of the inteface
309DataIn=get(heditxml,'UserData');
310t=xmltree(CurrentFile);%open the xml file
311head_element=get(t,1);
312if ~isfield(head_element,'name') | ~isfield(head_element,'attributes')
313    msgbox_uvmat('ERROR','root element of the .xml file not in correct format')
314end
315head_name=head_element.name;
316head_attr=head_element.attributes;% attribute of root gives the name of the associated schema
317xstest=0;
318for iattr=1:length(head_attr)
319    if isequal(head_attr{iattr}.key,'xmlns:xsi')& isequal(head_attr{iattr}.val,'none')%no schema to read
320         xs=[];
321%          xstest=1;
322    end
323    if isequal(head_attr{iattr}.key,'xsi:noNamespaceSchemaLocation') & exist(head_attr{iattr}.val,'file')
324        DataIn.Schema=head_attr{iattr}.val;
325        xs=xmltree(DataIn.Schema);%open the associated schema file
326        xstest=1;
327    end
328end
329if xstest==0  %look for the corresponding schema in the directory PARAM_LINUX.xml or PARAM_WIN.xml
330    head_name=get(t,1,'name');
331    %Path to shemas:
332    path_uvmat=which('editxml');% check the path detected for source file uvmat
333    path_UVMAT=fileparts(path_uvmat); %path to UVMAT
334%     if isunix
335xmlparam=fullfile(path_UVMAT,'PARAM.xml');
336if exist(xmlparam,'file')
337    tparam=xmltree(xmlparam);
338    sparam=convert(tparam);
339    if isfield(sparam,'SchemaPath')
340        schemapath=[fullfile(sparam.SchemaPath,head_name) '.xsd']
341    end
342    if exist(fullfile(path_UVMAT,schemapath)
343        schemapath=fullfile(path_UVMAT,schemapath);%look for relative path definition
344    end
345    schemapath=fullfile(path_UVMAT,schemapath);
346%         schemapath=['/coriolis/papillon/data/civbin/XML_SCHEMAS/' head_name '.xsd']%current dir for schema
347%     else
348%         xmlparam=fullfile(path_UVMAT,'PARAM_WIN.xml')
349%         if exist(xmlparam,'file')
350%             tparam=xmltree(xmlparam);
351%             sparam=convert(tparam)
352%             if isfield(sparam,'SchemaPath')
353%                 schemapath=[fullfile(sparam.SchemaPath,head_name) '.xsd']
354%             end
355%         end
356%         schemapath=['\\Papillon\data\civbin\XML_SCHEMAS\' head_name '.xsd']%current dir for schemas
357%     end
358    if exist(schemapath,'file')
359        xs=xmltree(schemapath);
360    else
361        msgbox_uvmat('ERROR',['The xml schema for ' CurrentFile ' is unknown, check the schema path set in the file PARAM.xml'])
362        [FileName, PathName]=uigetfile( ...
363       {'*.xsd', '(*.xsd)';
364        '*.xsd',  '.xsd files '; ...
365        '*.*',  'All Files (*.*)'}, ...
366        ['Pick a .xsd schema'] ,schemapath); %file browser
367        if ischar(PathName)&ischar(FileName)& exist(fullfile(PathName,FileName),'file')
368            DataIn.Schema=fullfile(PathName,FileName);
369            xs=xmltree(DataIn.Schema);%open the associated schema file
370        else
371            xs=[];
372        end
373    end
374end
375DataIn.CurrentUid=1;
376if isempty(xs)
377    displ_xml(handles,t,1,DataIn,get(hObject,'parent'));%no associated schema, default  display of the xml file
378%    set(handles.inport_file,'Visible','off')
379%    set(handles.RefFile,'Visible','off')
380else
381    DataIn.xs_CurrentUid=find(xs,'/xs:schema/xs:element');%uid of the root element in the schema
382        [nodeup,path,xs_element,xs_subelem]=scan_schema(xs,DataIn.xs_CurrentUid);%scan the schema at the root level
383        [element,subelem]=get_xml(t,path,xs_element,1,xs_subelem);% read the corresponding xml data
384        update_list(handles,path,xs_element,element,1,xs_subelem,subelem);%update the display of information on the interface
385   % set(handles.inport_file,'Visible','on')
386   % set(handles.RefFile,'Visible','on')
387end
388set(heditxml,'UserData',DataIn);%store the new input xml file name
389
390%-------------------------------------------------------
391%  function scan_schema: read the xml schema xs
392%--------------------------------------------------------
393%OUTPUT:
394%nodeup: parent node of nodeinput
395%path: path to nodeinput in the tree
396%xs_element: element corresponding to nodeinput
397    %xs_element.uid, =tag of the element in the schema (=nodeinput)
398    %xs_element.key: key label of nodeinput
399    %xs_element.type: type of data contained in the element
400    %xs_element.annot: annotation of nodeinput
401    %xs_element.attrib: list of accepted attributes keys for xs_element
402    %xs_element.enum: enumeration, list of accepted values for nodeinput
403    %xs_element.subtest: =1 if the element contains subelements in the schema, 0 else
404 
405%xs_subelement(k): subelement #k of xs_element
406    %xs_subelem(k).node: node number in the schema
407    %xs_subelem(k).key: key name of the element
408    %xs_subelem(k).testsub: =1 if element contains subelements, 0 else
409    %xs_subelem(k).minOccurs: =0 for a non mandatory element, =1 else
410    %xs_subelem(k).maxOccurs
411%
412%INPUT:
413%xs: schema xml tree
414%nodeinput: tag of the current root element in the schema
415function [nodeup,path,xs_element,xs_subelem]=scan_schema(xs,nodeinput)
416nodeup=[];
417path=[];
418xs_element.key=[];
419xs_element.type=[];
420xs_element.annot=[];
421xs_element.attrib=[];
422xs_element.subtest=0;
423xs_element.enum={};
424xs_subelem=[];%default
425% get default nodeinput (root of the file) if not defined
426if ~exist('nodeinput') | isempty(nodeinput)% we start at the root
427    node=find(xs,'/xs:schema/xs:element');%description of the root element
428else
429    node=nodeinput;
430end
431xs_element.uid=node;
432%get the key name and element_type of the element
433node_content=get(xs,node);
434if isempty(node_content),return,end;
435if ~isempty(node_content) & isfield(node_content,'attributes')
436    attrib=node_content.attributes;
437    for iattr=1:length(attrib)
438        struct=attrib{iattr};
439        if isequal(struct.key,'name')
440            xs_element.key=struct.val; % read element key name
441        elseif isequal(struct.key,'type')
442            xs_element.type=struct.val; % read element key name
443        end
444    end
445end
446
447%get the parent node of nodeinput
448if ~isempty(node_content)
449    nodeup=get(xs,node,'parent');%move up to the parent in the tree
450    if ~isempty(nodeup)
451        nodeup=get(xs,nodeup(1),'parent');%move up to the parent in the tree
452        if isequal(nodeup,[])
453            %OUVRIR FICHIER AMONT
454            up=0;
455        else
456            nodeup=get(xs,nodeup(1),'parent');%move up to the parent in the tree
457        end
458    end
459end
460%get the path to 'nodeinput' in the schema
461up=1;
462path=[];
463if ~isempty(nodeup)
464    attrib=attributes(xs,'get',nodeup,1);
465    path=attrib.val;
466    nodeup2=nodeup;
467    while up==1;
468        nodeup2=get(xs,nodeup2(1),'parent');%move up to the parent in the tree
469        nodeup2=get(xs,nodeup2(1),'parent');%move up to the parent in the tree
470        if isempty(nodeup2)
471            up=0;
472        else
473            nodeup2=get(xs,nodeup2(1),'parent');%move up to the parent in the tree
474            if isempty(nodeup2)
475                up=0;
476            else
477                attrib=attributes(xs,'get',nodeup2,1);
478                path=[attrib.val '/' path];
479            end
480        end
481    end
482end   
483
484%explore the subtree in the schema file
485node1=children(xs,node); %find the children of the root element
486test_sub=0; %no subtree in the .xml file by default
487comment='';
488element={};
489minOccurs={};
490maxOccurs={};
491testsub={};
492list_menu={};
493text={};
494if ~isempty(node1) 
495  for i=1:length(node1)
496    nodename1=get(xs,node1(i),'name');
497    node2=children(xs,node1(i));
498    if isequal(nodename1,'xs:annotation')
499         for j=1:length(node2)
500            nodename2=get(xs,node2(j),'name');
501            if isequal(nodename2,'xs:documentation')
502                node3=children(xs,node2(j));
503                xs_element.annot=get(xs,node3,'value');%read annotation
504            end
505        end
506    % pour les elements
507    elseif isequal(nodename1,'xs:simpleType')
508        for j=1:length(node2)
509            nodename2=get(xs,node2(j),'name');
510            if isequal(nodename2,'xs:restriction')
511                node3=children(xs,node2(j));
512                for k=1:length(node3)
513                    nodename3=get(xs,node3(k),'name');
514                    if isequal(nodename3,'xs:enumeration')
515                        node3_content=get(xs,node3(k));
516                        attr=node3_content.attributes;
517                        for m=1:length(attr)
518                            struct=attr{m};
519                            if isequal(struct.key,'value')
520                                xs_element.enum{k}=struct.val; % read enumeration
521                            end
522                        end   
523                   end
524               end
525            end
526        end
527     elseif isequal(nodename1,'xs:complexType')
528         for j=1:length(node2)
529             nodename2=get(xs,node2(j),'name');
530             if isequal(nodename2,'xs:attribute')
531                 node_content=get(xs,node2(j));
532                 attr=node_content.attributes;
533                 for k=1:length(attr)
534                    struct=attr{k};%read attributes
535                    if isequal(struct.key,'name')
536                        xs_element.attrib=struct.val; %read attributes of main node
537                    end
538                 end   
539             elseif isequal(nodename2,'xs:sequence')
540                 xs_element.subtest=1;
541                 node3=children(xs,node2(j));%nodes of the sequence
542                 for k=1:length(node3)
543                     xs_subelem(k).node=node3(k);
544                     xs_subelem(k).testsub=0;%default
545                     node_content=get(xs,node3(k));
546                     xs_subelem(k).minOccurs=1; %default
547                     xs_subelem(k).maxOccurs=1; %default
548%                      pref{k}=[]; %default
549                     if isequal(node_content.name,'xs:element')
550                        attr=node_content.attributes;
551%                         attr{:}.key
552                        for l=1:length(attr)
553                            if isequal(attr{l}.key,'name')
554                                xs_subelem(k).key=attr{l}.val;%name of the element
555                            elseif isequal(attr{l}.key, 'minOccurs')
556                                xs_subelem(k).minOccurs=attr{l}.val;
557                            elseif isequal(attr{l}.key, 'maxOccurs')
558                                xs_subelem(k).maxOccurs=attr{l}.val;
559                            end
560                        end
561                     end
562                     node4=children(xs,node3(k));
563                     for l=1:length(node4)
564                        res=get(xs,node4(l),'name');
565                        if isequal(res,'xs:complexType')%look whether the element k contains a subtree
566                           node5=children(xs,node4(l));
567                           for m=1:length(node5)
568                               res2=get(xs,node5(m),'name');
569                               if isequal(res2,'xs:sequence')
570                                    xs_subelem(k).testsub=1; %flag for the existence of a subtree
571                               end
572                           end
573                        end
574                     end
575                 end           
576
577             end
578         end     
579     end   
580  end
581end
582% look for predefined types
583if length(xs_element.type)>=3 & (xs_element.type([1:3])~='xs:')
584    node_type=find(xs,'/xs:schema/xs:simpleType')
585    for i=1:length(node_type)
586        content=get(xs,node_type(i));
587        nodeattr=content.attributes;
588        if ~isempty(nodeattr) & isequal(nodeattr{1}.key,'name') & isequal(nodeattr{1}.val,xs_element.type)
589            node1=children(xs,node_type(i));
590            node2=find(xs,node1,'name','xs:restriction');
591%             nodename1=find(xs,
592            node3=children(xs,node2);
593            node4=find(xs,node3,'name','xs:enumeration');
594            for ienum=1:length(node4)
595                struct2=get(xs,node4(ienum));
596                enumval=struct2.attributes;
597                xs_element.enum{ienum}=enumval{1}.val;
598            end
599        end       
600     end
601end
602
603%--------------------------------------------------------
604%OUTPUT:
605%element.val: value of the current element, =[] in the absence of chardata value
606%node: node (iud) of the element in t
607%element.attr_key{iattr}: attribute key #iattr of the current element
608%element.attr_val{iattr}: attribute value #iattr of the current element
609%element.attrup: %structure containing the attributes of the element, including the ones unheritated from parent nodes
610%subelem(iline).val : value of subelement # iline, concatenated with corresponding attributes
611%subelem(iline).xsindex: index k of the subelement #iline in the list xs_subelem of the schema
612%subelem(iline).index: index of the subelement #iline inside its xs_subelement, =0 when the xs_subelement is absent in t
613
614%INPUT:
615%t: xml tree
616%path: path to the current element in the schema
617%xs_element: current element in the schema
618    %xs_element.key: key label 
619    %xs_element.type: type of data contained in the element
620    %xs_element.annot: annotation of nodeinput
621    %xs_element.attrib: list of accepted attributes keys for xs_element
622    %xs_element.enum: enumeration, list of accepted values
623    %xs_element.subtest: =1 if the element contains subelements in the schema, 0 else
624%index: index of the element, =1 in case of single occurence in xs_element,=0 in case of missing element
625%xs_subelem(k): subelement #k of the current element in the schema
626    %xs_subelem(k).node: node iud of the
627    %xs_subelem(k).key: key name of the subelement #k in the schema
628    %xs_subelem(k).testsub: =1 if element contains subelements, 0 else
629    %xs_subelem(k).minOccurs
630    %xs_subelem(k).maxOccurs
631
632function [element,subelem]=get_xml(t,path,xs_element,node,xs_subelem)
633element.attr_key='';%default
634element.attr_val='';%default
635element.val='';
636% element.type='';
637% element.testmanual=testmanual %inheritates the input manual editing flag by default
638subelem=[]; %default
639attrup=[];
640% node=[];
641
642% %find the element properties in the xml file
643if node >= 1
644    elem_struct=get(t,node);
645    if ~xs_element.subtest
646        elem_contents=get(t,elem_struct.contents);
647        if isempty(elem_contents)
648            element.val=[];
649        else
650            element.val=elem_contents.value
651        end
652    end
653    if isfield(elem_struct,'attributes')
654        elem_attr=elem_struct.attributes;
655        for iattr=1:length(elem_attr)
656            element.attr_key{iattr}=elem_attr{iattr}.key ;
657            element.attr_val{iattr}=elem_attr{iattr}.val;
658%             attrup=setfield(attrup,elem_attr{iattr}.key,elem_attr{iattr}.val);
659           breakdetect=find(elem_attr{iattr}.key=='/'| elem_attr{iattr}.key==':'| elem_attr{iattr}.key=='.');% find '/'
660           if isempty(breakdetect)
661%                 comline=['attrup.' elem_attr{iattr}.key '=' elem_attr{iattr}.val ';']
662                eval(['attrup.' elem_attr{iattr}.key '=''' elem_attr{iattr}.val ''';'])
663           end
664        end
665    end
666end
667%get the parent node attributes
668up=1;
669if node>0
670        nodeup=node;
671        while up==1;
672        nodeup=get(t,nodeup,'parent');%move up to the parent in the tree
673        if isempty(nodeup)
674            up=0;
675        else
676            nodeup_content=get(t,nodeup);
677            attrib=nodeup_content.attributes;
678            for iattr=1:length(attrib)
679                key=attrib{iattr}.key;
680                breakdetect=find(key=='/'| key==':'| key=='.');% find '/'
681                if ~isfield(attrup,key) & isempty(breakdetect)
682                   eval(['attrup.' key '=''' attrib{iattr}.val ''';'])
683                end
684            end
685        end
686        end
687        element.attrup=attrup;
688end
689%find the subelement properties in the xml file
690if xs_element.subtest
691   iline=0;
692   for k=1:length(xs_subelem)%node2: list of subelements in the sub-sequence
693%     attr=attributes(xs,'get',node2(i),1);%
694%     element=attr.val;%name of the element
695     tag=find(t,['/' path '/' xs_element.key '/' xs_subelem(k).key]);%look for the corresponding element node in the .xml tree
696     struct_element=get(t,tag);%get the content of the element
697     if isempty(struct_element)
698         iline=iline+1;
699         subelem(iline).uid=0;
700         subelem(iline).xsindex=k;
701         subelem(iline).index=0;
702%          subelem(iline).testmanual=element.testmanual;% inheritates the manual editing flag by default
703         if isequal(xs_subelem(k).minOccurs,'0')
704             subelem(iline).val='[]';%element value not mandatory in the schema
705         else
706             subelem(iline).val='[MISSING]';%element value mandatory in the schema
707         end
708%          subelem(iline).attrup=attrup; %inheritated attributes
709     elseif isequal(length(struct_element),1)
710         contents=get(t,struct_element.contents);
711         iline=iline+1;
712         subelem(iline).uid=tag;
713         subelem(iline).xsindex=k;
714         subelem(iline).index=1;
715%          subelem(iline).testmanual=element.testmanual;%
716         if isfield(contents,'value') & ~isempty(contents.value)
717             subelem(iline).val=contents.value;
718         elseif xs_subelem(k).testsub
719             subelem(iline).val='';
720         elseif isequal(xs_subelem(k).minOccurs,0)
721             subelem(iline).val='[]';%element value not mandatory in the schema
722         else
723             subelem(iline).val='[MISSING]';%element value mandatory in the schema
724         end
725%          subelem(iline).attrup=attrup; %inheritated attributes
726         if isfield(struct_element,'attributes')
727            element_attr=struct_element.attributes;
728            attr_display=[];
729            for iattr=1:length(element_attr)
730%                 attr_display{iline}=[attr_display ' , ' element_attr{iattr}.key '  =  ' element_attr{iattr}.val];
731                subelem(iline).val=[subelem(iline).val attr_display ' , ' element_attr{iattr}.key '  =  ' element_attr{iattr}.val];
732%                 subelem(iline).attrup=setfield(subelem(iline).attrup,element_attr{iattr}.key,element_attr{iattr}.val);
733            end
734         end
735     else%case of a multiple element
736         for subindex=1:length(struct_element)
737             contents=get(t,struct_element{subindex}.contents);
738             iline=iline+1;
739             subelem(iline).index=subindex;%index of the element
740             subelem(iline).xsindex=k;
741%              subelem(iline).testmanual=element.testmanual;%
742             if isfield(contents,'value')& ~isempty(contents.value)
743                 subelem(iline).val=contents.value;
744             elseif xs_subelem(k).testsub
745                 subelem(iline).val='';
746             else
747                 subelem(iline).val='[]';
748             end
749%              subelem(iline).attrup=attrup; %inheritated attributes
750             if isfield(struct_element{subindex},'attributes')
751                element_attr=struct_element{subindex}.attributes;
752                attr_display=[];
753                for iattr=1:length(element_attr)
754%                     attr_display{iline}=[attr_display ' , ' element_attr{iattr}.key '  =  ' element_attr{iattr}.val];
755                    subelem(iline).val=[subelem(iline).val attr_display ' , ' element_attr{iattr}.key '  =  ' element_attr{iattr}.val];
756%                     subelem(iline).attrup=setfield(subelem(iline).attrup,element_attr{iattr}.key,element_attr{iattr}.val);
757                end
758            end
759        end
760     end
761  end
762end
763
764%-------------------------------------
765%updates the interface
766function update_list(handles,path,xs_element,element,node,xs_subelem,subelem)
767%-----------------------------
768if xs_element.subtest% we list the sub-elements of root
769    set(handles.export_list,'Value',1)
770    set(handles.export_list,'String','')%flush the export list
771    set(handles.CurrentElement,'String',[path '/' xs_element.key])
772%     title_element.key=[path '/' xs_element.key];
773%     if ~isempty(path)
774%         xsnode_index=get(handles.list_element,'UserDataIn');
775%         ind=get(handles.list_element,'Value');
776%         title_element.index=xsnode_index(2,ind);
777%     else
778%         title_element.index=1;
779%     end
780%     title_element.xsnode=xs_element.uid;
781%     title_element.node=node;
782%     set(handles.CurrentFile,'UserDataIn',title_element)%element corresponding to the title
783    set(handles.CurrentAnnotation,'String',xs_element.annot)
784    attr_col=[];
785    testedit=0;% cannot edit elements by default
786    for iattr=1:length(element.attr_key)
787%          if isequal(element.attr_key{iattr},'source') & isequal(element.attr_val{iattr},'manual')
788%             testedit=1;
789%         end
790        attr_col=strvcat(attr_col,[element.attr_key{iattr} ' = ' element.attr_val{iattr}]);
791    end
792    set(handles.CurrentAttributes,'String',attr_col)
793    pref_col='';
794    key_col='';
795    equal_sign='';
796    val_col='';
797    for iline=1:length(subelem)
798        xsindex=subelem(iline).xsindex;
799        index(iline)=subelem(iline).index;
800        subuid=subelem(iline).uid;
801        if isempty(subuid)
802            list.uid(iline)=0;
803        else
804            list.uid(iline)=subuid;
805        end
806        node(iline)=xs_subelem(xsindex).node;
807%         testmanual(iline)=subelem(iline).testmanual;
808        ikey=xs_subelem(xsindex).key;
809        if xs_subelem(xsindex).testsub
810            ival=[' + ' subelem(iline).val];
811        else
812            ival=[' = ' subelem(iline).val];
813        end
814        key_col=strvcat(key_col,ikey);
815        val_col=strvcat(val_col,ival);
816    end
817    list_element=[key_col val_col];
818    set(handles.list_element,'String',list_element)
819    set(handles.list_element,'Value',1)
820    list.xs_uid=node;
821    list.index=index;
822    set(handles.list_element,'UserData',list)
823    set(handles.element_attrib,'Visible','off')
824    set(handles.element_key,'Visible','off')
825    set(handles.element_value,'Visible','off')
826else % we edit an element
827
828    export_list=get(handles.export_list,'String');%export list
829    testadd=1;
830    for ilist=1:length(export_list)
831        if isequal(xs_element.key,export_list{ilist})
832            testadd=0;       
833            break
834        end
835    end
836    if testadd
837        export_list=[export_list;{xs_element.key}];
838        ilist=length(export_list);
839    end
840    set(handles.export_list,'String',export_list)
841    if iscell(element.val)
842        element_val=element.val{1};
843    else
844        element_val=element.val;
845    end
846    set(handles.element_value,'String',element_val)
847    export_val=get(handles.export_list,'UserData');
848    export_val{ilist}=element_val;
849    set(handles.export_list,'UserData',export_val);
850    set(handles.element_annot,'String',xs_element.annot)
851    set(handles.element_type,'String',['type:  ' xs_element.type])
852    attr_col=[];
853    testedit=0;% cannot edit element by default
854    for iattr=1:length(element.attr_key)
855%         if isequal(element.attr_key{iattr},'source') & isequal(element.attr_val{iattr},'manual')
856%             testedit=1;
857%         end
858        attr_col=strvcat(attr_col,[element.attr_key{iattr} ' = ' element.attr_val{iattr}]);
859    end
860    set(handles.element_attrib,'String',attr_col)
861    set(handles.element_key,'String',xs_element.key)
862
863 
864    if isempty(xs_element.enum)
865        set(handles.element_value,'Value',1)
866        set(handles.element_value,'Style','edit')
867    else % case of an enumeration of possible values
868         list_enum=[];
869         list_val=[];
870         for ienum=1:length(xs_element.enum)
871             list_enum{ienum,1}=xs_element.enum{ienum};
872             if isequal(xs_element.enum{ienum},element_val)
873                 list_val=ienum;
874             end
875         end
876         if isempty(list_val)
877             list_enum{length(xs_element.enum)+1,1}=['[' element_val ']'];%show the non-valid element between brackets
878             list_val=length(xs_element.enum)+1;
879         end
880         set(handles.element_value,'Style','popupmenu')
881         set(handles.element_value,'String',list_enum)
882         set(handles.element_value,'Value',list_val)
883     end
884     if isempty(element.val)
885         testedit=1;%allow element editing if value is missing
886     end     
887     set(handles.element_attrib,'Visible','On')
888     set(handles.element_key,'Visible','On')
889     set(handles.element_value,'Visible','On')
890end
891set(handles.element_value,'UserData',node)
892if ~testedit && isfield(element,'attrup') & isfield(element.attrup,'source')&& ~isequal(element.attrup.source,'manual')
893     set(handles.element_value,'Enable','inactive')
894else
895    set(handles.element_value,'Enable','on')
896end
897
898
899% --- Executes on button press in SAVE.
900function SAVE_Callback(hObject, eventdata, handles)
901global t
902DataIn=get(get(handles.SAVE,'parent'),'UserData');
903CurrentFile=get(handles.CurrentFile,'String');
904if isfield(DataIn,'Schema')
905if ~isempty(DataIn.Schema)% update ref to schema
906    attrxsd=attributes(t,'get',1);
907    setest=0;
908    for iattr=1:length(attrxsd)
909        if isequal(attrxsd{iattr}.key,'xsi:noNamespaceSchemaLocation')
910            t= attributes(t,'set',1,iattr,'xsi:noNamespaceSchemaLocation',DataIn.Schema);
911            setest=1;
912        end
913    end
914    if setest==0;
915        t=attributes(t,'add',1,'xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance');
916        t= attributes(t,'add',1,'xsi:noNamespaceSchemaLocation',DataIn.Schema);
917    end
918end
919end
920copyfile(CurrentFile,[CurrentFile '.bak']);
921save(t,CurrentFile);
922
923%-------------------------------------
924% creates and/or set values to an element in t
925%t: xml tree
926%RootUid: uid of t under which we introduce an element
927%node: uid of the element that we correct, if =0, a new element is created
928%key: key name of the element
929%value: new value of the element
930function t=set_element(t,RootUid,node,key,value)
931%create the subelement if needed
932if isequal(node,0)   
933   [t,node]= add(t,RootUid,'element',key);
934end
935node_chardata=children(t,node); %corresponding data node
936if isempty(node_chardata)%if the data does not exist in t, create it
937    t=add(t,node,'chardata',value);
938elseif isequal(length(node_chardata),1)&isequal(get(t,node_chardata,'type'),'chardata')% update only a simple element with 'chardata'
939    t=set(t,node_chardata,'value',value);%modify existing data
940end
941attr=attributes(t,'get',node);
942if isempty(attr)
943    t=attributes(t,'add',node,'source','manual');%indicate a manual eidting
944end
945
946% --- Executes on selection change in element_attr_val.
947function element_attr_val_Callback(hObject, eventdata, handles)
948% hObject    handle to element_attr_val (see GCBO)
949% eventdata  reserved - to be defined in a future version of MATLAB
950% handles    structure with handles and user data (see GUIDATA)
951
952% Hints: contents = get(hObject,'String') returns element_attr_val contents as cell array
953%        contents{get(hObject,'Value')} returns selected item from element_attr_val
954
955
956% --- Executes on button press in ADD.
957function ADD_Callback(hObject, eventdata, handles)
958% hObject    handle to ADD (see GCBO)
959% eventdata  reserved - to be defined in a future version of MATLAB
960% handles    structure with handles and user data (see GUIDATA)
961
962%----------------------------------------
963%read an xml file without schema
964function [Data,testsimple]=displ_xml(handles,t,root_uid,DataIn,heditxml)
965Data=DataIn;%default
966if ~isfield(Data,'CurrentUid')
967    Data.CurrentUid=[];
968end
969CurrentFile=get(handles.CurrentFile,'String');
970
971%display the current element
972root_element=get(t,root_uid);
973uidparent=root_uid;
974if isfield(root_element,'name')
975    CurrentElement=root_element.name;
976    while ~isequal(uidparent,1)%while the first level has not been reached
977        uidparent=parent(t,uidparent);
978        dirdat=get(t,uidparent);
979        if isfield(dirdat,'name')                       
980            CurrentElement=[dirdat.name '/' CurrentElement];
981        end
982    end
983    set(handles.CurrentElement,'String',CurrentElement)
984end
985list_uid=children(t,root_uid);
986%case of a single element
987testsimple=0;
988filedat=[];
989if ~isempty(list_uid)
990    filedat=get(t,list_uid(1))
991    if isfield(filedat,'type') & isequal(filedat.type,'chardata') &isfield(filedat,'value')
992        testsimple=1;%simple element
993    end
994end
995
996%attributes of the current element
997nbattrib= attributes(t,'length',root_uid);
998testopen=0;
999attr_col=[];
1000for iattr=1:nbattrib
1001    attr= attributes(t,'get',root_uid,iattr);
1002    if isequal(attr.key,'source')% look for 'source' attribute
1003        if isequal(attr.val,'file')%if the source is 'file', look for the path and open it
1004           if isfield(filedat,'type') & isequal(filedat.type,'chardata') &isfield(filedat,'value')
1005               cur_file=filedat.value;
1006               uidparent=root_uid;%initialization
1007               while ~isequal(uidparent,1)%while the first level has not been reached
1008                    uidparent=parent(t,uidparent);
1009                    dirdat=get(t,uidparent);
1010                    if isfield(dirdat,'type') & isequal(dirdat.type,'element') & isfield(dirdat,'name')
1011                        nbattrib_up= attributes(t,'length',uidparent);
1012                        for iattr_up=1:nbattrib_up
1013                            attr= attributes(t,'get',uidparent,iattr_up);
1014%                             if isequal(attr.key,'source')&isequal(attr.val,'directory')% look for 'source' attribute
1015                              if isequal(attr.key,'DirName')
1016                                 cur_file=fullfile(attr.val,cur_file);
1017                             end
1018                        end
1019                    end
1020               end
1021               RootPath=fileparts(CurrentFile);%path to the current .xml file
1022               cur_file=fullfile(RootPath,cur_file)
1023               set(handles.CurrentAttributes,'UserData',cur_file)%will be searched by uvmat
1024               [path,fil,ext]=fileparts(cur_file);
1025               if ~exist(cur_file,'file')
1026                   msgbox_uvmat('ERROR',['non-existent link file' cur_file]) % A FAIRE: propose to updtate the .xml file
1027                   return
1028               elseif isequal(ext,'.xml')
1029                   if ~isfield(Data,'UpFile')
1030                       Data.UpFile={CurrentFile};
1031                   else
1032                       Data.UpFile=[{CurrentFile};Data.UpFile];
1033                   end
1034                   set(heditxml,'UserData',Data)
1035                   set(handles.CurrentFile,'String',cur_file)
1036                   CurrentFile_Callback(handles.CurrentFile, [], handles)
1037               else
1038                   if isequal(get(heditxml,'Tag'),'browser'); %if editxml has been called as a browser
1039                       set(heditxml,'Tag','idle')% signal for uvmat browser
1040                   else
1041                       uvmat({cur_file}); %open the link fiel with uvmat
1042                   end
1043                   return
1044               end
1045           end
1046       %elseif isequal(attr.val,'dir') A FAIRE : check directory
1047       %else A FAIRE: edit the element
1048        end
1049    end
1050    attr_col=strvcat(attr_col,[attr.key ' = ' attr.val]);
1051end
1052set(handles.CurrentAttributes,'String',attr_col)
1053
1054%list subtree
1055if ~testsimple
1056    list_element=[];
1057%      Data.CurrentUid=[Data.CurrentUid root_uid]%record new current uid
1058        for iline=1:length(list_uid)
1059        element=get(t,list_uid(iline));
1060        if isfield(element,'type')&isequal(element.type,'element')
1061             list_element{iline,2}=element.name;
1062             child_uid=children(t,list_uid(iline));
1063             subelem=get(t,child_uid);
1064             if isfield(subelem,'type')& isfield(subelem,'value') & isequal(subelem.type,'chardata')
1065                data_read=subelem.value;
1066                list_element{iline,3}=['= ' data_read];
1067            end
1068            if iscell(subelem)|(isfield(subelem,'type')&isequal(subelem.type,'element'))
1069                list_element{iline,1}='+ ';%sign for subtree existence
1070            else
1071                list_element{iline,1}='  ';
1072            end
1073            nbattr=attributes(t,'length',list_uid(iline));
1074            if nbattr==1
1075                attr=attributes(t,'get',list_uid(iline));
1076                list_element{iline,4}=[attr.key '='];
1077                list_element{iline,5}=attr.val;
1078            elseif nbattr>1
1079                for iattr=1:nbattr
1080                    attr=attributes(t,'get',list_uid(iline),iattr);
1081                    list_element{iline,2+2*iattr}=[attr.key '='];
1082                    list_element{iline,3+2*iattr}=attr.val;
1083                end
1084            end
1085        end
1086        end
1087    set(handles.list_element,'Value',1)%select the first line of list_element by default
1088        set(handles.list_element,'String',cell2tab(list_element,' ') )
1089    list.uid=list_uid;
1090        set(handles.list_element,'UserData',list)
1091end
1092%---------------------------------------------------------
1093%-------------------------------------
1094%updates the interface
1095function update_ref_list(hh,xs_element,element,node,xs_subelem,subelem)
1096%-----------------------------
1097pref_col='';
1098key_col='';
1099equal_sign='';
1100val_col='';
1101for iline=1:length(subelem)
1102    xsindex=subelem(iline).xsindex;
1103    indexcur=subelem(iline).index;
1104    subuid=subelem(iline).uid;
1105        if isempty(subuid)
1106             RefDataIn.uid(iline)=0;
1107        else
1108             RefDataIn.uid(iline)=subuid;
1109        end
1110    index(iline)=indexcur;
1111    node(iline)=xs_subelem(xsindex).node;
1112%         testmanual(iline)=subelem(iline).testmanual;
1113    ikey=xs_subelem(xsindex).key;
1114    if xs_subelem(xsindex).testsub
1115        ival=[' + ' subelem(iline).val];
1116    else
1117        ival=[' = ' subelem(iline).val];
1118    end
1119    key_col=strvcat(key_col,ikey);
1120    val_col=strvcat(val_col,ival);
1121end
1122RefDataIn.xs_uid=node;
1123list_element=[key_col val_col];
1124siztext=size(list_element);
1125set(hh,'Value',[1:siztext(1)])
1126set(hh,'String',list_element)
1127set(hh,'UserData',RefDataIn)
1128
1129%
1130% function RefFile_Callback(hObject, eventdata, handles)
1131% global t_ref xs
1132% t_ref=xmltree(get(hObject,'String'));%open the xml file fileinput
1133% heditxml=get(hObject,'parent');
1134% DataIn=get(get(hObject,'parent'),'UserData');
1135% % set(heditxml,'Units','pixel')
1136% figpos=get(heditxml,'Position')
1137% % title_element=get(handles.element_cur,'UserDataIn');
1138% % xs_node=xsnode_index(1,ind);
1139% % index_chosen=xsnode_index(2,ind);
1140% if isfield(DataIn,'fig_ref')&ishandle(DataIn.fig_ref)
1141%     figure(DataIn.fig_ref);
1142% else
1143%     DataIn.fig_ref=figure;
1144% end
1145% set(DataIn.fig_ref,'Name',get(hObject,'String'))
1146% set(DataIn.fig_ref,'MenuBar','none')
1147% newfigpos=[figpos(1)+figpos(3) figpos(2)+0.4*figpos(4) 0.5*figpos(3) 0.3*figpos(4)];
1148% set(DataIn.fig_ref,'Units','normalized')
1149% set(DataIn.fig_ref,'Position',newfigpos)
1150% DataIn.h_ref=uicontrol('Style','listbox', 'Max',2,'Units','pixel','Position', [0 0 newfigpos(3) newfigpos(4)], ...
1151%         'FontName','FixedWidth','Tag','listbox');
1152% if isfield(DataIn,'xs_CurrentUid');
1153%     xs_CurrentUid=DataIn.xs_CurrentUid(end);
1154% else
1155%     DataIn.xs_CurrentUid=find(xs,'/xs:schema/xs:element');%uid of the root element in the schema
1156% end
1157% [nodeup,path,xs_element,xs_subelem]=scan_schema(xs,xs_CurrentUid(end));
1158% xs_element.key
1159% tag0=find(t_ref,['/' path '/' xs_element.key]);
1160% if length(tag0)>=1
1161%     CurrentRefNode=tag0(1);%chose the first occurence of the element
1162% else
1163%     CurrentRefNode=0;
1164% end
1165% [ref_element,ref_subelem]=get_xml(t_ref,path,xs_element,CurrentRefNode,xs_subelem);
1166% update_ref_list(DataIn.h_ref,xs_element,ref_element,CurrentRefNode,xs_subelem,ref_subelem);
1167% siztext=size(get(DataIn.h_ref,'String'));
1168% set(DataIn.h_ref,'Value',[1:siztext(1)]); %select the whole list by default
1169% set(heditxml,'UserData',DataIn)
1170% set(handles.ref_data,'Value',siztext(1)); %select the whole list by default
1171% 'TESTimport'
1172% title_element=get(handles.element_cur,'UserDataIn')
1173% xs_node=title_element.xsnode;%uid of the element in the schema
1174% node=title_element.node;
1175% t=flush(t,node);%removes the corresponding subtree in t
1176% [nodeup,path,xs_element,xs_subelem]=scan_schema(xs,xs_node);%scan the schema
1177% tag0=find(t_import,['/' path '/' xs_element.key])
1178% if isempty(tag)
1179%     errordlg(['element /' path '/' xs_element.key ' not found in' fileinput])
1180%     return
1181% end
1182% % [element_import,node_import]=get_xml(t_import,path,xs_element,1,xs_subelem);% read the corresponding xml data
1183% node2_import=children(t_import,tag0);
1184% % t_import=branch(t_import,node_import);% extract branch of the new file
1185% % %removes the corresponding subtree in t
1186% for inode=1:length(node2_import)
1187%     struct=get(t_import,node2_import(inode))
1188%     if isfield(struct,'type') & isfield(struct,'name')%if the node is an elmeent type
1189%         node3_import=children(t_import,node2_import(inode))
1190%        [t,newuid]=add(t,node,struct.type,struct.name);
1191%        for inode2=1:length(node3_import)
1192%            struct2=get(t_import,node3_import(inode2))
1193%            if isequal(struct2.type,'chardata')
1194%                 t=add(t,newuid,'chardata',struct2.value);
1195%             end
1196%         end
1197%     end
1198% end
1199% --- Executes on button press in replicate.
1200function replicate_Callback(hObject, eventdata, handles)
1201global xs  t
1202
1203export_list=get(handles.export_list,'String');
1204export_val=get(handles.export_list,'UserData');
1205heditxml=get(handles.replicate,'parent');
1206Data=get(heditxml,'UserData')
1207
1208hdataview=findobj(allchild(0),'Name','dataview')
1209if isempty(hdataview)
1210    hdataview=dataview;
1211    return
1212end
1213hhdataview=guidata(hdataview);
1214CurrentPath=get(hhdataview.CurrentFile,'String');
1215ListExperiments=get(hhdataview.ListExperiments,'String');
1216Value=get(hhdataview.ListExperiments,'Value');
1217if ~isequal(Value,1)
1218    ListExperiments=ListExperiments(Value);
1219end
1220ListDevices=get(hhdataview.ListDevices,'String');
1221Value=get(hhdataview.ListDevices,'Value');
1222if ~isequal(Value,1)
1223    ListDevices=ListDevices(Value);
1224end
1225ListRecords=get(hhdataview.ListRecords,'String');
1226Value=get(hhdataview.ListRecords,'Value');
1227if ~isequal(Value,1)
1228    ListRecords=ListRecords(Value);
1229end
1230% uvmat('runplus_Callback',hObject,eventdata,handleshaxes)
1231[ListDevices,ListRecords,ListXml,List]=ListDir(CurrentPath,ListExperiments,ListDevices,ListRecords);
1232ListXml=get(hhdataview.ListXml,'String');
1233Value=get(hhdataview.ListXml,'Value');
1234if isequal(Value,1)
1235    msgbox_uvmat('ERROR','you need to select the xml files to edit')
1236    return
1237end
1238ListXml=ListXml(Value);%list of
1239for iexp=1:length(List.Experiment)
1240    ExpName=List.Experiment{iexp}.name;
1241    if isfield(List.Experiment{iexp},'Device')
1242        for idevice=1:length(List.Experiment{iexp}.Device)
1243            DeviceName=List.Experiment{iexp}.Device{idevice}.name;       
1244            if isfield(List.Experiment{iexp}.Device{idevice},'xmlfile')
1245                for ixml=1:length(List.Experiment{iexp}.Device{idevice}.xmlfile)
1246                    FileName=List.Experiment{iexp}.Device{idevice}.xmlfile{ixml};
1247                    for ilistxml=1:length(ListXml)
1248                        if isequal(FileName,ListXml{ilistxml})
1249                            xmlfullname=fullfile(CurrentPath,ExpName,DeviceName,FileName);
1250                            t_export=xmltree(xmlfullname);
1251                            rootelement=get(t_export,1,'name');
1252                            uidlist=Data.CurrentUid;
1253                            if isequal(rootelement,get(t,1,'name'))
1254                                backupfile=xmlfullname;
1255                                testexist=2;
1256                                while testexist==2
1257                                   backupfile=[backupfile '~'];
1258                                   testexist=exist(backupfile,'file');
1259                                end
1260                                [success,message]=copyfile(xmlfullname,backupfile);%make backup
1261                                if ~isequal(success,1)
1262                                    msgbox_uvmat('ERROR',['Error in the backup of ' xmlfullname])
1263                                    return
1264                                end
1265                                findstr=['/' rootelement];
1266                                uid_export(1)=1;
1267                                % fill the root elements if absent
1268                                for index=2:length(uidlist)
1269                                    name_t=get(t,uidlist(index),'name')
1270                                    findstr=[findstr '/' name_t]
1271                                    uid=find(t_export,findstr)
1272                                    if isempty(uid)
1273                                        [t_export,uid_export(index)]=add(t_export,uid_export(index-1),'element',name_t);
1274                                    else
1275                                        uid_export(index)=uid;
1276                                    end                           
1277                                end
1278                                % chardata......
1279                            end
1280                            break
1281                        end
1282                    end
1283%                     [Title,test]=check_heading(Currentpath,Campaign,ExpName,DeviceName,[],FileName,SubCampaignTest);
1284%                     if test
1285%                         [List.Experiment{iexp}.Device{idevice}.xmlfile{ixml} ' , Heading updated']
1286%                     end
1287                end
1288             elseif isfield(List.Experiment{iexp}.Device{idevice},'Record')
1289                for irecord=1:length(List.Experiment{iexp}.Device{idevice}.Record)
1290                    RecordName=List.Experiment{iexp}.Device{idevice}.Record{irecord}.name;
1291                    if isfield(List.Experiment{iexp}.Device{idevice}.Record{irecord},'xmlfile')
1292                        for ixml=1:length(List.Experiment{iexp}.Device{idevice}.Record{irecord}.xmlfile)
1293                            FileName=List.Experiment{iexp}.Device{idevice}.Record{irecord}.xmlfile{ixml};
1294                            for ilistxml=1:length(ListXml)
1295                                if isequal(FileName,ListXml{ilistxml})
1296                                    xmlfullname=fullfile(CurrentPath,ExpName,DeviceName,RecordName,FileName)
1297                                    break
1298                                end
1299                            end
1300%                             [Title,test]=check_heading(Currentpath,Campaign,ExpName,DeviceName,RecordName,FileName,SubCampaignTest);
1301%                             if test
1302%                                 [FileName ' , Heading updated']
1303%                             end
1304                        end
1305                    end
1306                end
1307            end
1308        end
1309    end
1310end
1311return
1312%%%%%%%%%%% A REVOIR
1313% Copier la liste des elements selectionnés dans dataview
1314%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1315%ANCIEN:
1316heditxml=get(hObject,'parent');
1317DataIn=get(heditxml,'UserData');
1318if ~isfield(DataIn,'h_ref')
1319    DataIn.h_ref=[];
1320end
1321if ~ishandle(DataIn.h_ref)
1322    errordlg('no source file opened for import')
1323    return
1324end
1325%create the current root element if needed
1326LengthElement=length(DataIn.CurrentUid);
1327FilledUid=find(DataIn.CurrentUid~=0);
1328LengthFilled=FilledUid(end);
1329for irank=LengthFilled+1:LengthElement
1330    attrib=attributes(xs,'get',DataIn.xs_CurrentUid(irank),1);
1331    [t,DataIn.CurrentUid(irank)]=add(t,DataIn.CurrentUid(irank-1),'element',attrib.val);
1332end
1333
1334%copy list of subelements
1335RefDataIn=get(DataIn.h_ref,'UserData');
1336list=get(handles.list_element,'UserData');%=,[node;index]
1337for ilist=get(DataIn.h_ref,'Value')
1338    node_content=get(xs,RefDataIn.xs_uid(ilist));
1339    if ~isempty(node_content) & isfield(node_content,'attributes')
1340        attrib=node_content.attributes;
1341        for iattr=1:length(attrib)
1342            struct=attrib{iattr};
1343            if isequal(struct.key,'name')
1344                key=struct.val; % read element key name
1345            end
1346        end
1347    end
1348    value='';
1349    if ~isequal(RefDataIn.uid(ilist),0)
1350        child_uid=children(t_ref,RefDataIn.uid(ilist));     
1351        if isequal(length(child_uid),1)
1352            content=get(t_ref,child_uid);
1353            if isfield(content,'type') &isfield(content,'value')& isequal(content.type,'chardata')
1354                value=content.value;
1355            end
1356        end
1357    end
1358    t=set_element(t,DataIn.CurrentUid(end),list.uid(ilist),key,value);
1359end
1360set(heditxml,'UserData',DataIn)
1361%update the current listing
1362[nodeup,path,xs_element,xs_subelem]=scan_schema(xs,DataIn.xs_CurrentUid(end));
1363[element,subelem]=get_xml(t,path,xs_element,DataIn.CurrentUid(end),xs_subelem);
1364update_list(handles,path,xs_element,element,DataIn.CurrentUid(end),xs_subelem,subelem);
1365% t=set_xml(t,xs_DataIn,subelem)
1366%edit list of subelments:   
1367
1368%A REVOIR
1369% xsnode_index=get(handles.list_element,'UserDataIn')
1370% xs_node=xsnode_index(1,ind);
1371% RefDataIn=get(handles.ref_data,'UserDataIn');
1372% subelem=RefDataIn.subelem;
1373% xsnode_index=get(handles.list_element,'UserDataIn');%data on the xs_nodes of the subelements
1374% ref_list=get(handles.ref_data,'Value');%selected indices in the list of reference subelements
1375% xs_node=xsnode_index(1,ref_list);%xs_nodes of the selected subelements
1376% % index_chosen=xsnode_index(2,ref_list);% indices in the list of occurence for a subelement
1377% % for ilist=ref_list
1378% ilist=1;
1379% while icontinue
1380%     'TESTCOPY'
1381%     [nodeup,path,xs_element,xs_subelem]=scan_schema(xs,xs_node(ilist))
1382%     xs_subelem.key
1383%     tsub=subelem(ilist).tsub
1384%     xsnode_index(2,ilist)
1385%     [ref_elem,ref_node,ref_subelem]=get_xml(tsub,'',xs_element,1,xs_subelem)
1386%     ref_subelem.val
1387%     testedit= ~isfield(element.attrup,'source') | isequal(element.attrup.source,'manual') %| element vide
1388%     
1389%     
1390%     icontinue=0
1391% end
1392% function [element,node,subelem]=get_xml(t,path,xs_element,node,xs_subelem)
1393% element.attr_key='';%default
1394% element.attr_val='';%default
1395% element.val='';
1396% % element.type='';
1397% % element.testmanual=testmanual %inheritates the input manual editing flag by default
1398% subelem=[]; %default
1399% attrup=[];
1400% % node=[];
1401%
1402% % %find the element properties in the xml file
1403% if node >= 1
1404%     elem_struct=get(t,node);
1405%     if ~xs_element.subtest
1406%         elem_contents=get(t,elem_struct.contents);
1407%         if isempty(elem_contents)
1408%             element.val=[];
1409%         else
1410%             element.val=elem_contents.value
1411%         end
1412%     end
1413%     if isfield(elem_struct,'attributes')
1414%         elem_attr=elem_struct.attributes;
1415%         for iattr=1:length(elem_attr)
1416%             element.attr_key{iattr}=elem_attr{iattr}.key ;
1417%             element.attr_val{iattr}=elem_attr{iattr}.val;
1418% %             attrup=setfield(attrup,elem_attr{iattr}.key,elem_attr{iattr}.val);
1419%            breakdetect=find(elem_attr{iattr}.key=='/'| elem_attr{iattr}.key==':'| elem_attr{iattr}.key=='.');% find '/'
1420%            if isempty(breakdetect)
1421% %                 comline=['attrup.' elem_attr{iattr}.key '=' elem_attr{iattr}.val ';']
1422%                 eval(['attrup.' elem_attr{iattr}.key '=''' elem_attr{iattr}.val ''';'])
1423%            end
1424%         end
1425%     end
1426% end
1427% %get the parent node attributes
1428% up=1
1429% if node>0
1430%       nodeup=node;
1431%       while up==1;
1432%         nodeup=get(t,nodeup,'parent');%move up to the parent in the tree
1433%         if isempty(nodeup)
1434%             up=0;
1435%         else
1436%             nodeup_content=get(t,nodeup);
1437%             attrib=nodeup_content.attributes;
1438%             for iattr=1:length(attrib)
1439%                 key=attrib{iattr}.key;
1440%                 breakdetect=find(key=='/'| key==':'| key=='.');% find '/'
1441%                 if ~isfield(attrup,key) & isempty(breakdetect)
1442%                    eval(['attrup.' key '=''' attrib{iattr}.val ''';'])
1443%                 end
1444%             end
1445%         end
1446%       end
1447%       element.attrup=attrup;
1448% end
1449% %find the subelement properties in the xml file
1450% if xs_element.subtest
1451%    iline=0;
1452%    for k=1:length(xs_subelem)%node2: list of subelements in the sub-sequence
1453% %     attr=attributes(xs,'get',node2(i),1);%
1454% %     element=attr.val;%name of the element
1455%      tag=find(t,['/' path '/' xs_element.key '/' xs_subelem(k).key]);%look for the corresponding element node in the .xml tree
1456%      struct_element=get(t,tag);%get the content of the element
1457%      if isempty(struct_element)
1458%          iline=iline+1;
1459%          subelem(iline).xsindex=k;
1460%          subelem(iline).index=0;
1461% %          subelem(iline).testmanual=element.testmanual;% inheritates the manual editing flag by default
1462%          if isequal(xs_subelem(k).minOccurs,0)
1463%              subelem(iline).val='[]';%element value not mandatory in the schema
1464%          else
1465%              subelem(iline).val='[MISSING]';%element value mandatory in the schema
1466%          end
1467% %          subelem(iline).attrup=attrup; %inheritated attributes
1468%      elseif isequal(length(struct_element),1)
1469%          contents=get(t,struct_element.contents);
1470%          iline=iline+1;
1471%          subelem(iline).xsindex=k;
1472%          subelem(iline).index=1;
1473% %          subelem(iline).testmanual=element.testmanual;%
1474%          if isfield(contents,'value') & ~isempty(contents.value)
1475%              subelem(iline).val=contents.value;
1476%          elseif xs_subelem(k).testsub
1477%              subelem(iline).val='';
1478%          elseif isequal(xs_subelem(k).minOccurs,0)
1479%              subelem(iline).val='[]';%element value not mandatory in the schema
1480%          else
1481%              subelem(iline).val='[MISSING]';%element value mandatory in the schema
1482%          end
1483% %          subelem(iline).attrup=attrup; %inheritated attributes
1484%          if isfield(struct_element,'attributes')
1485%             element_attr=struct_element.attributes;
1486%             attr_display=[];
1487%             for iattr=1:length(element_attr)
1488% %                 attr_display{iline}=[attr_display ' , ' element_attr{iattr}.key '  =  ' element_attr{iattr}.val];
1489%                 subelem(iline).val=[subelem(iline).val attr_display ' , ' element_attr{iattr}.key '  =  ' element_attr{iattr}.val];
1490% %                 subelem(iline).attrup=setfield(subelem(iline).attrup,element_attr{iattr}.key,element_attr{iattr}.val);
1491%             end
1492%          end
1493%      else%case of a multiple element
1494%          for subindex=1:length(struct_element)
1495%              contents=get(t,struct_element{subindex}.contents);
1496%              iline=iline+1;
1497%              subelem(iline).index=subindex;%index of the element
1498%              subelem(iline).xsindex=k;
1499% %              subelem(iline).testmanual=element.testmanual;%
1500%              if isfield(contents,'value')& ~isempty(contents.value)
1501%                  subelem(iline).val=contents.value;
1502%              elseif xs_subelem(k).testsub
1503%                  subelem(iline).val='';
1504%              else
1505%                  subelem(iline).val='[]';
1506%              end
1507% %              subelem(iline).attrup=attrup; %inheritated attributes
1508%              if isfield(struct_element{subindex},'attributes')
1509%                 element_attr=struct_element{subindex}.attributes;
1510%                 attr_display=[];
1511%                 for iattr=1:length(element_attr)
1512% %                     attr_display{iline}=[attr_display ' , ' element_attr{iattr}.key '  =  ' element_attr{iattr}.val];
1513%                     subelem(iline).val=[subelem(iline).val attr_display ' , ' element_attr{iattr}.key '  =  ' element_attr{iattr}.val];
1514% %                     subelem(iline).attrup=setfield(subelem(iline).attrup,element_attr{iattr}.key,element_attr{iattr}.val);
1515%                 end
1516%             end
1517%         end
1518%      end
1519%   end
1520% end
1521
1522
1523% --- Executes on button press in HELP.
1524function HELP_Callback(hObject, eventdata, handles)
1525path_to_uvmat=which ('uvmat')% check the path of uvmat
1526pathelp=fileparts(path_to_uvmat);
1527helpfile=fullfile(pathelp,'UVMAT_DOC','uvmat_doc.html');
1528if isempty(dir(helpfile)), errordlg('Please put the help file uvmat_doc.html in the directory UVMAT/UVMAT_DOC')
1529else
1530web([helpfile '#editxml'])   
1531end
1532
1533
1534% --- Executes on button press in Export.
1535function Export_Callback(hObject, eventdata, handles)
1536val=get(handles.Export,'Value');
1537if val
1538    set(handles.Export,'BackgroundColor',[0 1 0])
1539    set(handles.export_list,'Visible','on')
1540    set(handles.replicate,'Visible','on')
1541    h_dataview=findobj(allchild(0),'name',dataview');
1542    if isempty(h_dataview)
1543       % CurrentFile=get(handles.CurrentFile,'String');
1544        dataview;
1545    end
1546else
1547     set(handles.Export,'BackgroundColor',[0.7 0.7 0.7])
1548    set(handles.export_list,'Visible','off')
1549    set(handles.replicate,'Visible','off')
1550end
1551
1552
1553% --- Executes on button press in pushbutton9.
1554function pushbutton9_Callback(hObject, eventdata, handles)
1555% hObject    handle to pushbutton9 (see GCBO)
1556% eventdata  reserved - to be defined in a future version of MATLAB
1557% handles    structure with handles and user data (see GUIDATA)
1558
1559
Note: See TracBrowser for help on using the repository browser.