Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014-2015 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  * either express or implied. See the License for the specific language
  * governing permissions and limitations under the License.
  */
 package org.lastaflute.di.naming;
 
 import java.util.Map;
 import java.util.Set;
 

Author(s):
modified by jflute (originated in Seasar)
 
 public class StyledNamingConvention implements NamingConventionDisposable {
 
     // ===================================================================================
     //                                                                          Definition
     //                                                                          ==========
     protected static final char PACKAGE_SEPARATOR = '_';
     protected static final String PACKAGE_SEPARATOR_STR = "_";
 
     // ===================================================================================
     //                                                                           Attribute
     //                                                                           =========
     protected boolean initialized;
     protected String actionSuffix = "Action";
     protected String formSuffix = "Form";
     protected String serviceSuffix = "Service";
     protected String logicSuffix = "Logic";
     protected String assistSuffix = "Assist";
     protected String helperSuffix = "Helper";
     protected String interceptorSuffix = "Interceptor";
     protected String validatorSuffix = "Validator";
     protected String converterSuffix = "Converter";
     protected String implementationSuffix = "Impl";
     protected String viewRootPath = "/view";
     protected String viewExtension = ".html";
 
     protected String[] rootPackageNames = new String[0];
     protected String subApplicationRootPackageName = "web";
     protected String[] ignorePackageNames = new String[0];
 
     protected final Set<StringhotdeployRootPackageNames = new HashSet<String>(4);
     protected final Map<StringResources[]> existCheckerArrays = LdiMapUtil.createHashMap();
     protected final Map<StringStringinterfaceToImplementationMap = new HashMap<StringString>(4);
     protected final Map<StringStringimplementationToInterfaceMap = new HashMap<StringString>(4);
 
     // ===================================================================================
     //                                                                         Constructor
     //                                                                         ===========
     public StyledNamingConvention() {
         initialize();
         setupPropertiesSmartPackage();
     }
 
     protected void setupPropertiesSmartPackage() {
         LastaDiProperties.getInstance().getSmartPackageList().forEach(pkg -> {
             addRootPackageName(pkg);
         });
     }
 
     // ===================================================================================
     //                                                                          Initialize
     //                                                                          ==========
     public void initialize() {
         if (!) {
             for (int i = 0; i < .; ++i) {
                 addExistChecker([i]);
             }
             DisposableUtil.add(this);
              = true;
         }
     }
    // ===================================================================================
    //                                                           Root Package Registration
    //                                                           =========================
    public void addRootPackageName(final String rootPackageName) {
        addRootPackageName(rootPackageNametrue);
    }
    public void addRootPackageName(final String rootPackageNamefinal boolean hotdeploy) {
         = (String[]) LdiArrayUtil.add(rootPackageName);
        if (hotdeploy) {
            .add(rootPackageName);
        }
        addExistChecker(rootPackageName);
    }
    public void addIgnorePackageName(final String ignorePackageName) {
         = (String[]) LdiArrayUtil.add(ignorePackageName);
    }
    public void addInterfaceToImplementationClassName(final String interfaceNamefinal String implementationClassName) {
        .put(interfaceNameimplementationClassName);
        .put(implementationClassNameinterfaceName);
    }
    // ===================================================================================
    //                                                          Root Package Determination
    //                                                          ==========================
    @Override
    public boolean isTargetClassName(final String classNamefinal String suffix) {
        if (isIgnoreClassName(className)) {
            return false;
        }
        if (!LdiStringUtil.trimSuffix(className).endsWith(suffix)) {
            return false;
        }
        final String shortClassName = LdiClassUtil.getShortClassName(className);
        if (className.endsWith() && !className.endsWith("." + getImplementationPackageName() + "." + shortClassName)) {
            return false;
        }
        final String middlePkgName = fromSuffixToPackageName(suffix);
        for (int i = 0; i < .; ++i) {
            if (className.startsWith([i] + "." + middlePkgName + ".")) {
                return true;
            }
            if (className.startsWith([i] + "." +  + ".")) {
                return true;
            }
        }
        return false;
    }
    @Override
    public boolean isTargetClassName(final String className) {
        if (isIgnoreClassName(className)) {
            return false;
        }
        for (int i = 0; i < .; ++i) {
            if (className.startsWith([i] + ".")) {
                return true;
            }
        }
        return false;
    }
    @Override
    public boolean isHotdeployTargetClassName(final String className) {
        if (isIgnoreClassName(className)) {
            return false;
        }
        for (int i = 0; i < .; ++i) {
            if (className.startsWith([i] + ".")) {
                return .contains([i]);
            }
        }
        return false;
    }
    @Override
    public boolean isIgnoreClassName(final String className) {
        for (int i = 0; i < .; ++i) {
            if (className.startsWith([i] + ".")) {
                return true;
            }
        }
        return false;
    }
    // ===================================================================================
    //                                                                        Convert from
    //                                                                        ============
    @Override
    public String fromSuffixToPackageName(final String suffix) {
        if (LdiStringUtil.isEmpty(suffix)) {
            throw new EmptyRuntimeException("suffix");
        }
        return suffix.toLowerCase();
    }
    @Override
    public String fromClassNameToShortComponentName(final String className) {
        if (LdiStringUtil.isEmpty(className)) {
            throw new EmptyRuntimeException("className");
        }
        String s = LdiStringUtil.decapitalize(LdiClassUtil.getShortClassName(className));
        if (s.endsWith()) {
            return s.substring(0, s.length() - .length());
        }
        return s;
    }
    @Override
    public String fromClassNameToComponentName(final String className) {
        if (LdiStringUtil.isEmpty(className)) {
            throw new EmptyRuntimeException("className");
        }
        String cname = toInterfaceClassName(className);
        String suffix = fromClassNameToSuffix(cname);
        String middlePackageName = fromSuffixToPackageName(suffix);
        String name = null;
        for (int i = 0; i < .; ++i) {
            String prefix = [i] + "." + middlePackageName + ".";
            if (cname.startsWith(prefix)) {
                name = cname.substring(prefix.length());
            }
        }
        if (LdiStringUtil.isEmpty(name)) {
            for (int i = 0; i < .; ++i) {
                String prefix = [i] + "." +  + ".";
                if (cname.startsWith(prefix)) {
                    name = cname.substring(prefix.length());
                }
            }
            if (LdiStringUtil.isEmpty(name)) {
                return fromClassNameToShortComponentName(className);
            }
        }
        String[] array = LdiStringUtil.split(name".");
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
            if (i == array.length - 1) {
                buf.append(LdiStringUtil.decapitalize(array[i]));
            } else {
                buf.append(array[i]);
                buf.append('_');
            }
        }
        return buf.toString();
    }
    @Override
    public Class<?> fromComponentNameToClass(final String componentName) {
        if (LdiStringUtil.isEmpty(componentName)) {
            throw new EmptyRuntimeException("componentName");
        }
        String suffix = fromComponentNameToSuffix(componentName);
        if (suffix == null) {
            return null;
        }
        String middlePackageName = fromSuffixToPackageName(suffix);
        String partOfClassName = fromComponentNameToPartOfClassName(componentName);
        boolean subAppSuffix = isSubApplicationSuffix(suffix);
        for (int i = 0; i < .; ++i) {
            String rootPackageName = [i];
            if (subAppSuffix) {
                Class<?> clazz = findClass(rootPackageNamepartOfClassName);
                if (clazz != null) {
                    return clazz;
                }
                clazz = findClass(rootPackageNamemiddlePackageNamepartOfClassName);
                if (clazz != null) {
                    return clazz;
                }
            } else {
                Class<?> clazz = findClass(rootPackageNamemiddlePackageNamepartOfClassName);
                if (clazz != null) {
                    return clazz;
                }
                clazz = findClass(rootPackageNamepartOfClassName);
                if (clazz != null) {
                    return clazz;
                }
            }
        }
        return null;
    }
    protected boolean isSubApplicationSuffix(final String suffix) {
        if (.equals(suffix)) {
            return true;
        }
        if (.equals(suffix)) {
            return true;
        }
        return false;
    }
    protected Class<?> findClass(final String rootPackageNamefinal String middlePackageNamefinal String partOfClassName) {
        initialize();
        final String backPartOfClassName = LdiClassUtil.concatName(middlePackageNamepartOfClassName);
        final String className = LdiClassUtil.concatName(rootPackageNamebackPartOfClassName);
        final String backPartOfImplClassName = toImplementationClassName(backPartOfClassName);
        final String implClassName = LdiClassUtil.concatName(rootPackageNamebackPartOfImplClassName);
        if (!isIgnoreClassName(implClassName) && isExist(rootPackageNamebackPartOfImplClassName)) {
            return LdiClassUtil.forName(implClassName);
        }
        if (!isIgnoreClassName(className) && isExist(rootPackageNamebackPartOfClassName)) {
            return LdiClassUtil.forName(className);
        }
        return null;
    }
    @Override
    public String fromComponentNameToSuffix(final String componentName) {
        return fromNameToSuffix(componentName);
    }
    @Override
    public String fromClassNameToSuffix(final String componentName) {
        return fromNameToSuffix(componentName);
    }
    @Override
    public String fromComponentNameToPartOfClassName(final String componentName) {
        if (componentName == null) {
            throw new EmptyRuntimeException("componentName");
        }
        String[] names = LdiStringUtil.split(componentName);
        StringBuffer buf = new StringBuffer(50);
        for (int i = 0; i < names.length; ++i) {
            if (i == names.length - 1) {
                buf.append(LdiStringUtil.capitalize(names[i]));
            } else {
                buf.append(names[i]).append(".");
            }
        }
        return buf.toString();
    }
    protected String fromNameToSuffix(final String name) {
        if (LdiStringUtil.isEmpty(name)) {
            throw new EmptyRuntimeException("name");
        }
        for (int i = name.length() - 1; i >= 0; --i) {
            if (Character.isUpperCase(name.charAt(i))) {
                return name.substring(i);
            }
        }
        return null;
    }
    protected String fromPathToComponentName(final String pathfinal String nameSuffix) {
        if (!path.startsWith() || !path.endsWith()) {
            throw new IllegalArgumentException(path);
        }
        String componentName =
                (path.substring(adjustViewRootPath().length() + 1, path.length() - .length()) + nameSuffix).replace('/''_');
        int pos = componentName.lastIndexOf('_');
        if (pos == -1) {
            return LdiStringUtil.decapitalize(componentName);
        }
        return componentName.substring(0, pos + 1) + LdiStringUtil.decapitalize(componentName.substring(pos + 1));
    }
    @Override
    public String fromPathToActionName(final String path) {
        return fromPathToComponentName(path);
    }
    @Override
    public String fromActionNameToPath(final String actionName) {
        if (!actionName.endsWith()) {
            throw new IllegalArgumentException(actionName);
        }
        String name = actionName.substring(0, actionName.length() - .length());
        return adjustViewRootPath() + "/" + name.replace('/') + ;
    }
    // ===================================================================================
    //                                                            Interface Implementation
    //                                                            ========================
    @Override
    public String toImplementationClassName(final String className) {
        final String implementationClassName = .get(className);
        if (implementationClassName != null) {
            return implementationClassName;
        }
        final int index = className.lastIndexOf('.');
        if (index < 0) {
            return getImplementationPackageName() + "." + className + ;
        }
        return className.substring(0, index) + "." + getImplementationPackageName() + "." + className.substring(index + 1)
                + ;
    }
    @Override
    public String toInterfaceClassName(final String className) {
        String interfaceClassName = (String.get(className);
        if (interfaceClassName != null) {
            return interfaceClassName;
        }
        if (!className.endsWith()) {
            return className;
        }
        String key = "." + getImplementationPackageName() + ".";
        int index = className.lastIndexOf(key);
        if (index < 0) {
            throw new IllegalArgumentException(className);
        }
        return className.substring(0, index) + "."
                + className.substring(index + key.length(), className.length() - .length());
    }
    @Override
    public boolean isSkipClass(final Class<?> clazz) {
        if (clazz.isInterface()) {
            return false;
        }
        for (final Iterator<Entry<StringString>> it = .entrySet().iterator(); it.hasNext();) {
            final Entry<StringStringentry = it.next();
            final Class<?> interfaceClass = LdiClassUtil.forName((Stringentry.getKey());
            if (interfaceClass.isAssignableFrom(clazz)) {
                return true;
            }
        }
        return false;
    }
    @Override
    public Class<?> toCompleteClass(final Class<?> clazz) {
        if (!clazz.isInterface()) {
            return clazz;
        }
        String className = toImplementationClassName(clazz.getName());
        if (LdiResourceUtil.isExist(LdiClassUtil.getResourcePath(className))) {
            return LdiClassUtil.forName(className);
        }
        return clazz;
    }
    // ===================================================================================
    //                                                                   Existence Checker
    //                                                                   =================
    protected boolean isExist(String rootPackageNameString lastClassName) {
        final Resources[] checkerArray = getExistCheckerArray(rootPackageName);
        for (int i = 0; i < checkerArray.length; ++i) {
            if (checkerArray[i].isExistClass(lastClassName)) {
                return true;
            }
        }
        return false;
    }
    protected Resources[] getExistCheckerArray(final String rootPackageName) {
        return (Resources[]) .get(rootPackageName);
    }
    protected void addExistChecker(final String rootPackageName) {
        final Resources[] checkerArray = LdiResourcesUtil.getResourcesTypes(rootPackageName);
        .put(rootPackageNamecheckerArray);
    }
    // ===================================================================================
    //                                                                             Dispose
    //                                                                             =======
    public void dispose() {
        for (final Iterator<Resources[]> it = .values().iterator(); it.hasNext();) {
            final Resources[] array = it.next();
            for (int i = 0; i < array.length; ++i) {
                array[i].close();
            }
        }
        .clear();
         = false;
    }
    // ===================================================================================
    //                                                                            Accessor
    //                                                                            ========
    // -----------------------------------------------------
    //                                      Component Suffix
    //                                      ----------------
    @Override
    public String getActionSuffix() {
        return ;
    }
    public void setActionSuffix(final String actionSuffix) {
        this. = actionSuffix;
    }
    @Override
    public String getFormSuffix() {
        return ;
    }
    public void setFormSuffix(final String formSuffix) {
        this. = formSuffix;
    }
    @Override
    public String getAssistSuffix() {
        return ;
    }
    public void setAssistSuffix(final String assistSuffix) {
        this. = assistSuffix;
    }
    @Override
    public String getHelperSuffix() {
        return ;
    }
    public void setHelperSuffix(final String helperSuffix) {
        this. = helperSuffix;
    }
    @Override
    public String getInterceptorSuffix() {
        return ;
    }
    public void setInterceptorSuffix(final String interceptorSuffix) {
        this. = interceptorSuffix;
    }
    @Override
    public String getLogicSuffix() {
        return ;
    }
    public void setLogicSuffix(final String logicSuffix) {
        this. = logicSuffix;
    }
    @Override
    public String getServiceSuffix() {
        return ;
    }
    public void setServiceSuffix(final String serviceSuffix) {
        this. = serviceSuffix;
    }
    @Override
    public String getValidatorSuffix() {
        return ;
    }
    public void setValidatorSuffix(final String validatorSuffix) {
        this. = validatorSuffix;
    }
    @Override
    public String getConverterSuffix() {
        return ;
    }
    public void setConverterSuffix(final String converterSuffix) {
        this. = converterSuffix;
    }
    @Override
    public String getImplementationSuffix() {
        return ;
    }
    public void setImplementationSuffix(final String implementationSuffix) {
        this. = implementationSuffix;
    }
    // -----------------------------------------------------
    //                                          Package Name
    //                                          ------------
    @Override
    public String getLogicPackageName() {
        return fromSuffixToPackageName();
    }
    @Override
    public String getHelperPackageName() {
        return fromSuffixToPackageName();
    }
    @Override
    public String getInterceptorPackageName() {
    }
    @Override
    public String getServicePackageName() {
        return fromSuffixToPackageName();
    }
    @Override
    public String getValidatorPackageName() {
    }
    @Override
    public String getConverterPackageName() {
    }
    @Override
    }
    // -----------------------------------------------------
    //                                             View Root
    //                                             ---------
    @Override
    public String getViewExtension() {
        return ;
    }
    public void setViewExtension(final String viewExtension) {
        this. = viewExtension;
    }
    @Override
    public String getViewRootPath() {
        return ;
    }
    public void setViewRootPath(final String viewRootPath) {
        this. = viewRootPath;
    }
    @Override
    public String adjustViewRootPath() {
        return "/".equals() ? "" : ;
    }
    // -----------------------------------------------------
    //                                          Root Package
    //                                          ------------
    @Override
    public String[] getRootPackageNames() {
        return ;
    }
    @Override
    public String[] getIgnorePackageNames() {
        return ;
    }
    @Override
        return ;
    }
    public void setSubApplicationRootPackageName(final String subApplicationRootPackageName) {
        this. = subApplicationRootPackageName;
    }
New to GrepCode? Check out our FAQ X