Example usage for java.io PrintWriter printf

List of usage examples for java.io PrintWriter printf

Introduction

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

Prototype

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

Source Link

Document

A convenience method to write a formatted string to this writer using the specified format string and arguments.

Usage

From source file:com.nridge.core.base.io.xml.DataBagListXML.java

/**
 * Saves the previous assigned bag list (e.g. via constructor or set method)
 * to the print writer stream wrapped in a tag name specified in the parameter.
 *
 * @param aPW PrintWriter stream instance.
 * @param aTagName Tag name.//w  w  w  .j  a  va  2 s.  c  o m
 * @param anIndentAmount Indentation count.
 *
 * @throws java.io.IOException I/O related exception.
 */
public void save(PrintWriter aPW, String aTagName, int anIndentAmount) throws IOException {
    DataBagXML dataBagXML;

    IOXML.indentLine(aPW, anIndentAmount);
    aPW.printf("<%sList count=\"%d\">%n", aTagName, mDataBagList.size());

    for (DataBag dataBag : mDataBagList) {
        dataBagXML = new DataBagXML(dataBag);
        dataBagXML.setIsSimpleFlag(mIsSimple);
        dataBagXML.save(aPW, aTagName, anIndentAmount + 1);
    }

    IOXML.indentLine(aPW, anIndentAmount);
    aPW.printf("</%sList>%n", aTagName);
}

