camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Claus Ibsen <claus.ib...@gmail.com>
Subject Re: Own Component with optional Parameters
Date Thu, 24 Sep 2015 09:29:51 GMT
The constructor in the endpoint is wrong, it should not create a new
component, but the component instance should be passed into the ctr.

eg such as seda etc where you should also call super

public SedaEndpoint(String endpointUri, Component component) {
   super(endpointUri, component);
   ...



On Thu, Sep 24, 2015 at 11:16 AM, burner
<bjoernpaulsen1980@googlemail.com> wrote:
> *CsvToXmlComponent.java:*
>
> public final class CsvToXmlComponent extends DefaultComponent {
>         private static final Logger LOG =
> LoggerFactory.getLogger(CsvToXmlComponent.class);
>
>         @Override
>         protected CsvToXmlEndpoint createEndpoint(String uri, String remaining,
>                         Map<String, Object> parameters) throws Exception {
>
>                 System.err.println(parameters.toString());
>                 CsvToXmlEndpoint endpoint = new CsvToXmlEndpoint(uri);
>
>                 if (parameters.get("delimiterChar") != null) {
>                         System.err.println("delChar:
> "+parameters.get("delimiterChar").toString());
>                         endpoint.setDelimiterChar(parameters.get("delimiterChar").toString());
>                 }
>                 if (parameters.get("quoteChar") != null) {
>                         endpoint.setQuoteChar(parameters.get("quoteChar").toString());
>                 }
>                 if (parameters.get("escapeChar") != null) {
>                         endpoint.setEscapeChar(parameters.get("escapeChar").toString());
>                 }
>                 return endpoint;
>         }
> }
>
>
>
> *CsvToXmlEndpoint.java:*
>
> public class CsvToXmlEndpoint extends ProcessorEndpoint {
>         private static final Logger LOG =
> LoggerFactory.getLogger(CsvToXmlEndpoint.class);
>         private String delimiterChar = ";";
>         private String quoteChar = "\"";
>         private String escapeChar = "\0";
>
>         public CsvToXmlEndpoint(String endpointUriString) throws URISyntaxException
> {
>
>                 super(endpointUriString, new CsvToXmlComponent(), new
> CsvToXmlProcessor());
>
>                 System.err.println(this.delimiterChar);
>
>                 try {
>                         this.setPropertiesToProcessor();
>                 } catch (Exception e) {
>                         e.printStackTrace();
>                 }
>         }
>
>         protected CsvToXmlProcessor createProcessor() throws Exception {
>                 return new CsvToXmlProcessor();
>         }
>
>         private void setPropertiesToProcessor() throws Exception {
>                 CsvToXmlProcessor csvToXmlProcessor;
>                 csvToXmlProcessor = (CsvToXmlProcessor) this.getProcessor();
>                 csvToXmlProcessor.delimiterChar = this.delimiterChar.charAt(0);
>                 csvToXmlProcessor.quoteChar = this.quoteChar.charAt(0);
>                 csvToXmlProcessor.escapeChar = this.escapeChar.charAt(0);
>         }
>
>         public String getDelimiterChar() {
>                 return delimiterChar;
>         }
>
>         public void setDelimiterChar(String delimiterChar) {
>                 this.delimiterChar = delimiterChar;
>
>                 try {
>                         this.setPropertiesToProcessor();
>                 } catch (Exception e) {
>                         e.printStackTrace();
>                 }
>         }
>
>         public String getEscapeChar() {
>                 return escapeChar;
>         }
>
>         public void setEscapeChar(String escapeChar) {
>                 this.escapeChar = escapeChar;
>
>                 try {
>                         this.setPropertiesToProcessor();
>                 } catch (Exception e) {
>                         e.printStackTrace();
>                 }
>         }
>
>         public String getQuoteChar() {
>                 return quoteChar;
>         }
>
>         public void setQuoteChar(String quoteChar) {
>                 this.quoteChar = quoteChar;
>
>                 try {
>                         this.setPropertiesToProcessor();
>                 } catch (Exception e) {
>                         e.printStackTrace();
>                 }
>         }
>
> }
>
> *CsvToXmlProcessor.java:*
>
> public class CsvToXmlProcessor implements Processor {
>         private static final Logger LOG =
> LoggerFactory.getLogger(CsvToXmlProcessor.class);
>
>         public char delimiterChar = ';';
>         public char quoteChar = '"';
>         public char escapeChar = '\0';
>
>
>         @Override
>         public void process(Exchange exchange) throws Exception {
>                 final String inBody = exchange.getIn().getBody(String.class);
>
>                 System.err.println("pro Del:"+delimiterChar);
>                 int linenumber = 0;
>
>                 CSVReader reader = new CSVReader(new StringReader(inBody),
> this.delimiterChar, this.quoteChar, this.escapeChar);
>                 String[] firstline = reader.readNext();
>                 String[] tokens;
>                 String XMLLines = "<root>\r\n";
>                 while ((tokens = reader.readNext()) != null) {
>                         linenumber = linenumber +1;
>                         String XMLLine = "\t<line number=\""+linenumber+"\">\r\n";
>                         for (int i = 0; i < tokens.length; i++) {
>                                 String valueStr = tokens[i].toString();
>                                 int lengthBefore = valueStr.length();
>                                 valueStr = valueStr.replaceAll("\r", "");
>                                 valueStr = valueStr.replaceAll("\n", "||");
>
>                                 String Feldname = firstline[i];
>                                 Feldname = Feldname.replaceAll("\\s","");
>                                 Feldname = Feldname.replaceAll("/","-");
>                                 Feldname = Feldname.toUpperCase();
>
>                                 if (valueStr.length() != lengthBefore) {
>                                         valueStr = StringEscapeUtils.escapeXml11(valueStr);
>                                         XMLLine += "\t\t<"+Feldname+"></&quot;+Feldname+&quot;>\r\n";
>                                 } else {
>                                         valueStr = StringEscapeUtils.escapeXml11(valueStr);
>                                         XMLLine +=
> "\t\t<"+Feldname+">"+valueStr+"</&quot;+Feldname+&quot;>\r\n";
>                                 }
>                         }
>                         XMLLines += XMLLine+"\t</line>\r\n";
>
>                 }
>                 reader.close();
>                 final String outBody = XMLLines+"</root>\r\n";
>                 LOG.info(linenumber+" Lines readed from CSV-Text");
>                 exchange.getOut().setBody(outBody);
>                 exchange.getOut().setHeaders(exchange.getIn().getHeaders());
>         }
>
> }
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Own-Component-with-optional-Parameters-tp5771889p5771895.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2nd edition: https://www.manning.com/books/ibsen2

Mime
View raw message