Example usage for org.apache.commons.lang StringUtils substring

List of usage examples for org.apache.commons.lang StringUtils substring

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils substring.

Prototype

public static String substring(String str, int start, int end) 

Source Link

Document

Gets a substring from the specified String avoiding exceptions.

Usage

From source file:org.eclipse.wb.internal.swing.model.property.editor.accelerator.KeyStrokePropertyEditor.java

/**
 * @return the textual presentation of given {@link KeyStroke}.
 *//*ww  w  .jav  a  2s . c om*/
private static String getText(KeyStroke stroke) {
    try {
        // prepare modifiers
        String modifiersText = "";
        {
            int modifiers = stroke.getModifiers();
            if ((modifiers & CTRL_MASK) != 0) {
                modifiersText += "Ctrl+";
            }
            if ((modifiers & ALT_MASK) != 0) {
                modifiersText += "Alt+";
            }
            if ((modifiers & SHIFT_MASK) != 0) {
                modifiersText += "Shift+";
            }
            if ((modifiers & META_MASK) != 0) {
                modifiersText += "Meta+";
            }
            if ((modifiers & ALT_GRAPH_MASK) != 0) {
                modifiersText += "AltGr+";
            }
            // remove trailing '+'
            if (modifiersText.length() != 0) {
                modifiersText = StringUtils.substring(modifiersText, 0, -1);
            }
        }
        // add key
        int keyCode = stroke.getKeyCode();
        if (keyCode != KeyEvent.VK_UNDEFINED) {
            return modifiersText + "-" + getKeyName(keyCode);
        }
        // no key
        return modifiersText;
    } catch (Throwable e) {
        DesignerPlugin.log(e);
    }
    return null;
}

From source file:org.eclipse.wb.internal.swing.model.property.editor.accelerator.KeyStrokePropertyEditor.java

/**
 * @return the source for given {@link KeyStroke}.
 *///from  w  ww .  jav a 2  s  .  c o m
private static String getKeyStrokeSource(KeyStroke keyStroke) {
    // prepare modifiers source
    String modifiersSource = "";
    {
        int modifiers = keyStroke.getModifiers();
        if ((modifiers & CTRL_MASK) != 0) {
            modifiersSource += "java.awt.event.InputEvent.CTRL_MASK | ";
        }
        if ((modifiers & ALT_MASK) != 0) {
            modifiersSource += "java.awt.event.InputEvent.ALT_MASK | ";
        }
        if ((modifiers & SHIFT_MASK) != 0) {
            modifiersSource += "java.awt.event.InputEvent.SHIFT_MASK | ";
        }
        if ((modifiers & META_MASK) != 0) {
            modifiersSource += "java.awt.event.InputEvent.META_MASK | ";
        }
        if ((modifiers & ALT_GRAPH_MASK) != 0) {
            modifiersSource += "java.awt.event.InputEvent.ALT_GRAPH_MASK | ";
        }
        //
        if (modifiersSource.length() != 0) {
            modifiersSource = StringUtils.substring(modifiersSource, 0, -" | ".length());
        } else {
            modifiersSource = "0";
        }
    }
    // prepare key source
    String keyCodeSource;
    {
        String keyName = getKeyName(keyStroke.getKeyCode());
        if (keyName == null) {
            return null;
        }
        keyCodeSource = "java.awt.event.KeyEvent.VK_" + keyName;
    }
    // prepare source
    return "javax.swing.KeyStroke.getKeyStroke(" + keyCodeSource + ", " + modifiersSource + ")";
}

From source file:org.ednovo.gooru.domain.service.resource.ResourceParser.java

public TitleAndText getTextAndTitle(String url) {
    ContentHandler textHandler = new BodyContentHandler();
    Metadata metadata = new Metadata();
    try {/*from w ww  . jav a  2s .com*/

        InputStream in = null;
        if (url.startsWith("http://")) {
            URL urlObject = new URL(url);
            URLConnection res = urlObject.openConnection();
            in = res.getInputStream();
        } else {
            in = new FileInputStream(url);
        }

        parser.parse(in, textHandler, metadata, new ParseContext());

    } catch (Exception e) {
        e.toString();
    }

    String title = metadata.get(Metadata.TITLE);
    String text = textHandler.toString().trim().replaceAll("\\s+", " ");
    if (StringUtils.isBlank(title)) {
        title = StringUtils.substring(text, 0, 50);
    }
    return new TitleAndText(title, text);
}

From source file:org.ednovo.gooru.domain.service.resource.ResourceServiceImpl.java

private void enrichWithTitleAndText(final Resource resource) {

    final String parentUrlString = resource.getParentUrl();

    List<String> possibleTitles = new ArrayList<String>();
    List<String> possibleTexts = new ArrayList<String>();

    // get possible title and text from url
    ResourceParser.TitleAndText textAndTitle = this.getResourceParser().getTextAndTitle(resource.getUrl());
    possibleTitles.add(textAndTitle.getTitle());
    possibleTexts.add(textAndTitle.getText());

    // get possible title and text from parent url
    if (StringUtils.isNotBlank(parentUrlString)) {
        ResourceParser.TitleAndText parentTextAndTitle = this.getResourceParser()
                .getTextAndTitle(parentUrlString);
        possibleTitles.add(parentTextAndTitle.getTitle());
        possibleTexts.add(parentTextAndTitle.getText());
    }//  www  . jav a  2s. co  m

    // set the first title that is not blank.
    for (final String title : possibleTitles) {
        if (StringUtils.isNotBlank(resource.getTitle())) {
            break;
        }
        resource.setTitle(StringUtils.substring(title, 0, 250));
    }
    // set the first text that is not blank.
}

