Example usage for org.apache.commons.lang3 StringUtils substringBeforeLast

List of usage examples for org.apache.commons.lang3 StringUtils substringBeforeLast

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils substringBeforeLast.

Prototype

public static String substringBeforeLast(final String str, final String separator) 

Source Link

Document

Gets the substring before the last occurrence of a separator.

Usage

From source file:org.spicyurl.UrlParser.java

private void parseLoginHostPort(String loginHostPort) {
    if (StringUtils.isEmpty(loginHostPort)) {
        url.getValidationErrorsModifiable().add(UrlErrors.HOST_IS_MISSING);
        return;//from w  w w. j a v a 2s. c om
    }
    if (loginHostPort.contains(LOGIN_SEP)) {
        parseLogin(StringUtils.substringBeforeLast(loginHostPort, LOGIN_SEP));
        parseHostPort(StringUtils.substringAfterLast(loginHostPort, LOGIN_SEP));
    } else {
        parseHostPort(loginHostPort);
    }
}

From source file:org.structr.core.function.AbbrFunction.java

@Override
public Object apply(final ActionContext ctx, final GraphObject entity, final Object[] sources)
        throws FrameworkException {

    try {//  w w w  .j a  va2 s .  c  o  m
        if (!arrayHasLengthAndAllElementsNotNull(sources, 2)) {

            return "";
        }

        int maxLength = Double.valueOf(sources[1].toString()).intValue();

        if (sources[0].toString().length() > maxLength) {

            return StringUtils
                    .substringBeforeLast(StringUtils.substring(sources[0].toString(), 0, maxLength), " ")
                    .concat("");

        } else {

            return sources[0];
        }

    } catch (final NumberFormatException nfe) {

        logException(nfe,
                "{0}: NumberFormatException in element \"{1}\". Can not parse \"{2}\" as Integer. Returning original string. Parameters: {3}",
                new Object[] { getName(), entity, sources[1], getParametersAsString(sources) });

        return sources[0];

    } catch (final IllegalArgumentException e) {

        logParameterError(entity, sources, ctx.isJavaScriptContext());

        return usage(ctx.isJavaScriptContext());
    }

}

From source file:org.structr.core.parser.function.AbbrFunction.java

@Override
public Object apply(final ActionContext ctx, final GraphObject entity, final Object[] sources)
        throws FrameworkException {

    if (arrayHasMinLengthAndAllElementsNotNull(sources, 2)) {

        try {/*from w w  w .jav a2 s.  c o  m*/
            int maxLength = Double.valueOf(sources[1].toString()).intValue();

            if (sources[0].toString().length() > maxLength) {

                return StringUtils
                        .substringBeforeLast(StringUtils.substring(sources[0].toString(), 0, maxLength), " ")
                        .concat("");

            } else {

                return sources[0];
            }

        } catch (NumberFormatException nfe) {

            return nfe.getMessage();

        }

    }

    return "";

}

From source file:org.structr.files.ftp.AbstractStructrFtpFile.java

@Override
public boolean move(final FtpFile target) {

    try (Tx tx = StructrApp.getInstance().tx()) {

        logger.log(Level.INFO, "move()");

        final AbstractStructrFtpFile targetFile = (AbstractStructrFtpFile) target;
        final String path = targetFile instanceof StructrFtpFile ? "/" : targetFile.getAbsolutePath();

        try {/*  ww  w  .  ja va2s  . c  om*/
            if (path.contains("/")) {

                String newParentPath = StringUtils.substringBeforeLast(path, "/");
                AbstractFile newParent = FileHelper
                        .getFileByAbsolutePath(SecurityContext.getSuperUserInstance(), newParentPath);

                if (newParent != null && newParent instanceof Folder) {

                    Folder newParentFolder = (Folder) newParent;
                    structrFile.setProperty(AbstractFile.parent, newParentFolder);

                } else {

                    // Move to /
                    structrFile.setProperty(AbstractFile.parent, null);

                }

            }

            if (!("/".equals(path))) {
                final String newName = path.contains("/") ? StringUtils.substringAfterLast(path, "/") : path;
                structrFile.setProperty(AbstractNode.name, newName);
            }

        } catch (FrameworkException ex) {
            logger.log(Level.SEVERE, "Could not move ftp file", ex);
            return false;
        }

        tx.success();

        return true;
    } catch (FrameworkException ex) {
        logger.log(Level.SEVERE, null, ex);
    }

    return false;
}

From source file:org.structr.files.ftp.FileOrFolder.java

