Example usage for java.lang StringBuffer substring

List of usage examples for java.lang StringBuffer substring

Introduction

In this page you can find the example usage for java.lang StringBuffer substring.

Prototype

@Override
public synchronized String substring(int start, int end) 

Source Link

Usage

From source file:org.guzz.util.PropertyUtil.java

/**
* Fetch the value of the given key, and resolve ${...} placeholders , then trim() the value and return.
* @param props Properties//from   w  ww  .j a v a2s .  c  o  m
* @param key the key to fetch
* @param defaultValue return defaultValue on props is null or the key doesn't exsits in props.
* @return the resolved value
* 
* @see #PLACEHOLDER_PREFIX
* @see #PLACEHOLDER_SUFFIX
*/
public static String getTrimPlaceholdersString(Properties props, String key, String defaultValue) {
    if (props == null) {
        return defaultValue;
    }
    String text = props.getProperty(key);
    if (text == null) {
        return defaultValue;
    }

    StringBuffer buf = new StringBuffer(text);

    // The following code does not use JDK 1.4's StringBuffer.indexOf(String)
    // method to retain JDK 1.3 compatibility. The slight loss in performance
    // is not really relevant, as this code will typically just run on startup.

    int startIndex = text.indexOf(PLACEHOLDER_PREFIX);
    while (startIndex != -1) {
        int endIndex = buf.toString().indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length());
        if (endIndex != -1) {
            String placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
            String propVal = props.getProperty(placeholder);
            if (propVal != null) {
                buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal);
                startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX, startIndex + propVal.length());
            } else {
                log.warn("Could not resolve placeholder '" + placeholder + "' in [" + text + "]");
                startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX, endIndex + PLACEHOLDER_SUFFIX.length());
            }
        } else {
            startIndex = -1;
        }
    }

    return buf.toString().trim();
}

From source file:org.talend.mdm.webapp.base.server.ForeignKeyHelper.java

public static String unwrapKeyValueToString(String value, String symbol) {
    if (value.startsWith("[") && value.endsWith("]")) { //$NON-NLS-1$ //$NON-NLS-2$
        StringBuffer sb = new StringBuffer();
        if (value.contains("][")) { //$NON-NLS-1$
            for (String s : value.split("]")) { //$NON-NLS-1$
                sb = sb.append(s.substring(1, s.length()) + symbol);
            }//from   ww w .j av  a2 s . co  m
            return sb.substring(0, sb.length() - symbol.length());
        } else {
            return org.talend.mdm.webapp.base.shared.util.CommonUtil.unwrapFkValue(value);
        }
    } else {
        return value;
    }
}

From source file:org.qedeq.base.utility.StringUtility.java

/**
 * Replaces all occurrences of <code>search</code> in <code>text</code>
 * by <code>replace</code>./*w  w w .ja va 2  s. c o m*/
 *
 * @param   text    Text to work on. Must not be <code>null</code>.
 * @param   search  replace this text by <code>replace</code>. Can be <code>null</code>.
 * @param   replace replacement for <code>search</code>. Can be <code>null</code>.
 * @throws  NullPointerException    <code>text</code> is <code>null</code>.
 */
public static void replace(final StringBuffer text, final String search, final String replace) {
    if (search == null || search.length() <= 0) {
        return;
    }
    final StringBuffer result = new StringBuffer(text.length() + 16);
    int pos1 = 0;
    int pos2;
    final int len = search.length();
    while (0 <= (pos2 = text.indexOf(search, pos1))) {
        result.append(text.substring(pos1, pos2));
        result.append(replace != null ? replace : "");
        pos1 = pos2 + len;
    }
    if (pos1 < text.length()) {
        result.append(text.substring(pos1));
    }
    text.setLength(0);
    text.append(result);
}

From source file:org.openmrs.module.idcards.web.controller.PrintEmptyIdcardsFormController.java

/**
 * Handles an upload of identifiers to print onto empty id cards
 *//*  w w  w  . ja  v  a 2 s  .c  om*/
@RequestMapping(method = RequestMethod.POST, value = "/module/idcards/uploadAndPrintIdCards")
public void uploadAndPrintIdCards(ModelMap model, HttpServletRequest request, HttpServletResponse response,
        @RequestParam(required = true, value = "templateId") Integer templateId,
        @RequestParam(required = true, value = "inputFile") MultipartFile inputFile,
        @RequestParam(required = true, value = "pdfPassword") String pdfPassword) throws Exception {

    // Get identifiers from file
    List<String> identifiers = new ArrayList<String>();
    BufferedReader r = null;
    try {
        r = new BufferedReader(new InputStreamReader(inputFile.getInputStream()));
        for (String s = r.readLine(); s != null; s = r.readLine()) {
            identifiers.add(s);
        }
    } finally {
        if (r != null) {
            r.close();
        }
    }

    IdcardsService is = (IdcardsService) Context.getService(IdcardsService.class);
    IdcardsTemplate card = is.getIdcardsTemplate(templateId);

    StringBuffer requestURL = request.getRequestURL();
    String baseURL = requestURL.substring(0, requestURL.indexOf("/module/idcards"));

    PrintEmptyIdcardsServlet.generateOutputForIdentifiers(card, baseURL, response, identifiers, pdfPassword);
}

