Example usage for java.io PrintWriter format

List of usage examples for java.io PrintWriter format

Introduction

In this page you can find the example usage for java.io PrintWriter format.

Prototype

public PrintWriter format(Locale l, String format, Object... args) 

Source Link

Document

Writes a formatted string to this writer using the specified format string and arguments.

Usage

From source file:com.sun.socialsite.web.rest.servlets.GadgetizerDataServlet.java

/**
 * <p>/*from   ww  w.ja v  a2  s  .com*/
 *  Override GET to retrieve gadgetizer data based on the specified request.
 *  The data is returned in JSON format.
 * </p>
 * <p>
 *  Required Request Parameters:
 *  <ul>
 *   <li>context: the context (such as viewerId/ownerId) in which the client is operating</li>
 *   <li>items: the actual items (gadgets) which will the client expects to eventually render</li>
 *  </ul>
 * </p>
 *
 * @param request the servlet request object.
 * @param response the servlet response object.
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    try {

        String callback = request.getParameter("callback");
        URL referer = ((request.getHeader("Referer") != null) ? new URL(request.getHeader("Referer")) : null);
        ConsumerContext context = new ConsumerContext(referer, new JSONObject(request.getParameter("context")));
        contextHandler.handleContext(context);

        SocialSiteToken containerPageToken = tokenBuilder.buildContainerPageToken(context);
        JSONObject items = new JSONObject(request.getParameter("items"));

        JSONObject json = new JSONObject();
        json.put("libs", new JSONArray(libs));
        json.put("css", new JSONArray(css));
        json.put("containerHelper", getContainerHelperJSON(containerPageToken, context, request));
        json.put("gadgets", getGadgetsJSON(containerPageToken, context, items, request));

        response.setStatus(HttpServletResponse.SC_OK);
        response.setContentType("application/x-javascript; charset=utf-8");
        PrintWriter out = response.getWriter();
        if (callback != null) {
            out.format("%s(%s)", callback, json.toString());
        } else {
            out.print(json.toString());
        }
        out.close();

    } catch (Exception e) {
        log.error("Failed to produce response", e);
        sendErrorJSON(response, e.getMessage());
    }

}

From source file:org.apache.parquet.tools.command.MergeCommand.java

@Override
public void execute(CommandLine options) throws Exception {
    // Prepare arguments
    List<String> args = options.getArgList();
    List<Path> inputFiles = getInputFiles(args.subList(0, args.size() - 1));
    Path outputFile = new Path(args.get(args.size() - 1));

    // Merge schema and extraMeta
    FileMetaData mergedMeta = mergedMetadata(inputFiles);
    PrintWriter out = new PrintWriter(Main.out, true);

    // Merge data
    ParquetFileWriter writer = new ParquetFileWriter(conf, mergedMeta.getSchema(), outputFile,
            ParquetFileWriter.Mode.CREATE);
    writer.start();// w  w  w. ja  va 2s.  c o m
    boolean tooSmallFilesMerged = false;
    for (Path input : inputFiles) {
        if (input.getFileSystem(conf).getFileStatus(input).getLen() < TOO_SMALL_FILE_THRESHOLD) {
            out.format("Warning: file %s is too small, length: %d\n", input,
                    input.getFileSystem(conf).getFileStatus(input).getLen());
            tooSmallFilesMerged = true;
        }

        writer.appendFile(HadoopInputFile.fromPath(input, conf));
    }

    if (tooSmallFilesMerged) {
        out.println("Warning: you merged too small files. "
                + "Although the size of the merged file is bigger, it STILL contains small row groups, thus you don't have the advantage of big row groups, "
                + "which usually leads to bad query performance!");
    }
    writer.end(mergedMeta.getKeyValueMetaData());
}

From source file:org.apache.jackrabbit.oak.plugins.segment.Segment.java

