Home > m > helper > ltpda_xmlwrite.m

ltpda_xmlwrite

PURPOSE ^

LTPDA_XMLWRITE Add an object to a xml DOM project.

SYNOPSIS ^

function ltpda_xmlwrite(objs, xml, parent, property_name)

DESCRIPTION ^

 LTPDA_XMLWRITE Add an object to a xml DOM project.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 DESCRIPTION:   LTPDA_XMLWRITE Add an object to a xml DOM project.

 EXAMPLE:       xml = com.mathworks.xml.XMLUtils.createDocument('ltpda_object');
                parent = xml.getDocumentElement;

                ltpda_xmlwrite(a, xml, parent, '');

                xmlwrite('foo.xml', xml);

 FORMAT:        This function writes the object into the following xml format:

                ----------------------   ltpda_object   ----------------------

                <ltpda_object>

                   --> <object> ...
                   --> <cell>   ...

                </ltpda_object>

                -------------------------   object   -------------------------

                <object> ('type' - attribute)

                   <property> ...

                </object>

                ------------------------   property   ------------------------
                --------------------------   cell   --------------------------

                <property> ('type', 'prop_name' -attributes)
            OR  <cell>     ('type'              -attribute)

                   --> atomic element
                         - empty cell
                         - empty double
                         - empty char
                         - char
                         - double
                         - logical
                         - java (necessary for timezone)
                   --> <cell>      ...
                   --> <object>    ...
                   --> <real_data> ...
                       <imag_data> ...

                </property>
            OR  </cell>

                -------   real_data   --------|--------   imag_data   --------
                                              |
                <real_data>('type'-attribute) | <imag_data> ('type' -attribute)
                           ('shape'-attribute)|             ('shape'-attribute)
                                              |
                   --> <matrix> ...           |    --> <matrix> ...
                   --> <vector> ...           |    --> <vector> ...
                                              |
                </real_data>                  | </imag_data>
                                              |
                ---------   matrix   --------------------   vector   ---------
                                              |
                <matrix> ('type' -attribute)  | <vector> ('type' -attribute)
                                              |
                   row vector (double)        |    column vector (double)
                                              |
                </matrix>                     | </vector>

 SYMBOLS:       --> Marks a choice between alternatives.
                ... Indicate that an element may be repeated.

 VERSION:       $Id: ltpda_xmlwrite.m,v 1.8 2008/02/25 16:13:56 ingo Exp $

 HISTORY:       31-01-2008 Diepholz
                   Creation

 SEE ALSO:      ltpda_xmlwrite, ltpda_xmlread, ltpda_saveobj

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 % LTPDA_XMLWRITE Add an object to a xml DOM project.
0002 %
0003 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0004 %
0005 % DESCRIPTION:   LTPDA_XMLWRITE Add an object to a xml DOM project.
0006 %
0007 % EXAMPLE:       xml = com.mathworks.xml.XMLUtils.createDocument('ltpda_object');
0008 %                parent = xml.getDocumentElement;
0009 %
0010 %                ltpda_xmlwrite(a, xml, parent, '');
0011 %
0012 %                xmlwrite('foo.xml', xml);
0013 %
0014 % FORMAT:        This function writes the object into the following xml format:
0015 %
0016 %                ----------------------   ltpda_object   ----------------------
0017 %
0018 %                <ltpda_object>
0019 %
0020 %                   --> <object> ...
0021 %                   --> <cell>   ...
0022 %
0023 %                </ltpda_object>
0024 %
0025 %                -------------------------   object   -------------------------
0026 %
0027 %                <object> ('type' - attribute)
0028 %
0029 %                   <property> ...
0030 %
0031 %                </object>
0032 %
0033 %                ------------------------   property   ------------------------
0034 %                --------------------------   cell   --------------------------
0035 %
0036 %                <property> ('type', 'prop_name' -attributes)
0037 %            OR  <cell>     ('type'              -attribute)
0038 %
0039 %                   --> atomic element
0040 %                         - empty cell
0041 %                         - empty double
0042 %                         - empty char
0043 %                         - char
0044 %                         - double
0045 %                         - logical
0046 %                         - java (necessary for timezone)
0047 %                   --> <cell>      ...
0048 %                   --> <object>    ...
0049 %                   --> <real_data> ...
0050 %                       <imag_data> ...
0051 %
0052 %                </property>
0053 %            OR  </cell>
0054 %
0055 %                -------   real_data   --------|--------   imag_data   --------
0056 %                                              |
0057 %                <real_data>('type'-attribute) | <imag_data> ('type' -attribute)
0058 %                           ('shape'-attribute)|             ('shape'-attribute)
0059 %                                              |
0060 %                   --> <matrix> ...           |    --> <matrix> ...
0061 %                   --> <vector> ...           |    --> <vector> ...
0062 %                                              |
0063 %                </real_data>                  | </imag_data>
0064 %                                              |
0065 %                ---------   matrix   --------------------   vector   ---------
0066 %                                              |
0067 %                <matrix> ('type' -attribute)  | <vector> ('type' -attribute)
0068 %                                              |
0069 %                   row vector (double)        |    column vector (double)
0070 %                                              |
0071 %                </matrix>                     | </vector>
0072 %
0073 % SYMBOLS:       --> Marks a choice between alternatives.
0074 %                ... Indicate that an element may be repeated.
0075 %
0076 % VERSION:       $Id: ltpda_xmlwrite.m,v 1.8 2008/02/25 16:13:56 ingo Exp $
0077 %
0078 % HISTORY:       31-01-2008 Diepholz
0079 %                   Creation
0080 %
0081 % SEE ALSO:      ltpda_xmlwrite, ltpda_xmlread, ltpda_saveobj
0082 %
0083 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0084 function ltpda_xmlwrite(objs, xml, parent, property_name)
0085 
0086   %%%%%%   If the property_name is filled then create a new property node   %%%%%
0087   if ~isempty(property_name)
0088     shape = sprintf('%dx%d', size(objs,1), size(objs,2));
0089 
0090     prop_node = xml.createElement('property');
0091     prop_node.setAttribute('prop_name', property_name);
0092     prop_node.setAttribute('shape', shape);
0093     prop_node.setAttribute('type', class(objs));
0094     parent.appendChild(prop_node);
0095     parent = prop_node;
0096   end
0097 
0098   %%%%%%%%%%%%%%%%   The object is a class object or a struct   %%%%%%%%%%%%%%%%%
0099   if (isobject(objs) || isstruct(objs)) && ~isa(objs, 'sym')
0100 
0101     for ii = 1:numel(objs)
0102 
0103       obj = objs(ii);
0104 
0105       shape = sprintf('%dx%d', size(objs,1), size(objs,2));
0106 
0107       obj_node = xml.createElement('object');
0108       obj_node.setAttribute('type', class(obj));
0109       obj_node.setAttribute('shape', shape);
0110 
0111       parent.appendChild(obj_node);
0112 
0113       fields = fieldnames(obj);
0114 
0115       for jj = 1:length(fields)
0116         field = fields{jj};
0117         ltpda_xmlwrite(obj.(field), xml, obj_node, field);
0118       end
0119     end
0120 
0121   %%%%%%%%%%%%%%%%%%%%%%%   The object is a java object   %%%%%%%%%%%%%%%%%%%%%%%
0122   elseif isjava(objs)
0123     if strcmp(class(objs), 'sun.util.calendar.ZoneInfo')
0124       content = xml.createTextNode(char(objs.getID));
0125       parent.appendChild(content);
0126     else
0127       error('### Unknown Java');
0128     end
0129 
0130   %%%%%%%%%%%%%%%%%%%%%%%   The object is a cell object   %%%%%%%%%%%%%%%%%%%%%%%
0131   elseif iscell(objs)
0132 
0133     for ii = 1:numel(objs)
0134 
0135       obj = objs{ii};
0136 
0137       shape = sprintf('%dx%d', size(obj,1), size(obj,2));
0138 
0139       cell_node = xml.createElement('cell');
0140       cell_node.setAttribute('type', class(obj));
0141       cell_node.setAttribute('prop_name', property_name);
0142       cell_node.setAttribute('shape', shape);
0143 
0144       parent.appendChild(cell_node);
0145 
0146       ltpda_xmlwrite(obj, xml, cell_node, '');
0147 
0148     end
0149 
0150   %%%%%%%%%%%%%%%%%%%%   The object is a character string   %%%%%%%%%%%%%%%%%%%%%
0151   elseif ischar(objs)
0152     content = xml.createTextNode(objs);
0153     parent.appendChild(content);
0154 
0155   %%%%%%%%%%%%%%%%%%%%%%%%%   The object is a logical   %%%%%%%%%%%%%%%%%%%%%%%%%
0156   elseif islogical(objs)
0157     if numel(objs) == 1
0158       if objs
0159         content = xml.createTextNode('true');
0160       else
0161         content = xml.createTextNode('false');
0162       end
0163       parent.appendChild(content);
0164     else
0165       error ('### At the moment is only one logical allowed. The size is [%dx%d]', size(objs,1), size(objs,2));
0166     end
0167 
0168   %%%%%%%%%%%%%%%%%%%%%%%%%   The object is a number   %%%%%%%%%%%%%%%%%%%%%%%%%%
0169   elseif isnumeric(objs) || isa(objs, 'sym')
0170 
0171     %%%%%   objs is a singel value   %%%%%
0172     if (size(objs,1) == 1) && (size(objs,2) == 1)
0173       if strcmp(class(objs), 'sym')
0174         number_str = char(objs, 20);
0175       else
0176         number_str = num2str(objs, 20);
0177       end
0178       content = xml.createTextNode(number_str);
0179       parent.appendChild(content);
0180 
0181       %%%%%   objs is a matrix   %%%%%
0182     elseif (size(objs,1) > 1) && (size(objs,2) > 1)
0183 
0184       xml_addmatrix(objs, xml, parent);
0185 
0186       %%%%%   objs is a vector   %%%%%
0187     elseif (size(objs,1) > 1) || (size(objs,2) > 1)
0188 
0189       xml_addvector(objs, xml, parent);
0190 
0191     end
0192 
0193   else
0194     error('### unknown type [%s]', class(objs));
0195   end
0196 end
0197 
0198 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0199 %
0200 % DESCRIPTION: XML_ADDVECTOR Add a vector element to the xml node. The element
0201 %              have the form:
0202 %
0203 % REAL DATA:   <real_data type="vector">
0204 %                 <vector type="double"> 1  2  3  4  5  6  7  8  9 10</vector>
0205 %                 <vector type="double">11 12 13 14 15 16 17 18 19 20</vector>
0206 %              </real_data>
0207 %
0208 % COMPLEX DATA:<real_data type="vector">
0209 %                 <vector type="double"> 1  2  3  4  5  6  7  8  9 10</vector>
0210 %                 <vector type="double">11 12 13 14 15 16 17 18 19 20</vector>
0211 %              </real_data>
0212 %
0213 %              <imag_data type="vector">
0214 %                 <vector type="double"> 1  2  3  4  5  6  7  8  9 10</vector>
0215 %                 <vector type="double">11 12 13 14 15 16 17 18 19 20</vector>
0216 %              </imag_data>
0217 %
0218 %              The vector objs will be split into several parts dependent from
0219 %              the maximum size in one vector element.
0220 %              Each part creates its own vectror element.
0221 %
0222 % HISTORY:     31-01-2008 Diepholz
0223 %                 Creation
0224 %
0225 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0226 function xml_addvector(objs, xml, parent)
0227 
0228   n_min            = getappdata(0, 'xmlsetsize');
0229   header_displayed = true;
0230 
0231   shape = sprintf('%dx%d', size(objs,1), size(objs,2));
0232 
0233   %%%%%   Real data   %%%%%
0234   node_real = xml.createElement('real_data');
0235   node_real.setAttribute('type', 'vector');
0236   node_real.setAttribute('shape', shape);
0237   %% Set the parent attribute 'type' to vector
0238   parent.setAttribute('type', 'vector');
0239   parent.appendChild(node_real);
0240 
0241   write_number = real(objs);
0242   sample_num   = 0;
0243 
0244   while ~isempty(write_number)
0245 
0246     n = min(n_min, length(write_number));
0247 
0248     sample_num = sample_num + n;
0249     header_displayed = TerminalOutput(parent, header_displayed, true, 'vector', sample_num);
0250 
0251     item = xml.createElement('vector');
0252     item.setAttribute('type', class(objs));
0253 
0254     if strcmp(class(write_number), 'sym')
0255       number_str = char(write_number(1:n));
0256     else
0257       number_str = ltpda_num2str(write_number(1:n));
0258     end
0259 
0260     content = xml.createTextNode(number_str);
0261     node_real.appendChild(item);
0262     item.appendChild(content);
0263 
0264     write_number = write_number(n+1:end);
0265   end
0266 
0267   %%%%%   Imaginary data   %%%%%
0268   if ~isreal(objs)
0269     node_imag = xml.createElement('imag_data');
0270     node_imag.setAttribute('type', 'vector')
0271     node_imag.setAttribute('shape', shape);
0272     parent.appendChild(node_imag);
0273 
0274     write_number = imag(objs);
0275     sample_num   = 0;
0276 
0277     while ~isempty(write_number)
0278       n = min(n_min, length(write_number));
0279 
0280       sample_num = sample_num + n;
0281       header_displayed = TerminalOutput(parent, header_displayed, false, 'vector', sample_num);
0282 
0283       item = xml.createElement('vector');
0284       item.setAttribute('type', class(objs));
0285 
0286       if strcmp(class(write_number), 'sym')
0287         number_str = char(write_number(1:n));
0288       else
0289         number_str = ltpda_num2str(write_number(1:n));
0290       end
0291 
0292       content = xml.createTextNode(number_str);
0293       node_imag.appendChild(item);
0294       item.appendChild(content);
0295 
0296       write_number = write_number(n+1:end);
0297     end
0298   end
0299 end
0300 
0301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0302 %
0303 % DESCRIPTION: XML_ADDMATRIX Add a matrix element to the xml node. The element
0304 %              have the form:
0305 %
0306 % REAL DATA:   <real_data type="matrix">
0307 %                 <matrix type="double">1 2 3</matrix>
0308 %                 <matrix type="double">4 5 6</matrix>
0309 %                 <matrix type="double">7 8 9</matrix>
0310 %              </real_data>
0311 %
0312 % COMPLEX DATA:<real_data type="matrix">
0313 %                 <matrix type="double">1 2 3</matrix>
0314 %                 <matrix type="double">4 5 6</matrix>
0315 %                 <matrix type="double">7 8 9</matrix>
0316 %              </real_data>
0317 %
0318 %              <imag_data type="matrix">
0319 %                 <matrix type="double">9 8 7</matrix>
0320 %                 <matrix type="double">6 5 4</matrix>
0321 %                 <matrix type="double">3 2 1</matrix>
0322 %              </imag_data>
0323 %
0324 %              Each row in objs creates a matrix element.
0325 %
0326 % HISTORY:     31-01-2008 Diepholz
0327 %                 Creation
0328 %
0329 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0330 function xml_addmatrix(objs, xml, parent)
0331 
0332   shape            = sprintf('%dx%d', size(objs,1), size(objs,2));
0333   header_displayed = true;
0334 
0335   %%% Get the property name only for displaying the property name
0336 
0337   %%%%%   Real data   %%%%%
0338   node_real = xml.createElement('real_data');
0339   node_real.setAttribute('type', 'matrix');
0340   node_real.setAttribute('shape', shape);
0341   %% Set the parent attribute 'type' to matrix
0342   parent.setAttribute('type', 'matrix');
0343   parent.appendChild(node_real);
0344 
0345   write_number = real(objs);
0346 
0347   for ii = 1:size(write_number,1)
0348 
0349     item = xml.createElement('matrix');
0350     item.setAttribute('type', class(objs));
0351 
0352     if strcmp(class(write_number), 'sym')
0353       number_str = char(write_number(ii,:));
0354     else
0355       number_str = ltpda_num2str(write_number(ii,:));
0356     end
0357 
0358     content = xml.createTextNode(number_str);
0359     node_real.appendChild(item);
0360     item.appendChild(content);
0361   end
0362 
0363   TerminalOutput(parent, header_displayed, true, 'matrix', ii);
0364 
0365   %%%%%   Imaginary data   %%%%%
0366   if ~isreal(objs)
0367     node_imag = xml.createElement('imag_data');
0368     node_imag.setAttribute('type', 'matrix');
0369     node_imag.setAttribute('shape', shape);
0370     parent.appendChild(node_imag);
0371 
0372     write_number = imag(objs);
0373 
0374     for ii = 1:size(write_number,1)
0375 
0376       item = xml.createElement('matrix');
0377       item.setAttribute('type', class(objs));
0378 
0379       if strcmp(class(write_number), 'sym')
0380         number_str = char(write_number(ii,:));
0381       else
0382         number_str = ltpda_num2str(write_number(ii,:));
0383       end
0384 
0385       content = xml.createTextNode(number_str);
0386       node_imag.appendChild(item);
0387       item.appendChild(content);
0388     end
0389     TerminalOutput(parent, header_displayed, false, 'matrix', ii);
0390   end
0391 end
0392 
0393 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0394 %
0395 % DESCRIPTION: Displays the terminal output.
0396 %
0397 % HISTORY:     31-01-2008 Diepholz
0398 %                 Creation
0399 %
0400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0401 function header_displayed = TerminalOutput(parent, header_displayed, isreal_num, obj_type, number)
0402 
0403   THRESHOLD_DISP_MATRIX = 10;
0404   THRESHOLD_DISP_VECTOR = 1000;
0405 
0406   showing = false;
0407 
0408   if strcmp(obj_type, 'matrix')
0409     if number >= THRESHOLD_DISP_MATRIX
0410       showing = true;
0411     end
0412     add_text = 'matrix lines';
0413   else
0414     if number >= THRESHOLD_DISP_VECTOR
0415       showing = true;
0416     end
0417     add_text = 'data samples';
0418   end
0419 
0420   if showing == true
0421 
0422     if header_displayed == true
0423       if parent.hasAttribute('prop_name')
0424         disp_prop_name = char(parent.getAttribute('prop_name'));
0425       else
0426         disp_prop_name = 'Unknown Property Name';
0427       end
0428       disp (sprintf('\n%%%%%%%%%%   Write property [%s]   %%%%%%%%%%', disp_prop_name));
0429 
0430       if isreal_num == true
0431         disp('%%%%%   real data   %%%%%')
0432       else
0433         disp('%%%%%   imaginary data   %%%%%')
0434       end
0435       header_displayed = false;
0436     end
0437 
0438     disp(sprintf('Write [%d] %s',number, add_text));
0439   end
0440 end
0441

Generated on Tue 26-Feb-2008 10:52:52 by m2html © 2003