From source file:org.kuali.kra.protocol.actions.ProtocolActionAjaxServiceImplBase.java

/**
 * Clip the last character from the string buffer. The last character, if there is one, is always a separator that must be
 * removed./*from  w w w  .j  a  va2  s . co  m*/
 * 
 * @param ajaxList
 * @return
 */
protected String clipLastChar(StringBuffer ajaxList) {
    if (ajaxList.length() == 0) {
        return ajaxList.toString();
    }
    return ajaxList.substring(0, ajaxList.length() - 1);
}

From source file:org.opencastproject.archive.opencast.solr.SolrIndexManager.java

static String mkString(Collection<?> as, String sep) {
    StringBuffer b = new StringBuffer();
    for (Object a : as) {
        b.append(a).append(sep);//from w  w w .java2s .  co m
    }
    return b.substring(0, b.length() - sep.length());
}

From source file:org.deegree.console.webservices.ServiceConfig.java

public String getCapabilitiesUrl() {
    OWS ows = getWorkspace().getResource(OWSProvider.class, id);
    String type = ((OWSProvider) ows.getMetadata().getProvider()).getImplementationMetadata()
            .getImplementedServiceName()[0];

    HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
            .getRequest();//from w w w. j  av  a  2  s.  co m
    StringBuffer sb = req.getRequestURL();

    // HACK HACK HACK
    int index = sb.indexOf("/console");
    return sb.substring(0, index) + "/services/" + id + "?service=" + type + "&request=GetCapabilities";
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.authenticate.BaseLoginServlet.java

/**
 * If we don't have a referrer, send them to the home page.
 *///from  w  ww. j  a  v  a2s  . c om
protected String figureHomePageUrl(HttpServletRequest req) {
    StringBuffer url = req.getRequestURL();
    String uri = req.getRequestURI();
    int authLength = url.length() - uri.length();
    String auth = url.substring(0, authLength);
    return auth + req.getContextPath();
}

From source file:org.openmrs.module.idcards.web.controller.TemplateFormController.java

/**
 * Handles the user's submission of the form.
 *///from   w  w w  .  j a v  a  2  s  .c  om
@RequestMapping(method = RequestMethod.POST, params = "action=saveAndPrintEmpty")
public void onSaveAndPrintEmpty(@ModelAttribute("template") IdcardsTemplate template,
        HttpServletRequest request, HttpServletResponse response) throws IOException {

    if (Context.isAuthenticated()) {
        IdcardsService service = (IdcardsService) Context.getService(IdcardsService.class);
        service.saveIdcardsTemplate(template);

        try {
            StringBuffer requestURL = request.getRequestURL();
            String baseURL = requestURL.substring(0, requestURL.indexOf("/module"));

            PrintEmptyIdcardsServlet.generateOutput(template, baseURL, response, Collections.nCopies(10, 0),
                    null);
        } catch (Exception e) {
            log.error("Unable to print cards", e);
            e.printStackTrace(response.getWriter());
        }
    }

}

From source file:org.openmrs.module.idcards.web.controller.TemplateFormController.java

/**
 * Handles the user's submission of the form.
 *//*  w  w w .  j a va2  s . co  m*/
@RequestMapping(method = RequestMethod.POST, params = "action=saveAndReprint")
public void onSaveAndReprint(@ModelAttribute("template") IdcardsTemplate template, HttpServletRequest request,
        HttpServletResponse response) throws IOException {

    if (Context.isAuthenticated()) {
        IdcardsService service = (IdcardsService) Context.getService(IdcardsService.class);
        service.saveIdcardsTemplate(template);

        Cohort cohort = new Cohort();
        cohort.addMember(1);
        cohort.addMember(2);

        try {
            StringBuffer requestURL = request.getRequestURL();
            String baseURL = requestURL.substring(0, requestURL.indexOf("/module"));

            PrintIdcardsServlet.generateOutput(template, baseURL, cohort, response);
        } catch (Exception e) {
            log.error("Unable to print cards", e);
            e.printStackTrace(response.getWriter());
        }
    }

}