import { Dictionary } from '../model' ; function workaroundForRDFXmlParser ( body ) { return body . replace ( /parseType=["']Collection["']/ig , 'parseType="Collection1"' ) ; } const POSTFIX_TO_MIME = { 'xml' : 'application/rdf+xml' , 'rdf' : 'application/rdf+xml' , 'owl' : 'application/rdf+xml' , 'nttl' : 'application/x-turtle' , 'jsonld' : 'application/ld+json' , 'rj' : 'application/ld+json' , 'ttl' : 'text/turtle' , 'nt' : 'text/turtle' , 'nq' : 'text/turtle' , } ; function getMimeTypeByFileName ( fileName ) { const postfix = ( fileName . match ( /\.([\S]*)$/i ) || [ ] ) [ 1 ] ; return postfix ? POSTFIX_TO_MIME [ postfix ] : undefined ; } export class RDFCompositeParser { constructor ( public parserMap ) { } parse ( body , mimeType ? , fileName ? ) { if ( mimeType ) { if ( mimeType === 'application/rdf+xml' ) { body = workaroundForRDFXmlParser ( body ) ; } if ( ! this . parserMap [ mimeType ] ) { throw Error ( 'There is no parser for this MIME type' ) ; } return this . parserMap [ mimeType ] . parse ( body ) ; } else { return this . tryToGuessMimeType ( body , fileName ) ; } } private tryToGuessMimeType ( body , fileName ? ) { let mimeTypeIndex = 0 ; let mimeTypes = Object . keys ( this . parserMap ) ; if ( fileName ) { const mime = getMimeTypeByFileName ( fileName ) ; if ( mime ) { mimeTypes = [ mime ] . concat ( mimeTypes . filter ( type => type !== mime ) ) ; } } const errors = [ ] ; const recursion = ( ) : Promise < any > => { if ( mimeTypeIndex < mimeTypes . length ) { const mimeType = mimeTypes [ mimeTypeIndex ++ ] ; try { const bodyToParse = mimeType === 'application/rdf+xml' ? workaroundForRDFXmlParser ( body ) : body ; return this . parserMap [ mimeType ] . parse ( bodyToParse ) . catch ( ( error ) => { errors . push ( { mimeType , error } ) ; return recursion ( ) ; } ) ; } catch ( error ) { return recursion ( ) ; } } else { throw new Error ( 'Unknow mime type. Parse errors:\n' + errors . map ( e => `${ e . mimeType }: ${ e . error . message } ${ e . error . stack };\n` ) . join ( '\n' ) ) ; } } ; return recursion ( ) ; } }