attributes.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. function varargout = attributes(varargin)
  2. % XMLTREE/ATTRIBUTES Method (handle attributes of an element node)
  3. % FORMAT varargout = attributes(varargin)
  4. %
  5. % tree - XMLTree object
  6. % method - 'set','get','add','del' or 'length'
  7. % uid - the UID of an element node
  8. % n - indice of the attribute
  9. % key - string key="..."
  10. % val - string ...="val"
  11. % attr - cell array of struct(key,val) or just struct(key,val)
  12. % l - number of attributes of the element node uid
  13. %
  14. % tree = attributes(tree,'set',uid,n,key,val)
  15. % attr = attributes(tree,'get',uid[,n])
  16. % tree = attributes(tree,'add',uid,key,val)
  17. % tree = attributes(tree,'del',uid[,n])
  18. % l = attributes(tree,'length',uid)
  19. %__________________________________________________________________________
  20. %
  21. % Handle attributes of an element node.
  22. % The tree parameter must be in input AND in output for 'set', 'add' and
  23. % 'del' methods.
  24. %__________________________________________________________________________
  25. % Copyright (C) 2002-2011 http://www.artefact.tk/
  26. % Guillaume Flandin
  27. % $Id: attributes.m 4460 2011-09-05 14:52:16Z guillaume $
  28. %error(nargchk(3,6,nargin));
  29. tree = varargin{1};
  30. if ~ischar(varargin{2}) || ...
  31. ~any(strcmp(varargin{2},{'set','get','add','del','length'}))
  32. error('[XMLTree] Unknown method.');
  33. end
  34. uid = varargin{3};
  35. if ~isa(uid,'double') || any(uid>length(tree)) || any(uid<1)
  36. error('[XMLTree] UID must be a positive integer scalar.');
  37. end
  38. if ~strcmp(tree.tree{uid}.type,'element')
  39. error('[XMLTree] This node has no attributes.');
  40. end
  41. switch varargin{2}
  42. case 'set'
  43. %error(nargchk(6,6,nargin));
  44. if ~isa(varargin{4},'double') || ...
  45. any(varargin{4}>length(tree.tree{uid}.attributes)) || ...
  46. any(varargin{4}<1)
  47. error('[XMLTree] Invalid attribute indice.');
  48. end
  49. ind = varargin{4};
  50. tree.tree{uid}.attributes{ind} = struct('key',varargin{5},'val',varargin{6});
  51. varargout{1} = tree;
  52. case 'get'
  53. %error(nargchk(3,4,nargin));
  54. if nargin == 4
  55. if ischar(varargin{4})
  56. for i=1:length(tree.tree{uid}.attributes)
  57. if strcmp(varargin{4},tree.tree{uid}.attributes{i}.key)
  58. varargout{1} = tree.tree{uid}.attributes{i}.val;
  59. return;
  60. end
  61. end
  62. varargout{1} = [];
  63. elseif ~isa(varargin{4},'double') || ...
  64. any(varargin{4}>length(tree.tree{uid}.attributes)) || ...
  65. any(varargin{4}<1)
  66. error('[XMLTree] Invalid attribute indice.');
  67. else
  68. if length(varargin{4}) == 1
  69. varargout{1} = tree.tree{uid}.attributes{varargin{4}(1)};
  70. else
  71. varargout{1} = {};
  72. for i=1:length(varargin{4})
  73. varargout{1}{i} = tree.tree{uid}.attributes{varargin{4}(i)};
  74. end
  75. end
  76. end
  77. else
  78. if length(tree.tree{uid}.attributes) == 1
  79. varargout{1} = tree.tree{uid}.attributes{1};
  80. else
  81. varargout{1} = {};
  82. for i=1:length(tree.tree{uid}.attributes)
  83. varargout{1}{i} = tree.tree{uid}.attributes{i};
  84. end
  85. end
  86. end
  87. case 'add'
  88. %error(nargchk(5,5,nargin));
  89. ind = length(tree.tree{uid}.attributes) + 1;
  90. tree.tree{uid}.attributes{ind} = struct('key',varargin{4},'val',varargin{5});
  91. varargout{1} = tree;
  92. case 'del'
  93. %error(nargchk(3,4,nargin));
  94. if nargin == 4
  95. if ~isa(varargin{4},'double') || ...
  96. any(varargin{4}>length(tree.tree{uid}.attributes)) || ...
  97. any(varargin{4}<1)
  98. error('[XMLTree] Invalid attribute indice.');
  99. end
  100. ind = varargin{4};
  101. tree.tree{uid}.attributes(ind) = [];
  102. else
  103. tree.tree{uid}.attributes = [];
  104. end
  105. varargout{1} = tree;
  106. case 'length'
  107. %error(nargchk(3,3,nargin));
  108. varargout{1} = length(tree.tree{uid}.attributes);
  109. otherwise
  110. error('[XMLTree] Unknown method.');
  111. end