From source file:org.ejbca.ui.web.pub.CertDistServlet.java

private void handleCaChainCommands(AuthenticationToken administrator, String issuerdn, int caid, String format,
        HttpServletResponse res) throws IOException, NoSuchFieldException {
    try {//from  w  w w.  jav a 2 s.  c o  m
        Certificate[] chain = getCertificateChain(administrator, caid, issuerdn);
        // Reverse the chain to get proper ordering for chain file
        // (top-level CA first, requested CA last).
        ArrayUtils.reverse(chain);

        // Construct the filename based on requested CA. Fail-back to
        // name "ca-chain.EXT".
        String filename = RequestHelper.getFileNameFromCertNoEnding(chain[chain.length - 1], "ca") + "-chain."
                + format.toLowerCase();

        byte[] outbytes = new byte[0];
        // Encode and send back
        if ((format == null) || StringUtils.equalsIgnoreCase(format, "pem")) {
            outbytes = CertTools.getPemFromCertificateChain(Arrays.asList(chain));
        } else {
            // Create a JKS truststore with the CA certificates in
            final KeyStore store = KeyStore.getInstance("JKS");
            store.load(null, null);
            for (int i = 0; i < chain.length; i++) {
                String cadn = CertTools.getSubjectDN(chain[i]);
                String alias = CertTools.getPartFromDN(cadn, "CN");
                if (alias == null) {
                    alias = CertTools.getPartFromDN(cadn, "O");
                }
                if (alias == null) {
                    alias = "cacert" + i;
                }
                alias = StringUtils.replaceChars(alias, ' ', '_');
                alias = StringUtils.substring(alias, 0, 15);
                store.setCertificateEntry(alias, chain[i]);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                store.store(out, "changeit".toCharArray());
                out.close();
                outbytes = out.toByteArray();
            }
        }
        // We must remove cache headers for IE
        ServletUtils.removeCacheHeaders(res);
        res.setHeader("Content-disposition",
                "attachment; filename=\"" + StringTools.stripFilename(filename) + "\"");
        res.setContentType("application/octet-stream");
        res.setContentLength(outbytes.length);
        res.getOutputStream().write(outbytes);
        log.debug("Sent CA certificate chain to client, len=" + outbytes.length + ".");
    } catch (CertificateEncodingException e) {
        log.debug("Error getting CA certificate chain: ", e);
        res.sendError(HttpServletResponse.SC_NOT_FOUND, "Error getting CA certificate chain.");
    } catch (KeyStoreException e) {
        log.debug("Error creating JKS with CA certificate chain: ", e);
        res.sendError(HttpServletResponse.SC_NOT_FOUND, "Error creating JKS with CA certificate chain.");
    } catch (NoSuchAlgorithmException e) {
        log.debug("Error creating JKS with CA certificate chain: ", e);
        res.sendError(HttpServletResponse.SC_NOT_FOUND, "Error creating JKS with CA certificate chain.");
    } catch (CertificateException e) {
        log.debug("Error creating JKS with CA certificate chain: ", e);
        res.sendError(HttpServletResponse.SC_NOT_FOUND, "Error creating JKS with CA certificate chain.");
    } catch (EJBException e) {
        log.debug("CA does not exist: ", e);
        res.sendError(HttpServletResponse.SC_NOT_FOUND,
                "CA does not exist: " + HTMLTools.htmlescape(e.getMessage()));
    } catch (AuthorizationDeniedException e) {
        log.debug("Authotization denied: ", e);
        res.sendError(HttpServletResponse.SC_UNAUTHORIZED,
                "Authorization denied: " + HTMLTools.htmlescape(e.getMessage()));
    }
}

From source file:org.fenixedu.ulisboa.specifications.dto.evaluation.markSheet.CompetenceCourseMarkSheetBean.java

public void setExecutionCourseDataSource(final Set<ExecutionCourse> value) {

    if (value.size() == 1) {
        setExecutionCourse(value.iterator().next());
    }//www.jav a 2  s. c o m

    this.executionCourseDataSource = value.stream().sorted(ExecutionCourse.EXECUTION_COURSE_NAME_COMPARATOR)
            .map(x -> {
                TupleDataSourceBean tuple = new TupleDataSourceBean();
                tuple.setId(x.getExternalId());

                final String name = x.getNameI18N().getContent();
                tuple.setText(name.replace("'", " ").replace("\"", " ") + " ["
                        + x.getAssociatedCurricularCoursesSet().stream().map(i -> {
                            final String dcp = i.getDegreeCurricularPlan().getName();
                            final int index = dcp.contains(" ") ? dcp.indexOf(" ") : 6;
                            return i.getDegree().getCode() + " - " + StringUtils.substring(dcp, 0, index);
                        }).collect(Collectors.joining("; ")) + "]");

                return tuple;

            }).collect(Collectors.toList());
}

