Example usage for org.apache.commons.lang ClassUtils getPackageName

List of usage examples for org.apache.commons.lang ClassUtils getPackageName

Introduction

In this page you can find the example usage for org.apache.commons.lang ClassUtils getPackageName.

Prototype

public static String getPackageName(String className) 

Source Link

Document

Gets the package name from a String.

The string passed in is assumed to be a class name - it is not checked.

If the class is unpackaged, return an empty string.

Usage

From source file:org.apache.click.util.ClickUtils.java

/**
 * Deploys required files (from a file list) for a control that repsects a specific convention.
 * <p/>/*from  w w  w  .  j ava2s  .  c  o m*/
 * <b>Convention:</b>
 * <p/>
 * There's a descriptor file generated by the <code>tools/standalone/dev-tasks/ListFilesTask</code>.
 * The files to deploy are all in a subdirectory placed in the same directory with the control.
 * See documentation for more details. <p/>
 *
 * <b>Usage:</b><p/>
 * In your Control simply use the code below, and everything should work automatically.
 * <pre class="prettyprint">
 * public void onDeploy(ServletContext servletContext) {
 *    ClickUtils.deployFileList(servletContext, HeavyControl.class, "click");
 * } </pre>
 *
 * @param servletContext the web applications servlet context
 * @param controlClass the class of the Control that has files for deployment
 * @param targetDir target directory where to deploy the files to. In most cases this
 * is only the reserved directory <code>click</code>
 */
public static void deployFileList(ServletContext servletContext, Class<? extends Control> controlClass,
        String targetDir) {

    String packageName = ClassUtils.getPackageName(controlClass);
    packageName = StringUtils.replaceChars(packageName, '.', '/');
    packageName = "/" + packageName;
    String controlName = ClassUtils.getShortClassName(controlClass);

    ConfigService configService = getConfigService(servletContext);
    LogService logService = configService.getLogService();
    String descriptorFile = packageName + "/" + controlName + ".files";
    logService.debug("Use deployment descriptor file:" + descriptorFile);

    try {
        InputStream is = getResourceAsStream(descriptorFile, ClickUtils.class);
        List fileList = IOUtils.readLines(is);
        if (fileList == null || fileList.isEmpty()) {
            logService.info("there are no files to deploy for control " + controlClass.getName());
            return;
        }

        // a target dir list is required cause the ClickUtils.deployFile() is too inflexible to autodetect
        // required subdirectories.
        List<String> targetDirList = new ArrayList<String>(fileList.size());
        for (int i = 0; i < fileList.size(); i++) {
            String filePath = (String) fileList.get(i);
            String destination = "";
            int index = filePath.lastIndexOf('/');
            if (index != -1) {
                destination = filePath.substring(0, index + 1);
            }
            targetDirList.add(i, targetDir + "/" + destination);
            fileList.set(i, packageName + "/" + filePath);
        }

        for (int i = 0; i < fileList.size(); i++) {
            String source = (String) fileList.get(i);
            String targetDirName = targetDirList.get(i);
            ClickUtils.deployFile(servletContext, source, targetDirName);
        }

    } catch (IOException e) {
        String msg = "error occurred getting resource " + descriptorFile + ", error " + e;
        logService.warn(msg);
    }
}

From source file:org.codehaus.griffon.commons.AbstractGriffonClass.java

/**
 * Used by all child classes to create a new instance and get the name right.
 *
 * @param clazz the Griffon class//w w  w  .j  a  v  a 2  s. co  m
 * @param trailingName the trailing part of the name for this class type
 */
public AbstractGriffonClass(Class<?> clazz, String trailingName) {
    Assert.notNull(clazz, "Clazz parameter should not be null");

    this.clazz = clazz;
    fullName = clazz.getName();
    packageName = ClassUtils.getPackageName(clazz);
    naturalName = GriffonUtil.getNaturalName(clazz.getName());
    shortName = ClassUtils.getShortClassName(clazz);
    name = GriffonUtil.getLogicalName(clazz, trailingName);
    propertyName = GriffonUtil.getPropertyNameRepresentation(shortName);
    if (StringUtils.isBlank(name)) {
        logicalPropertyName = propertyName;
    } else {
        logicalPropertyName = GriffonUtil.getPropertyNameRepresentation(name);
    }
    classPropertyFetcher = ClassPropertyFetcher.forClass(clazz);
}

From source file:org.codehaus.groovy.grails.commons.AbstractGrailsClass.java

/**
 * Used by all child classes to create a new instance and get the name right.
 *
 * @param clazz the Grails class/*from ww w  .  j  a  v  a2  s . co  m*/
 * @param trailingName the trailing part of the name for this class type
 */