From source file:com.gu.management.spring.ManagementUrlDiscoveryController.java

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws IOException {

    response.setContentType("text/html;charset=UTF-8");

    PrintWriter writer = response.getWriter();

    writer.write("<html><head><title>Management URLs</title></head><body><h2>Management URLs</h2><ul>\n");
    Collection<String> managementUrls = service.getManagementUrls();
    for (String url : managementUrls) {
        if (!"/management".equals(url)) {
            writer.printf("<li><a href=\"management%s\">%s</a></li>\n", url, url);
        }/*from www . j a v a 2 s.  c o m*/
    }

    writer.write("</ul></body></html>");

    return null;
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.clustering.entropy.ClusterTopicMatrixGenerator.java

@Override
public void collectionProcessComplete() throws AnalysisEngineProcessException {
    super.collectionProcessComplete();

    try {//w w w  .  jav a2  s .c  o m
        if (outputFile != null) {
            PrintWriter pw = new PrintWriter(new FileOutputStream(outputFile));
            for (int i = 0; i < clusterTopicMatrix.numRows(); i++) {
                for (int j = 0; j < clusterTopicMatrix.numColumns(); j++) {
                    pw.printf(Locale.ENGLISH, "%.5f\t", clusterTopicMatrix.get(i, j));
                }
                pw.println();
            }
            IOUtils.closeQuietly(pw);
        } else {
            // print the matrix
            System.out.println(clusterTopicMatrix);
        }
    } catch (IOException e) {
        throw new AnalysisEngineProcessException(e);
    }
}

From source file:name.livitski.databag.cli.Syntax.java

protected void listOptions(Collection<Option> options, PrintWriter out) {
    HelpFormatter formatter = getHelpFormatter();
    for (Option option : options) {
        out.printf("    %s%s%n", formatOptionHeader(option), formatArguments(option));
        String summary = "## NO DESCRIPTION PROVIDED ##";
        try {//from w  w  w  .j a  va 2s  . c  o m
            String id = getOptionId(option);
            summary = getResources().getString(USAGE_BUNDLE, getClass(), id);
        } catch (MissingResourceException missing) {
        }
        out.print("        ");
        formatter.printWrapped(out, OUTPUT_WIDTH - 8, 8, summary);
    }
    if (!options.isEmpty())
        out.println();
}

From source file:com.day.cq.wcm.foundation.Sitemap.java

public void draw(Writer w) throws IOException {
    PrintWriter out = new PrintWriter(w);

    int previousLevel = -1;

    for (Link aLink : links) {
        if (aLink.getLevel() > previousLevel)
            out.print("<div class=\"linkcontainer\">");
        else if (aLink.getLevel() < previousLevel) {
            for (int i = aLink.getLevel(); i < previousLevel; i++)
                out.print("</div>");
        }/*from w  w w .  j  ava  2  s. c  om*/

        out.printf("<div class=\"link\"><a href=\"%s.html\">%s</a></div>",
                StringEscapeUtils.escapeHtml4(aLink.getPath()), aLink.getTitle());

        previousLevel = aLink.getLevel();
    }

    for (int i = -1; i < previousLevel; i++)
        out.print("</div>");
}

From source file:org.nuxeo.runtime.metrics.CsvReporter.java

private void report(long timestamp, String name, String header, String line, Object... values) {
    try {/*ww w  .j a v  a  2 s .c  o m*/
        final File file = new File(directory, sanitize(name) + ".csv");
        final boolean fileAlreadyExists = file.exists();
        if (fileAlreadyExists || file.createNewFile()) {
            final PrintWriter out = new PrintWriter(
                    new OutputStreamWriter(new FileOutputStream(file, true), UTF_8));
            try {
                if (!fileAlreadyExists) {
                    out.println("t," + header);
                }
                out.printf(locale, String.format(locale, "%d,%s%n", timestamp, line), values);
            } finally {
                out.close();
            }
        }
    } catch (IOException e) {
        LOGGER.warn("Error writing to " + name, e);
    }
}

From source file:com.nridge.core.base.io.xml.DocumentXML.java

/**
 * Saves the previous assigned document (e.g. via constructor or set method)
 * to the print writer stream wrapped in a tag name specified in the parameter.
 *
 * @param aPW            PrintWriter stream instance.
 * @param aParentTag     Parent tag name.
 * @param aDocument      Document instance.
 * @param anIndentAmount Indentation count.
 *
 * @throws java.io.IOException I/O related exception.
 *///from   ww w  . j a va 2 s.  c  o  m
public void save(PrintWriter aPW, String aParentTag, Document aDocument, int anIndentAmount)
        throws IOException {
    RelationshipXML relationshipXML;
    String docType = StringUtils.remove(aDocument.getType(), StrUtl.CHAR_SPACE);
    String parentTag = StringUtils.remove(aParentTag, StrUtl.CHAR_SPACE);

    IOXML.indentLine(aPW, anIndentAmount);
    if (StringUtils.isNotEmpty(aParentTag))
        aPW.printf("<%s-%s", parentTag, docType);
    else
        aPW.printf("<%s", docType);
    if (!mIsSimple) {
        IOXML.writeAttrNameValue(aPW, "type", aDocument.getType());
        IOXML.writeAttrNameValue(aPW, "name", aDocument.getName());
        IOXML.writeAttrNameValue(aPW, "title", aDocument.getTitle());
        IOXML.writeAttrNameValue(aPW, "schemaVersion", aDocument.getSchemaVersion());
    }
    for (Map.Entry<String, String> featureEntry : aDocument.getFeatures().entrySet())
        IOXML.writeAttrNameValue(aPW, featureEntry.getKey(), featureEntry.getValue());
    aPW.printf(">%n");
    DataTableXML dataTableXML = new DataTableXML(aDocument.getTable());
    dataTableXML.setSaveFieldsWithoutValues(mSaveFieldsWithoutValues);
    dataTableXML.save(aPW, IO.XML_TABLE_NODE_NAME, anIndentAmount + 1);
    if (aDocument.relationshipCount() > 0) {
        ArrayList<Relationship> docRelationships = aDocument.getRelationships();
        IOXML.indentLine(aPW, anIndentAmount + 1);
        aPW.printf("<%s>%n", IO.XML_RELATED_NODE_NAME);
        for (Relationship relationship : docRelationships) {
            relationshipXML = new RelationshipXML(relationship);
            relationshipXML.setSaveFieldsWithoutValues(mSaveFieldsWithoutValues);
            relationshipXML.save(aPW, IO.XML_RELATIONSHIP_NODE_NAME, anIndentAmount + 2);
        }
        IOXML.indentLine(aPW, anIndentAmount + 1);
        aPW.printf("</%s>%n", IO.XML_RELATED_NODE_NAME);
    }
    HashMap<String, String> docACL = aDocument.getACL();
    if (docACL.size() > 0) {
        IOXML.indentLine(aPW, anIndentAmount + 1);
        aPW.printf("<%s>%n", IO.XML_ACL_NODE_NAME);
        for (Map.Entry<String, String> aclEntry : docACL.entrySet()) {
            IOXML.indentLine(aPW, anIndentAmount + 2);
            aPW.printf("<%s", IO.XML_ACE_NODE_NAME);
            IOXML.writeAttrNameValue(aPW, "name", aclEntry.getKey());
            aPW.printf(">%s</%s>%n", StringEscapeUtils.escapeXml10(aclEntry.getValue()), IO.XML_ACE_NODE_NAME);
        }
        IOXML.indentLine(aPW, anIndentAmount + 1);
        aPW.printf("</%s>%n", IO.XML_ACL_NODE_NAME);
    }
    IOXML.indentLine(aPW, anIndentAmount);
    if (StringUtils.isNotEmpty(aParentTag))
        aPW.printf("</%s-%s>%n", parentTag, docType);
    else
        aPW.printf("</%s>%n", docType);
}

From source file:de.tudarmstadt.ukp.dkpro.core.io.conll.Conll2012Writer.java

@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {
    PrintWriter out = null;
    try {/*w  w  w .ja va  2s  .c  om*/
        out = new PrintWriter(new OutputStreamWriter(getOutputStream(aJCas, filenameSuffix), encoding));

        String documentId = DocumentMetaData.get(aJCas).getDocumentId();
        int partNumber = 0;
        if (documentId.contains("#")) {
            partNumber = Integer.parseInt(StringUtils.substringAfterLast(documentId, "#"));
            documentId = StringUtils.substringBeforeLast(documentId, "#");
        }
        out.printf("#begin document (%s); part %03d%n", documentId, partNumber);

        convert(aJCas, out);
    } catch (Exception e) {
        throw new AnalysisEngineProcessException(e);
    } finally {
        closeQuietly(out);
    }
}

From source file:net.greghaines.jesque.web.controller.JesqueController.java

@RequestMapping(value = "/stats.txt", method = GET)
public void statsTxt(final HttpServletResponse resp) throws IOException {
    final Map<String, Object> resqueStats = createResqueStats();
    final List<QueueInfo> queueInfos = this.queueInfoDAO.getQueueInfos();
    resp.setContentType("text/html");
    final PrintWriter pw = resp.getWriter();
    pw.println("resque.pending=" + resqueStats.get("pending"));
    pw.println("resque.processed=" + resqueStats.get("processed"));
    pw.println("resque.failed=" + resqueStats.get("failed"));
    pw.println("resque.workers=" + resqueStats.get("workers"));
    pw.println("resque.working=" + resqueStats.get("working"));
    for (final QueueInfo queueInfo : queueInfos) {
        pw.printf("queues.%s=%d%n", queueInfo.getName(), queueInfo.getSize());
    }/*  www . j a v  a 2  s  .  com*/
    pw.flush();
    pw.close();
}

From source file:com.nridge.core.base.io.console.DataBagConsole.java

/**
 * Write bag name and values to the console.
 * <p>/*from  w ww  .  j a v a 2 s.c o m*/
 * <b>Note:</b> This method can only support single value
 * fields.
 * </p>
 *
 * @param aPW PrintWriter instance.
 *
 * @param aTitle A title string for the presentation.
 */
public void writeBag(PrintWriter aPW, String aTitle) {
    StringBuilder stringBuilder;
    String fieldTitle, fieldValue, titleString;

    int maxTitleLength = 0;
    for (DataField dataField : mBag.getFields()) {
        if (dataField.isDisplayable()) {
            if (mIsBasedOnTitle)
                fieldTitle = dataField.getTitle();
            else
                fieldTitle = dataField.getName();
            maxTitleLength = Math.max(maxTitleLength, fieldTitle.length());
        }
    }

    if (StringUtils.isNotEmpty(aTitle)) {
        stringBuilder = new StringBuilder();
        for (int j = aTitle.length(); j < maxTitleLength; j++)
            stringBuilder.append(StrUtl.CHAR_SPACE);

        aPW.printf("%n%s%s%n%n", stringBuilder.toString(), aTitle);
    }

    for (DataField dataField : mBag.getFields()) {
        if (dataField.isDisplayable()) {
            if (mIsBasedOnTitle)
                fieldTitle = dataField.getTitle();
            else
                fieldTitle = dataField.getName();
            if (dataField.isMultiValue())
                fieldValue = StrUtl.collapseToSingle(dataField.getValues(), StrUtl.CHAR_COMMA);
            else
                fieldValue = dataField.getValue();
            stringBuilder = new StringBuilder();
            for (int j = fieldTitle.length(); j < maxTitleLength; j++)
                stringBuilder.append(StrUtl.CHAR_SPACE);

            aPW.printf("%s%s: %s%n", stringBuilder.toString(), fieldTitle, fieldValue);

        }
    }
}