mxObjectCodec.js 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxObjectCodec
  7. *
  8. * Generic codec for JavaScript objects that implements a mapping between
  9. * JavaScript objects and XML nodes that maps each field or element to an
  10. * attribute or child node, and vice versa.
  11. *
  12. * Atomic Values:
  13. *
  14. * Consider the following example.
  15. *
  16. * (code)
  17. * var obj = new Object();
  18. * obj.foo = "Foo";
  19. * obj.bar = "Bar";
  20. * (end)
  21. *
  22. * This object is encoded into an XML node using the following.
  23. *
  24. * (code)
  25. * var enc = new mxCodec();
  26. * var node = enc.encode(obj);
  27. * (end)
  28. *
  29. * The output of the encoding may be viewed using <mxLog> as follows.
  30. *
  31. * (code)
  32. * mxLog.show();
  33. * mxLog.debug(mxUtils.getPrettyXml(node));
  34. * (end)
  35. *
  36. * Finally, the result of the encoding looks as follows.
  37. *
  38. * (code)
  39. * <Object foo="Foo" bar="Bar"/>
  40. * (end)
  41. *
  42. * In the above output, the foo and bar fields have been mapped to attributes
  43. * with the same names, and the name of the constructor was used for the
  44. * nodename.
  45. *
  46. * Booleans:
  47. *
  48. * Since booleans are numbers in JavaScript, all boolean values are encoded
  49. * into 1 for true and 0 for false. The decoder also accepts the string true
  50. * and false for boolean values.
  51. *
  52. * Objects:
  53. *
  54. * The above scheme is applied to all atomic fields, that is, to all non-object
  55. * fields of an object. For object fields, a child node is created with a
  56. * special attribute that contains the fieldname. This special attribute is
  57. * called "as" and hence, as is a reserved word that should not be used for a
  58. * fieldname.
  59. *
  60. * Consider the following example where foo is an object and bar is an atomic
  61. * property of foo.
  62. *
  63. * (code)
  64. * var obj = {foo: {bar: "Bar"}};
  65. * (end)
  66. *
  67. * This will be mapped to the following XML structure by mxObjectCodec.
  68. *
  69. * (code)
  70. * <Object>
  71. * <Object bar="Bar" as="foo"/>
  72. * </Object>
  73. * (end)
  74. *
  75. * In the above output, the inner Object node contains the as-attribute that
  76. * specifies the fieldname in the enclosing object. That is, the field foo was
  77. * mapped to a child node with an as-attribute that has the value foo.
  78. *
  79. * Arrays:
  80. *
  81. * Arrays are special objects that are either associative, in which case each
  82. * key, value pair is treated like a field where the key is the fieldname, or
  83. * they are a sequence of atomic values and objects, which is mapped to a
  84. * sequence of child nodes. For object elements, the above scheme is applied
  85. * without the use of the special as-attribute for creating each child. For
  86. * atomic elements, a special add-node is created with the value stored in the
  87. * value-attribute.
  88. *
  89. * For example, the following array contains one atomic value and one object
  90. * with a field called bar. Furthermore it contains two associative entries
  91. * called bar with an atomic value, and foo with an object value.
  92. *
  93. * (code)
  94. * var obj = ["Bar", {bar: "Bar"}];
  95. * obj["bar"] = "Bar";
  96. * obj["foo"] = {bar: "Bar"};
  97. * (end)
  98. *
  99. * This array is represented by the following XML nodes.
  100. *
  101. * (code)
  102. * <Array bar="Bar">
  103. * <add value="Bar"/>
  104. * <Object bar="Bar"/>
  105. * <Object bar="Bar" as="foo"/>
  106. * </Array>
  107. * (end)
  108. *
  109. * The Array node name is the name of the constructor. The additional
  110. * as-attribute in the last child contains the key of the associative entry,
  111. * whereas the second last child is part of the array sequence and does not
  112. * have an as-attribute.
  113. *
  114. * References:
  115. *
  116. * Objects may be represented as child nodes or attributes with ID values,
  117. * which are used to lookup the object in a table within <mxCodec>. The
  118. * <isReference> function is in charge of deciding if a specific field should
  119. * be encoded as a reference or not. Its default implementation returns true if
  120. * the fieldname is in <idrefs>, an array of strings that is used to configure
  121. * the <mxObjectCodec>.
  122. *
  123. * Using this approach, the mapping does not guarantee that the referenced
  124. * object itself exists in the document. The fields that are encoded as
  125. * references must be carefully chosen to make sure all referenced objects
  126. * exist in the document, or may be resolved by some other means if necessary.
  127. *
  128. * For example, in the case of the graph model all cells are stored in a tree
  129. * whose root is referenced by the model's root field. A tree is a structure
  130. * that is well suited for an XML representation, however, the additional edges
  131. * in the graph model have a reference to a source and target cell, which are
  132. * also contained in the tree. To handle this case, the source and target cell
  133. * of an edge are treated as references, whereas the children are treated as
  134. * objects. Since all cells are contained in the tree and no edge references a
  135. * source or target outside the tree, this setup makes sure all referenced
  136. * objects are contained in the document.
  137. *
  138. * In the case of a tree structure we must further avoid infinite recursion by
  139. * ignoring the parent reference of each child. This is done by returning true
  140. * in <isExcluded>, whose default implementation uses the array of excluded
  141. * fieldnames passed to the mxObjectCodec constructor.
  142. *
  143. * References are only used for cells in mxGraph. For defining other
  144. * referencable object types, the codec must be able to work out the ID of an
  145. * object. This is done by implementing <mxCodec.reference>. For decoding a
  146. * reference, the XML node with the respective id-attribute is fetched from the
  147. * document, decoded, and stored in a lookup table for later reference. For
  148. * looking up external objects, <mxCodec.lookup> may be implemented.
  149. *
  150. * Expressions:
  151. *
  152. * For decoding JavaScript expressions, the add-node may be used with a text
  153. * content that contains the JavaScript expression. For example, the following
  154. * creates a field called foo in the enclosing object and assigns it the value
  155. * of <mxConstants.ALIGN_LEFT>.
  156. *
  157. * (code)
  158. * <Object>
  159. * <add as="foo">mxConstants.ALIGN_LEFT</add>
  160. * </Object>
  161. * (end)
  162. *
  163. * The resulting object has a field called foo with the value "left". Its XML
  164. * representation looks as follows.
  165. *
  166. * (code)
  167. * <Object foo="left"/>
  168. * (end)
  169. *
  170. * This means the expression is evaluated at decoding time and the result of
  171. * the evaluation is stored in the respective field. Valid expressions are all
  172. * JavaScript expressions, including function definitions, which are mapped to
  173. * functions on the resulting object.
  174. *
  175. * Expressions are only evaluated if <allowEval> is true.
  176. *
  177. * Constructor: mxObjectCodec
  178. *
  179. * Constructs a new codec for the specified template object.
  180. * The variables in the optional exclude array are ignored by
  181. * the codec. Variables in the optional idrefs array are
  182. * turned into references in the XML. The optional mapping
  183. * may be used to map from variable names to XML attributes.
  184. * The argument is created as follows:
  185. *
  186. * (code)
  187. * var mapping = new Object();
  188. * mapping['variableName'] = 'attribute-name';
  189. * (end)
  190. *
  191. * Parameters:
  192. *
  193. * template - Prototypical instance of the object to be
  194. * encoded/decoded.
  195. * exclude - Optional array of fieldnames to be ignored.
  196. * idrefs - Optional array of fieldnames to be converted to/from
  197. * references.
  198. * mapping - Optional mapping from field- to attributenames.
  199. */
  200. function mxObjectCodec(template, exclude, idrefs, mapping)
  201. {
  202. this.template = template;
  203. this.exclude = (exclude != null) ? exclude : [];
  204. this.idrefs = (idrefs != null) ? idrefs : [];
  205. this.mapping = (mapping != null) ? mapping : [];
  206. this.reverse = new Object();
  207. for (var i in this.mapping)
  208. {
  209. this.reverse[this.mapping[i]] = i;
  210. }
  211. };
  212. /**
  213. * Variable: allowEval
  214. *
  215. * Static global switch that specifies if expressions in arrays are allowed.
  216. * Default is false. NOTE: Enabling this carries a possible security risk.
  217. */
  218. mxObjectCodec.allowEval = false;
  219. /**
  220. * Variable: template
  221. *
  222. * Holds the template object associated with this codec.
  223. */
  224. mxObjectCodec.prototype.template = null;
  225. /**
  226. * Variable: exclude
  227. *
  228. * Array containing the variable names that should be
  229. * ignored by the codec.
  230. */
  231. mxObjectCodec.prototype.exclude = null;
  232. /**
  233. * Variable: idrefs
  234. *
  235. * Array containing the variable names that should be
  236. * turned into or converted from references. See
  237. * <mxCodec.getId> and <mxCodec.getObject>.
  238. */
  239. mxObjectCodec.prototype.idrefs = null;
  240. /**
  241. * Variable: mapping
  242. *
  243. * Maps from from fieldnames to XML attribute names.
  244. */
  245. mxObjectCodec.prototype.mapping = null;
  246. /**
  247. * Variable: reverse
  248. *
  249. * Maps from from XML attribute names to fieldnames.
  250. */
  251. mxObjectCodec.prototype.reverse = null;
  252. /**
  253. * Function: getName
  254. *
  255. * Returns the name used for the nodenames and lookup of the codec when
  256. * classes are encoded and nodes are decoded. For classes to work with
  257. * this the codec registry automatically adds an alias for the classname
  258. * if that is different than what this returns. The default implementation
  259. * returns the classname of the template class.
  260. */
  261. mxObjectCodec.prototype.getName = function()
  262. {
  263. return mxUtils.getFunctionName(this.template.constructor);
  264. };
  265. /**
  266. * Function: cloneTemplate
  267. *
  268. * Returns a new instance of the template for this codec.
  269. */
  270. mxObjectCodec.prototype.cloneTemplate = function()
  271. {
  272. return new this.template.constructor();
  273. };
  274. /**
  275. * Function: getFieldName
  276. *
  277. * Returns the fieldname for the given attributename.
  278. * Looks up the value in the <reverse> mapping or returns
  279. * the input if there is no reverse mapping for the
  280. * given name.
  281. */
  282. mxObjectCodec.prototype.getFieldName = function(attributename)
  283. {
  284. if (attributename != null)
  285. {
  286. var mapped = this.reverse[attributename];
  287. if (mapped != null)
  288. {
  289. attributename = mapped;
  290. }
  291. }
  292. return attributename;
  293. };
  294. /**
  295. * Function: getAttributeName
  296. *
  297. * Returns the attributename for the given fieldname.
  298. * Looks up the value in the <mapping> or returns
  299. * the input if there is no mapping for the
  300. * given name.
  301. */
  302. mxObjectCodec.prototype.getAttributeName = function(fieldname)
  303. {
  304. if (fieldname != null)
  305. {
  306. var mapped = this.mapping[fieldname];
  307. if (mapped != null)
  308. {
  309. fieldname = mapped;
  310. }
  311. }
  312. return fieldname;
  313. };
  314. /**
  315. * Function: isExcluded
  316. *
  317. * Returns true if the given attribute is to be ignored by the codec. This
  318. * implementation returns true if the given fieldname is in <exclude> or
  319. * if the fieldname equals <mxObjectIdentity.FIELD_NAME>.
  320. *
  321. * Parameters:
  322. *
  323. * obj - Object instance that contains the field.
  324. * attr - Fieldname of the field.
  325. * value - Value of the field.
  326. * write - Boolean indicating if the field is being encoded or decoded.
  327. * Write is true if the field is being encoded, else it is being decoded.
  328. */
  329. mxObjectCodec.prototype.isExcluded = function(obj, attr, value, write)
  330. {
  331. return attr == mxObjectIdentity.FIELD_NAME ||
  332. mxUtils.indexOf(this.exclude, attr) >= 0;
  333. };
  334. /**
  335. * Function: isReference
  336. *
  337. * Returns true if the given fieldname is to be treated
  338. * as a textual reference (ID). This implementation returns
  339. * true if the given fieldname is in <idrefs>.
  340. *
  341. * Parameters:
  342. *
  343. * obj - Object instance that contains the field.
  344. * attr - Fieldname of the field.
  345. * value - Value of the field.
  346. * write - Boolean indicating if the field is being encoded or decoded.
  347. * Write is true if the field is being encoded, else it is being decoded.
  348. */
  349. mxObjectCodec.prototype.isReference = function(obj, attr, value, write)
  350. {
  351. return mxUtils.indexOf(this.idrefs, attr) >= 0;
  352. };
  353. /**
  354. * Function: encode
  355. *
  356. * Encodes the specified object and returns a node
  357. * representing then given object. Calls <beforeEncode>
  358. * after creating the node and <afterEncode> with the
  359. * resulting node after processing.
  360. *
  361. * Enc is a reference to the calling encoder. It is used
  362. * to encode complex objects and create references.
  363. *
  364. * This implementation encodes all variables of an
  365. * object according to the following rules:
  366. *
  367. * - If the variable name is in <exclude> then it is ignored.
  368. * - If the variable name is in <idrefs> then <mxCodec.getId>
  369. * is used to replace the object with its ID.
  370. * - The variable name is mapped using <mapping>.
  371. * - If obj is an array and the variable name is numeric
  372. * (ie. an index) then it is not encoded.
  373. * - If the value is an object, then the codec is used to
  374. * create a child node with the variable name encoded into
  375. * the "as" attribute.
  376. * - Else, if <encodeDefaults> is true or the value differs
  377. * from the template value, then ...
  378. * - ... if obj is not an array, then the value is mapped to
  379. * an attribute.
  380. * - ... else if obj is an array, the value is mapped to an
  381. * add child with a value attribute or a text child node,
  382. * if the value is a function.
  383. *
  384. * If no ID exists for a variable in <idrefs> or if an object
  385. * cannot be encoded, a warning is issued using <mxLog.warn>.
  386. *
  387. * Returns the resulting XML node that represents the given
  388. * object.
  389. *
  390. * Parameters:
  391. *
  392. * enc - <mxCodec> that controls the encoding process.
  393. * obj - Object to be encoded.
  394. */
  395. mxObjectCodec.prototype.encode = function(enc, obj)
  396. {
  397. var node = enc.document.createElement(this.getName());
  398. obj = this.beforeEncode(enc, obj, node);
  399. this.encodeObject(enc, obj, node);
  400. return this.afterEncode(enc, obj, node);
  401. };
  402. /**
  403. * Function: encodeObject
  404. *
  405. * Encodes the value of each member in then given obj into the given node using
  406. * <encodeValue>.
  407. *
  408. * Parameters:
  409. *
  410. * enc - <mxCodec> that controls the encoding process.
  411. * obj - Object to be encoded.
  412. * node - XML node that contains the encoded object.
  413. */
  414. mxObjectCodec.prototype.encodeObject = function(enc, obj, node)
  415. {
  416. enc.setAttribute(node, 'id', enc.getId(obj));
  417. for (var i in obj)
  418. {
  419. var name = i;
  420. var value = obj[name];
  421. if (value != null && !this.isExcluded(obj, name, value, true))
  422. {
  423. if (mxUtils.isInteger(name))
  424. {
  425. name = null;
  426. }
  427. this.encodeValue(enc, obj, name, value, node);
  428. }
  429. }
  430. };
  431. /**
  432. * Function: encodeValue
  433. *
  434. * Converts the given value according to the mappings
  435. * and id-refs in this codec and uses <writeAttribute>
  436. * to write the attribute into the given node.
  437. *
  438. * Parameters:
  439. *
  440. * enc - <mxCodec> that controls the encoding process.
  441. * obj - Object whose property is going to be encoded.
  442. * name - XML node that contains the encoded object.
  443. * value - Value of the property to be encoded.
  444. * node - XML node that contains the encoded object.
  445. */
  446. mxObjectCodec.prototype.encodeValue = function(enc, obj, name, value, node)
  447. {
  448. if (value != null)
  449. {
  450. if (this.isReference(obj, name, value, true))
  451. {
  452. var tmp = enc.getId(value);
  453. if (tmp == null)
  454. {
  455. mxLog.warn('mxObjectCodec.encode: No ID for ' +
  456. this.getName() + '.' + name + '=' + value);
  457. return; // exit
  458. }
  459. value = tmp;
  460. }
  461. var defaultValue = this.template[name];
  462. // Checks if the value is a default value and
  463. // the name is correct
  464. if (name == null || enc.encodeDefaults || defaultValue != value)
  465. {
  466. name = this.getAttributeName(name);
  467. this.writeAttribute(enc, obj, name, value, node);
  468. }
  469. }
  470. };
  471. /**
  472. * Function: writeAttribute
  473. *
  474. * Writes the given value into node using <writePrimitiveAttribute>
  475. * or <writeComplexAttribute> depending on the type of the value.
  476. */
  477. mxObjectCodec.prototype.writeAttribute = function(enc, obj, name, value, node)
  478. {
  479. if (typeof(value) != 'object' /* primitive type */)
  480. {
  481. this.writePrimitiveAttribute(enc, obj, name, value, node);
  482. }
  483. else /* complex type */
  484. {
  485. this.writeComplexAttribute(enc, obj, name, value, node);
  486. }
  487. };
  488. /**
  489. * Function: writePrimitiveAttribute
  490. *
  491. * Writes the given value as an attribute of the given node.
  492. */
  493. mxObjectCodec.prototype.writePrimitiveAttribute = function(enc, obj, name, value, node)
  494. {
  495. value = this.convertAttributeToXml(enc, obj, name, value, node);
  496. if (name == null)
  497. {
  498. var child = enc.document.createElement('add');
  499. if (typeof(value) == 'function')
  500. {
  501. child.appendChild(enc.document.createTextNode(value));
  502. }
  503. else
  504. {
  505. enc.setAttribute(child, 'value', value);
  506. }
  507. node.appendChild(child);
  508. }
  509. else if (typeof(value) != 'function')
  510. {
  511. enc.setAttribute(node, name, value);
  512. }
  513. };
  514. /**
  515. * Function: writeComplexAttribute
  516. *
  517. * Writes the given value as a child node of the given node.
  518. */
  519. mxObjectCodec.prototype.writeComplexAttribute = function(enc, obj, name, value, node)
  520. {
  521. var child = enc.encode(value);
  522. if (child != null)
  523. {
  524. if (name != null)
  525. {
  526. child.setAttribute('as', name);
  527. }
  528. node.appendChild(child);
  529. }
  530. else
  531. {
  532. mxLog.warn('mxObjectCodec.encode: No node for ' + this.getName() + '.' + name + ': ' + value);
  533. }
  534. };
  535. /**
  536. * Function: convertAttributeToXml
  537. *
  538. * Converts true to "1" and false to "0" is <isBooleanAttribute> returns true.
  539. * All other values are not converted.
  540. *
  541. * Parameters:
  542. *
  543. * enc - <mxCodec> that controls the encoding process.
  544. * obj - Objec to convert the attribute for.
  545. * name - Name of the attribute to be converted.
  546. * value - Value to be converted.
  547. */
  548. mxObjectCodec.prototype.convertAttributeToXml = function(enc, obj, name, value)
  549. {
  550. // Makes sure to encode boolean values as numeric values
  551. if (this.isBooleanAttribute(enc, obj, name, value))
  552. {
  553. // Checks if the value is true (do not use the value as is, because
  554. // this would check if the value is not null, so 0 would be true)
  555. value = (value == true) ? '1' : '0';
  556. }
  557. return value;
  558. };
  559. /**
  560. * Function: isBooleanAttribute
  561. *
  562. * Returns true if the given object attribute is a boolean value.
  563. *
  564. * Parameters:
  565. *
  566. * enc - <mxCodec> that controls the encoding process.
  567. * obj - Objec to convert the attribute for.
  568. * name - Name of the attribute to be converted.
  569. * value - Value of the attribute to be converted.
  570. */
  571. mxObjectCodec.prototype.isBooleanAttribute = function(enc, obj, name, value)
  572. {
  573. return (typeof(value.length) == 'undefined' && (value == true || value == false));
  574. };
  575. /**
  576. * Function: convertAttributeFromXml
  577. *
  578. * Converts booleans and numeric values to the respective types. Values are
  579. * numeric if <isNumericAttribute> returns true.
  580. *
  581. * Parameters:
  582. *
  583. * dec - <mxCodec> that controls the decoding process.
  584. * attr - XML attribute to be converted.
  585. * obj - Objec to convert the attribute for.
  586. */
  587. mxObjectCodec.prototype.convertAttributeFromXml = function(dec, attr, obj)
  588. {
  589. var value = attr.value;
  590. if (this.isNumericAttribute(dec, attr, obj))
  591. {
  592. value = parseFloat(value);
  593. if (isNaN(value) || !isFinite(value))
  594. {
  595. value = 0;
  596. }
  597. }
  598. return value;
  599. };
  600. /**
  601. * Function: isNumericAttribute
  602. *
  603. * Returns true if the given XML attribute is or should be a numeric value.
  604. *
  605. * Parameters:
  606. *
  607. * dec - <mxCodec> that controls the decoding process.
  608. * attr - XML attribute to be converted.
  609. * obj - Objec to convert the attribute for.
  610. */
  611. mxObjectCodec.prototype.isNumericAttribute = function(dec, attr, obj)
  612. {
  613. // Handles known numeric attributes for generic objects
  614. var result = (obj.constructor == mxGeometry &&
  615. (attr.name == 'x' || attr.name == 'y' ||
  616. attr.name == 'width' || attr.name == 'height')) ||
  617. (obj.constructor == mxPoint &&
  618. (attr.name == 'x' || attr.name == 'y')) ||
  619. mxUtils.isNumeric(attr.value);
  620. return result;
  621. };
  622. /**
  623. * Function: beforeEncode
  624. *
  625. * Hook for subclassers to pre-process the object before
  626. * encoding. This returns the input object. The return
  627. * value of this function is used in <encode> to perform
  628. * the default encoding into the given node.
  629. *
  630. * Parameters:
  631. *
  632. * enc - <mxCodec> that controls the encoding process.
  633. * obj - Object to be encoded.
  634. * node - XML node to encode the object into.
  635. */
  636. mxObjectCodec.prototype.beforeEncode = function(enc, obj, node)
  637. {
  638. return obj;
  639. };
  640. /**
  641. * Function: afterEncode
  642. *
  643. * Hook for subclassers to post-process the node
  644. * for the given object after encoding and return the
  645. * post-processed node. This implementation returns
  646. * the input node. The return value of this method
  647. * is returned to the encoder from <encode>.
  648. *
  649. * Parameters:
  650. *
  651. * enc - <mxCodec> that controls the encoding process.
  652. * obj - Object to be encoded.
  653. * node - XML node that represents the default encoding.
  654. */
  655. mxObjectCodec.prototype.afterEncode = function(enc, obj, node)
  656. {
  657. return node;
  658. };
  659. /**
  660. * Function: decode
  661. *
  662. * Parses the given node into the object or returns a new object
  663. * representing the given node.
  664. *
  665. * Dec is a reference to the calling decoder. It is used to decode
  666. * complex objects and resolve references.
  667. *
  668. * If a node has an id attribute then the object cache is checked for the
  669. * object. If the object is not yet in the cache then it is constructed
  670. * using the constructor of <template> and cached in <mxCodec.objects>.
  671. *
  672. * This implementation decodes all attributes and childs of a node
  673. * according to the following rules:
  674. *
  675. * - If the variable name is in <exclude> or if the attribute name is "id"
  676. * or "as" then it is ignored.
  677. * - If the variable name is in <idrefs> then <mxCodec.getObject> is used
  678. * to replace the reference with an object.
  679. * - The variable name is mapped using a reverse <mapping>.
  680. * - If the value has a child node, then the codec is used to create a
  681. * child object with the variable name taken from the "as" attribute.
  682. * - If the object is an array and the variable name is empty then the
  683. * value or child object is appended to the array.
  684. * - If an add child has no value or the object is not an array then
  685. * the child text content is evaluated using <mxUtils.eval>.
  686. *
  687. * For add nodes where the object is not an array and the variable name
  688. * is defined, the default mechanism is used, allowing to override/add
  689. * methods as follows:
  690. *
  691. * (code)
  692. * <Object>
  693. * <add as="hello"><![CDATA[
  694. * function(arg1) {
  695. * mxUtils.alert('Hello '+arg1);
  696. * }
  697. * ]]></add>
  698. * </Object>
  699. * (end)
  700. *
  701. * If no object exists for an ID in <idrefs> a warning is issued
  702. * using <mxLog.warn>.
  703. *
  704. * Returns the resulting object that represents the given XML node
  705. * or the object given to the method as the into parameter.
  706. *
  707. * Parameters:
  708. *
  709. * dec - <mxCodec> that controls the decoding process.
  710. * node - XML node to be decoded.
  711. * into - Optional objec to encode the node into.
  712. */
  713. mxObjectCodec.prototype.decode = function(dec, node, into)
  714. {
  715. var id = node.getAttribute('id');
  716. var obj = dec.objects[id];
  717. if (obj == null)
  718. {
  719. obj = into || this.cloneTemplate();
  720. if (id != null)
  721. {
  722. dec.putObject(id, obj);
  723. }
  724. }
  725. node = this.beforeDecode(dec, node, obj);
  726. this.decodeNode(dec, node, obj);
  727. return this.afterDecode(dec, node, obj);
  728. };
  729. /**
  730. * Function: decodeNode
  731. *
  732. * Calls <decodeAttributes> and <decodeChildren> for the given node.
  733. *
  734. * Parameters:
  735. *
  736. * dec - <mxCodec> that controls the decoding process.
  737. * node - XML node to be decoded.
  738. * obj - Objec to encode the node into.
  739. */
  740. mxObjectCodec.prototype.decodeNode = function(dec, node, obj)
  741. {
  742. if (node != null)
  743. {
  744. this.decodeAttributes(dec, node, obj);
  745. this.decodeChildren(dec, node, obj);
  746. }
  747. };
  748. /**
  749. * Function: decodeAttributes
  750. *
  751. * Decodes all attributes of the given node using <decodeAttribute>.
  752. *
  753. * Parameters:
  754. *
  755. * dec - <mxCodec> that controls the decoding process.
  756. * node - XML node to be decoded.
  757. * obj - Objec to encode the node into.
  758. */
  759. mxObjectCodec.prototype.decodeAttributes = function(dec, node, obj)
  760. {
  761. var attrs = node.attributes;
  762. if (attrs != null)
  763. {
  764. for (var i = 0; i < attrs.length; i++)
  765. {
  766. this.decodeAttribute(dec, attrs[i], obj);
  767. }
  768. }
  769. };
  770. /**
  771. * Function: isIgnoredAttribute
  772. *
  773. * Returns true if the given attribute should be ignored. This implementation
  774. * returns true if the attribute name is "as" or "id".
  775. *
  776. * Parameters:
  777. *
  778. * dec - <mxCodec> that controls the decoding process.
  779. * attr - XML attribute to be decoded.
  780. * obj - Objec to encode the attribute into.
  781. */
  782. mxObjectCodec.prototype.isIgnoredAttribute = function(dec, attr, obj)
  783. {
  784. return attr.nodeName == 'as' || attr.nodeName == 'id';
  785. };
  786. /**
  787. * Function: decodeAttribute
  788. *
  789. * Reads the given attribute into the specified object.
  790. *
  791. * Parameters:
  792. *
  793. * dec - <mxCodec> that controls the decoding process.
  794. * attr - XML attribute to be decoded.
  795. * obj - Objec to encode the attribute into.
  796. */
  797. mxObjectCodec.prototype.decodeAttribute = function(dec, attr, obj)
  798. {
  799. if (!this.isIgnoredAttribute(dec, attr, obj))
  800. {
  801. var name = attr.nodeName;
  802. // Converts the string true and false to their boolean values.
  803. // This may require an additional check on the obj to see if
  804. // the existing field is a boolean value or uninitialized, in
  805. // which case we may want to convert true and false to a string.
  806. var value = this.convertAttributeFromXml(dec, attr, obj);
  807. var fieldname = this.getFieldName(name);
  808. if (this.isReference(obj, fieldname, value, false))
  809. {
  810. var tmp = dec.getObject(value);
  811. if (tmp == null)
  812. {
  813. mxLog.warn('mxObjectCodec.decode: No object for ' +
  814. this.getName() + '.' + name + '=' + value);
  815. return; // exit
  816. }
  817. value = tmp;
  818. }
  819. if (!this.isExcluded(obj, name, value, false))
  820. {
  821. //mxLog.debug(mxUtils.getFunctionName(obj.constructor)+'.'+name+'='+value);
  822. obj[name] = value;
  823. }
  824. }
  825. };
  826. /**
  827. * Function: decodeChildren
  828. *
  829. * Decodes all children of the given node using <decodeChild>.
  830. *
  831. * Parameters:
  832. *
  833. * dec - <mxCodec> that controls the decoding process.
  834. * node - XML node to be decoded.
  835. * obj - Objec to encode the node into.
  836. */
  837. mxObjectCodec.prototype.decodeChildren = function(dec, node, obj)
  838. {
  839. var child = node.firstChild;
  840. while (child != null)
  841. {
  842. var tmp = child.nextSibling;
  843. if (child.nodeType == mxConstants.NODETYPE_ELEMENT &&
  844. !this.processInclude(dec, child, obj))
  845. {
  846. this.decodeChild(dec, child, obj);
  847. }
  848. child = tmp;
  849. }
  850. };
  851. /**
  852. * Function: decodeChild
  853. *
  854. * Reads the specified child into the given object.
  855. *
  856. * Parameters:
  857. *
  858. * dec - <mxCodec> that controls the decoding process.
  859. * child - XML child element to be decoded.
  860. * obj - Objec to encode the node into.
  861. */
  862. mxObjectCodec.prototype.decodeChild = function(dec, child, obj)
  863. {
  864. var fieldname = this.getFieldName(child.getAttribute('as'));
  865. if (fieldname == null || !this.isExcluded(obj, fieldname, child, false))
  866. {
  867. var template = this.getFieldTemplate(obj, fieldname, child);
  868. var value = null;
  869. if (child.nodeName == 'add')
  870. {
  871. value = child.getAttribute('value');
  872. if (value == null && mxObjectCodec.allowEval)
  873. {
  874. value = mxUtils.eval(mxUtils.getTextContent(child));
  875. }
  876. }
  877. else
  878. {
  879. value = dec.decode(child, template);
  880. }
  881. try
  882. {
  883. this.addObjectValue(obj, fieldname, value, template);
  884. }
  885. catch (e)
  886. {
  887. throw new Error(e.message + ' for ' + child.nodeName);
  888. }
  889. }
  890. };
  891. /**
  892. * Function: getFieldTemplate
  893. *
  894. * Returns the template instance for the given field. This returns the
  895. * value of the field, null if the value is an array or an empty collection
  896. * if the value is a collection. The value is then used to populate the
  897. * field for a new instance. For strongly typed languages it may be
  898. * required to override this to return the correct collection instance
  899. * based on the encoded child.
  900. */
  901. mxObjectCodec.prototype.getFieldTemplate = function(obj, fieldname, child)
  902. {
  903. var template = obj[fieldname];
  904. // Non-empty arrays are replaced completely
  905. if (template instanceof Array && template.length > 0)
  906. {
  907. template = null;
  908. }
  909. return template;
  910. };
  911. /**
  912. * Function: addObjectValue
  913. *
  914. * Sets the decoded child node as a value of the given object. If the
  915. * object is a map, then the value is added with the given fieldname as a
  916. * key. If the fieldname is not empty, then setFieldValue is called or
  917. * else, if the object is a collection, the value is added to the
  918. * collection. For strongly typed languages it may be required to
  919. * override this with the correct code to add an entry to an object.
  920. */
  921. mxObjectCodec.prototype.addObjectValue = function(obj, fieldname, value, template)
  922. {
  923. if (value != null && value != template)
  924. {
  925. if (fieldname != null && fieldname.length > 0)
  926. {
  927. obj[fieldname] = value;
  928. }
  929. else
  930. {
  931. obj.push(value);
  932. }
  933. //mxLog.debug('Decoded '+mxUtils.getFunctionName(obj.constructor)+'.'+fieldname+': '+value);
  934. }
  935. };
  936. /**
  937. * Function: processInclude
  938. *
  939. * Returns true if the given node is an include directive and
  940. * executes the include by decoding the XML document. Returns
  941. * false if the given node is not an include directive.
  942. *
  943. * Parameters:
  944. *
  945. * dec - <mxCodec> that controls the encoding/decoding process.
  946. * node - XML node to be checked.
  947. * into - Optional object to pass-thru to the codec.
  948. */
  949. mxObjectCodec.prototype.processInclude = function(dec, node, into)
  950. {
  951. if (node.nodeName == 'include')
  952. {
  953. var name = node.getAttribute('name');
  954. if (name != null)
  955. {
  956. try
  957. {
  958. var xml = mxUtils.load(name).getDocumentElement();
  959. if (xml != null)
  960. {
  961. dec.decode(xml, into);
  962. }
  963. }
  964. catch (e)
  965. {
  966. // ignore
  967. }
  968. }
  969. return true;
  970. }
  971. return false;
  972. };
  973. /**
  974. * Function: beforeDecode
  975. *
  976. * Hook for subclassers to pre-process the node for
  977. * the specified object and return the node to be
  978. * used for further processing by <decode>.
  979. * The object is created based on the template in the
  980. * calling method and is never null. This implementation
  981. * returns the input node. The return value of this
  982. * function is used in <decode> to perform
  983. * the default decoding into the given object.
  984. *
  985. * Parameters:
  986. *
  987. * dec - <mxCodec> that controls the decoding process.
  988. * node - XML node to be decoded.
  989. * obj - Object to encode the node into.
  990. */
  991. mxObjectCodec.prototype.beforeDecode = function(dec, node, obj)
  992. {
  993. return node;
  994. };
  995. /**
  996. * Function: afterDecode
  997. *
  998. * Hook for subclassers to post-process the object after
  999. * decoding. This implementation returns the given object
  1000. * without any changes. The return value of this method
  1001. * is returned to the decoder from <decode>.
  1002. *
  1003. * Parameters:
  1004. *
  1005. * enc - <mxCodec> that controls the encoding process.
  1006. * node - XML node to be decoded.
  1007. * obj - Object that represents the default decoding.
  1008. */
  1009. mxObjectCodec.prototype.afterDecode = function(dec, node, obj)
  1010. {
  1011. return obj;
  1012. };
  1013. __mxOutput.mxObjectCodec = typeof mxObjectCodec !== 'undefined' ? mxObjectCodec : undefined;