Example usage for java.lang StringBuffer insert

List of usage examples for java.lang StringBuffer insert

Introduction

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

Prototype

@Override
public StringBuffer insert(int offset, double d) 

Source Link

Usage

From source file:org.j2free.util.ServletUtils.java

/**
 * Redirects the user to the current url over SSL
 *
 * @param request a HttpServletRequest/*w ww .ja v a 2 s  . co  m*/
 * @param response a HttpServletResponse
 * @param sslPort the port SSL requests should be forwarded to
 * @throws ServletException
 * @throws IOException
 */
public static void redirectOverSSL(HttpServletRequest request, HttpServletResponse response, int sslPort)
        throws ServletException, IOException {

    StringBuffer url = request.getRequestURL();

    // Make sure we're on https
    if (url.charAt(4) != 's')
        url.insert(4, 's');

    // If there is a ssl port, make sure we're on it,
    // otherwise assume we're already on the right port
    if (sslPort > 0) {
        int portStart = url.indexOf(":", 8) + 1;
        int portEnd = url.indexOf("/", 8);

        if (portEnd == -1) // If their isn't a trailing slash, then the end is the last char
            portEnd = url.length() - 1;

        if (portStart > 0 && portStart < portEnd) { // If we detected a : before the trailing slash or end of url, delete the port
            url.delete(portStart, portEnd);
        } else {
            url.insert(portEnd, ':'); // If the url didn't have a port, add in the :
            portStart = portEnd;
        }

        url.insert(portStart, sslPort); // Insert the right port where it should be
    }

    LogFactory.getLog(ServletUtils.class).debug("redirectOverSSL sending 301: " + url.toString());
    sendPermanentRedirect(response, url.toString());
}

From source file:org.j2free.util.ServletUtils.java

/**
 * Redirects the user to the provided url over SSL
 *
 * @param request a HttpServletRequest/*from   ww  w  . ja v a2s  .  c o  m*/
 * @param response a HttpServletResponse
 * @param urlStr 
 * @param sslPort the port SSL requests should be forwarded to
 * @throws ServletException
 * @throws IOException
 */
public static void redirectOverSSL(HttpServletRequest request, HttpServletResponse response, String urlStr,
        int sslPort) throws ServletException, IOException {

    StringBuffer url = new StringBuffer(urlStr);

    // Make sure we're on https
    if (url.charAt(4) != 's')
        url.insert(4, 's');

    // If there is a ssl port, make sure we're on it,
    // otherwise assume we're already on the right port
    if (sslPort > 0) {
        int portStart = url.indexOf(":", 8) + 1;
        int portEnd = url.indexOf("/", 8);

        if (portEnd == -1) // If their isn't a trailing slash, then the end is the last char
            portEnd = url.length() - 1;

        if (portStart > 0 && portStart < portEnd) { // If we detected a : before the trailing slash or end of url, delete the port
            url.delete(portStart, portEnd);
        } else {
            url.insert(portEnd, ':'); // If the url didn't have a port, add in the :
            portStart = portEnd;
        }

        url.insert(portStart, sslPort); // Insert the right port where it should be
    }

    LogFactory.getLog(ServletUtils.class).debug("redirectOverSSL sending 301: " + url.toString());
    sendPermanentRedirect(response, url.toString());
}

From source file:corner.orm.hibernate.impl.PaginatedEntityService.java

License:asdf

public Iterator find(final Class<?> persistClass, final Object conditions, final String order, final int start,
        final int offset) {
    return (Iterator) this.template.execute(new HibernateCallback() {
        /**// w w w .  j av  a  2  s.  com
         * @see org.springframework.orm.hibernate3.HibernateCallback#doInHibernate(org.hibernate.Session)
         */
        @Override
        public Object doInHibernate(final Session session) throws HibernateException, SQLException {
            Iterable con = typeCoercer.coerce(conditions, Iterable.class);
            final Iterator it = con == null ? null : con.iterator();
            final StringBuffer sb = buildConditionHQL(persistClass, it);
            appendOrder(sb, order);
            sb.insert(0, SELECT_ID_CLAUSE);
            Query query = session.createQuery(sb.toString());
            if (it != null) {
                int i = 0;
                while (it.hasNext()) {
                    query.setParameter(i++, it.next());
                }
            }
            query.setFirstResult(start);
            query.setMaxResults(offset);

            ResultTransformer transformer = new LazyLoadEntityTransformer(session, persistClass);
            query.setResultTransformer(transformer);
            List list = query.list();
            return list.iterator();
        }
    });
}

From source file:corner.orm.gae.impl.PaginatedJapEntityService.java

License:asdf

