import { AbstractMessageParser } from './abstract-message-parser' ; import { ParsedMessage } from './parsed-message' ; import { ParsedMessagePartStartTag } from './parsed-message-part-start-tag' ; import { ParsedMessagePartEndTag } from './parsed-message-part-end-tag' ; import { ParsedMessagePartPlaceholder } from './parsed-message-part-placeholder' ; import { TagMapping } from './tag-mapping' ; import { ParsedMessagePartEmptyTag } from './parsed-message-part-empty-tag' ; import { ParsedMessagePartICUMessageRef } from './parsed-message-part-icu-message-ref' ; import { isNullOrUndefined } from 'util' ; import { ParsedMessagePartType } from './parsed-message-part' ; import { ParsedMessagePartText } from './parsed-message-part-text' ; /**
 * Created by roobm on 10.05.2017.
 * A message parser for XLIFF 1.2
 */ export class XliffMessageParser extends AbstractMessageParser { param elementNode param message return protected processStartElement ( elementNode , message ) { const tagName = elementNode . tagName ; const tagMapping = new TagMapping ( ) ; if ( tagName === 'x' ) { const id = elementNode . getAttribute ( 'id' ) ; if ( ! id ) { return ; } if ( id . startsWith ( 'INTERPOLATION' ) ) { const index = this . parsePlaceholderIndexFromId ( id ) ; message . addPlaceholder ( index , null ) ; } else if ( id . startsWith ( 'ICU' ) ) { const index = this . parseICUMessageRefIndexFromId ( id ) ; message . addICUMessageRef ( index , null ) ; } else if ( id . startsWith ( 'START_' ) ) { const normalizedTagName = tagMapping . getTagnameFromStartTagPlaceholderName ( id ) ; if ( normalizedTagName ) { const idcount = this . parseIdCountFromName ( id ) ; message . addStartTag ( normalizedTagName , idcount ) ; } } else if ( id . startsWith ( 'CLOSE_' ) ) { const normalizedTagName = tagMapping . getTagnameFromCloseTagPlaceholderName ( id ) ; if ( normalizedTagName ) { message . addEndTag ( normalizedTagName ) ; } } else if ( tagMapping . isEmptyTagPlaceholderName ( id ) ) { const normalizedTagName = tagMapping . getTagnameFromEmptyTagPlaceholderName ( id ) ; if ( normalizedTagName ) { const idcount = this . parseIdCountFromName ( id ) ; message . addEmptyTag ( normalizedTagName , idcount ) ; } } } return true ; } param elementNode param message protected processEndElement ( elementNode , message ) { } param id return private parsePlaceholderIndexFromId ( id ) { let indexString = '' ; if ( id === 'INTERPOLATION' ) { indexString = '0' ; } else { indexString = id . substring ( 'INTERPOLATION_' . length ) ; } return Number . parseInt ( indexString , 10 ) ; } param id return private parseICUMessageRefIndexFromId ( id ) { let indexString = '' ; if ( id === 'ICU' ) { indexString = '0' ; } else { indexString = id . substring ( 'ICU_' . length ) ; } return Number . parseInt ( indexString , 10 ) ; } protected addXmlRepresentationToRoot ( message , rootElem ) { message . parts ( ) . forEach ( ( part ) => { let child ; switch ( part . type ) { case ParsedMessagePartType . TEXT : child = this . createXmlRepresentationOfTextPart ( < ParsedMessagePartText > part , rootElem ) ; break ; case ParsedMessagePartType . START_TAG : child = this . createXmlRepresentationOfStartTagPart ( ( < ParsedMessagePartStartTag > part ) , rootElem ) ; break ; case ParsedMessagePartType . END_TAG : child = this . createXmlRepresentationOfEndTagPart ( ( < ParsedMessagePartEndTag > part ) , rootElem ) ; break ; case ParsedMessagePartType . EMPTY_TAG : child = this . createXmlRepresentationOfEmptyTagPart ( ( < ParsedMessagePartEmptyTag > part ) , rootElem ) ; break ; case ParsedMessagePartType . PLACEHOLDER : child = this . createXmlRepresentationOfPlaceholderPart ( ( < ParsedMessagePartPlaceholder > part ) , rootElem ) ; break ; case ParsedMessagePartType . ICU_MESSAGE_REF : child = this . createXmlRepresentationOfICUMessageRefPart ( ( < ParsedMessagePartICUMessageRef > part ) , rootElem ) ; break ; } if ( child ) { rootElem . appendChild ( child ) ; } } ) ; } param part param rootElem protected createXmlRepresentationOfStartTagPart ( part , rootElem ) { const xElem = rootElem . ownerDocument . createElement ( 'x' ) ; const tagMapping = new TagMapping ( ) ; const idAttrib = tagMapping . getStartTagPlaceholderName ( part . tagName ( ) , part . idCounter ( ) ) ; const ctypeAttrib = tagMapping . getCtypeForTag ( part . tagName ( ) ) ; const equivTextAttr = '<' + part . tagName ( ) + '>' ; xElem . setAttribute ( 'id' , idAttrib ) ; xElem . setAttribute ( 'ctype' , ctypeAttrib ) ; xElem . setAttribute ( 'equiv-text' , equivTextAttr ) ; return xElem ; } param part param rootElem protected createXmlRepresentationOfEndTagPart ( part , rootElem ) { const xElem = rootElem . ownerDocument . createElement ( 'x' ) ; const tagMapping = new TagMapping ( ) ; const idAttrib = tagMapping . getCloseTagPlaceholderName ( part . tagName ( ) ) ; const ctypeAttrib = 'x-' + part . tagName ( ) ; xElem . setAttribute ( 'id' , idAttrib ) ; xElem . setAttribute ( 'ctype' , ctypeAttrib ) ; return xElem ; } param part param rootElem protected createXmlRepresentationOfEmptyTagPart ( part , rootElem ) { const xElem = rootElem . ownerDocument . createElement ( 'x' ) ; const tagMapping = new TagMapping ( ) ; const idAttrib = tagMapping . getEmptyTagPlaceholderName ( part . tagName ( ) , part . idCounter ( ) ) ; const ctypeAttrib = tagMapping . getCtypeForTag ( part . tagName ( ) ) ; const equivTextAttr = '<' + part . tagName ( ) + '/>' ; xElem . setAttribute ( 'id' , idAttrib ) ; xElem . setAttribute ( 'ctype' , ctypeAttrib ) ; xElem . setAttribute ( 'equiv-text' , equivTextAttr ) ; return xElem ; } param part param rootElem protected createXmlRepresentationOfPlaceholderPart ( part , rootElem ) { const xElem = rootElem . ownerDocument . createElement ( 'x' ) ; let idAttrib = 'INTERPOLATION' ; if ( part . index ( ) > 0 ) { idAttrib = 'INTERPOLATION_' + part . index ( ) . toString ( 10 ) ; } const equivTextAttr = part . disp ( ) ; xElem . setAttribute ( 'id' , idAttrib ) ; if ( equivTextAttr ) { xElem . setAttribute ( 'equiv-text' , equivTextAttr ) ; } return xElem ; } param part param rootElem protected createXmlRepresentationOfICUMessageRefPart ( part , rootElem ) { const xElem = rootElem . ownerDocument . createElement ( 'x' ) ; let idAttrib = 'ICU' ; if ( part . index ( ) > 0 ) { idAttrib = 'ICU_' + part . index ( ) . toString ( 10 ) ; } xElem . setAttribute ( 'id' , idAttrib ) ; return xElem ; } }