Hello,

Camel internally parses the route files and instantiates route steps as Processes. I would like to call this function in my program.

Currently, with xml and Camel 2 I'm doing it with Jaxb, but I would like to employ the existing Camel functions that can do it for both xml and yaml.

To parse an xml I am wrapping it with some root element and then unmarshal it's children with Jaxb.

So we have a String that contains this:

<unmarshal>
    <csv delimiter=";" useMaps="true" />
</unmarshal>

The code that creates a Processor from this is this:

            JAXBContext jc = JAXBContext.newInstance(
                    "org.apache.camel.model:"
                    + "org.apache.camel.model.language:"
                    + "org.apache.camel.model.dataformat:"
                    + "org.apache.camel.model.rest:"
                    + "org.apache.camel.model.transformer:"
                    + "org.apache.camel.model.validator:"
                    + "org.apache.camel.model.config:"
                    + "org.apache.camel.model.loadbalancer:"
                    + "org.apache.camel.model.cloud:"
                    + "org.apache.camel.model.loadbalancer");
            _unmarshal = jc.createUnmarshaller();

...

            RouteContext rc = new DefaultRouteContext(_context);
            rc.getRoute().setId(.....);


        NodeList nl = _convertStringToNodeList(processorsDefinition);
        ArrayList<Processor> ret = new ArrayList<>();
        for (int i = 0; i < nl.getLength(); i++) {
            if (nl.item(i) instanceof Element) {
                Object def = _unmarshal.unmarshal(nl.item(i));
                if (def instanceof ProcessorDefinition) {
                    ProcessorDefinition pd = (ProcessorDefinition) def;
                    Processor processor = pd.createProcessor(rc);
                    //_context.addService(processor, true);
                    ret.add(processor);
                }
            }
        }

        return ret;

.....

    private static NodeList _convertStringToNodeList(String xmlStr) { // TODO: optimize when all other problems are solved         String xml = "<root xmlns=\"" + CAMELNAMESPACE + "\">" + xmlStr + "</root>";         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder;
        try {
            builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new InputSource(new StringReader(xml)));             return doc.getElementsByTagName("root").item(0).getChildNodes();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


On 17.09.2023 12:36, Claus Ibsen wrote:
Hi

I dont think we understand your question. Can you try to explain this in
more detail?

On Thu, Sep 7, 2023 at 10:31 AM Fyodor Kravchenko <f...@vsetec.com> wrote:

Hello,

what is the reliable way in a Java program to create a Processor from a
String containing a definition of a processor that usually are parts of
the route? Camel 4. Like


<setBody>
      <simple>${body[0]}</simple>
</setBody>


or


- unmarshal:
      csv:
        delimiter: ";"
        useMaps: "true"


Thank you!


Reply via email to