97 lines
3.3 KiB
TypeScript
Raw Normal View History

2021-07-16 11:41:58 -03:00
/*
2021-12-25 14:39:34 -08:00
* Copyright [2021] [wisemapping]
2021-07-16 11:41:58 -03:00
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2021-12-24 09:40:08 -08:00
import { $assert } from '@wisemapping/core-js';
import ModelCodeName from './ModelCodeName';
import Beta2PelaMigrator from './Beta2PelaMigrator';
import Pela2TangoMigrator from './Pela2TangoMigrator';
2021-12-23 11:58:50 -08:00
import XMLSerializerBeta from './XMLSerializerBeta';
import XMLSerializerTango from './XMLSerializerTango';
import Mindmap from '../model/Mindmap';
2022-01-03 08:27:00 -08:00
import XMLMindmapSerializer from './XMLMindmapSerializer';
2021-07-16 11:41:58 -03:00
2022-07-13 01:45:36 +00:00
const codeToSerializer: { codeName: string; serializer; migrator }[] = [
2021-10-04 17:05:34 -07:00
{
codeName: ModelCodeName.BETA,
2021-12-19 09:56:22 -08:00
serializer: XMLSerializerBeta,
2021-10-04 17:05:34 -07:00
migrator() {
2022-01-24 11:24:16 -08:00
// Ignore ..
2021-10-04 17:05:34 -07:00
},
},
{
codeName: ModelCodeName.PELA,
2022-02-15 11:36:14 -08:00
serializer: XMLSerializerTango,
2021-10-04 17:05:34 -07:00
migrator: Beta2PelaMigrator,
},
{
codeName: ModelCodeName.TANGO,
2021-12-19 09:56:22 -08:00
serializer: XMLSerializerTango,
2021-10-04 17:05:34 -07:00
migrator: Pela2TangoMigrator,
},
];
2021-07-16 11:41:58 -03:00
2021-12-24 09:40:08 -08:00
class XMLSerializerFactory {
/**
* @param {mindplot.model.IMindmap} mindmap
* @return {mindplot.persistence.XMLSerializer_Beta|mindplot.persistence.XMLSerializer_Pela|
* mindplot.persistence.XMLSerializer_Tango} serializer corresponding to the mindmap's version
*/
2022-01-03 08:27:00 -08:00
static createInstanceFromMindmap(mindmap: Mindmap) {
2022-07-13 01:45:36 +00:00
return XMLSerializerFactory.getSerializer(mindmap.getVersion());
2021-12-24 09:40:08 -08:00
}
/**
* @param domDocument
* @return serializer corresponding to the mindmap's version
*/
2022-01-03 08:27:00 -08:00
static createInstanceFromDocument(domDocument: Document) {
2021-12-24 09:40:08 -08:00
const rootElem = domDocument.documentElement;
2022-01-02 10:37:33 -08:00
// Legacy version don't have version defined.
let version = rootElem.getAttribute('version');
version = version || ModelCodeName.BETA;
return XMLSerializerFactory.getSerializer(version);
2021-12-24 09:40:08 -08:00
}
/**
* retrieves the serializer for the mindmap's version and migrates to the current version,
* e.g. for a Beta mindmap and current version Tango:
* serializer = new Pela2TangoMigrator(new Beta2PelaMigrator(new XMLSerializer_Beta()))
* @param {String} version the version name
* @return serializer
*/
2022-01-03 08:27:00 -08:00
static getSerializer(version = ModelCodeName.TANGO): XMLMindmapSerializer {
2021-12-24 09:40:08 -08:00
let found = false;
let result = null;
for (let i = 0; i < codeToSerializer.length; i++) {
if (!found) {
found = codeToSerializer[i].codeName === version;
// eslint-disable-next-line new-cap
2022-07-13 01:45:36 +00:00
if (found) result = new codeToSerializer[i].serializer();
2021-12-24 09:40:08 -08:00
} else {
2022-01-31 00:05:22 +00:00
const { migrator } = codeToSerializer[i];
// eslint-disable-next-line new-cap
result = new migrator(result);
2021-12-24 09:40:08 -08:00
}
}
$assert(result, `Cound not find serialized for ${version}`);
return result;
}
}
2021-10-04 17:05:34 -07:00
export default XMLSerializerFactory;