converter/src/app/converter-registry.service.ts

77 lines
3.2 KiB
TypeScript

import {Base64Decoder} from './converter/base64decoder';
import {Base64Encoder} from './converter/base64encoder';
import {BinToDecConverter} from './converter/bintodecconverter';
import {Converter} from './converter/converter';
import {DecToBinConverter} from './converter/dectobinconverter';
import {DecToHexConverter} from './converter/dectohexconverter';
import {HexToDecConverter} from './converter/hextodecconverter';
import {HTMLEntitiesDecoder} from './converter/htmlentitiesdecoder';
import {HTMLEntitiesEncoder} from './converter/htmlentitiesencoder';
import {Injectable} from '@angular/core';
import {NativeLibraryWrapperService} from './nativelibrarywrapper.service';
import {PunycodeDecoder} from './converter/punycodedecoder';
import {PunycodeEncoder} from './converter/punycodeencoder';
import {QuotedPrintableDecoder} from './converter/quotedprintabledecoder';
import {QuotedPrintableEncoder} from './converter/quotedprintableencoder';
import {ROT13Converter} from './converter/rot13converter';
import {URIComponentDecoder} from './converter/uricomponentdecoder';
import {URIComponentEncoder} from './converter/uricomponentencoder';
import {URIDecoder} from './converter/uridecoder';
import {URIEncoder} from './converter/uriencoder';
import {UTF8Decoder} from './converter/utf8decoder';
import {UTF8Encoder} from './converter/utf8encoder';
@Injectable({
providedIn: 'root'
})
export class ConverterRegistryService {
private converters: Converter[] = [];
constructor(private wrapper: NativeLibraryWrapperService) {
this.init();
}
public getAllConverters(): Converter[] {
return this.converters;
}
public getConverter(id: string): Converter {
for (let i = 0; i < this.converters.length; i++) {
if (this.converters[i].getId() === id) {
return this.converters[i];
}
}
return undefined;
}
private init(): void {
this.registerConverter(new Base64Encoder());
this.registerConverter(new Base64Decoder());
this.registerConverter(new URIEncoder());
this.registerConverter(new URIDecoder());
this.registerConverter(new URIComponentEncoder());
this.registerConverter(new URIComponentDecoder());
this.registerConverter(new HTMLEntitiesEncoder());
this.registerConverter(new HTMLEntitiesDecoder());
this.registerConverter(new QuotedPrintableEncoder(this.wrapper));
this.registerConverter(new QuotedPrintableDecoder(this.wrapper));
this.registerConverter(new DecToHexConverter());
this.registerConverter(new HexToDecConverter());
this.registerConverter(new DecToBinConverter());
this.registerConverter(new BinToDecConverter());
this.registerConverter(new PunycodeEncoder(this.wrapper));
this.registerConverter(new PunycodeDecoder(this.wrapper));
this.registerConverter(new UTF8Encoder(this.wrapper));
this.registerConverter(new UTF8Decoder(this.wrapper));
this.registerConverter(new ROT13Converter());
}
private registerConverter(converter: Converter): void {
this.converters.forEach((c: Converter) => {
if (c.getId() === converter.getId()) {
throw new Error('Converter-ID ' + converter.getId() + ' is already registered!');
}
});
this.converters.push(converter);
}
}