@@ -5,9 +5,6 @@ import { getAdapter } from '~/services/adapter-service'
55import { FlowConfig } from './canvas/flow.config'
66import { isGroupNode , isStickyNote } from '~/stores/flow-store'
77import type { GroupNode } from './canvas/nodetypes/group-node'
8- import { fetchFrankConfigXsd } from '~/services/xsd-service'
9- import { getMandatoryAttributeNames , parseXsd } from '~/utils/xsd-utils'
10- import { sortAttributes } from '~/utils/xml-attribute-sort'
118
129interface ReactFlowJson {
1310 nodes : FlowNode [ ]
@@ -45,14 +42,6 @@ export async function exportFlowToXml(
4542 : existingAdapterXml
4643 const adapterAttributes = getAdapterAttributes ( adapterXml )
4744
48- let xsdDoc : Document | null = null
49-
50- try {
51- xsdDoc = parseXsd ( await fetchFrankConfigXsd ( ) )
52- } catch {
53- console . warn ( 'Could not fetch FrankConfig XSD; attribute order may not be optimal.' )
54- }
55-
5645 const { nodes, edges } = json
5746 const validNodes = nodes . filter ( ( node ) => hasDataProperty ( node ) )
5847 const nodeMap = new Map ( validNodes . map ( ( n ) => [ n . id , n ] ) )
@@ -87,13 +76,13 @@ export async function exportFlowToXml(
8776
8877 const type = node . data . type ?. toLowerCase ( )
8978 if ( type === 'receiver' ) {
90- receivers . push ( generateXmlElement ( node , edgeMap , exitNodeIds , nodeMap , xsdDoc ) )
79+ receivers . push ( generateXmlElement ( node , edgeMap , exitNodeIds , nodeMap ) )
9180 } else if ( type === 'pipe' ) {
92- pipelineParts . push ( generateXmlElement ( node , edgeMap , exitNodeIds , nodeMap , xsdDoc ) )
81+ pipelineParts . push ( generateXmlElement ( node , edgeMap , exitNodeIds , nodeMap ) )
9382 }
9483 }
9584
96- const exitsXml = exitNodes . length > 0 ? ` <Exits>\n${ generateExitsXml ( exitNodes , xsdDoc ) } \n </Exits>` : ''
85+ const exitsXml = exitNodes . length > 0 ? ` <Exits>\n${ generateExitsXml ( exitNodes ) } \n </Exits>` : ''
9786 const flowXml = generateFlowElementsXml ( nodes )
9887
9988 return `
@@ -108,18 +97,17 @@ ${pipelineParts.join('\n')}
10897}
10998
11099export function replaceAdapterInXml ( configXml : string , adapterIndex : number , newAdapterXml : string ) : string {
111- const starts = [ ...configXml . matchAll ( / < [ A a ] d a p t e r \b / g) ] . map ( ( m ) => m . index )
100+ const matches = [ ...configXml . matchAll ( / < ( A d a p t e r | a d a p t e r ) \b / g) ]
112101
113- if ( adapterIndex >= starts . length ) return configXml
102+ if ( adapterIndex >= matches . length ) return configXml
114103
115- const start = starts [ adapterIndex ]
116- const closeRegex = / < \/ [ A a ] d a p t e r > / g
117- closeRegex . lastIndex = start
104+ const match = matches [ adapterIndex ]
105+ const start = match . index
106+ const closingTag = `</${ match [ 1 ] } >`
107+ const closeIndex = configXml . indexOf ( closingTag , start )
108+ if ( closeIndex === - 1 ) return configXml
118109
119- const closeMatch = closeRegex . exec ( configXml )
120- if ( ! closeMatch ) return configXml
121-
122- return configXml . slice ( 0 , start ) + newAdapterXml + configXml . slice ( closeMatch . index + closeMatch [ 0 ] . length )
110+ return configXml . slice ( 0 , start ) + newAdapterXml + configXml . slice ( closeIndex + closingTag . length )
123111}
124112
125113function buildEdgeMaps ( edges : Edge [ ] ) {
@@ -178,7 +166,6 @@ function generateXmlElement(
178166 edgeMap : Map < string , { targetId : string ; label : string } [ ] > ,
179167 exitNodeIds : Set < string > ,
180168 nodeMap : Map < string , FlowNode > ,
181- xsdDoc : Document | null ,
182169) : string {
183170 const { subtype, name } = node . data as NodeData
184171 const { x, y } = node . position
@@ -190,7 +177,7 @@ function generateXmlElement(
190177 width = node . measured . width
191178 }
192179
193- const height : number | undefined = node . height ?? undefined
180+ const height : number | null = node . height ?? null
194181 const attributes = ( node . data as NodeData ) . attributes || { }
195182 const children = ( node . data as NodeData ) . children || [ ]
196183
@@ -200,14 +187,13 @@ function generateXmlElement(
200187 'flow:x' : String ( roundedX ) ,
201188 'flow:y' : String ( roundedY ) ,
202189 'flow:width' : String ( width ) ,
203- ...( height === undefined ? { } : { 'flow:height' : String ( height ) } ) ,
190+ ...( height === null ? { } : { 'flow:height' : String ( height ) } ) ,
204191 }
205- const mandatory = xsdDoc ? getMandatoryAttributeNames ( xsdDoc , subtype ) : new Set < string > ( )
206- const attrStr = sortAttributes ( allAttrs , mandatory )
192+ const attrStr = Object . entries ( allAttrs )
207193 . map ( ( [ k , v ] ) => `${ k } ="${ escapeXml ( v ) } "` )
208194 . join ( ' ' )
209195
210- const childXml = children . map ( ( child : ChildNode ) => generateChildXml ( child , 4 , xsdDoc ) ) . join ( '\n' )
196+ const childXml = children . map ( ( child : ChildNode ) => generateChildXml ( child , 4 ) ) . join ( '\n' )
211197
212198 const type = ( node . data as NodeData ) . type ?. toLowerCase ( )
213199
@@ -232,16 +218,15 @@ function generateXmlElement(
232218 return content ? ` <${ subtype } ${ attrStr } >\n${ content } \n </${ subtype } >` : ` <${ subtype } ${ attrStr } />`
233219}
234220
235- function generateChildXml ( child : ChildNode , indent : number , xsdDoc : Document | null ) : string {
221+ function generateChildXml ( child : ChildNode , indent : number ) : string {
236222 const spaces = ' ' . repeat ( indent )
237223
238224 const childAttrs : Record < string , string > = {
239225 ...( child . name ? { name : child . name } : { } ) ,
240226 ...child . attributes ,
241227 }
242228
243- const mandatory = xsdDoc ? getMandatoryAttributeNames ( xsdDoc , child . subtype ) : new Set < string > ( )
244- const attrStr = sortAttributes ( childAttrs , mandatory )
229+ const attrStr = Object . entries ( childAttrs )
245230 . map ( ( [ k , v ] ) => `${ k } ="${ escapeXml ( v ) } "` )
246231 . join ( ' ' )
247232
@@ -252,16 +237,14 @@ function generateChildXml(child: ChildNode, indent: number, xsdDoc: Document | n
252237 return `${ spaces } <${ child . subtype } ${ attrs } />`
253238 }
254239
255- const childXmlStrings = child . children ! . map ( ( nested ) => generateChildXml ( nested , indent + 2 , xsdDoc ) )
240+ const childXmlStrings = child . children ! . map ( ( nested ) => generateChildXml ( nested , indent + 2 ) )
256241
257242 return `${ spaces } <${ child . subtype } ${ attrs } >
258243${ childXmlStrings . join ( '\n' ) }
259244${ spaces } </${ child . subtype } >`
260245}
261246
262- function generateExitsXml ( exitNodes : FlowNode [ ] , xsdDoc : Document | null ) : string {
263- const mandatory = xsdDoc ? getMandatoryAttributeNames ( xsdDoc , 'Exit' ) : new Set < string > ( )
264-
247+ function generateExitsXml ( exitNodes : FlowNode [ ] ) : string {
265248 return exitNodes
266249 . map ( ( node ) => {
267250 const { name } = node . data as NodeData
@@ -284,7 +267,7 @@ function generateExitsXml(exitNodes: FlowNode[], xsdDoc: Document | null): strin
284267 'flow:width' : String ( width ) ,
285268 'flow:height' : String ( height ) ,
286269 }
287- const attrStr = sortAttributes ( allAttrs , mandatory )
270+ const attrStr = Object . entries ( allAttrs )
288271 . map ( ( [ k , v ] ) => `${ k } ="${ escapeXml ( v ) } "` )
289272 . join ( ' ' )
290273
0 commit comments