From source file:org.fornax.utilities.xtendtools.lib.StringsExtension.java

/**
 * Delegate for StringUtils.substring./*from  w w w.  j a va2  s .  c o m*/
 * 
 * @see StringUtils#substring(String, int, int)
 */
public static String substring(final String str, final Integer start, final Integer end) {
    return StringUtils.substring(str, start, end);
}

From source file:org.gaixie.micrite.security.service.impl.LoginServiceImpl.java

public List<Map<String, Object>> loadChildNodes(User user, String node) {
    Set<Map<String, Object>> menu = new HashSet<Map<String, Object>>();

    Set<Role> roles = user.getRoles();
    String name, iconCls;/*  ww w  . j av a 2s. co m*/
    int order;
    for (Role role : roles) {
        List<Authority> auths = authorityDAO.findByRoleId(role.getId());
        for (Authority auth : auths) {
            // "/"?menu
            if (StringUtils.indexOf(auth.getName(), "/") >= 0) {
                //                   if(auth.getName().matches("/CRM Modules/Customer List"))continue;
                if (auth.getState() != 0)
                    continue;
                //                    /*
                //                     * ?????idtext ?User3??(
                //                     * "/N1/N2/N3" , "/N1/N4"  "/N5" )
                //                     * 3???split?token?"N1""N5"
                //                     * 2"N1"Set???add
                //                     * ?map?put?(url)???? N1:
                //                     * 1,2??substring"/N2/N3""N4"??split?"N2""N4"
                //                     * 3?????continue
                //                     */
                if ("allModulesRoot".equals(node)) {
                    //                       
                    //                       if(auth.getName().matches("/Security Modules/User List")){
                    //                          String tmp1 = "/?/";
                    //                          auth.setName(tmp1);
                    //                       }
                    //                       if(auth.getName().matches("/Security Modules/Authority List")){
                    //                          String tmp2 = "/?/?";
                    //                          auth.setName(tmp2) ;
                    //                       }
                    //                       if(auth.getName().matches("/Security Modules/Role List")){
                    //                          String tmp3 ="/?/??";
                    //                          auth.setName(tmp3);
                    //                       }
                    name = auth.getName();
                    iconCls = auth.getIconCls1();
                    order = auth.getOrder1();

                    /*
                     * "/"????
                     * /HTTP/HTTP POST   
                     * /MMS/HTTP MT/HTTP MT Top20 Cell  
                     */
                } else if (StringUtils.indexOf(auth.getName(), node + "/") >= 0) {
                    name = StringUtils.substringAfter(auth.getName(), node + "/");
                    iconCls = auth.getIconCls2();
                    order = auth.getOrder2();

                } else
                    continue;

                Map<String, Object> map = new HashMap<String, Object>();
                String[] names = StringUtils.split(name, "/");
                map.put("text", names[0]);
                map.put("id", "/" + names[0]);

                if (iconCls != null)
                    map.put("iconCls", iconCls);
                map.put("sort", order);
                if (names.length > 1) {
                    //  ???url??name??node
                    map.put("url", names[0]);
                    map.put("leaf", false);

                } else {
                    // "/"?"*"??extjs autoload
                    map.put("url", StringUtils.substring(auth.getValue(), 1, auth.getValue().length() - 1));
                    map.put("leaf", true);
                }
                menu.add(map);
            }
        }
    }
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(menu);
    Collections.sort(list, new Comparator<Map<String, Object>>() {
        public int compare(Map<String, Object> o1, Map<String, Object> o2) {
            //return (o2.getValue() - o1.getValue()); 
            return ((Integer) o1.get("sort")) - ((Integer) o2.get("sort"));
        }
    });
    for (int i = 0; i < list.size(); i++) {
        list.get(i).remove("sort");

    }
    //        return menu;
    return list;
}

From source file:org.hibernate.search.test.performance.task.QueryBooksBySummaryTask.java

private void assertResult(List<Book> result, String phrase) {
    for (Book book : result) {
        assertTrue(//w w  w.  j a v a 2  s .c  om
                "QueryBooksBySummaryTask: phrase=" + phrase + ", summary="
                        + StringUtils.substring(book.getSummary(), 0, 50),
                StringUtils.containsIgnoreCase(book.getSummary(), phrase));
    }
}

From source file:org.hoteia.qalingo.core.domain.Customer.java

public String getScreenName() {
    String screenName = (String) getValue(CustomerAttribute.CUSTOMER_ATTRIBUTE_SCREENAME, null, null);
    if (StringUtils.isEmpty(screenName)) {
        screenName = getFirstname();/*from  w w  w.  j a  v  a  2s  . c o  m*/
    }
    if (StringUtils.isEmpty(screenName)) {
        screenName = StringUtils.substring(getLastname(), 0, 1).toUpperCase() + ".";
    }
    return screenName;
}