/**
* 
* magic paginate method.//from www  .jav a 2 s . co  m
* eg:
* <code>
*  options.setPage(2);
*
*
*  paginate(Member.class,new Object[]{"email=?","asdf@asdf.net"},"userName desc",options)
        
*  paginate(Member.class,"email='asdf@asdf.net'","userName desc",options)
*
*  List conditions = new ArrayList();
*  conditions.add("userName=? and password=?");
*  conditions.add(userName);
*  conditions.add(password);
*  paginate(Member.class,conditions,"userName desc",options)
* 
* </code>
* Magic conditions query criteria
* @param persistClass persistence class
* @param conditions query criteria
* @param order order by sql
* @param options pagination options.
* @return include result and totalRecord.
*/
public PaginationList paginate(final Class<?> persistClass, final Object conditions, final String order,
        final PaginationOptions options) {
    final Iterable con = typeCoercer.coerce(conditions, Iterable.class);

    return (PaginationList) this.template.execute(new JpaCallback() {
        @Override
        public Object doInJpa(EntityManager entityManager) throws PersistenceException {

            final Iterator it = con == null ? null : con.iterator();

            String conditionJPQL = buildConditionJPQL(persistClass, it).toString();

            //query list
            final StringBuffer queryJPQL = new StringBuffer(conditionJPQL);
            appendOrder(queryJPQL, order);
            queryJPQL.insert(0, "select root." + EntityConstants.ID_PROPERTY_NAME);
            Query query = entityManager.createQuery(queryJPQL.toString());

            //count query
            final StringBuffer countJPQL = new StringBuffer(conditionJPQL);
            countJPQL.insert(0, "select count(root) ");
            Query countQuery = entityManager.createQuery(countJPQL.toString());

            if (it != null) {
                int i = 0;
                while (it.hasNext()) {
                    i++;
                    Object obj = it.next();
                    query.setParameter(String.valueOf(i), obj);
                    countQuery.setParameter(String.valueOf(i), obj);
                }
            }
            //get perpage
            int perPage = options.getPerPage();
            int page = options.getPage();
            if (page < 1) {
                page = 1;
            }
            query.setFirstResult((page - 1) * perPage);
            query.setMaxResults(perPage);
            PaginationList list = new PaginationList(query.getResultList().iterator(), options);

            //query total record number
            //beacause jpa rowCount is integer type.so convert as long
            options.setTotalRecord(Long.parseLong(countQuery.getSingleResult().toString()));
            return list;
        }
    });
}

From source file:org.xwiki.rendering.internal.renderer.xwiki20.XWikiSyntaxEscapeHandler.java

private void escapeURI(StringBuffer accumulatedBuffer, String match) {
    int pos = accumulatedBuffer.indexOf(match);
    if (pos > -1) {
        // Escape the ":" symbol
        accumulatedBuffer.insert(pos + match.length() - 1, '~');
    }/*  w  ww .ja  v a2  s.  com*/
}

From source file:com.exilant.eGov.src.reports.GeneralLedgerReport.java

public static StringBuffer numberToString(final String strNumberToConvert) {
    String strNumber = "", signBit = "";
    if (strNumberToConvert.startsWith("-")) {
        strNumber = "" + strNumberToConvert.substring(1, strNumberToConvert.length());
        signBit = "-";
    } else/*from   www .  j a v a 2s  . com*/
        strNumber = "" + strNumberToConvert;
    final DecimalFormat dft = new DecimalFormat("##############0.00");
    final String strtemp = "" + dft.format(Double.parseDouble(strNumber));
    StringBuffer strbNumber = new StringBuffer(strtemp);
    final int intLen = strbNumber.length();

    for (int i = intLen - 6; i > 0; i = i - 2)
        strbNumber.insert(i, ',');
    if (signBit.equals("-"))
        strbNumber = strbNumber.insert(0, "-");
    return strbNumber;
}

From source file:gov.medicaid.screening.dao.impl.NurseAnesthetistsLicenseDAOBean.java

/**
 * Parses the full name into a User object.
 *
 * @param fullName the full name displayed on the site
 * @return the parsed name/* www  .ja  v  a  2  s  .  c  o m*/
 */
private User parseName(String fullName) {
    fullName = fullName.substring(0, fullName.indexOf(",")); // remove certificate title
    User user = new User();
    Stack<String> nameParts = new Stack<String>();
    for (String string : fullName.split(" ")) {
        nameParts.push(string);
    }
    user.setLastName(nameParts.pop());
    if (nameParts.size() > 1) {
        user.setMiddleName(nameParts.pop());
    }
    StringBuffer sb = new StringBuffer();
    while (!nameParts.isEmpty()) {
        sb.insert(0, nameParts.pop() + " ");
    }
    user.setFirstName(sb.toString().trim());
    return user;
}