@Override
public String toString() {
    StringWriter string = new StringWriter();
    PrintWriter writer = new PrintWriter(string);

    int length = data.remaining();

    writer.format("Segment %s (%d bytes)%n", id, length);
    String segmentInfo = getSegmentInfo();
    if (segmentInfo != null) {
        writer.format("Info: %s%n", segmentInfo);
    }/*from w  ww . j a v  a2s  . c  o m*/
    if (id.isDataSegmentId()) {
        writer.println("--------------------------------------------------------------------------");
        int refcount = getRefCount();
        for (int refid = 0; refid < refcount; refid++) {
            writer.format("reference %02x: %s%n", refid, getRefId(refid));
        }
        int rootcount = data.getShort(ROOT_COUNT_OFFSET) & 0xffff;
        int pos = data.position() + refcount * 16;
        for (int rootid = 0; rootid < rootcount; rootid++) {
            writer.format("root %d: %s at %04x%n", rootid,
                    RecordType.values()[data.get(pos + rootid * 3) & 0xff],
                    data.getShort(pos + rootid * 3 + 1) & 0xffff);
        }
        int blobrefcount = data.getShort(BLOBREF_COUNT_OFFSET) & 0xffff;
        pos += rootcount * 3;
        for (int blobrefid = 0; blobrefid < blobrefcount; blobrefid++) {
            int offset = data.getShort(pos + blobrefid * 2) & 0xffff;
            SegmentBlob blob = new SegmentBlob(new RecordId(id, offset << RECORD_ALIGN_BITS));
            writer.format("blobref %d: %s at %04x%n", blobrefid, blob.getBlobId(), offset);
        }
    }
    writer.println("--------------------------------------------------------------------------");
    int pos = data.limit() - ((length + 15) & ~15);
    while (pos < data.limit()) {
        writer.format("%04x: ", (MAX_SEGMENT_SIZE - data.limit() + pos) >> RECORD_ALIGN_BITS);
        for (int i = 0; i < 16; i++) {
            if (i > 0 && i % 4 == 0) {
                writer.append(' ');
            }
            if (pos + i >= data.position()) {
                byte b = data.get(pos + i);
                writer.format("%02x ", b & 0xff);
            } else {
                writer.append("   ");
            }
        }
        writer.append(' ');
        for (int i = 0; i < 16; i++) {
            if (pos + i >= data.position()) {
                byte b = data.get(pos + i);
                if (b >= ' ' && b < 127) {
                    writer.append((char) b);
                } else {
                    writer.append('.');
                }
            } else {
                writer.append(' ');
            }
        }
        writer.println();
        pos += 16;
    }
    writer.println("--------------------------------------------------------------------------");

    writer.close();
    return string.toString();
}

From source file:org.andromda.cartridges.java.EnumClassGenerator.java

/**
 * Generate Enumeration constants./*from w w  w  .j ava2 s . c  o m*/
 * 
 * @param clazz
 *            the UML class
 * @param ast
 *            the JDT Java AST
 * @param ed
 *            Enumeration declaration for Java JDT
 */
public void generateConstants(Classifier clazz, PrintWriter writer) {
    // Get all properties for this enumeration class
    Class enumerationClass = (Class) clazz;
    EList<Property> attributes = enumerationClass.getAttributes();
    int count = 0;
    for (Property enumLiteral : attributes) {
        count++;
        String comments = andromdaHelper.concatComments(enumLiteral.getOwnedComments());
        if (StringUtils.isNotEmpty(comments)) {
            generateSimpleComment(writer, comments.split("\n"));
        }
        String literalName = nameMasker.mask(enumLiteral.getName(), NameMasker.UPPERUNDERSCORE);
        writer.format("%s(%s)", literalName, resolveConstantValue(enumLiteral));
        if (count < attributes.size()) {
            writer.println(",");
        } else {
            writer.println(";");
        }
    }
}

From source file:de.tudarmstadt.lt.lm.web.servlet.LanguageModelProviderServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    req.setCharacterEncoding("UTF-8");
    resp.setContentType("text/html; charset=utf-8");
    PrintWriter w = new PrintWriter(resp.getWriter());
    String plaintext = req.getParameter("plaintext");
    String crawl_uri = req.getParameter("crawluri");
    String lm_key = req.getParameter("lm");
    String inputtype = req.getParameter("inputtype");
    boolean show_all_ngrams = req.getParameter("showall") != null;
    try {/*from   www  .j  a va  2 s  .  c  o  m*/
        if (lm_key == null) // no parameter set
            show_available(w);
        else
            show(inputtype, lm_key, plaintext, crawl_uri, show_all_ngrams, w);
    } catch (Exception e) {
        w.format("Error %s: %s.", e.getClass().getSimpleName(), e.getMessage());
    }
    w.flush();
    w.close();
}

From source file:com.act.lcms.db.io.writer.PlateCompositionWriter.java

protected void writeCellTable(PrintWriter w, String tableName, String[][] values, int expectedXDim,
        int expectedYDim) {
    StringBuilder headerBuilder = new StringBuilder(String.format(">>%s", tableName));
    for (int i = 0; i < expectedXDim; i++) {
        // Columns are one-indexed in composition tables.
        headerBuilder.append(String.format("\t%d", i + 1));
    }/*  www.  j av  a 2s . c o  m*/
    w.println(headerBuilder.toString());

    if (values.length != expectedYDim) {
        throw new RuntimeException(String.format(
                "ERROR: found incorrect row dimension for 96 well plate.  Expected %d, but got %d.\n",
                expectedYDim, values.length));
    }

    char rowName = 'A';
    for (int i = 0; i < values.length; i++) {
        if (values[i].length != expectedXDim) {
            throw new RuntimeException(String.format(
                    "ERROR: found incorrect column dimension for 96 well plate.  Expected %d, but got %d.\n",
                    expectedXDim, values.length));
        }
        w.format("%c\t%s\n", rowName, StringUtils.join(values[i], '\t'));
        rowName++;
    }
    w.println();
}