public AbstractGrailsClass(Class<?> clazz, String trailingName) {
    Assert.notNull(clazz, "Clazz parameter should not be null");

    this.clazz = clazz;
    fullName = clazz.getName();
    packageName = ClassUtils.getPackageName(clazz);
    naturalName = GrailsNameUtils.getNaturalName(clazz.getName());
    shortName = ClassUtils.getShortClassName(clazz);
    name = GrailsNameUtils.getLogicalName(clazz, trailingName);
    propertyName = GrailsNameUtils.getPropertyNameRepresentation(shortName);
    if (StringUtils.isBlank(name)) {
        logicalPropertyName = propertyName;
    } else {
        logicalPropertyName = GrailsNameUtils.getPropertyNameRepresentation(name);
    }
    classPropertyFetcher = ClassPropertyFetcher.forClass(clazz);
    isAbstract = Modifier.isAbstract(clazz.getModifiers());
}

From source file:org.diffkit.diff.conf.DKMagicPlanBuilder.java

private boolean isCacheable(Class<?> targetClass_) {
    if (ArrayUtils.contains(NON_CACHEABLE_CLASSES, targetClass_))
        return false;
    String targetPackageName = ClassUtils.getPackageName(targetClass_);
    if (targetPackageName.startsWith("org.diffkit"))
        return true;
    return false;
}

From source file:org.eclipse.wb.internal.rcp.databinding.xwt.model.ConverterInfo.java

public void setValue(String value, boolean staticResurce) throws Exception {
    m_update = true;/*from   w  ww  .j a v  a2 s  .co  m*/
    //
    if (value == null) {
        if (m_resourceReference == null && m_className == null) {
            return;
        }
        //
        m_staticResurce = false;
        m_resourceReference = null;
        m_className = null;
        m_namespace = null;
    } else {
        if (m_staticResurce == staticResurce && value.equals(getValue())) {
            return;
        }
        //
        m_staticResurce = staticResurce;
        //
        if (m_staticResurce) {
            m_resourceReference = value;
            m_className = null;
            m_namespace = null;
        } else {
            if (m_namespace != null) {
                String oldPackageName = ClassUtils.getPackageName(m_className);
                String newPackageName = ClassUtils.getPackageName(value);
                //
                if (!oldPackageName.equals(newPackageName)) {
                    m_namespace = null;
                }
            }
            //
            m_resourceReference = null;
            m_className = value;
        }
    }
}

From source file:org.eclipse.wb.internal.rcp.databinding.xwt.model.ConverterInfo.java

private void createNamespace(DocumentElement rootElement) throws Exception {
    Set<String> domains = Sets.newHashSet();
    String domainName = "clr-namespace:" + ClassUtils.getPackageName(m_className);
    for (DocumentAttribute attribute : rootElement.getDocumentAttributes()) {
        if (attribute.getName().startsWith("xmlns:") && attribute.getValue().startsWith("clr-namespace:")) {
            String domainNameSpace = attribute.getName().substring(6);
            if (attribute.getValue().equals(domainName)) {
                m_namespace = domainNameSpace;
                return;
            }//from w ww  . j a  v  a  2  s  . com
            domains.add(domainNameSpace);
        }
    }
    //
    int index = 0;
    while (true) {
        String domainNameSpace = "c" + Integer.toString(index);
        if (domains.contains(domainNameSpace)) {
            index++;
        } else {
            m_namespace = domainNameSpace;
            rootElement.setAttribute("xmlns:" + domainNameSpace, domainName);
            return;
        }
    }
}

From source file:org.eclipse.wb.internal.rcp.databinding.xwt.model.ValidationInfo.java

public void appendValue(StringBuffer value) throws Exception {
    if (m_update && !m_classNames.isEmpty()) {
        if (m_classNames.size() == 1) {
            String className = m_classNames.get(0);
            String namespace = m_packageToNamespace.get(ClassUtils.getPackageName(className));
            value.append(", validationRule=" + StringUtils.defaultString(namespace) + ":"
                    + ClassUtils.getShortClassName(className));
        } else {//from  w ww . ja  v a 2s .c om
            value.append(", validationRules={");
            int index = 0;
            for (String className : m_classNames) {
                if (index++ > 0) {
                    value.append(", ");
                }
                String namespace = m_packageToNamespace.get(ClassUtils.getPackageName(className));
                value.append(
                        StringUtils.defaultString(namespace) + ":" + ClassUtils.getShortClassName(className));
            }
            value.append("}");
        }
    }
}

