source: trunk/src/editxml.m @ 823

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