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     */
017    package org.apache.camel.component.flatpack;
018    
019    import java.util.Map;
020    
021    import org.apache.camel.Endpoint;
022    import org.apache.camel.component.ResourceBasedComponent;
023    import org.apache.camel.util.ObjectHelper;
024    import org.springframework.core.io.Resource;
025    
026    /**
027     * A <a href="http://flatpack.sourceforge.net/">Flatpack Component</a>
028     * for working with fixed width and delimited files
029     *
030     * @version $Revision: 16270 $
031     */
032    public class FlatpackComponent extends ResourceBasedComponent {
033    
034        public static final String HEADER_ID = "header";
035        public static final String TRAILER_ID = "trailer";
036    
037        protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
038            boolean fixed = false;
039            if (remaining.startsWith("fixed:")) {
040                fixed = true;
041                remaining = remaining.substring("fixed:".length());
042            } else if (remaining.startsWith("delim:")) {
043                remaining = remaining.substring("delim:".length());
044            } else {
045                // lets assume the rest of the string is just a name
046                // to differentiate different named delimited endpoints
047                remaining = "";
048            }
049            Resource resource = null;
050            if (fixed) {
051                resource = resolveMandatoryResource(remaining);
052            } else {
053                if (ObjectHelper.isNotEmpty(remaining)) {
054                    resource = getResourceLoader().getResource(remaining);
055                }
056            }
057            if (log.isDebugEnabled()) {
058                log.debug(this + " using flatpack map resource: " + resource);
059            }
060            FixedLengthEndpoint answer;
061            if (fixed) {
062                answer = new FixedLengthEndpoint(uri, resource);
063            } else {
064                answer = new DelimitedEndpoint(uri, resource);
065            }
066            answer.setCamelContext(getCamelContext());
067            setProperties(answer, parameters);
068            return answer;
069        }
070    }