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

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

Introduction

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

Prototype

public static String leftPad(String str, int size, String padStr) 

Source Link

Document

Left pad a String with a specified String.

Usage

From source file:org.projectforge.common.StringHelper.java

/**
 * 0 -> "000", 1 -> "001", ..., 9 -> "009", 10 -> "010", 100 -> "0100", 1000 -> "1000" etc. Uses
 * StringUtils.leftPad(str, 2, '0');/*from   www. jav  a 2s .  co m*/
 * @param value
 * @return
 * @see StringUtils#leftPad(String, int, char)
 */
public static String format3DigitNumber(final int value) {
    return StringUtils.leftPad(String.valueOf(value), 3, '0');
}

From source file:org.projectforge.web.wicket.converter.IntegerConverter.java

@Override
public String convertToString(final Integer value, final Locale locale) {
    if (value == null) {
        return "";
    }//from   ww  w .j av a 2s.  c o  m
    return StringUtils.leftPad(value.toString(), digits, '0');
}

From source file:org.qxsched.doc.afp.AfpStructuredFieldDefinitions.java

/**
 * Returns the supplied integer as hexadecimal string. The string starts
 * with <code>"0x"</code>. The hexadecimal string is padded with
 * <code>'0'</code> characters to get to the required width.
 * //w w w .j a v  a  2  s .c o  m
 * @param val
 *            the integer value.
 * @param width
 *            the qequired width, without considering the leading
 *            <code>"0x"</code>.
 * @return the supplied integer as hexadecimal string.
 */
public static String hexString(Integer val, int width) {
    return "0x" + StringUtils.leftPad(Integer.toString(val, 16).toUpperCase(), width, '0');
}

From source file:org.qxsched.doc.afp.GenericAfpRecord.java

public void write(BufferedWriter out, AfpReadWriteProperties props, int level)
        throws IOException, AfpException {

    // Make prefix
    StringBuffer prefixSB = new StringBuffer();
    for (int i = 0; i < level; i++) {
        prefixSB.append(" ");
    }//from   w ww .j  av  a  2 s  .  co  m
    String prefix = prefixSB.toString();

    // Write identifier
    String abbrev = afpDefs.getAbbreviation(identifier);
    String desc = afpDefs.getDescription(identifier);

    out.write(prefix);
    if (abbrev == null) {
        out.write(StringUtils.leftPad(getSFIdentifierString().toUpperCase(), 6, '0'));
        out.write(": ");
    } else {
        out.write(abbrev);
        out.write(": ");
        out.write(desc);
        out.newLine();
        out.write(prefix);
        out.write("  ");
    }

    // Write length
    out.write("length:");
    if (mustWriteMD5(props)) {
        out.write("????");
    } else {
        out.write(StringUtils.leftPad(AfpStructuredFieldDefinitions.hexString(length, 4), 4, '0'));
    }

    // Write flags
    out.write(" flags:");
    out.write(StringUtils.leftPad(AfpStructuredFieldDefinitions.hexString(flags, 2), 2, '0'));

    // Write reserved
    out.write(" reserved:");
    out.write(StringUtils.leftPad(AfpStructuredFieldDefinitions.hexString(reserved, 4), 4, '0'));

    // Write data
    out.newLine();
    writeData(out, props, prefix);
}

From source file:org.qxsched.doc.afp.GenericAfpTriplet.java

public void write(BufferedWriter out, AfpReadWriteProperties props, String prefix)
        throws IOException, AfpException {

    // Get data//ww w  .  jav a  2  s .  co m
    byte[] data = getData();

    // Loop
    for (int i = 0; i < data.length;) {

        int iStart = i;
        out.write(prefix);
        out.write("  ");
        if (i == 0) {
            out.write("TRP ");
        } else {
            out.write("    ");
        }

        for (int j = i; j < data.length && j < iStart + 16; j++) {
            i++;

            int val = data[j] & 0xff;
            out.write(StringUtils.leftPad(Integer.toString(val, 16).toLowerCase(), 2, '0'));
            out.write(" ");

        }
        out.newLine();
    }
}

From source file:org.qxsched.doc.afp.impl.AfpClasses.java

