Example usage for java.util Formatter format

List of usage examples for java.util Formatter format

Introduction

In this page you can find the example usage for java.util Formatter format.

Prototype

public Formatter format(String format, Object... args) 

Source Link

Document

Writes a formatted string to this object's destination using the specified format string and arguments.

Usage

From source file:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.CFLibXmlUtil.java

public static String formatXmlString(String str) {
    final String S_ProcName = "formatXmlString";
    if (str == null) {
        throw CFLib.getDefaultExceptionFactory().newNullArgumentException(CFLibXmlUtil.class, S_ProcName, 1,
                "str");
    }/*from  w  ww  .  j  a v  a 2  s.co m*/
    StringBuffer buff = new StringBuffer();
    char ch;
    int idx;
    int len = str.length();
    for (idx = 0; idx < len; idx++) {
        ch = str.charAt(idx);
        if (Character.isWhitespace(ch)) {
            buff.append(ch);
        } else {
            switch (ch) {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
            case 'a':
            case 'b':
            case 'c':
            case 'd':
            case 'e':
            case 'f':
            case 'g':
            case 'h':
            case 'i':
            case 'j':
            case 'k':
            case 'l':
            case 'm':
            case 'n':
            case 'o':
            case 'p':
            case 'q':
            case 'r':
            case 's':
            case 't':
            case 'u':
            case 'v':
            case 'w':
            case 'x':
            case 'y':
            case 'z':
            case 'A':
            case 'B':
            case 'C':
            case 'D':
            case 'E':
            case 'F':
            case 'G':
            case 'H':
            case 'I':
            case 'J':
            case 'K':
            case 'L':
            case 'M':
            case 'N':
            case 'O':
            case 'P':
            case 'Q':
            case 'R':
            case 'S':
            case 'T':
            case 'U':
            case 'V':
            case 'W':
            case 'X':
            case 'Y':
            case 'Z':
            case '~':
            case '!':
            case '#':
            case '$':
            case '%':
            case '^':
            case '*':
            case '(':
            case ')':
            case '-':
            case '_':
            case '+':
            case '=':
            case '{':
            case '}':
            case '[':
            case ']':
            case ':':
            case ';':
            case ',':
            case '.':
            case '?':
            case '/':
            case '\\':
            case '|':
                buff.append(ch);
                break;
            case '\'':
                buff.append("&apos;");
                break;
            case '"':
                buff.append("&quot;");
                break;
            case '&':
                buff.append("&amp;");
                break;
            case '<':
                buff.append("&lt;");
                break;
            case '>':
                buff.append("&gt;");
                break;
            default:
                if (!Character.isValidCodePoint(ch)) {
                    throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(CFLibXmlUtil.class,
                            S_ProcName, "Only valid code points can be formatted, ch is out of range");
                }
                StringBuffer fmtbuff = new StringBuffer();
                fmtbuff.append("&#");
                Formatter fmt = new Formatter(fmtbuff);
                fmt.format("%1$d", ch);
                fmtbuff.append(";");
                String tmp = fmtbuff.toString();
                buff.append(tmp);
                fmt.close();
                break;
            }
        }
    }
    String retval = buff.toString();
    return (retval);
}

From source file:com.itemanalysis.psychometrics.scaling.PercentileRank.java

@Override
public String toString() {
    if (freqTable.getUniqueCount() == 0 || freqTable.getSumFreq() == 0) {
        return "Percentile ranks not computed.";
    }/*w  w  w .j  a  v a2 s  .c o  m*/
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);
    String f1 = "%10.4f";
    String f2 = "%10s";

    //        double[][] prank = this.evaluate();
    this.createLookupTable();

    //add header
    f.format("%28s", "SCORE TABLE");
    f.format("%n");
    f.format("%45s", "=============================================");
    f.format("%n");
    f.format(f2, "Original");
    f.format("%5s", "");
    f.format(f2, "Percentile");
    f.format("%n");

    f.format(f2, "Value");
    f.format("%5s", "");
    f.format(f2, "Rank");
    f.format("%n");
    f.format("%45s", "---------------------------------------------");
    f.format("%n");

    Iterator<Integer> iter = prankTable.keySet().iterator();
    Integer key = 1;
    while (iter.hasNext()) {
        key = iter.next();
        f1 = "%10." + precision + "f";

        f.format(f2, key);
        f.format("%5s", "");
        f.format(f1, prankTable.get(key));
        f.format("%5s", "");
        f.format("%n");
    }
    f.format("%45s", "=============================================");
    f.format("%n");
    return f.toString();
}

