davsclaus commented on code in PR #17563: URL: https://github.com/apache/camel/pull/17563#discussion_r2011158246
########## components/camel-dfdl/src/main/java/org/apache/camel/component/dfdl/DfdlEndpoint.java: ########## @@ -0,0 +1,153 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ +package org.apache.camel.component.dfdl; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.channels.Channels; + +import org.w3c.dom.Document; + +import org.apache.camel.Category; +import org.apache.camel.Exchange; +import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.Resource; +import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.spi.UriParam; +import org.apache.camel.spi.UriPath; +import org.apache.camel.support.ProcessorEndpoint; +import org.apache.camel.support.ResourceHelper; +import org.apache.daffodil.japi.Daffodil; +import org.apache.daffodil.japi.DataProcessor; +import org.apache.daffodil.japi.Diagnostic; +import org.apache.daffodil.japi.ParseResult; +import org.apache.daffodil.japi.ProcessorFactory; +import org.apache.daffodil.japi.UnparseResult; +import org.apache.daffodil.japi.infoset.W3CDOMInfosetInputter; +import org.apache.daffodil.japi.infoset.W3CDOMInfosetOutputter; +import org.apache.daffodil.japi.io.InputSourceDataInputStream; + +/** + * Transforms fixed format data such as EDI message from/to XML using a Data Format Description Language (DFDL). + */ +@UriEndpoint(firstVersion = "4.11.0", scheme = "dfdl", title = "DFDL", syntax = "dfdl:schemaUri", producerOnly = true, + category = { Category.TRANSFORMATION }) +public class DfdlEndpoint extends ProcessorEndpoint { + + @UriPath + @Metadata(required = true, description = "The path to the DFDL schema file.") + private String schemaUri; + + @UriParam + @Metadata(defaultValue = "PARSE", description = "Transform direction. Either PARSE or UNPARSE") + private ParseDirection parseDirection; + + @UriParam(description = "The root element of the schema to use. If not specified, the first root element in the schema will be used.", + defaultValue = "") + private String rootElement = ""; + + @UriParam(description = "The root namespace of the schema to use.", defaultValue = "") + private String rootNamespace = ""; + + private DataProcessor daffodilProcessor; + + public DfdlEndpoint(String uri, DfdlComponent component, String schemaFile) { + super(uri, component); + this.schemaUri = schemaFile; + } + + @Override + protected void onExchange(Exchange exchange) throws Exception { + if (getParseDirection() == ParseDirection.UNPARSE) { + Document xmlDocument = exchange.getIn().getBody(Document.class); + W3CDOMInfosetInputter inputter = new W3CDOMInfosetInputter(xmlDocument); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + UnparseResult result = getDaffodilProcessor().unparse(inputter, Channels.newChannel(bos)); + if (result.isError()) { + exchange.setException(new DfdlUnparseException(result)); + return; + } + exchange.getMessage().setBody(bos); + } else { + byte[] binary = exchange.getIn().getBody(byte[].class); + var inputStream = new InputSourceDataInputStream(binary); + var outputter = new W3CDOMInfosetOutputter(); + ParseResult result = getDaffodilProcessor().parse(inputStream, outputter); + if (result.isError()) { + exchange.setException(new DfdlParseException(result)); + return; + } + exchange.getMessage().setBody(outputter.getResult()); + } + } + + public DataProcessor getDaffodilProcessor() throws IOException { Review Comment: This is likely better to do this in doInit so this is loaded eager during bootstrap. Otherwise you can have a concurrency issue if multiple threads is creating this processor because they see it as null on first request. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org