private void init() throws AfpException {

    // Get definitions
    AfpStructuredFieldDefinitions defs = AfpStructuredFieldDefinitions.instance();

    // Get class loader
    ClassLoader ldr = Thread.currentThread().getContextClassLoader();

    // Get package directory
    Package pkg = AfpClasses.class.getPackage();
    String recClassBaseName = AfpRecord.class.getName().replaceFirst("^.*\\.", "");
    String recClassBase = pkg.getName() + "." + recClassBaseName;

    // Iterate through all abbreviations and try and find classes
    Set<String> abbrevs = defs.getAbbrev();
    for (String abbrev : abbrevs) {

        // Make class name
        String recordClassName = recClassBase + abbrev;
        if (LOG.isTraceEnabled()) {
            LOG.trace("AFP record class name: " + recordClassName);
        }//from  w w  w .ja  v  a  2  s.  com

        // Load class
        Class<?> recordClass = null;
        try {
            recordClass = ldr.loadClass(recordClassName);
        } catch (ClassNotFoundException e) {
        }

        // Ignore null
        if (recordClass == null) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Ignoring unfound class: " + recordClassName);
            }
            continue;
        }

        // Ignore non AfpRecords
        if (!AfpRecord.class.isAssignableFrom(recordClass)) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Ignoring non AfpRecord: " + recordClassName);
            }
            continue;
        }

        // Map
        Integer code = defs.getCode(abbrev);
        recordCode2class.put(code, (Class<AfpRecord>) recordClass);
        recordAbbrev2class.put(abbrev, (Class<AfpRecord>) recordClass);
    }

    // Try finding al triplet classes this package implements
    String tripClassBaseName = AfpTriplet.class.getName().replaceFirst("^.*\\.", "");
    String tripClassBase = pkg.getName() + "." + tripClassBaseName;
    for (int i = 0; i < 0x100; i++) {

        // Make class name
        String abbrev = StringUtils.leftPad(Integer.toString(i, 16).toUpperCase(), 2, '0');
        String tripClassName = tripClassBase + abbrev;
        if (LOG.isTraceEnabled()) {
            LOG.trace("AFP triplet class name: " + tripClassName);
        }

        // Load class
        Class<?> tripClass = null;
        try {
            tripClass = ldr.loadClass(tripClassName);
        } catch (ClassNotFoundException e) {
        }

        // Ignore null
        if (tripClass == null) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Ignoring unfound class: " + tripClassName);
            }
            continue;
        }

        // Ignore non AfpTriplet
        if (!AfpTriplet.class.isAssignableFrom(tripClass)) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Ignoring non AfpTriplet: " + tripClassName);
            }
            continue;
        }

        // Map
        if (LOG.isTraceEnabled()) {
            LOG.trace("Found AfpTriplet: " + tripClassName);
        }
        tripletCode2class.put(i, (Class<AfpTriplet>) tripClass);
    }
}

From source file:org.qxsched.doc.afp.util.AfpDump.java

public static void dumpData(BufferedWriter out, AfpReadWriteProperties props, String label, String prefix,
        byte[] data) throws IOException {

    // Loop/*  www  . jav a  2 s .  c  o  m*/
    for (int i = 0; i < data.length;) {

        int iStart = i;
        out.write(prefix);
        out.write("    ");
        out.write(label);

        for (int j = i; j < data.length && j < iStart + 16; j++) {
            i++;

            int val = data[j] & 0xff;
            out.write(StringUtils.leftPad(Integer.toString(val, 16).toLowerCase(), 2, '0'));
            out.write(" ");

        }
        out.newLine();
    }
}

From source file:org.qxsched.doc.afp.util.AfpDump.java

public static void dumpData(StringBuffer sb, AfpReadWriteProperties props, String label, String prefix,
        byte[] data, boolean doLastNl) {

    // Loop// ww  w  .  j  a  v  a  2s  . c  o m
    for (int i = 0; i < data.length;) {

        int iStart = i;
        sb.append(prefix);
        sb.append("    ");
        sb.append(label);

        for (int j = i; j < data.length && j < iStart + 16; j++) {
            i++;

            int val = data[j] & 0xff;
            sb.append(StringUtils.leftPad(Integer.toString(val, 16).toLowerCase(), 2, '0'));
            sb.append(" ");

        }
        if (doLastNl) {
            sb.append(System.getProperty("line.separator"));
        }
    }
}

From source file:org.rapidcontext.util.StringUtil.java

/**
 * Returns a 4 digit hexadecimal representation of a character.
 *
 * @param chr            the character to convert
 *
 * @return the hexadecimal character code
 */// ww  w.  j av  a2s. c  o m
public static String toHex(char chr) {
    int high = (chr & 0xFF00) >>> 16;
    int low = chr & 0xFF;
    return StringUtils.leftPad(Integer.toHexString(high), 2, '0')
            + StringUtils.leftPad(Integer.toHexString(low), 2, '0');
}

From source file:org.sakaiproject.nakamura.api.activity.ActivityUtils.java

/**
 * @return Creates a unique path to an activity in the form of 2010-01-21-09-randombit
 *///from  w ww.j a v a2  s .  co m
public static String createId() {
    Calendar c = Calendar.getInstance();

    String[] vals = new String[4];
    vals[0] = "" + c.get(Calendar.YEAR);
    vals[1] = StringUtils.leftPad("" + (c.get(Calendar.MONTH) + 1), 2, "0");
    vals[2] = StringUtils.leftPad("" + c.get(Calendar.DAY_OF_MONTH), 2, "0");
    vals[3] = StringUtils.leftPad("" + c.get(Calendar.HOUR_OF_DAY), 2, "0");

    StringBuilder id = new StringBuilder();

    for (String v : vals) {
        id.append(v).append("-");
    }

    byte[] bytes = new byte[20];
    String randomHash = "";
    try {
        if (random == null) {
            random = SecureRandom.getInstance("SHA1PRNG");
        }
        random.nextBytes(bytes);
        randomHash = Arrays.toString(bytes);
        randomHash = org.sakaiproject.nakamura.util.StringUtils.sha1Hash(randomHash);
    } catch (NoSuchAlgorithmException e) {
    } catch (UnsupportedEncodingException e) {
    }

    id.append(randomHash);
    return id.toString();
}