From source file:com.imellon.android.vatchecker.VatCheckerActivity.java

/**
 * Request info./* w w  w  .  j  a v a  2 s .  co m*/
 *
 * @param keyphrase the keyphrase
 */
public void requestInfo(String keyphrase) {
    HttpPost httppost = new HttpPost("https://www1.gsis.gr/wsgsis/RgWsBasStoixN/RgWsBasStoixNSoapHttpPort");

    try {
        InputStream inputStream = getResources().openRawResource(R.raw.gsisrequesttemplate);
        String envelope = convertInputStreamToString(inputStream);
        Formatter formatter = new Formatter();
        formatter.format(envelope, keyphrase);
        envelope = formatter.toString();

        StringEntity se = new StringEntity(envelope, HTTP.UTF_8);

        se.setContentType("text/xml");
        httppost.setEntity(se);

        HttpEntity entity = null;
        HttpResponse response = sClient.execute(httppost);
        InputStream in = null;

        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            entity = response.getEntity();
            in = entity.getContent();
        }

        String retStr = convertInputStreamToString(in);

        AndroidXMLParser p = new AndroidXMLParser(retStr);
        message = p.parse();
        updateViews();

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:net.sourceforge.msscodefactory.v1_10.MSSBamPg8.MSSBamPg8Schema.java

public static String getDateString(Calendar val) {
    if (val == null) {
        return ("null");
    } else {/*from w w w .  j a  v  a 2s .co m*/
        StringBuffer buff = new StringBuffer("'");
        Formatter fmt = new Formatter(buff);
        fmt.format("%1$04d", val.get(Calendar.YEAR));
        buff.append("-");
        fmt.format("%1$02d", val.get(Calendar.MONTH) + 1);
        buff.append("-");
        fmt.format("%1$02d", val.get(Calendar.DAY_OF_MONTH));
        buff.append("'");
        return (buff.toString());
    }
}

From source file:net.sourceforge.msscodefactory.v1_10.MSSBamPg8.MSSBamPg8Schema.java

public static String getTimeString(Calendar val) {
    if (val == null) {
        return ("null");
    } else {/*from  www.  j a v  a 2s  .co  m*/
        StringBuffer buff = new StringBuffer("'");
        Formatter fmt = new Formatter(buff);
        fmt.format("%1$02d", val.get(Calendar.HOUR_OF_DAY));
        buff.append("-");
        fmt.format("%1$02d", val.get(Calendar.MINUTE));
        buff.append("-");
        fmt.format("%1$02d", val.get(Calendar.SECOND));
        buff.append("'");
        return (buff.toString());
    }
}

From source file:net.sourceforge.msscodefactory.v1_10.MSSBamPg8.MSSBamPg8Schema.java

public static String getTZDateString(Calendar val) {
    if (val == null) {
        return ("null");
    } else {//from  ww w .  j  av  a2s .co m
        StringBuffer buff = new StringBuffer("'");
        Formatter fmt = new Formatter(buff);
        fmt.format("%1$04d", val.get(Calendar.YEAR));
        buff.append("-");
        fmt.format("%1$02d", val.get(Calendar.MONTH) + 1);
        buff.append("-");
        fmt.format("%1$02d", val.get(Calendar.DAY_OF_MONTH));
        buff.append(" CST");
        buff.append("'");
        return (buff.toString());
    }
}

From source file:net.sourceforge.msscodefactory.v1_10.MSSBamPg8.MSSBamPg8Schema.java

public static String getTZTimeString(Calendar val) {
    if (val == null) {
        return ("null");
    } else {//w  w  w. j a v  a2 s  .co  m
        StringBuffer buff = new StringBuffer("'");
        Formatter fmt = new Formatter(buff);
        fmt.format("%1$02d", val.get(Calendar.HOUR_OF_DAY));
        buff.append("-");
        fmt.format("%1$02d", val.get(Calendar.MINUTE));
        buff.append("-");
        fmt.format("%1$02d", val.get(Calendar.SECOND));
        buff.append(" CST");
        buff.append("'");
        return (buff.toString());
    }
}

From source file:net.sourceforge.msscodefactory.v1_10.MSSBamPg8.MSSBamPg8Schema.java

public static String getTimestampString(Calendar val) {
    if (val == null) {
        return ("null");
    } else {//  w ww . j  a va  2  s  .co  m
        StringBuffer buff = new StringBuffer("'");
        Formatter fmt = new Formatter(buff);
        fmt.format("%1$04d", val.get(Calendar.YEAR));
        buff.append("-");
        fmt.format("%1$02d", val.get(Calendar.MONTH) + 1);
        buff.append("-");
        fmt.format("%1$02d", val.get(Calendar.DAY_OF_MONTH));
        buff.append(" ");
        fmt.format("%1$02d", val.get(Calendar.HOUR_OF_DAY));
        buff.append("-");
        fmt.format("%1$02d", val.get(Calendar.MINUTE));
        buff.append("-");
        fmt.format("%1$02d", val.get(Calendar.SECOND));
        buff.append("'");
        return (buff.toString());
    }
}

From source file:net.sourceforge.msscodefactory.v1_10.MSSBamPg8.MSSBamPg8Schema.java

public static String getTZTimestampString(Calendar val) {
    if (val == null) {
        return ("null");
    } else {/* w w w  . j  a  v a 2s  .  c om*/
        StringBuffer buff = new StringBuffer("'");
        Formatter fmt = new Formatter(buff);
        fmt.format("%1$04d", val.get(Calendar.YEAR));
        buff.append("-");
        fmt.format("%1$02d", val.get(Calendar.MONTH) + 1);
        buff.append("-");
        fmt.format("%1$02d", val.get(Calendar.DAY_OF_MONTH));
        buff.append(" ");
        fmt.format("%1$02d", val.get(Calendar.HOUR_OF_DAY));
        buff.append("-");
        fmt.format("%1$02d", val.get(Calendar.MINUTE));
        buff.append("-");
        fmt.format("%1$02d", val.get(Calendar.SECOND));
        buff.append(" CST");
        buff.append("'");
        return (buff.toString());
    }
}

From source file:org.owasp.appsensor.block.proxy.servlet.ProxyServlet.java

/**
 * Encodes characters in the query or fragment part of the URI.
 *
 * <p>Unfortunately, an incoming URI sometimes has characters disallowed by the spec.  HttpClient
 * insists that the outgoing proxied request has a valid URI because it uses Java's {@link URI}.
 * To be more forgiving, we must escape the problematic characters.  See the URI class for the
 * spec./*from   ww  w . j av  a  2 s .c om*/
 *
 * @param in example: name=value&foo=bar#fragment
 */
@SuppressWarnings("resource")
protected static CharSequence encodeUriQuery(CharSequence in) {
    //Note that I can't simply use URI.java to encode because it will escape pre-existing escaped things.
    StringBuilder outBuf = null;
    Formatter formatter = null;
    for (int i = 0; i < in.length(); i++) {
        char c = in.charAt(i);
        boolean escape = true;
        if (c < 128) {
            if (asciiQueryChars.get((int) c)) {
                escape = false;
            }
        } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {//not-ascii
            escape = false;
        }
        if (!escape) {
            if (outBuf != null)
                outBuf.append(c);
        } else {
            //escape
            if (outBuf == null) {
                outBuf = new StringBuilder(in.length() + 5 * 3);
                outBuf.append(in, 0, i);
                formatter = new Formatter(outBuf);
            }
            //leading %, 0 padded, width 2, capital hex
            formatter.format("%%%02X", (int) c);//TODO
        }
    }
    return outBuf != null ? outBuf : in;
}