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.html,v 1.7 2008/03/31 10:27:31 hewitson 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.html,v 1.7 2008/03/31 10:27:31 hewitson 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         if isreal(objs)
0177           number_str = sprintf('%.17g ', objs);
0178         else
0179           number_str = num2str(objs, 20);
0180         end
0181       end
0182       content = xml.createTextNode(number_str);
0183       parent.appendChild(content);
0184 
0185       %%%%%   objs is a matrix   %%%%%
0186     elseif (size(objs,1) > 1) && (size(objs,2) > 1)
0187 
0188       xml_addmatrix(objs, xml, parent);
0189 
0190       %%%%%   objs is a vector   %%%%%
0191     elseif (size(objs,1) > 1) || (size(objs,2) > 1)
0192 
0193       xml_addvector(objs, xml, parent);
0194 
0195     end
0196 
0197   else
0198     error('### unknown type [%s]', class(objs));
0199   end
0200 end
0201 
0202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0203 %
0204 % DESCRIPTION: XML_ADDVECTOR Add a vector element to the xml node. The element
0205 %              have the form:
0206 %
0207 % REAL DATA:   <real_data type="vector">
0208 %                 <vector type="double"> 1  2  3  4  5  6  7  8  9 10</vector>
0209 %                 <vector type="double">11 12 13 14 15 16 17 18 19 20</vector>
0210 %              </real_data>
0211 %
0212 % COMPLEX DATA:<real_data type="vector">
0213 %                 <vector type="double"> 1  2  3  4  5  6  7  8  9 10</vector>
0214 %                 <vector type="double">11 12 13 14 15 16 17 18 19 20</vector>
0215 %              </real_data>
0216 %
0217 %              <imag_data type="vector">
0218 %                 <vector type="double"> 1  2  3  4  5  6  7  8  9 10</vector>
0219 %                 <vector type="double">11 12 13 14 15 16 17 18 19 20</vector>
0220 %              </imag_data>
0221 %
0222 %              The vector objs will be split into several parts dependent from
0223 %              the maximum size in one vector element.
0224 %              Each part creates its own vectror element.
0225 %
0226 % HISTORY:     31-01-2008 Diepholz
0227 %                 Creation
0228 %
0229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0230 function xml_addvector(objs, xml, parent)
0231 
0232   n_min            = getappdata(0, 'xmlsetsize');
0233   header_displayed = true;
0234 
0235   shape = sprintf('%dx%d', size(objs,1), size(objs,2));
0236 
0237   %%%%%   Real data   %%%%%
0238   node_real = xml.createElement('real_data');
0239   node_real.setAttribute('type', 'vector');
0240   node_real.setAttribute('shape', shape);
0241   %% Set the parent attribute 'type' to vector
0242   parent.setAttribute('type', 'vector');
0243   parent.appendChild(node_real);
0244 
0245   write_number = real(objs);
0246   sample_num   = 0;
0247 
0248   while ~isempty(write_number)
0249 
0250     n = min(n_min, length(write_number));
0251 
0252     sample_num = sample_num + n;
0253     header_displayed = TerminalOutput(parent, header_displayed, true, 'vector', sample_num);
0254 
0255     item = xml.createElement('vector');
0256     item.setAttribute('type', class(objs));
0257 
0258     if strcmp(class(write_number), 'sym')
0259       number_str = char(write_number(1:n));
0260     else
0261       number_str = ltpda_num2str(write_number(1:n));
0262     end
0263 
0264     content = xml.createTextNode(number_str);
0265     node_real.appendChild(item);
0266     item.appendChild(content);
0267 
0268     write_number = write_number(n+1:end);
0269   end
0270 
0271   %%%%%   Imaginary data   %%%%%
0272   if ~isreal(objs)
0273     node_imag = xml.createElement('imag_data');
0274     node_imag.setAttribute('type', 'vector')
0275     node_imag.setAttribute('shape', shape);
0276     parent.appendChild(node_imag);
0277 
0278     write_number = imag(objs);
0279     sample_num   = 0;
0280 
0281     while ~isempty(write_number)
0282       n = min(n_min, length(write_number));
0283 
0284       sample_num = sample_num + n;
0285       header_displayed = TerminalOutput(parent, header_displayed, false, 'vector', sample_num);
0286 
0287       item = xml.createElement('vector');
0288       item.setAttribute('type', class(objs));
0289 
0290       if strcmp(class(write_number), 'sym')
0291         number_str = char(write_number(1:n));
0292       else
0293         number_str = ltpda_num2str(write_number(1:n));
0294       end
0295 
0296       content = xml.createTextNode(number_str);
0297       node_imag.appendChild(item);
0298       item.appendChild(content);
0299 
0300       write_number = write_number(n+1:end);
0301     end
0302   end
0303 end
0304 
0305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0306 %
0307 % DESCRIPTION: XML_ADDMATRIX Add a matrix element to the xml node. The element
0308 %              have the form:
0309 %
0310 % REAL DATA:   <real_data type="matrix">
0311 %                 <matrix type="double">1 2 3</matrix>
0312 %                 <matrix type="double">4 5 6</matrix>
0313 %                 <matrix type="double">7 8 9</matrix>
0314 %              </real_data>
0315 %
0316 % COMPLEX DATA:<real_data type="matrix">
0317 %                 <matrix type="double">1 2 3</matrix>
0318 %                 <matrix type="double">4 5 6</matrix>
0319 %                 <matrix type="double">7 8 9</matrix>
0320 %              </real_data>
0321 %
0322 %              <imag_data type="matrix">
0323 %                 <matrix type="double">9 8 7</matrix>
0324 %                 <matrix type="double">6 5 4</matrix>
0325 %                 <matrix type="double">3 2 1</matrix>
0326 %              </imag_data>
0327 %
0328 %              Each row in objs creates a matrix element.
0329 %
0330 % HISTORY:     31-01-2008 Diepholz
0331 %                 Creation
0332 %
0333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0334 function xml_addmatrix(objs, xml, parent)
0335 
0336   shape            = sprintf('%dx%d', size(objs,1), size(objs,2));
0337   header_displayed = true;
0338 
0339   %%% Get the property name only for displaying the property name
0340 
0341   %%%%%   Real data   %%%%%
0342   node_real = xml.createElement('real_data');
0343   node_real.setAttribute('type', 'matrix');
0344   node_real.setAttribute('shape', shape);
0345   %% Set the parent attribute 'type' to matrix
0346   parent.setAttribute('type', 'matrix');
0347   parent.appendChild(node_real);
0348 
0349   write_number = real(objs);
0350 
0351   for ii = 1:size(write_number,1)
0352 
0353     item = xml.createElement('matrix');
0354     item.setAttribute('type', class(objs));
0355 
0356     if strcmp(class(write_number), 'sym')
0357       number_str = char(write_number(ii,:));
0358     else
0359       number_str = ltpda_num2str(write_number(ii,:));
0360     end
0361 
0362     content = xml.createTextNode(number_str);
0363     node_real.appendChild(item);
0364     item.appendChild(content);
0365   end
0366 
0367   TerminalOutput(parent, header_displayed, true, 'matrix', ii);
0368 
0369   %%%%%   Imaginary data   %%%%%
0370   if ~isreal(objs)
0371     node_imag = xml.createElement('imag_data');
0372     node_imag.setAttribute('type', 'matrix');
0373     node_imag.setAttribute('shape', shape);
0374     parent.appendChild(node_imag);
0375 
0376     write_number = imag(objs);
0377 
0378     for ii = 1:size(write_number,1)
0379 
0380       item = xml.createElement('matrix');
0381       item.setAttribute('type', class(objs));
0382 
0383       if strcmp(class(write_number), 'sym')
0384         number_str = char(write_number(ii,:));
0385       else
0386         number_str = ltpda_num2str(write_number(ii,:));
0387       end
0388 
0389       content = xml.createTextNode(number_str);
0390       node_imag.appendChild(item);
0391       item.appendChild(content);
0392     end
0393     TerminalOutput(parent, header_displayed, false, 'matrix', ii);
0394   end
0395 end
0396 
0397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0398 %
0399 % DESCRIPTION: Displays the terminal output.
0400 %
0401 % HISTORY:     31-01-2008 Diepholz
0402 %                 Creation
0403 %
0404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0405 function header_displayed = TerminalOutput(parent, header_displayed, isreal_num, obj_type, number)
0406 
0407   THRESHOLD_DISP_MATRIX = 10;
0408   THRESHOLD_DISP_VECTOR = 1000;
0409 
0410   showing = false;
0411 
0412   if strcmp(obj_type, 'matrix')
0413     if number >= THRESHOLD_DISP_MATRIX
0414       showing = true;
0415     end
0416     add_text = 'matrix lines';
0417   else
0418     if number >= THRESHOLD_DISP_VECTOR
0419       showing = true;
0420     end
0421     add_text = 'data samples';
0422   end
0423 
0424   if showing == true
0425 
0426     if header_displayed == true
0427       if parent.hasAttribute('prop_name')
0428         disp_prop_name = char(parent.getAttribute('prop_name'));
0429       else
0430         disp_prop_name = 'Unknown Property Name';
0431       end
0432       disp (sprintf('\n%%%%%%%%%%   Write property [%s]   %%%%%%%%%%', disp_prop_name));
0433 
0434       if isreal_num == true
0435         disp('%%%%%   real data        %%%%%')
0436       else
0437         disp('%%%%%   imaginary data   %%%%%')
0438       end
0439       header_displayed = false;
0440     end
0441 
0442     disp(sprintf('Write [%d] %s',number, add_text));
0443   end
0444 end
0445

Generated on Mon 31-Mar-2008 12:20:24 by m2html © 2003