From source file:com.netfinworks.rest.convert.MultiPartFormDataParamConvert.java

private <T> Object getPropertyValue(T pojo, Class<?> clazz, String propertyName, String prefix)
        throws NoSuchMethodException {
    StringBuffer buf = new StringBuffer(propertyName);
    int char0 = buf.charAt(0);
    buf.setCharAt(0, (char) (char0 >= 97 ? char0 - 32 : char0));
    buf.insert(0, prefix);
    Method m = null;/*  w ww .  j a va 2s .c  om*/
    try {
        m = clazz.getMethod(buf.toString(), emptyClazzArray);
        return m.invoke(pojo, new Object[0]);
    } catch (SecurityException e) {
        logger.debug("property can't be access! ");
        return null;
    } catch (NoSuchMethodException e) {
        logger.debug("property '{}' doesn't exist! ", buf.toString());
        throw e;
    } catch (IllegalArgumentException e) {
        logger.debug("method can't be invoke! wrong argument. " + e.toString());
        return null;
    } catch (IllegalAccessException e) {
        logger.debug("method can't be invoke! access exception. " + e.toString());
        return null;
    } catch (InvocationTargetException e) {
        logger.debug("method can't be invoke! invocation target. " + e.toString());
        return null;
    }

}

From source file:corner.orm.hibernate.impl.PaginatedEntityService.java

License:asdf

/**
 * //w  ww. j a v a2  s . co m
 * magic paginate method.
 * eg:
 * <code>
 *  options.setPage(2);
 *
 *
 *  paginate(Member.class,new Object[]{"email=?","asdf@asdf.net"},"userName desc",options)
        
 *  paginate(Member.class,"email='asdf@asdf.net'","userName desc",options)
 *
 *  List conditions = new ArrayList();
 *  conditions.add("userName=? and password=?");
 *  conditions.add(userName);
 *  conditions.add(password);
 *  paginate(Member.class,conditions,"userName desc",options)
 * 
 * </code>
 * Magic conditions query criteria
 * @param persistClass persistence class
 * @param conditions query criteria
 * @param order order by sql
 * @param options pagination options.
 * @return include result and totalRecord.
 */
public PaginationList paginate(final Class<?> persistClass, final Object conditions, final String order,
        final PaginationOptions options) {
    final Iterable con = typeCoercer.coerce(conditions, Iterable.class);

    return (PaginationList) this.template.execute(new HibernateCallback() {
        /**
         * @see org.springframework.orm.hibernate3.HibernateCallback#doInHibernate(org.hibernate.Session)
         */
        @Override
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            final Iterator it = con == null ? null : con.iterator();

            String conditionHQL = buildConditionHQL(persistClass, it).toString();

            //query list
            final StringBuffer queryHQL = new StringBuffer(conditionHQL);
            appendOrder(queryHQL, order);
            queryHQL.insert(0, SELECT_ID_CLAUSE);

            Query query = session.createQuery(queryHQL.toString());

            //count query
            final StringBuffer countHQL = new StringBuffer(conditionHQL);
            countHQL.insert(0, "select count(*) ");
            Query countQuery = session.createQuery(countHQL.toString());

            if (it != null) {
                int i = 0;
                while (it.hasNext()) {
                    Object obj = it.next();
                    query.setParameter(i, obj);
                    countQuery.setParameter(i, obj);
                    i++;
                }
            }
            //get perpage
            int perPage = options.getPerPage();
            int page = options.getPage();
            if (page < 1) {
                page = 1;
            }
            query.setFirstResult((page - 1) * perPage);
            query.setMaxResults(perPage);
            //query total record number
            options.setTotalRecord((Long) countQuery.iterate().next());

            ResultTransformer transformer = new LazyLoadEntityTransformer(session, persistClass);
            query.setResultTransformer(transformer);

            PaginationList list = new PaginationList(query.list().iterator(), options);

            return list;

        }
    });
}

From source file:javax.faces.webapp.UIComponentTag.java

/** Generate diagnostic output. */
private static void getPathToComponent(UIComponent component, StringBuffer buf) {
    if (component == null)
        return;// ww w.j  a va2 s  . c  o m

    StringBuffer intBuf = new StringBuffer();

    intBuf.append("[Class: ");
    intBuf.append(component.getClass().getName());
    if (component instanceof UIViewRoot) {
        intBuf.append(",ViewId: ");
        intBuf.append(((UIViewRoot) component).getViewId());
    } else {
        intBuf.append(",Id: ");
        intBuf.append(component.getId());
    }
    intBuf.append("]");

    buf.insert(0, intBuf);

    getPathToComponent(component.getParent(), buf);
}