@Override
public boolean mkdir() {

    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {

        logger.log(Level.INFO, "mkdir() Folder");

        AbstractFile existing = FileHelper.getFileByAbsolutePath(SecurityContext.getSuperUserInstance(),
                newPath);/*from w  w w.j ava 2 s  . c  o  m*/
        if (existing != null) {
            logger.log(Level.WARNING, "File {0} already exists.", newPath);
            return false;
        }

        final Folder parentFolder = (Folder) FileHelper.getFileByAbsolutePath(
                SecurityContext.getSuperUserInstance(), StringUtils.substringBeforeLast(newPath, "/"));

        try {
            Folder newFolder = (Folder) StructrApp.getInstance().command(CreateNodeCommand.class).execute(
                    new NodeAttribute(AbstractNode.type, Folder.class.getSimpleName()),
                    new NodeAttribute(AbstractNode.owner, owner.getStructrUser()),
                    new NodeAttribute(AbstractNode.name, getName()));

            if (parentFolder != null) {
                newFolder.setProperty(AbstractFile.parent, parentFolder);
            }

        } catch (FrameworkException ex) {
            logger.log(Level.SEVERE, null, ex);
            return false;
        }

        tx.success();

        return true;

    } catch (FrameworkException ex) {
        logger.log(Level.SEVERE, null, ex);
        return false;
    }
}

From source file:org.structr.files.ftp.FileOrFolder.java

@Override
public OutputStream createOutputStream(final long l) throws IOException {

    try (Tx tx = StructrApp.getInstance().tx()) {

        if (structrFile == null) {

            final Folder parentFolder = (Folder) FileHelper.getFileByAbsolutePath(
                    SecurityContext.getSuperUserInstance(), StringUtils.substringBeforeLast(newPath, "/"));

            try {
                structrFile = FileHelper.createFile(SecurityContext.getSuperUserInstance(), new byte[0], null,
                        File.class, getName());
                structrFile.setProperty(AbstractNode.type, File.class.getSimpleName());
                structrFile.setProperty(AbstractNode.owner, owner.getStructrUser());

                if (parentFolder != null) {
                    structrFile.setProperty(AbstractFile.parent, parentFolder);
                }//from   w w w  .  java2s.  com

            } catch (FrameworkException ex) {
                logger.log(Level.SEVERE, null, ex);
                return null;
            }
        }

        tx.success();

        return ((File) structrFile).getOutputStream();

    } catch (FrameworkException fex) {
        logger.log(Level.SEVERE, null, fex);
    }

    return null;
}

From source file:org.structr.files.ftp.StructrFileSystemView.java

@Override
public FtpFile getFile(String requestedPath) throws FtpException {

    logger.log(Level.INFO, "Requested path: {0}", requestedPath);

    try (Tx tx = StructrApp.getInstance().tx()) {

        if (StringUtils.isBlank(requestedPath) || "/".equals(requestedPath)) {
            return getHomeDirectory();
        }/* w ww.  j a  v  a 2 s .c o  m*/

        StructrFtpFolder cur = (StructrFtpFolder) getWorkingDirectory();

        if (".".equals(requestedPath) || "./".equals(requestedPath)) {
            return cur;
        }

        if ("..".equals(requestedPath) || "../".equals(requestedPath)) {
            return new StructrFtpFolder(cur.getStructrFile().getProperty(AbstractFile.parent));
        }

        // If relative path requested, prepend base path
        if (!requestedPath.startsWith("/")) {

            String basePath = cur.getAbsolutePath();

            logger.log(Level.INFO, "Base path: {0}", basePath);

            while (requestedPath.startsWith("..")) {
                requestedPath = StringUtils.stripStart(StringUtils.stripStart(requestedPath, ".."), "/");
                basePath = StringUtils.substringBeforeLast(basePath, "/");
            }

            requestedPath = StringUtils.stripEnd(basePath.equals("/") ? "/".concat(requestedPath)
                    : basePath.concat("/").concat(requestedPath), "/");

            logger.log(Level.INFO, "Base path: {0}, requestedPath: {1}",
                    new Object[] { basePath, requestedPath });

        }

        AbstractFile file = FileHelper.getFileByAbsolutePath(SecurityContext.getSuperUserInstance(),
                requestedPath);

        if (file != null) {

            if (file instanceof Folder) {
                tx.success();
                return new StructrFtpFolder((Folder) file);
            } else {
                tx.success();
                return new StructrFtpFile((File) file);
            }
        }

        // Look up a page by its name
        Page page = StructrApp.getInstance().nodeQuery(Page.class).andName(PathHelper.getName(requestedPath))
                .getFirst();
        if (page != null) {
            tx.success();
            return page;
        }

        logger.log(Level.WARNING, "No existing file found: {0}", requestedPath);

        tx.success();
        return new FileOrFolder(requestedPath, user);

    } catch (FrameworkException fex) {
        logger.log(Level.SEVERE, "Error in getFile()", fex);
    }

    return null;

}

From source file:org.structr.javaparser.JavaParserModule.java

