001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019package org.apache.isis.viewer.restfulobjects.rendering.service.swagger.internal;
020
021import java.util.regex.Matcher;
022import java.util.regex.Pattern;
023
024import javax.inject.Named;
025
026import org.springframework.stereotype.Component;
027
028import org.apache.isis.applib.annotation.Programmatic;
029
030@Component
031@Named("isisMetaModel.TaggerDefault")
032public class TaggerDefault implements Tagger {
033
034    static Pattern tagSpringFramework = Pattern.compile("^org\\.springframework\\.([^\\.]+)\\.(.+)$");
035    static Pattern tagPatternIsisExtensions2 = Pattern.compile("^isisExt(.+)$");
036    static Pattern tagPatternIsisExtensions = Pattern.compile("^org\\.apache\\.isis\\.extensions\\.([^\\.]+)\\.(.+)$");
037    static Pattern tagPatternForFqcn = Pattern.compile("^.*\\.([^\\.]+)\\.([^\\.]+)$");
038    static Pattern tagPatternForTwoParts = Pattern.compile("^([^\\.]+)\\.([^\\.]+)$");
039    static Pattern tagPatternForJaxbDto = Pattern.compile("^.*\\.([^\\.]+)\\.(v[0-9][^\\.]*)\\.([^\\.]+)$");
040
041    @Override
042    @Programmatic
043    public String tagForObjectType(final String objType, final String fallback) {
044
045        if (objType.startsWith("org.apache.isis.")) {
046            return ". apache isis internals";
047        }
048        
049        Matcher matcher;
050        matcher = tagSpringFramework.matcher(objType);
051        if (matcher.matches()) {
052            return "> spring framework " + matcher.group(1);
053        }
054        matcher = tagPatternIsisExtensions.matcher(objType);
055        if (matcher.matches()) {
056            return ". apache isis extensions - " + matcher.group(1);
057        }
058        matcher = tagPatternForJaxbDto.matcher(objType);
059        if (matcher.matches()) {
060            return matcher.group(1);
061        }
062        matcher = tagPatternForFqcn.matcher(objType);
063        if (matcher.matches()) {
064            return matcher.group(1);
065        }
066        matcher = tagPatternForTwoParts.matcher(objType);
067        if (matcher.matches()) {
068            if (objType.startsWith("isisApplib")) {
069                return ". apache isis applib";
070            }
071
072            if (objType.startsWith("isisExt")) {
073                return ". apache isis extensions - " + matcher.group(1);
074            }
075
076            return matcher.group(1);
077        }
078        
079        return fallback != null? fallback: objType;
080    }
081
082}