source: trunk/src/editxml.m @ 100

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

many bug corrections and cleaning. Activation of the BW option in uvmat. Improvement of the interaction of get_field with uvmat.

File size: 62.3 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    xmlparam=fullfile(path_UVMAT,'PARAM.xml');
335    if exist(xmlparam,'file')
336        tparam=xmltree(xmlparam);
337        sparam=convert(tparam);
338        if isfield(sparam,'SchemaPath')
339            schemafile=[fullfile(sparam.SchemaPath,head_name) '.xsd'];
340        end
341        if ~exist(schemafile,'file')
342            schemafile=fullfile(path_UVMAT,schemafile);%look for relative path definition
343        end
344        if exist(schemafile,'file')
345            xs=xmltree(schemafile);
346        else
347            msgbox_uvmat('ERROR',['The xml schema for ' CurrentFile ' is unknown, check the schema path set in the file PARAM.xml'])
348            [FileName, PathName]=uigetfile( ...
349           {'*.xsd', '(*.xsd)';
350            '*.xsd',  '.xsd files '; ...
351            '*.*',  'All Files (*.*)'}, ...
352            'Pick a .xsd schema' ,schemapath); %file browser
353            if ischar(PathName) && ischar(FileName) && exist(fullfile(PathName,FileName),'file')
354                DataIn.Schema=fullfile(PathName,FileName);
355                xs=xmltree(DataIn.Schema);%open the associated schema file
356            else
357                xs=[];
358            end
359        end
360    end
361end
362DataIn.CurrentUid=1;
363if isempty(xs)
364    displ_xml(handles,t,1,DataIn,get(hObject,'parent'));%no associated schema, default  display of the xml file
365else
366    DataIn.xs_CurrentUid=find(xs,'/xs:schema/xs:element');%uid of the root element in the schema
367        [nodeup,path,xs_element,xs_subelem]=scan_schema(xs,DataIn.xs_CurrentUid);%scan the schema at the root level
368        [element,subelem]=get_xml(t,path,xs_element,1,xs_subelem);% read the corresponding xml data
369        update_list(handles,path,xs_element,element,1,xs_subelem,subelem);%update the display of information on the interface
370end
371set(heditxml,'UserData',DataIn);%store the new input xml file name
372
373%-------------------------------------------------------
374%  function scan_schema: read the xml schema xs
375%--------------------------------------------------------
376%OUTPUT:
377%nodeup: parent node of nodeinput
378%path: path to nodeinput in the tree
379%xs_element: element corresponding to nodeinput
380    %xs_element.uid, =tag of the element in the schema (=nodeinput)
381    %xs_element.key: key label of nodeinput
382    %xs_element.type: type of data contained in the element
383    %xs_element.annot: annotation of nodeinput
384    %xs_element.attrib: list of accepted attributes keys for xs_element
385    %xs_element.enum: enumeration, list of accepted values for nodeinput
386    %xs_element.subtest: =1 if the element contains subelements in the schema, 0 else
387 
388%xs_subelement(k): subelement #k of xs_element
389    %xs_subelem(k).node: node number in the schema
390    %xs_subelem(k).key: key name of the element
391    %xs_subelem(k).testsub: =1 if element contains subelements, 0 else
392    %xs_subelem(k).minOccurs: =0 for a non mandatory element, =1 else
393    %xs_subelem(k).maxOccurs
394%
395%INPUT:
396%xs: schema xml tree
397%nodeinput: tag of the current root element in the schema
398function [nodeup,path,xs_element,xs_subelem]=scan_schema(xs,nodeinput)
399nodeup=[];
400path=[];
401xs_element.key=[];
402xs_element.type=[];
403xs_element.annot=[];
404xs_element.attrib=[];
405xs_element.subtest=0;
406xs_element.enum={};
407xs_subelem=[];%default
408% get default nodeinput (root of the file) if not defined
409if ~exist('nodeinput') | isempty(nodeinput)% we start at the root
410    node=find(xs,'/xs:schema/xs:element');%description of the root element
411else
412    node=nodeinput;
413end
414xs_element.uid=node;
415%get the key name and element_type of the element
416node_content=get(xs,node);
417if isempty(node_content),return,end;
418if ~isempty(node_content) & isfield(node_content,'attributes')
419    attrib=node_content.attributes;
420    for iattr=1:length(attrib)
421        struct=attrib{iattr};
422        if isequal(struct.key,'name')
423            xs_element.key=struct.val; % read element key name
424        elseif isequal(struct.key,'type')
425            xs_element.type=struct.val; % read element key name
426        end
427    end
428end
429
430%get the parent node of nodeinput
431if ~isempty(node_content)
432    nodeup=get(xs,node,'parent');%move up to the parent in the tree
433    if ~isempty(nodeup)
434        nodeup=get(xs,nodeup(1),'parent');%move up to the parent in the tree
435        if isequal(nodeup,[])
436            %OUVRIR FICHIER AMONT
437            up=0;
438        else
439            nodeup=get(xs,nodeup(1),'parent');%move up to the parent in the tree
440        end
441    end
442end
443%get the path to 'nodeinput' in the schema
444up=1;
445path=[];
446if ~isempty(nodeup)
447    attrib=attributes(xs,'get',nodeup,1);
448    path=attrib.val;
449    nodeup2=nodeup;
450    while up==1;
451        nodeup2=get(xs,nodeup2(1),'parent');%move up to the parent in the tree
452        nodeup2=get(xs,nodeup2(1),'parent');%move up to the parent in the tree
453        if isempty(nodeup2)
454            up=0;
455        else
456            nodeup2=get(xs,nodeup2(1),'parent');%move up to the parent in the tree
457            if isempty(nodeup2)
458                up=0;
459            else
460                attrib=attributes(xs,'get',nodeup2,1);
461                path=[attrib.val '/' path];
462            end
463        end
464    end
465end   
466
467%explore the subtree in the schema file
468node1=children(xs,node); %find the children of the root element
469test_sub=0; %no subtree in the .xml file by default
470comment='';
471element={};
472minOccurs={};
473maxOccurs={};
474testsub={};
475list_menu={};
476text={};
477if ~isempty(node1) 
478  for i=1:length(node1)
479    nodename1=get(xs,node1(i),'name');
480    node2=children(xs,node1(i));
481    if isequal(nodename1,'xs:annotation')
482         for j=1:length(node2)
483            nodename2=get(xs,node2(j),'name');
484            if isequal(nodename2,'xs:documentation')
485                node3=children(xs,node2(j));
486                xs_element.annot=get(xs,node3,'value');%read annotation
487            end
488        end
489    % pour les elements
490    elseif isequal(nodename1,'xs:simpleType')
491        for j=1:length(node2)
492            nodename2=get(xs,node2(j),'name');
493            if isequal(nodename2,'xs:restriction')
494                node3=children(xs,node2(j));
495                for k=1:length(node3)
496                    nodename3=get(xs,node3(k),'name');
497                    if isequal(nodename3,'xs:enumeration')
498                        node3_content=get(xs,node3(k));
499                        attr=node3_content.attributes;
500                        for m=1:length(attr)
501                            struct=attr{m};
502                            if isequal(struct.key,'value')
503                                xs_element.enum{k}=struct.val; % read enumeration
504                            end
505                        end   
506                   end
507               end
508            end
509        end
510     elseif isequal(nodename1,'xs:complexType')
511         for j=1:length(node2)
512             nodename2=get(xs,node2(j),'name');
513             if isequal(nodename2,'xs:attribute')
514                 node_content=get(xs,node2(j));
515                 attr=node_content.attributes;
516                 for k=1:length(attr)
517                    struct=attr{k};%read attributes
518                    if isequal(struct.key,'name')
519                        xs_element.attrib=struct.val; %read attributes of main node
520                    end
521                 end   
522             elseif isequal(nodename2,'xs:sequence')
523                 xs_element.subtest=1;
524                 node3=children(xs,node2(j));%nodes of the sequence
525                 for k=1:length(node3)
526                     xs_subelem(k).node=node3(k);
527                     xs_subelem(k).testsub=0;%default
528                     node_content=get(xs,node3(k));
529                     xs_subelem(k).minOccurs=1; %default
530                     xs_subelem(k).maxOccurs=1; %default
531%                      pref{k}=[]; %default
532                     if isequal(node_content.name,'xs:element')
533                        attr=node_content.attributes;
534%                         attr{:}.key
535                        for l=1:length(attr)
536                            if isequal(attr{l}.key,'name')
537                                xs_subelem(k).key=attr{l}.val;%name of the element
538                            elseif isequal(attr{l}.key, 'minOccurs')
539                                xs_subelem(k).minOccurs=attr{l}.val;
540                            elseif isequal(attr{l}.key, 'maxOccurs')
541                                xs_subelem(k).maxOccurs=attr{l}.val;
542                            end
543                        end
544                     end
545                     node4=children(xs,node3(k));
546                     for l=1:length(node4)
547                        res=get(xs,node4(l),'name');
548                        if isequal(res,'xs:complexType')%look whether the element k contains a subtree
549                           node5=children(xs,node4(l));
550                           for m=1:length(node5)
551                               res2=get(xs,node5(m),'name');
552                               if isequal(res2,'xs:sequence')
553                                    xs_subelem(k).testsub=1; %flag for the existence of a subtree
554                               end
555                           end
556                        end
557                     end
558                 end           
559
560             end
561         end     
562     end   
563  end
564end
565% look for predefined types
566if length(xs_element.type)>=3 & (xs_element.type([1:3])~='xs:')
567    node_type=find(xs,'/xs:schema/xs:simpleType')
568    for i=1:length(node_type)
569        content=get(xs,node_type(i));
570        nodeattr=content.attributes;
571        if ~isempty(nodeattr) & isequal(nodeattr{1}.key,'name') & isequal(nodeattr{1}.val,xs_element.type)
572            node1=children(xs,node_type(i));
573            node2=find(xs,node1,'name','xs:restriction');
574%             nodename1=find(xs,
575            node3=children(xs,node2);
576            node4=find(xs,node3,'name','xs:enumeration');
577            for ienum=1:length(node4)
578                struct2=get(xs,node4(ienum));
579                enumval=struct2.attributes;
580                xs_element.enum{ienum}=enumval{1}.val;
581            end
582        end       
583     end
584end
585
586%--------------------------------------------------------
587%OUTPUT:
588%element.val: value of the current element, =[] in the absence of chardata value
589%node: node (iud) of the element in t
590%element.attr_key{iattr}: attribute key #iattr of the current element
591%element.attr_val{iattr}: attribute value #iattr of the current element
592%element.attrup: %structure containing the attributes of the element, including the ones unheritated from parent nodes
593%subelem(iline).val : value of subelement # iline, concatenated with corresponding attributes
594%subelem(iline).xsindex: index k of the subelement #iline in the list xs_subelem of the schema
595%subelem(iline).index: index of the subelement #iline inside its xs_subelement, =0 when the xs_subelement is absent in t
596
597%INPUT:
598%t: xml tree
599%path: path to the current element in the schema
600%xs_element: current element in the schema
601    %xs_element.key: key label 
602    %xs_element.type: type of data contained in the element
603    %xs_element.annot: annotation of nodeinput
604    %xs_element.attrib: list of accepted attributes keys for xs_element
605    %xs_element.enum: enumeration, list of accepted values
606    %xs_element.subtest: =1 if the element contains subelements in the schema, 0 else
607%index: index of the element, =1 in case of single occurence in xs_element,=0 in case of missing element
608%xs_subelem(k): subelement #k of the current element in the schema
609    %xs_subelem(k).node: node iud of the
610    %xs_subelem(k).key: key name of the subelement #k in the schema
611    %xs_subelem(k).testsub: =1 if element contains subelements, 0 else
612    %xs_subelem(k).minOccurs
613    %xs_subelem(k).maxOccurs
614
615function [element,subelem]=get_xml(t,path,xs_element,node,xs_subelem)
616element.attr_key='';%default
617element.attr_val='';%default
618element.val='';
619% element.type='';
620% element.testmanual=testmanual %inheritates the input manual editing flag by default
621subelem=[]; %default
622attrup=[];
623% node=[];
624
625% %find the element properties in the xml file
626if node >= 1
627    elem_struct=get(t,node);
628    if ~xs_element.subtest
629        elem_contents=get(t,elem_struct.contents);
630        if isempty(elem_contents)
631            element.val=[];
632        else
633            element.val=elem_contents.value
634        end
635    end
636    if isfield(elem_struct,'attributes')
637        elem_attr=elem_struct.attributes;
638        for iattr=1:length(elem_attr)
639            element.attr_key{iattr}=elem_attr{iattr}.key ;
640            element.attr_val{iattr}=elem_attr{iattr}.val;
641%             attrup=setfield(attrup,elem_attr{iattr}.key,elem_attr{iattr}.val);
642           breakdetect=find(elem_attr{iattr}.key=='/'| elem_attr{iattr}.key==':'| elem_attr{iattr}.key=='.');% find '/'
643           if isempty(breakdetect)
644%                 comline=['attrup.' elem_attr{iattr}.key '=' elem_attr{iattr}.val ';']
645                eval(['attrup.' elem_attr{iattr}.key '=''' elem_attr{iattr}.val ''';'])
646           end
647        end
648    end
649end
650%get the parent node attributes
651up=1;
652if node>0
653        nodeup=node;
654        while up==1;
655        nodeup=get(t,nodeup,'parent');%move up to the parent in the tree
656        if isempty(nodeup)
657            up=0;
658        else
659            nodeup_content=get(t,nodeup);
660            attrib=nodeup_content.attributes;
661            for iattr=1:length(attrib)
662                key=attrib{iattr}.key;
663                breakdetect=find(key=='/'| key==':'| key=='.');% find '/'
664                if ~isfield(attrup,key) & isempty(breakdetect)
665                   eval(['attrup.' key '=''' attrib{iattr}.val ''';'])
666                end
667            end
668        end
669        end
670        element.attrup=attrup;
671end
672%find the subelement properties in the xml file
673if xs_element.subtest
674   iline=0;
675   for k=1:length(xs_subelem)%node2: list of subelements in the sub-sequence
676%     attr=attributes(xs,'get',node2(i),1);%
677%     element=attr.val;%name of the element
678     tag=find(t,['/' path '/' xs_element.key '/' xs_subelem(k).key]);%look for the corresponding element node in the .xml tree
679     struct_element=get(t,tag);%get the content of the element
680     if isempty(struct_element)
681         iline=iline+1;
682         subelem(iline).uid=0;
683         subelem(iline).xsindex=k;
684         subelem(iline).index=0;
685%          subelem(iline).testmanual=element.testmanual;% inheritates the manual editing flag by default
686         if isequal(xs_subelem(k).minOccurs,'0')
687             subelem(iline).val='[]';%element value not mandatory in the schema
688         else
689             subelem(iline).val='[MISSING]';%element value mandatory in the schema
690         end
691%          subelem(iline).attrup=attrup; %inheritated attributes
692     elseif isequal(length(struct_element),1)
693         contents=get(t,struct_element.contents);
694         iline=iline+1;
695         subelem(iline).uid=tag;
696         subelem(iline).xsindex=k;
697         subelem(iline).index=1;
698%          subelem(iline).testmanual=element.testmanual;%
699         if isfield(contents,'value') & ~isempty(contents.value)
700             subelem(iline).val=contents.value;
701         elseif xs_subelem(k).testsub
702             subelem(iline).val='';
703         elseif 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         if isfield(struct_element,'attributes')
710            element_attr=struct_element.attributes;
711            attr_display=[];
712            for iattr=1:length(element_attr)
713%                 attr_display{iline}=[attr_display ' , ' element_attr{iattr}.key '  =  ' element_attr{iattr}.val];
714                subelem(iline).val=[subelem(iline).val attr_display ' , ' element_attr{iattr}.key '  =  ' element_attr{iattr}.val];
715%                 subelem(iline).attrup=setfield(subelem(iline).attrup,element_attr{iattr}.key,element_attr{iattr}.val);
716            end
717         end
718     else%case of a multiple element
719         for subindex=1:length(struct_element)
720             contents=get(t,struct_element{subindex}.contents);
721             iline=iline+1;
722             subelem(iline).index=subindex;%index of the element
723             subelem(iline).xsindex=k;
724%              subelem(iline).testmanual=element.testmanual;%
725             if isfield(contents,'value')& ~isempty(contents.value)
726                 subelem(iline).val=contents.value;
727             elseif xs_subelem(k).testsub
728                 subelem(iline).val='';
729             else
730                 subelem(iline).val='[]';
731             end
732%              subelem(iline).attrup=attrup; %inheritated attributes
733             if isfield(struct_element{subindex},'attributes')
734                element_attr=struct_element{subindex}.attributes;
735                attr_display=[];
736                for iattr=1:length(element_attr)
737%                     attr_display{iline}=[attr_display ' , ' element_attr{iattr}.key '  =  ' element_attr{iattr}.val];
738                    subelem(iline).val=[subelem(iline).val attr_display ' , ' element_attr{iattr}.key '  =  ' element_attr{iattr}.val];
739%                     subelem(iline).attrup=setfield(subelem(iline).attrup,element_attr{iattr}.key,element_attr{iattr}.val);
740                end
741            end
742        end
743     end
744  end
745end
746
747%-------------------------------------
748%updates the interface
749function update_list(handles,path,xs_element,element,node,xs_subelem,subelem)
750%-----------------------------
751if xs_element.subtest% we list the sub-elements of root
752    set(handles.export_list,'Value',1)
753    set(handles.export_list,'String','')%flush the export list
754    set(handles.CurrentElement,'String',[path '/' xs_element.key])
755%     title_element.key=[path '/' xs_element.key];
756%     if ~isempty(path)
757%         xsnode_index=get(handles.list_element,'UserDataIn');
758%         ind=get(handles.list_element,'Value');
759%         title_element.index=xsnode_index(2,ind);
760%     else
761%         title_element.index=1;
762%     end
763%     title_element.xsnode=xs_element.uid;
764%     title_element.node=node;
765%     set(handles.CurrentFile,'UserDataIn',title_element)%element corresponding to the title
766    set(handles.CurrentAnnotation,'String',xs_element.annot)
767    attr_col=[];
768    testedit=0;% cannot edit elements by default
769    for iattr=1:length(element.attr_key)
770%          if isequal(element.attr_key{iattr},'source') & isequal(element.attr_val{iattr},'manual')
771%             testedit=1;
772%         end
773        attr_col=strvcat(attr_col,[element.attr_key{iattr} ' = ' element.attr_val{iattr}]);
774    end
775    set(handles.CurrentAttributes,'String',attr_col)
776    pref_col='';
777    key_col='';
778    equal_sign='';
779    val_col='';
780    for iline=1:length(subelem)
781        xsindex=subelem(iline).xsindex;
782        index(iline)=subelem(iline).index;
783        subuid=subelem(iline).uid;
784        if isempty(subuid)
785            list.uid(iline)=0;
786        else
787            list.uid(iline)=subuid;
788        end
789        node(iline)=xs_subelem(xsindex).node;
790%         testmanual(iline)=subelem(iline).testmanual;
791        ikey=xs_subelem(xsindex).key;
792        if xs_subelem(xsindex).testsub
793            ival=[' + ' subelem(iline).val];
794        else
795            ival=[' = ' subelem(iline).val];
796        end
797        key_col=strvcat(key_col,ikey);
798        val_col=strvcat(val_col,ival);
799    end
800    list_element=[key_col val_col];
801    set(handles.list_element,'String',list_element)
802    set(handles.list_element,'Value',1)
803    list.xs_uid=node;
804    list.index=index;
805    set(handles.list_element,'UserData',list)
806    set(handles.element_attrib,'Visible','off')
807    set(handles.element_key,'Visible','off')
808    set(handles.element_value,'Visible','off')
809else % we edit an element
810
811    export_list=get(handles.export_list,'String');%export list
812    testadd=1;
813    for ilist=1:length(export_list)
814        if isequal(xs_element.key,export_list{ilist})
815            testadd=0;       
816            break
817        end
818    end
819    if testadd
820        export_list=[export_list;{xs_element.key}];
821        ilist=length(export_list);
822    end
823    set(handles.export_list,'String',export_list)
824    if iscell(element.val)
825        element_val=element.val{1};
826    else
827        element_val=element.val;
828    end
829    set(handles.element_value,'String',element_val)
830    export_val=get(handles.export_list,'UserData');
831    export_val{ilist}=element_val;
832    set(handles.export_list,'UserData',export_val);
833    set(handles.element_annot,'String',xs_element.annot)
834    set(handles.element_type,'String',['type:  ' xs_element.type])
835    attr_col=[];
836    testedit=0;% cannot edit element by default
837    for iattr=1:length(element.attr_key)
838%         if isequal(element.attr_key{iattr},'source') & isequal(element.attr_val{iattr},'manual')
839%             testedit=1;
840%         end
841        attr_col=strvcat(attr_col,[element.attr_key{iattr} ' = ' element.attr_val{iattr}]);
842    end
843    set(handles.element_attrib,'String',attr_col)
844    set(handles.element_key,'String',xs_element.key)
845
846 
847    if isempty(xs_element.enum)
848        set(handles.element_value,'Value',1)
849        set(handles.element_value,'Style','edit')
850    else % case of an enumeration of possible values
851         list_enum=[];
852         list_val=[];
853         for ienum=1:length(xs_element.enum)
854             list_enum{ienum,1}=xs_element.enum{ienum};
855             if isequal(xs_element.enum{ienum},element_val)
856                 list_val=ienum;
857             end
858         end
859         if isempty(list_val)
860             list_enum{length(xs_element.enum)+1,1}=['[' element_val ']'];%show the non-valid element between brackets
861             list_val=length(xs_element.enum)+1;
862         end
863         set(handles.element_value,'Style','popupmenu')
864         set(handles.element_value,'String',list_enum)
865         set(handles.element_value,'Value',list_val)
866     end
867     if isempty(element.val)
868         testedit=1;%allow element editing if value is missing
869     end     
870     set(handles.element_attrib,'Visible','On')
871     set(handles.element_key,'Visible','On')
872     set(handles.element_value,'Visible','On')
873end
874set(handles.element_value,'UserData',node)
875if ~testedit && isfield(element,'attrup') && isfield(element.attrup,'source')&& ~isequal(element.attrup.source,'manual')
876     set(handles.element_value,'Enable','inactive')
877else
878    set(handles.element_value,'Enable','on')
879end
880
881
882% --- Executes on button press in SAVE.
883function SAVE_Callback(hObject, eventdata, handles)
884global t
885DataIn=get(get(handles.SAVE,'parent'),'UserData');
886CurrentFile=get(handles.CurrentFile,'String');
887if isfield(DataIn,'Schema')
888if ~isempty(DataIn.Schema)% update ref to schema
889    attrxsd=attributes(t,'get',1);
890    setest=0;
891    for iattr=1:length(attrxsd)
892        if isequal(attrxsd{iattr}.key,'xsi:noNamespaceSchemaLocation')
893            t= attributes(t,'set',1,iattr,'xsi:noNamespaceSchemaLocation',DataIn.Schema);
894            setest=1;
895        end
896    end
897    if setest==0;
898        t=attributes(t,'add',1,'xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance');
899        t= attributes(t,'add',1,'xsi:noNamespaceSchemaLocation',DataIn.Schema);
900    end
901end
902end
903copyfile(CurrentFile,[CurrentFile '.bak']);
904save(t,CurrentFile);
905
906%-------------------------------------
907% creates and/or set values to an element in t
908%t: xml tree
909%RootUid: uid of t under which we introduce an element
910%node: uid of the element that we correct, if =0, a new element is created
911%key: key name of the element
912%value: new value of the element
913function t=set_element(t,RootUid,node,key,value)
914%create the subelement if needed
915if isequal(node,0)   
916   [t,node]= add(t,RootUid,'element',key);
917end
918node_chardata=children(t,node); %corresponding data node
919if isempty(node_chardata)%if the data does not exist in t, create it
920    t=add(t,node,'chardata',value);
921elseif isequal(length(node_chardata),1)&isequal(get(t,node_chardata,'type'),'chardata')% update only a simple element with 'chardata'
922    t=set(t,node_chardata,'value',value);%modify existing data
923end
924attr=attributes(t,'get',node);
925if isempty(attr)
926    t=attributes(t,'add',node,'source','manual');%indicate a manual eidting
927end
928
929% --- Executes on selection change in element_attr_val.
930function element_attr_val_Callback(hObject, eventdata, handles)
931% hObject    handle to element_attr_val (see GCBO)
932% eventdata  reserved - to be defined in a future version of MATLAB
933% handles    structure with handles and user data (see GUIDATA)
934
935% Hints: contents = get(hObject,'String') returns element_attr_val contents as cell array
936%        contents{get(hObject,'Value')} returns selected item from element_attr_val
937
938
939% --- Executes on button press in ADD.
940function ADD_Callback(hObject, eventdata, handles)
941% hObject    handle to ADD (see GCBO)
942% eventdata  reserved - to be defined in a future version of MATLAB
943% handles    structure with handles and user data (see GUIDATA)
944
945%----------------------------------------
946%read an xml file without schema
947function [Data,testsimple]=displ_xml(handles,t,root_uid,DataIn,heditxml)
948Data=DataIn;%default
949if ~isfield(Data,'CurrentUid')
950    Data.CurrentUid=[];
951end
952CurrentFile=get(handles.CurrentFile,'String');
953
954%display the current element
955root_element=get(t,root_uid);
956uidparent=root_uid;
957if isfield(root_element,'name')
958    CurrentElement=root_element.name;
959    while ~isequal(uidparent,1)%while the first level has not been reached
960        uidparent=parent(t,uidparent);
961        dirdat=get(t,uidparent);
962        if isfield(dirdat,'name')                       
963            CurrentElement=[dirdat.name '/' CurrentElement];
964        end
965    end
966    set(handles.CurrentElement,'String',CurrentElement)
967end
968list_uid=children(t,root_uid);
969%case of a single element
970testsimple=0;
971filedat=[];
972if ~isempty(list_uid)
973    filedat=get(t,list_uid(1))
974    if isfield(filedat,'type') & isequal(filedat.type,'chardata') &isfield(filedat,'value')
975        testsimple=1;%simple element
976    end
977end
978
979%attributes of the current element
980nbattrib= attributes(t,'length',root_uid);
981testopen=0;
982attr_col=[];
983for iattr=1:nbattrib
984    attr= attributes(t,'get',root_uid,iattr);
985    if isequal(attr.key,'source')% look for 'source' attribute
986        if isequal(attr.val,'file')%if the source is 'file', look for the path and open it
987           if isfield(filedat,'type') & isequal(filedat.type,'chardata') &isfield(filedat,'value')
988               cur_file=filedat.value;
989               uidparent=root_uid;%initialization
990               while ~isequal(uidparent,1)%while the first level has not been reached
991                    uidparent=parent(t,uidparent);
992                    dirdat=get(t,uidparent);
993                    if isfield(dirdat,'type') & isequal(dirdat.type,'element') & isfield(dirdat,'name')
994                        nbattrib_up= attributes(t,'length',uidparent);
995                        for iattr_up=1:nbattrib_up
996                            attr= attributes(t,'get',uidparent,iattr_up);
997%                             if isequal(attr.key,'source')&isequal(attr.val,'directory')% look for 'source' attribute
998                              if isequal(attr.key,'DirName')
999                                 cur_file=fullfile(attr.val,cur_file);
1000                             end
1001                        end
1002                    end
1003               end
1004               RootPath=fileparts(CurrentFile);%path to the current .xml file
1005               cur_file=fullfile(RootPath,cur_file)
1006               set(handles.CurrentAttributes,'UserData',cur_file)%will be searched by uvmat
1007               [path,fil,ext]=fileparts(cur_file);
1008               if ~exist(cur_file,'file')
1009                   msgbox_uvmat('ERROR',['non-existent link file' cur_file]) % A FAIRE: propose to updtate the .xml file
1010                   return
1011               elseif isequal(ext,'.xml')
1012                   if ~isfield(Data,'UpFile')
1013                       Data.UpFile={CurrentFile};
1014                   else
1015                       Data.UpFile=[{CurrentFile};Data.UpFile];
1016                   end
1017                   set(heditxml,'UserData',Data)
1018                   set(handles.CurrentFile,'String',cur_file)
1019                   CurrentFile_Callback(handles.CurrentFile, [], handles)
1020               else
1021                   if isequal(get(heditxml,'Tag'),'browser'); %if editxml has been called as a browser
1022                       set(heditxml,'Tag','idle')% signal for uvmat browser
1023                   else
1024                       uvmat({cur_file}); %open the link fiel with uvmat
1025                   end
1026                   return
1027               end
1028           end
1029       %elseif isequal(attr.val,'dir') A FAIRE : check directory
1030       %else A FAIRE: edit the element
1031        end
1032    end
1033    attr_col=strvcat(attr_col,[attr.key ' = ' attr.val]);
1034end
1035set(handles.CurrentAttributes,'String',attr_col)
1036
1037%list subtree
1038if ~testsimple
1039    list_element=[];
1040%      Data.CurrentUid=[Data.CurrentUid root_uid]%record new current uid
1041        for iline=1:length(list_uid)
1042        element=get(t,list_uid(iline));
1043        if isfield(element,'type')&isequal(element.type,'element')
1044             list_element{iline,2}=element.name;
1045             child_uid=children(t,list_uid(iline));
1046             subelem=get(t,child_uid);
1047             if isfield(subelem,'type')& isfield(subelem,'value') & isequal(subelem.type,'chardata')
1048                data_read=subelem.value;
1049                list_element{iline,3}=['= ' data_read];
1050            end
1051            if iscell(subelem)|(isfield(subelem,'type')&isequal(subelem.type,'element'))
1052                list_element{iline,1}='+ ';%sign for subtree existence
1053            else
1054                list_element{iline,1}='  ';
1055            end
1056            nbattr=attributes(t,'length',list_uid(iline));
1057            if nbattr==1
1058                attr=attributes(t,'get',list_uid(iline));
1059                list_element{iline,4}=[attr.key '='];
1060                list_element{iline,5}=attr.val;
1061            elseif nbattr>1
1062                for iattr=1:nbattr
1063                    attr=attributes(t,'get',list_uid(iline),iattr);
1064                    list_element{iline,2+2*iattr}=[attr.key '='];
1065                    list_element{iline,3+2*iattr}=attr.val;
1066                end
1067            end
1068        end
1069        end
1070    set(handles.list_element,'Value',1)%select the first line of list_element by default
1071        set(handles.list_element,'String',cell2tab(list_element,' ') )
1072    list.uid=list_uid;
1073        set(handles.list_element,'UserData',list)
1074end
1075%---------------------------------------------------------
1076%-------------------------------------
1077%updates the interface
1078function update_ref_list(hh,xs_element,element,node,xs_subelem,subelem)
1079%-----------------------------
1080pref_col='';
1081key_col='';
1082equal_sign='';
1083val_col='';
1084for iline=1:length(subelem)
1085    xsindex=subelem(iline).xsindex;
1086    indexcur=subelem(iline).index;
1087    subuid=subelem(iline).uid;
1088        if isempty(subuid)
1089             RefDataIn.uid(iline)=0;
1090        else
1091             RefDataIn.uid(iline)=subuid;
1092        end
1093    index(iline)=indexcur;
1094    node(iline)=xs_subelem(xsindex).node;
1095%         testmanual(iline)=subelem(iline).testmanual;
1096    ikey=xs_subelem(xsindex).key;
1097    if xs_subelem(xsindex).testsub
1098        ival=[' + ' subelem(iline).val];
1099    else
1100        ival=[' = ' subelem(iline).val];
1101    end
1102    key_col=strvcat(key_col,ikey);
1103    val_col=strvcat(val_col,ival);
1104end
1105RefDataIn.xs_uid=node;
1106list_element=[key_col val_col];
1107siztext=size(list_element);
1108set(hh,'Value',[1:siztext(1)])
1109set(hh,'String',list_element)
1110set(hh,'UserData',RefDataIn)
1111
1112%
1113% function RefFile_Callback(hObject, eventdata, handles)
1114% global t_ref xs
1115% t_ref=xmltree(get(hObject,'String'));%open the xml file fileinput
1116% heditxml=get(hObject,'parent');
1117% DataIn=get(get(hObject,'parent'),'UserData');
1118% % set(heditxml,'Units','pixel')
1119% figpos=get(heditxml,'Position')
1120% % title_element=get(handles.element_cur,'UserDataIn');
1121% % xs_node=xsnode_index(1,ind);
1122% % index_chosen=xsnode_index(2,ind);
1123% if isfield(DataIn,'fig_ref')&ishandle(DataIn.fig_ref)
1124%     figure(DataIn.fig_ref);
1125% else
1126%     DataIn.fig_ref=figure;
1127% end
1128% set(DataIn.fig_ref,'Name',get(hObject,'String'))
1129% set(DataIn.fig_ref,'MenuBar','none')
1130% newfigpos=[figpos(1)+figpos(3) figpos(2)+0.4*figpos(4) 0.5*figpos(3) 0.3*figpos(4)];
1131% set(DataIn.fig_ref,'Units','normalized')
1132% set(DataIn.fig_ref,'Position',newfigpos)
1133% DataIn.h_ref=uicontrol('Style','listbox', 'Max',2,'Units','pixel','Position', [0 0 newfigpos(3) newfigpos(4)], ...
1134%         'FontName','FixedWidth','Tag','listbox');
1135% if isfield(DataIn,'xs_CurrentUid');
1136%     xs_CurrentUid=DataIn.xs_CurrentUid(end);
1137% else
1138%     DataIn.xs_CurrentUid=find(xs,'/xs:schema/xs:element');%uid of the root element in the schema
1139% end
1140% [nodeup,path,xs_element,xs_subelem]=scan_schema(xs,xs_CurrentUid(end));
1141% xs_element.key
1142% tag0=find(t_ref,['/' path '/' xs_element.key]);
1143% if length(tag0)>=1
1144%     CurrentRefNode=tag0(1);%chose the first occurence of the element
1145% else
1146%     CurrentRefNode=0;
1147% end
1148% [ref_element,ref_subelem]=get_xml(t_ref,path,xs_element,CurrentRefNode,xs_subelem);
1149% update_ref_list(DataIn.h_ref,xs_element,ref_element,CurrentRefNode,xs_subelem,ref_subelem);
1150% siztext=size(get(DataIn.h_ref,'String'));
1151% set(DataIn.h_ref,'Value',[1:siztext(1)]); %select the whole list by default
1152% set(heditxml,'UserData',DataIn)
1153% set(handles.ref_data,'Value',siztext(1)); %select the whole list by default
1154% 'TESTimport'
1155% title_element=get(handles.element_cur,'UserDataIn')
1156% xs_node=title_element.xsnode;%uid of the element in the schema
1157% node=title_element.node;
1158% t=flush(t,node);%removes the corresponding subtree in t
1159% [nodeup,path,xs_element,xs_subelem]=scan_schema(xs,xs_node);%scan the schema
1160% tag0=find(t_import,['/' path '/' xs_element.key])
1161% if isempty(tag)
1162%     errordlg(['element /' path '/' xs_element.key ' not found in' fileinput])
1163%     return
1164% end
1165% % [element_import,node_import]=get_xml(t_import,path,xs_element,1,xs_subelem);% read the corresponding xml data
1166% node2_import=children(t_import,tag0);
1167% % t_import=branch(t_import,node_import);% extract branch of the new file
1168% % %removes the corresponding subtree in t
1169% for inode=1:length(node2_import)
1170%     struct=get(t_import,node2_import(inode))
1171%     if isfield(struct,'type') & isfield(struct,'name')%if the node is an elmeent type
1172%         node3_import=children(t_import,node2_import(inode))
1173%        [t,newuid]=add(t,node,struct.type,struct.name);
1174%        for inode2=1:length(node3_import)
1175%            struct2=get(t_import,node3_import(inode2))
1176%            if isequal(struct2.type,'chardata')
1177%                 t=add(t,newuid,'chardata',struct2.value);
1178%             end
1179%         end
1180%     end
1181% end
1182% --- Executes on button press in replicate.
1183function replicate_Callback(hObject, eventdata, handles)
1184global xs  t
1185
1186export_list=get(handles.export_list,'String');
1187export_val=get(handles.export_list,'UserData');
1188heditxml=get(handles.replicate,'parent');
1189Data=get(heditxml,'UserData')
1190
1191hdataview=findobj(allchild(0),'Name','dataview')
1192if isempty(hdataview)
1193    hdataview=dataview;
1194    return
1195end
1196hhdataview=guidata(hdataview);
1197CurrentPath=get(hhdataview.CurrentFile,'String');
1198ListExperiments=get(hhdataview.ListExperiments,'String');
1199Value=get(hhdataview.ListExperiments,'Value');
1200if ~isequal(Value,1)
1201    ListExperiments=ListExperiments(Value);
1202end
1203ListDevices=get(hhdataview.ListDevices,'String');
1204Value=get(hhdataview.ListDevices,'Value');
1205if ~isequal(Value,1)
1206    ListDevices=ListDevices(Value);
1207end
1208ListRecords=get(hhdataview.ListRecords,'String');
1209Value=get(hhdataview.ListRecords,'Value');
1210if ~isequal(Value,1)
1211    ListRecords=ListRecords(Value);
1212end
1213% uvmat('runplus_Callback',hObject,eventdata,handleshaxes)
1214[ListDevices,ListRecords,ListXml,List]=ListDir(CurrentPath,ListExperiments,ListDevices,ListRecords);
1215ListXml=get(hhdataview.ListXml,'String');
1216Value=get(hhdataview.ListXml,'Value');
1217if isequal(Value,1)
1218    msgbox_uvmat('ERROR','you need to select the xml files to edit')
1219    return
1220end
1221ListXml=ListXml(Value);%list of
1222for iexp=1:length(List.Experiment)
1223    ExpName=List.Experiment{iexp}.name;
1224    if isfield(List.Experiment{iexp},'Device')
1225        for idevice=1:length(List.Experiment{iexp}.Device)
1226            DeviceName=List.Experiment{iexp}.Device{idevice}.name;       
1227            if isfield(List.Experiment{iexp}.Device{idevice},'xmlfile')
1228                for ixml=1:length(List.Experiment{iexp}.Device{idevice}.xmlfile)
1229                    FileName=List.Experiment{iexp}.Device{idevice}.xmlfile{ixml};
1230                    for ilistxml=1:length(ListXml)
1231                        if isequal(FileName,ListXml{ilistxml})
1232                            xmlfullname=fullfile(CurrentPath,ExpName,DeviceName,FileName);
1233                            t_export=xmltree(xmlfullname);
1234                            rootelement=get(t_export,1,'name');
1235                            uidlist=Data.CurrentUid;
1236                            if isequal(rootelement,get(t,1,'name'))
1237                                backupfile=xmlfullname;
1238                                testexist=2;
1239                                while testexist==2
1240                                   backupfile=[backupfile '~'];
1241                                   testexist=exist(backupfile,'file');
1242                                end
1243                                [success,message]=copyfile(xmlfullname,backupfile);%make backup
1244                                if ~isequal(success,1)
1245                                    msgbox_uvmat('ERROR',['Error in the backup of ' xmlfullname])
1246                                    return
1247                                end
1248                                findstr=['/' rootelement];
1249                                uid_export(1)=1;
1250                                % fill the root elements if absent
1251                                for index=2:length(uidlist)
1252                                    name_t=get(t,uidlist(index),'name')
1253                                    findstr=[findstr '/' name_t]
1254                                    uid=find(t_export,findstr)
1255                                    if isempty(uid)
1256                                        [t_export,uid_export(index)]=add(t_export,uid_export(index-1),'element',name_t);
1257                                    else
1258                                        uid_export(index)=uid;
1259                                    end                           
1260                                end
1261                                % chardata......
1262                            end
1263                            break
1264                        end
1265                    end
1266%                     [Title,test]=check_heading(Currentpath,Campaign,ExpName,DeviceName,[],FileName,SubCampaignTest);
1267%                     if test
1268%                         [List.Experiment{iexp}.Device{idevice}.xmlfile{ixml} ' , Heading updated']
1269%                     end
1270                end
1271             elseif isfield(List.Experiment{iexp}.Device{idevice},'Record')
1272                for irecord=1:length(List.Experiment{iexp}.Device{idevice}.Record)
1273                    RecordName=List.Experiment{iexp}.Device{idevice}.Record{irecord}.name;
1274                    if isfield(List.Experiment{iexp}.Device{idevice}.Record{irecord},'xmlfile')
1275                        for ixml=1:length(List.Experiment{iexp}.Device{idevice}.Record{irecord}.xmlfile)
1276                            FileName=List.Experiment{iexp}.Device{idevice}.Record{irecord}.xmlfile{ixml};
1277                            for ilistxml=1:length(ListXml)
1278                                if isequal(FileName,ListXml{ilistxml})
1279                                    xmlfullname=fullfile(CurrentPath,ExpName,DeviceName,RecordName,FileName)
1280                                    break
1281                                end
1282                            end
1283%                             [Title,test]=check_heading(Currentpath,Campaign,ExpName,DeviceName,RecordName,FileName,SubCampaignTest);
1284%                             if test
1285%                                 [FileName ' , Heading updated']
1286%                             end
1287                        end
1288                    end
1289                end
1290            end
1291        end
1292    end
1293end
1294return
1295%%%%%%%%%%% A REVOIR
1296% Copier la liste des elements selectionnés dans dataview
1297%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1298%ANCIEN:
1299heditxml=get(hObject,'parent');
1300DataIn=get(heditxml,'UserData');
1301if ~isfield(DataIn,'h_ref')
1302    DataIn.h_ref=[];
1303end
1304if ~ishandle(DataIn.h_ref)
1305    errordlg('no source file opened for import')
1306    return
1307end
1308%create the current root element if needed
1309LengthElement=length(DataIn.CurrentUid);
1310FilledUid=find(DataIn.CurrentUid~=0);
1311LengthFilled=FilledUid(end);
1312for irank=LengthFilled+1:LengthElement
1313    attrib=attributes(xs,'get',DataIn.xs_CurrentUid(irank),1);
1314    [t,DataIn.CurrentUid(irank)]=add(t,DataIn.CurrentUid(irank-1),'element',attrib.val);
1315end
1316
1317%copy list of subelements
1318RefDataIn=get(DataIn.h_ref,'UserData');
1319list=get(handles.list_element,'UserData');%=,[node;index]
1320for ilist=get(DataIn.h_ref,'Value')
1321    node_content=get(xs,RefDataIn.xs_uid(ilist));
1322    if ~isempty(node_content) & isfield(node_content,'attributes')
1323        attrib=node_content.attributes;
1324        for iattr=1:length(attrib)
1325            struct=attrib{iattr};
1326            if isequal(struct.key,'name')
1327                key=struct.val; % read element key name
1328            end
1329        end
1330    end
1331    value='';
1332    if ~isequal(RefDataIn.uid(ilist),0)
1333        child_uid=children(t_ref,RefDataIn.uid(ilist));     
1334        if isequal(length(child_uid),1)
1335            content=get(t_ref,child_uid);
1336            if isfield(content,'type') &isfield(content,'value')& isequal(content.type,'chardata')
1337                value=content.value;
1338            end
1339        end
1340    end
1341    t=set_element(t,DataIn.CurrentUid(end),list.uid(ilist),key,value);
1342end
1343set(heditxml,'UserData',DataIn)
1344%update the current listing
1345[nodeup,path,xs_element,xs_subelem]=scan_schema(xs,DataIn.xs_CurrentUid(end));
1346[element,subelem]=get_xml(t,path,xs_element,DataIn.CurrentUid(end),xs_subelem);
1347update_list(handles,path,xs_element,element,DataIn.CurrentUid(end),xs_subelem,subelem);
1348% t=set_xml(t,xs_DataIn,subelem)
1349%edit list of subelments:   
1350
1351%A REVOIR
1352% xsnode_index=get(handles.list_element,'UserDataIn')
1353% xs_node=xsnode_index(1,ind);
1354% RefDataIn=get(handles.ref_data,'UserDataIn');
1355% subelem=RefDataIn.subelem;
1356% xsnode_index=get(handles.list_element,'UserDataIn');%data on the xs_nodes of the subelements
1357% ref_list=get(handles.ref_data,'Value');%selected indices in the list of reference subelements
1358% xs_node=xsnode_index(1,ref_list);%xs_nodes of the selected subelements
1359% % index_chosen=xsnode_index(2,ref_list);% indices in the list of occurence for a subelement
1360% % for ilist=ref_list
1361% ilist=1;
1362% while icontinue
1363%     'TESTCOPY'
1364%     [nodeup,path,xs_element,xs_subelem]=scan_schema(xs,xs_node(ilist))
1365%     xs_subelem.key
1366%     tsub=subelem(ilist).tsub
1367%     xsnode_index(2,ilist)
1368%     [ref_elem,ref_node,ref_subelem]=get_xml(tsub,'',xs_element,1,xs_subelem)
1369%     ref_subelem.val
1370%     testedit= ~isfield(element.attrup,'source') | isequal(element.attrup.source,'manual') %| element vide
1371%     
1372%     
1373%     icontinue=0
1374% end
1375% function [element,node,subelem]=get_xml(t,path,xs_element,node,xs_subelem)
1376% element.attr_key='';%default
1377% element.attr_val='';%default
1378% element.val='';
1379% % element.type='';
1380% % element.testmanual=testmanual %inheritates the input manual editing flag by default
1381% subelem=[]; %default
1382% attrup=[];
1383% % node=[];
1384%
1385% % %find the element properties in the xml file
1386% if node >= 1
1387%     elem_struct=get(t,node);
1388%     if ~xs_element.subtest
1389%         elem_contents=get(t,elem_struct.contents);
1390%         if isempty(elem_contents)
1391%             element.val=[];
1392%         else
1393%             element.val=elem_contents.value
1394%         end
1395%     end
1396%     if isfield(elem_struct,'attributes')
1397%         elem_attr=elem_struct.attributes;
1398%         for iattr=1:length(elem_attr)
1399%             element.attr_key{iattr}=elem_attr{iattr}.key ;
1400%             element.attr_val{iattr}=elem_attr{iattr}.val;
1401% %             attrup=setfield(attrup,elem_attr{iattr}.key,elem_attr{iattr}.val);
1402%            breakdetect=find(elem_attr{iattr}.key=='/'| elem_attr{iattr}.key==':'| elem_attr{iattr}.key=='.');% find '/'
1403%            if isempty(breakdetect)
1404% %                 comline=['attrup.' elem_attr{iattr}.key '=' elem_attr{iattr}.val ';']
1405%                 eval(['attrup.' elem_attr{iattr}.key '=''' elem_attr{iattr}.val ''';'])
1406%            end
1407%         end
1408%     end
1409% end
1410% %get the parent node attributes
1411% up=1
1412% if node>0
1413%       nodeup=node;
1414%       while up==1;
1415%         nodeup=get(t,nodeup,'parent');%move up to the parent in the tree
1416%         if isempty(nodeup)
1417%             up=0;
1418%         else
1419%             nodeup_content=get(t,nodeup);
1420%             attrib=nodeup_content.attributes;
1421%             for iattr=1:length(attrib)
1422%                 key=attrib{iattr}.key;
1423%                 breakdetect=find(key=='/'| key==':'| key=='.');% find '/'
1424%                 if ~isfield(attrup,key) & isempty(breakdetect)
1425%                    eval(['attrup.' key '=''' attrib{iattr}.val ''';'])
1426%                 end
1427%             end
1428%         end
1429%       end
1430%       element.attrup=attrup;
1431% end
1432% %find the subelement properties in the xml file
1433% if xs_element.subtest
1434%    iline=0;
1435%    for k=1:length(xs_subelem)%node2: list of subelements in the sub-sequence
1436% %     attr=attributes(xs,'get',node2(i),1);%
1437% %     element=attr.val;%name of the element
1438%      tag=find(t,['/' path '/' xs_element.key '/' xs_subelem(k).key]);%look for the corresponding element node in the .xml tree
1439%      struct_element=get(t,tag);%get the content of the element
1440%      if isempty(struct_element)
1441%          iline=iline+1;
1442%          subelem(iline).xsindex=k;
1443%          subelem(iline).index=0;
1444% %          subelem(iline).testmanual=element.testmanual;% inheritates the manual editing flag by default
1445%          if isequal(xs_subelem(k).minOccurs,0)
1446%              subelem(iline).val='[]';%element value not mandatory in the schema
1447%          else
1448%              subelem(iline).val='[MISSING]';%element value mandatory in the schema
1449%          end
1450% %          subelem(iline).attrup=attrup; %inheritated attributes
1451%      elseif isequal(length(struct_element),1)
1452%          contents=get(t,struct_element.contents);
1453%          iline=iline+1;
1454%          subelem(iline).xsindex=k;
1455%          subelem(iline).index=1;
1456% %          subelem(iline).testmanual=element.testmanual;%
1457%          if isfield(contents,'value') & ~isempty(contents.value)
1458%              subelem(iline).val=contents.value;
1459%          elseif xs_subelem(k).testsub
1460%              subelem(iline).val='';
1461%          elseif isequal(xs_subelem(k).minOccurs,0)
1462%              subelem(iline).val='[]';%element value not mandatory in the schema
1463%          else
1464%              subelem(iline).val='[MISSING]';%element value mandatory in the schema
1465%          end
1466% %          subelem(iline).attrup=attrup; %inheritated attributes
1467%          if isfield(struct_element,'attributes')
1468%             element_attr=struct_element.attributes;
1469%             attr_display=[];
1470%             for iattr=1:length(element_attr)
1471% %                 attr_display{iline}=[attr_display ' , ' element_attr{iattr}.key '  =  ' element_attr{iattr}.val];
1472%                 subelem(iline).val=[subelem(iline).val attr_display ' , ' element_attr{iattr}.key '  =  ' element_attr{iattr}.val];
1473% %                 subelem(iline).attrup=setfield(subelem(iline).attrup,element_attr{iattr}.key,element_attr{iattr}.val);
1474%             end
1475%          end
1476%      else%case of a multiple element
1477%          for subindex=1:length(struct_element)
1478%              contents=get(t,struct_element{subindex}.contents);
1479%              iline=iline+1;
1480%              subelem(iline).index=subindex;%index of the element
1481%              subelem(iline).xsindex=k;
1482% %              subelem(iline).testmanual=element.testmanual;%
1483%              if isfield(contents,'value')& ~isempty(contents.value)
1484%                  subelem(iline).val=contents.value;
1485%              elseif xs_subelem(k).testsub
1486%                  subelem(iline).val='';
1487%              else
1488%                  subelem(iline).val='[]';
1489%              end
1490% %              subelem(iline).attrup=attrup; %inheritated attributes
1491%              if isfield(struct_element{subindex},'attributes')
1492%                 element_attr=struct_element{subindex}.attributes;
1493%                 attr_display=[];
1494%                 for iattr=1:length(element_attr)
1495% %                     attr_display{iline}=[attr_display ' , ' element_attr{iattr}.key '  =  ' element_attr{iattr}.val];
1496%                     subelem(iline).val=[subelem(iline).val attr_display ' , ' element_attr{iattr}.key '  =  ' element_attr{iattr}.val];
1497% %                     subelem(iline).attrup=setfield(subelem(iline).attrup,element_attr{iattr}.key,element_attr{iattr}.val);
1498%                 end
1499%             end
1500%         end
1501%      end
1502%   end
1503% end
1504
1505
1506% --- Executes on button press in HELP.
1507function HELP_Callback(hObject, eventdata, handles)
1508path_to_uvmat=which ('uvmat')% check the path of uvmat
1509pathelp=fileparts(path_to_uvmat);
1510helpfile=fullfile(pathelp,'UVMAT_DOC','uvmat_doc.html');
1511if isempty(dir(helpfile)), errordlg('Please put the help file uvmat_doc.html in the directory UVMAT/UVMAT_DOC')
1512else
1513web([helpfile '#editxml'])   
1514end
1515
1516
1517% --- Executes on button press in Export.
1518function Export_Callback(hObject, eventdata, handles)
1519val=get(handles.Export,'Value');
1520if val
1521    set(handles.Export,'BackgroundColor',[0 1 0])
1522    set(handles.export_list,'Visible','on')
1523    set(handles.replicate,'Visible','on')
1524    h_dataview=findobj(allchild(0),'name',dataview');
1525    if isempty(h_dataview)
1526       % CurrentFile=get(handles.CurrentFile,'String');
1527        dataview;
1528    end
1529else
1530     set(handles.Export,'BackgroundColor',[0.7 0.7 0.7])
1531    set(handles.export_list,'Visible','off')
1532    set(handles.replicate,'Visible','off')
1533end
1534
1535
1536% --- Executes on button press in pushbutton9.
1537function pushbutton9_Callback(hObject, eventdata, handles)
1538% hObject    handle to pushbutton9 (see GCBO)
1539% eventdata  reserved - to be defined in a future version of MATLAB
1540% handles    structure with handles and user data (see GUIDATA)
1541
1542
Note: See TracBrowser for help on using the repository browser.