001/*
002 * (C) Copyright 2006 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.project.sample;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import org.nuxeo.runtime.model.ComponentInstance;
025import org.nuxeo.runtime.model.DefaultComponent;
026
027public class BookTitleServiceImpl extends DefaultComponent implements BookTitleService {
028
029    private List<BookTitleDescriptor> config = new ArrayList<BookTitleDescriptor>();
030
031    public String correctTitle(String title) {
032        boolean addInitialCap = false;
033        boolean removeExtension = false;
034        String addComment = null;
035
036        for (BookTitleDescriptor d : config) {
037            if (d.addInitialCap) {
038                addInitialCap = true;
039            }
040            if (d.getRemoveExtension()) {
041                removeExtension = true;
042            }
043            if (d.getAddComment() != null) {
044                addComment = d.getAddComment();
045            }
046        }
047
048        if (removeExtension) {
049            int dot = title.lastIndexOf('.');
050            if (dot > title.length() - 5) {
051                title = title.substring(0, dot);
052            }
053        }
054        if (addInitialCap) {
055            title = title.toUpperCase();
056        }
057        if (addComment != null) {
058            title += ' ' + addComment;
059        }
060        return title;
061    }
062
063    @Override
064    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
065        config.add((BookTitleDescriptor) contribution);
066    }
067
068    @Override
069    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
070        config.remove(contribution);
071    }
072
073}