| 1 | function varargout = add(tree,uid,type,parameter) |
|---|
| 2 | % XMLTREE/ADD Method (add childs to elements of an XML Tree) |
|---|
| 3 | % FORMAT vararout = add(tree,uid,type,parameter) |
|---|
| 4 | % |
|---|
| 5 | % tree - XMLTree object |
|---|
| 6 | % uid - array of uid's |
|---|
| 7 | % type - 'element', 'chardata', 'cdata', 'pi' or 'comment' |
|---|
| 8 | % parameter - property name (a character array unless type='pi' for |
|---|
| 9 | % which parameter=struct('target','','value','')) |
|---|
| 10 | % |
|---|
| 11 | % new_uid - UID's of the newly created nodes |
|---|
| 12 | % |
|---|
| 13 | % tree = add(tree,uid,type,parameter); |
|---|
| 14 | % [tree, new_uid] = add(tree,uid,type,parameter); |
|---|
| 15 | %_______________________________________________________________________ |
|---|
| 16 | % |
|---|
| 17 | % Add a node (element, chardata, cdata, pi or comment) in the XML Tree. |
|---|
| 18 | % It adds a child to the element whose UID is iud. |
|---|
| 19 | % Use attributes({'set','get','add','del','length'},...) function to |
|---|
| 20 | % deal with the attributes of an element node (initialized empty). |
|---|
| 21 | % The tree parameter must be in input AND in output. |
|---|
| 22 | %_______________________________________________________________________ |
|---|
| 23 | % @(#)add.m Guillaume Flandin 02/03/29 |
|---|
| 24 | |
|---|
| 25 | error(nargchk(4,4,nargin)); |
|---|
| 26 | |
|---|
| 27 | if ~isa(uid,'double') |
|---|
| 28 | error('[XMLTree] UID must be a double array.'); |
|---|
| 29 | end |
|---|
| 30 | if ~ischar(type) |
|---|
| 31 | error('[XMLTree] TYPE must be a valid item type.'); |
|---|
| 32 | end |
|---|
| 33 | if strcmp(type,'pi') |
|---|
| 34 | if ~isfield(parameter,'target') | ~isfield(parameter,'value') | ... |
|---|
| 35 | ~ischar(parameter.target) | ~ischar(parameter.value) |
|---|
| 36 | error('[XMLTree] For a Processing Instruction, ',... |
|---|
| 37 | 'PARAMETER must be a struct.'); |
|---|
| 38 | end |
|---|
| 39 | elseif ~ischar(parameter) |
|---|
| 40 | error('[XMLTree] PARAMETER must be a string.'); |
|---|
| 41 | end |
|---|
| 42 | |
|---|
| 43 | if nargout == 2 |
|---|
| 44 | l = length(tree.tree); |
|---|
| 45 | varargout{2} = (l+1):(l+prod(size(uid))); |
|---|
| 46 | end |
|---|
| 47 | |
|---|
| 48 | for i=1:prod(size(uid)) |
|---|
| 49 | if uid(i)<1 | uid(i)>length(tree.tree) |
|---|
| 50 | error('[XMLTree] Invalid UID.'); |
|---|
| 51 | end |
|---|
| 52 | if ~strcmp(tree.tree{uid(i)}.type,'element') |
|---|
| 53 | error('[XMLTree] Cannot add a child to a non-element node.'); |
|---|
| 54 | end |
|---|
| 55 | l = length(tree.tree); |
|---|
| 56 | switch type |
|---|
| 57 | case 'element' |
|---|
| 58 | tree.tree{l+1} = struct('type','element',... |
|---|
| 59 | 'name',parameter,... |
|---|
| 60 | 'attributes',[],... |
|---|
| 61 | 'contents',[],... |
|---|
| 62 | 'parent',[],... |
|---|
| 63 | 'uid',l+1); |
|---|
| 64 | case 'chardata' |
|---|
| 65 | tree.tree{l+1} = struct('type','chardata',... |
|---|
| 66 | 'value',parameter,... |
|---|
| 67 | 'parent',[],... |
|---|
| 68 | 'uid',l+1); |
|---|
| 69 | case 'cdata' |
|---|
| 70 | tree.tree{l+1} = struct('type','cdata',... |
|---|
| 71 | 'value',parameter,... |
|---|
| 72 | 'parent',[],... |
|---|
| 73 | 'uid',l+1); |
|---|
| 74 | case 'pi' |
|---|
| 75 | tree.tree{l+1} = struct('type','pi',... |
|---|
| 76 | 'target',parameter.target,... |
|---|
| 77 | 'value',parameter.value,... |
|---|
| 78 | 'parent',[],... |
|---|
| 79 | 'uid',l+1); |
|---|
| 80 | case 'comment' |
|---|
| 81 | tree.tree{l+1} = struct('type','comment',... |
|---|
| 82 | 'value',parameter,... |
|---|
| 83 | 'parent',[],... |
|---|
| 84 | 'uid',l+1); |
|---|
| 85 | otherwise |
|---|
| 86 | error(sprintf('[XMLTree] %s: unknown item type.',type)); |
|---|
| 87 | end |
|---|
| 88 | tree.tree{uid(i)}.contents = [tree.tree{uid(i)}.contents l+1]; |
|---|
| 89 | tree.tree{l+1}.parent = uid(i); |
|---|
| 90 | end |
|---|
| 91 | |
|---|
| 92 | varargout{1} = tree; |
|---|