source: trunk/src/editxml.m @ 604

Last change on this file since 604 was 598, checked in by sommeria, 11 years ago

various bugs repaired . civ_series further developed

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