From source file:org.eclipse.wb.internal.rcp.databinding.xwt.model.ValidationInfo.java

public void applyChanges(AbstractDocumentObject object) throws Exception {
    if (m_update) {
        if (object instanceof DocumentAttribute) {
            if (m_classNames.size() == 1) {
                String className = m_classNames.get(0);
                String namespace = m_packageToNamespace.get(ClassUtils.getPackageName(className));
                if (namespace == null) {
                    DocumentAttribute attribute = (DocumentAttribute) object;
                    namespace = getNamespace(attribute.getEnclosingElement().getRoot(), className);
                    //
                    String value = attribute.getValue();
                    int index = value.indexOf("validationRule=") + 15;
                    attribute.setValue(value.substring(0, index) + namespace + value.substring(index));
                }//from   w w  w .  j ava2  s.c  om
            } else {
                List<String> nonExistingNamespace = Lists.newArrayList();
                DocumentAttribute attribute = (DocumentAttribute) object;
                for (String className : m_classNames) {
                    String namespace = m_packageToNamespace.get(ClassUtils.getPackageName(className));
                    if (namespace == null || nonExistingNamespace.contains(namespace)) {
                        if (namespace == null) {
                            // create non-existing namespace
                            namespace = getNamespace(attribute.getEnclosingElement().getRoot(), className);
                            nonExistingNamespace.add(namespace);
                        }
                        //
                        String value = attribute.getValue();
                        int index = value.indexOf(":" + ClassUtils.getShortClassName(className),
                                value.indexOf("validationRules=") + 16);
                        attribute.setValue(value.substring(0, index) + namespace + value.substring(index));
                    }
                }
            }
        } else {
            DocumentElement bindingElement = (DocumentElement) object;
            if (m_classNames.isEmpty()) {
                DocumentElement validationRules = bindingElement.getChild("Binding.validationRules", true);
                if (validationRules != null) {
                    validationRules.remove();
                }
                //
                DocumentElement validationRule = bindingElement.getChild("Binding.validationRule", true);
                if (validationRule != null) {
                    validationRule.remove();
                }
            } else {
                DocumentElement validationRule = bindingElement.getChild("Binding.validationRule", true);
                if (validationRule != null) {
                    validationRule.remove();
                }
                //
                DocumentElement validationRules = bindingElement.getChild("Binding.validationRules", true);
                if (validationRules == null) {
                    validationRules = new DocumentElement("Binding.validationRule");
                    bindingElement.addChild(validationRules);
                } else {
                    validationRules.removeChildren();
                }
                //
                DocumentElement rootElement = bindingElement.getRoot();
                //
                for (String className : m_classNames) {
                    DocumentElement ruleElement = new DocumentElement();
                    ruleElement.setTag(getNamespace(rootElement, className) + ":"
                            + ClassUtils.getShortClassName(className));
                    validationRules.addChild(ruleElement);
                }
            }
        }
        m_update = false;
    }
}

From source file:org.eclipse.wb.internal.rcp.databinding.xwt.model.ValidationInfo.java

private String getNamespace(DocumentElement rootElement, String className) throws Exception {
    String packageValue = ClassUtils.getPackageName(className);
    String namespace = m_packageToNamespace.get(packageValue);
    if (namespace == null) {
        int index = 0;
        while (true) {
            namespace = "v" + Integer.toString(index);
            if (m_packageToNamespace.containsValue(namespace)) {
                index++;//from  www.  j a  v a2 s.c o m
            } else {
                m_packageToNamespace.put(packageValue, namespace);
                rootElement.setAttribute("xmlns:" + namespace, "clr-namespace:" + packageValue);
                break;
            }
        }
    }
    return namespace;
}

From source file:org.kuali.rice.devtools.jpa.eclipselink.conv.parser.helper.AnnotationHelper.java

private boolean imported(List<ImportDeclaration> imports, String fullyQualifiedName) {
    final String packageName = ClassUtils.getPackageName(fullyQualifiedName);

    for (final ImportDeclaration i : imports) {
        if (!i.isStatic()) {
            final String importName = i.getName().toString();
            if (i.isAsterisk()) {
                if (packageName.equals(importName)) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("found import " + packageName + ".* on " + getTypeNameForMsg(i) + ".");
                    }//w  w  w. j  a v  a2 s . c om
                    return true;
                }
            } else {
                if (fullyQualifiedName.equals(importName)) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("found import " + fullyQualifiedName + " on " + getTypeNameForMsg(i) + ".");
                    }
                    return true;
                }
            }
        }
    }
    return false;
}