private void analyzeMethodsInJavaFiles(final Folder folder) {

    if (ignoreTests && "test".equals(folder.getName())) {
        return;/*www.  j ava 2s.c  o m*/
    }

    for (final File file : folder.getFiles()) {

        if (file.getContentType().equals("text/x-java")) {

            final String javaFileName = file.getName();

            if (!(javaFileName.equals("package-info.java") || javaFileName.equals("testPackage-info.java"))) {

                final String clsName = StringUtils.substringBeforeLast(javaFileName, ".java");
                final String javaContent = file.getFavoriteContent();

                analyzeMethodsInJavaFile(javaContent, clsName);
            }
        }
    }
}

From source file:org.structr.javaparser.MethodVisitorAdapter.java

@Override
public void visit(final MethodCallExpr methodCall, final Object arg) {

    final Map<String, Object> params = (HashMap) arg;

    final String clsName = (String) params.get("clsName");
    final JavaParserFacade facade = (JavaParserFacade) params.get("facade");
    final App app = (App) params.get("app");

    logger.info("###### " + clsName + ": " + methodCall.getName());

    try {//w  ww. ja v a  2  s.  c o m
        ////// !!!!!!!!!!! Methoden-Aufruf kann in den meisten Fllen nicht aufgelst werden!!
        final SymbolReference<ResolvedMethodDeclaration> ref = facade.solve(methodCall);

        if (ref.isSolved()) {

            final String qualifiedSignature = ref.getCorrespondingDeclaration().getQualifiedSignature();
            //final String scopeString = scope.toString();
            final String parentNodeAsString = methodCall.getParentNode().toString();
            //logger.info("Resolved to " + qualifiedSignature + ", scope: " + scopeString + ", parent node: " + parentNodeAsString);
            logger.info("Resolved to " + qualifiedSignature + ", parent node: " + parentNodeAsString);

            final String calledMethodQualifiedName = StringUtils.replacePattern(qualifiedSignature, "\\(.*\\)",
                    "");
            final String calledMethodQualifiedClassName = StringUtils
                    .substringBeforeLast(calledMethodQualifiedName, ".");
            final String calledMethodName = StringUtils.substringAfterLast(calledMethodQualifiedName, ".");

            Method calledMethod = null;

            final JavaClass calledMethodClass = (JavaClass) app.nodeQuery(JavaClass.class)
                    .and(JavaClass.name, calledMethodQualifiedClassName).getFirst();
            if (calledMethodClass != null) {

                logger.info(" Found called class in graph: " + calledMethodClass.getName());
                calledMethod = (Method) app.nodeQuery(Method.class).and(Method.name, calledMethodName)
                        .and(Method.classOrInterface, calledMethodClass).getFirst();

                if (calledMethod != null) {
                    logger.info(" Found called method in graph: "
                            + calledMethod.getProperty(Method.declaration));

                    final Optional<MethodDeclaration> callingMethod = methodCall
                            .getAncestorOfType(MethodDeclaration.class);
                    if (callingMethod.isPresent()) {

                        final String callingMethodDeclaration = callingMethod.get().getDeclarationAsString();

                        logger.info(" Calling method: " + callingMethodDeclaration);

                        final String callingMethodName = callingMethod.get().getNameAsString();
                        final Optional<TypeDeclaration> typeDecl = callingMethod.get()
                                .getAncestorOfType(TypeDeclaration.class);

                        if (typeDecl.isPresent()) {
                            final String callingMethodClassName = typeDecl.get().getNameAsString();

                            // Find compilation unit
                            final Optional<CompilationUnit> localCU = typeDecl.get()
                                    .getAncestorOfType(CompilationUnit.class);
                            if (localCU.isPresent()) {

                                // Does it have a package declaration?
                                final Optional<PackageDeclaration> packageDecl = localCU.get()
                                        .getPackageDeclaration();
                                if (packageDecl.isPresent()) {

                                    // Assemble qualified class name
                                    final String packageName = packageDecl.get().getNameAsString();
                                    final String fqcn = packageName + "." + callingMethodClassName;

                                    // Find class in graph
                                    final JavaClass callingClass = (JavaClass) app.nodeQuery(JavaClass.class)
                                            .and(JavaClass.name, fqcn).getFirst();
                                    if (callingClass != null) {

                                        final Method method = (Method) app.nodeQuery(Method.class)
                                                .and(Method.name, callingMethodName)
                                                .and(Method.classOrInterface, callingClass).getFirst();
                                        if (method != null) {

                                            logger.info("Found calling method in graph: " + method.getName());

                                            final List<Method> methodsCalled = method
                                                    .getProperty(Method.methodsCalled);
                                            methodsCalled.add(calledMethod);

                                            method.setProperty(Method.methodsCalled, methodsCalled);

                                            logger.info("Added " + calledMethod.getName()
                                                    + " to list of methods called in " + method.getName());
                                        }

                                    }
                                }
                            }

                        }
                    }

                }

            }

        }

    } catch (final Throwable t) {
        logger.info("Unable to resolve " + clsName, t);
    }
}

From source file:org.structr.rest.auth.SessionHelper.java

public static String getShortSessionId(final String sessionId) {
    return StringUtils.substringBeforeLast(sessionId, ".");
}