source: issm/trunk-jpl/src/m/kml/kml_object.m@ 13646

Last change on this file since 13646 was 13646, checked in by Mathieu Morlighem, 12 years ago

CHG: cosmetics, removed multiple blank lines

File size: 5.5 KB
Line 
1%
2% definition for the kml_object super (base) abstract class.
3%
4% [kml]=kml_object(varargin)
5%
6% where the optional varargin and defaults are:
7% id (char, object id, '')
8%
9% note that zero arguments constructs a default instance; one
10% argument of the class copies the instance; and two or more
11% arguments constructs a new instance from the arguments.
12%
13classdef kml_object < handle
14 properties
15 id ='';
16 end
17
18 methods
19 function [kml]=kml_object(varargin)
20
21 switch nargin
22
23% create a default object
24
25 case 0
26
27% copy the object or create the object from the input
28
29 otherwise
30 if (nargin == 1) && isa(varargin{1},class(kml))
31 kml=varargin{1};
32
33 else
34 fnames=fieldnames(kml_object());
35
36 for i=1:min(nargin,length(fnames))
37 if isa(varargin{i},class(kml.(fnames{i})))
38 if ~isempty(varargin{i})
39 kml.(fnames{i})=varargin{i};
40 end
41 else
42 if ~isempty(inputname(i))
43 warning('Argument ''%s'' for property ''%s'' is a ''%s'' class object, not ''%s''.',...
44 inputname(i),fnames{i},class(varargin{i}),class(kml.(fnames{i})));
45 else
46 warning('Argument %d for property ''%s'' is a ''%s'' class object, not ''%s''.',...
47 i ,fnames{i},class(varargin{i}),class(kml.(fnames{i})));
48 end
49 end
50 end
51 end
52
53 end
54
55 end
56
57% display the object
58
59 function []=disp(kml)
60
61 for i=1:numel(kml)
62 if strcmp(class(kml),'kml_object')
63 disp(sprintf('class ''%s'' object ''%s%s'' = \n',...
64 class(kml),inputname(1),string_dim(kml,i)));
65 end
66
67 if strcmp(class(kml),'kml_object')
68 disp(sprintf(' id: ''%s''\n',kml(i).id));
69 else
70 disp(sprintf(' id: ''%s''' ,kml(i).id));
71 end
72 end
73
74 end
75
76% return the fieldnames of the object
77
78 function [fnames]=fieldnames(kml)
79
80% fieldnames for a sub (derived) class list those before super (base)
81
82 fnames={'id'};
83
84 end
85
86% set the properties of the object
87
88 function [kml]=setprops(kml,varargin)
89
90 kmlref=feval(class(kml));
91 fnames=fieldnames(kmlref);
92
93% loop through each parameter in the input list (comparing to the reference
94% object in case property types have been changed)
95
96 for i=1:2:length(varargin)
97 if ismember(varargin{i},fnames) && (i+1 <= length(varargin))
98 if isa(varargin{i+1},class(kmlref.(varargin{i})))
99 kml.(varargin{i})=varargin{i+1};
100 else
101 if ~isempty(inputname(i+1))
102 warning('Argument ''%s'' for property ''%s'' is a ''%s'' class object, not ''%s''.',...
103 inputname(i+2),varargin{i},class(varargin{i+1}),class(kmlref.(varargin{i})));
104 else
105 warning('Argument %d for property ''%s'' is a ''%s'' class object, not ''%s''.',...
106 i+2 ,varargin{i},class(varargin{i+1}),class(kmlref.(varargin{i})));
107 end
108 end
109 else
110 warning('Property ''%s'' for class ''%s'' does not exist.',...
111 varargin{i},class(kmlref));
112 end
113 end
114
115 end
116
117% write the object
118
119 function []=kml_write(kml,fid,indent)
120
121 if ~exist('fid','var') || isempty(fid)
122 fid=1;
123 end
124 if ~exist('indent','var') || isempty(indent)
125 indent='';
126 end
127
128% loop over the objects
129
130 for i=1:numel(kml)
131 kmli=kml(i);
132 if strcmp(class(kml),'kml_object')
133 if ~isempty(kmli.id)
134 fprintf(fid,'%s<!Object id="%s">\n',indent,kmli.id);
135 else
136 fprintf(fid,'%s<!Object>\n',indent);
137 end
138 end
139
140 if strcmp(class(kml),'kml_object')
141 fprintf(fid,'%s</!Object>\n',indent);
142 end
143 end
144
145 end
146
147% string write the object
148
149 function [sbuf]=kml_swrite(kml,sbuf,indent)
150
151 if ~exist('sbuf','var') || isempty(sbuf)
152 sbuf=string_buf;
153 end
154 if ~exist('indent','var') || isempty(indent)
155 indent='';
156 end
157
158% loop over the objects
159
160 for i=1:numel(kml)
161 kmli=kml(i);
162 if strcmp(class(kml),'kml_object')
163 if ~isempty(kmli.id)
164 sbuf=add(sbuf,sprintf('%s<!Object id="%s">\n',indent,kmli.id));
165 else
166 sbuf=add(sbuf,sprintf('%s<!Object>\n',indent));
167 end
168 end
169
170 if strcmp(class(kml),'kml_object')
171 sbuf=add(sbuf,sprintf('%s</!Object>\n',indent));
172 end
173 end
174
175 end
176
177 end
178
179end
Note: See TracBrowser for help on using the repository browser.