001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.reifier; 018 019import org.apache.camel.CamelContext; 020import org.apache.camel.Expression; 021import org.apache.camel.Predicate; 022import org.apache.camel.model.ExpressionSubElementDefinition; 023import org.apache.camel.model.language.ExpressionDefinition; 024import org.apache.camel.reifier.language.ExpressionReifier; 025import org.apache.camel.spi.RouteContext; 026import org.apache.camel.support.CamelContextHelper; 027 028public abstract class AbstractReifier { 029 030 protected final RouteContext routeContext; 031 protected final CamelContext camelContext; 032 033 public AbstractReifier(RouteContext routeContext) { 034 this.routeContext = routeContext; 035 this.camelContext = routeContext.getCamelContext(); 036 } 037 038 public AbstractReifier(CamelContext camelContext) { 039 this.routeContext = null; 040 this.camelContext = camelContext; 041 } 042 043 protected String parseString(String text) { 044 return CamelContextHelper.parseText(camelContext, text); 045 } 046 047 protected Boolean parseBoolean(String text) { 048 return CamelContextHelper.parseBoolean(camelContext, text); 049 } 050 051 protected boolean parseBoolean(String text, boolean def) { 052 Boolean b = parseBoolean(text); 053 return b != null ? b : def; 054 } 055 056 protected Long parseLong(String text) { 057 return CamelContextHelper.parseLong(camelContext, text); 058 } 059 060 protected long parseLong(String text, long def) { 061 Long l = parseLong(text); 062 return l != null ? l : def; 063 } 064 065 protected Integer parseInt(String text) { 066 return CamelContextHelper.parseInteger(camelContext, text); 067 } 068 069 protected int parseInt(String text, int def) { 070 Integer i = parseInt(text); 071 return i != null ? i : def; 072 } 073 074 protected Float parseFloat(String text) { 075 return CamelContextHelper.parseFloat(camelContext, text); 076 } 077 078 protected float parseFloat(String text, float def) { 079 Float f = parseFloat(text); 080 return f != null ? f : def; 081 } 082 083 protected <T> T parse(Class<T> clazz, String text) { 084 return CamelContextHelper.parse(camelContext, clazz, text); 085 } 086 087 protected <T> T parse(Class<T> clazz, Object text) { 088 if (text instanceof String) { 089 text = parseString((String) text); 090 } 091 return CamelContextHelper.convertTo(camelContext, clazz, text); 092 } 093 094 protected Expression createExpression(ExpressionDefinition expression) { 095 return ExpressionReifier.reifier(camelContext, expression).createExpression(); 096 } 097 098 protected Expression createExpression(ExpressionSubElementDefinition expression) { 099 return ExpressionReifier.reifier(camelContext, expression).createExpression(); 100 } 101 102 protected Predicate createPredicate(ExpressionDefinition expression) { 103 return ExpressionReifier.reifier(camelContext, expression).createPredicate(); 104 } 105 106 protected Predicate createPredicate(ExpressionSubElementDefinition expression) { 107 return ExpressionReifier.reifier(camelContext, expression).createPredicate(); 108 } 109 110 protected Object or(Object a, Object b) { 111 return a != null ? a : b; 112 } 113 114 protected Object asRef(String s) { 115 return s != null ? s.startsWith("#") ? s : "#" + s : null; 116 } 117 118}