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:org.apache.marmotta.platform.ldp.patch.RdfPatchIO.java

/**
 * Write the provided patch to the given Writer.
 * @param writer the target to write to, will <em>not</em> be closed.
 * @param patch the patch to write/* www. j  a  v a 2s.c om*/
 * @param namespaces the namespaces to write (and replace)
 */
public static void writePatch(Writer writer, List<PatchLine> patch, Map<String, String> namespaces) {
    PrintWriter ps = new PrintWriter(writer);

    HashMap<String, String> inverseNamespaceMap = new HashMap<>(namespaces.size());
    for (Map.Entry<String, String> ns : namespaces.entrySet()) {
        inverseNamespaceMap.put(ns.getValue(), ns.getKey());
        ps.printf("@prefix %s: <%s> .%n", ns.getKey(), ns.getValue());
    }
    ps.println();
    for (PatchLine patchLine : patch) {
        final Statement st = patchLine.getStatement();
        ps.printf("%S %s %s %s .", patchLine.getOperator().getCommand(),
                io(st.getSubject(), inverseNamespaceMap), io(st.getPredicate(), inverseNamespaceMap),
                io(st.getObject(), inverseNamespaceMap));
    }

    ps.flush();
}

From source file:com.tamingtext.tagging.LuceneCategoryExtractor.java

/** dump the terms found in the field specified to the specified writer in the form:
 * /*w  w  w. java  2s  . c  o  m*/
 *  <pre>term(tab)document_frequency</pre>
 *  
 * @param indexDir the index to read.
 * @param field the name of the field.
 * @param out the print writer output will be written to
 * @throws IOException
 */
public static void dumpTerms(File indexDir, String field, PrintWriter out) throws IOException {
    Directory dir = FSDirectory.open(indexDir);
    IndexReader reader = IndexReader.open(dir, true);
    TermEnum te = reader.terms(new Term(field, ""));
    do {
        Term term = te.term();
        if (term == null || term.field().equals(field) == false) {
            break;
        }
        out.printf("%s %d\n", term.text(), te.docFreq());
    } while (te.next());
    te.close();
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.feature.coreference.CoreferenceFeatures.java

private static String chainToString(List<CoreferenceLink> chain) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    for (CoreferenceLink link : chain) {
        pw.printf("-> %s (%s) ", link.getCoveredText(), link.getReferenceType());
    }//from   www.  ja v a  2s .c om

    pw.close();
    return sw.toString();
}

From source file:daylightchart.daylightchart.calculation.RiseSetUtility.java

/**
 * Debug calculations.// w  w w .j a v  a2 s.  co  m
 *
 * @param writer
 *        Writer to write to
 * @param location
 *        Location to debug
 * @param daylightBandType
 *        Types of band type to write to
 */
@SuppressWarnings("boxing")
private static void writeCalculations(final Writer writer, final Location location, final TwilightType twilight,
        final DaylightBandType... daylightBandType) {
    if (writer == null || location == null) {
        return;
    }

    final DecimalFormat format = new DecimalFormat("00.000");
    format.setMaximumFractionDigits(3);

    final int year = Year.now().getValue();
    final Options options = new Options();
    options.setTwilightType(twilight);
    final RiseSetYearData riseSetYear = createRiseSetYear(location, year, options);

    final PrintWriter printWriter = new PrintWriter(writer, true);
    // Header
    printWriter.printf("Location\t%s%nDate\t%s%n%n", location, year);
    // Bands
    final List<DaylightBand> bands = riseSetYear.getBands();
    for (final Iterator<DaylightBand> iterator = bands.iterator(); iterator.hasNext();) {
        final DaylightBand band = iterator.next();
        if (!ArrayUtils.contains(daylightBandType, band.getDaylightBandType())) {
            iterator.remove();
        }
    }
    printWriter.printf("\t\t\t");
    if (ArrayUtils.contains(daylightBandType, DaylightBandType.twilight)) {
        printWriter.printf("\t\t");
    }
    for (final DaylightBand band : bands) {
        printWriter.printf("Band\t%s\t", band.getName());
    }
    printWriter.println();
    // Data rows
    printWriter.print("Date\tSunrise\tSunset");
    if (ArrayUtils.contains(daylightBandType, DaylightBandType.twilight)) {
        printWriter.println("\tTwilight Rise\tTwilight Set");
    } else {
        printWriter.println();
    }
    final List<RawRiseSet> rawRiseSets = riseSetYear.getRawRiseSets();
    final List<RawRiseSet> rawTwilights = riseSetYear.getRawTwilights();
    for (int i = 0; i < rawRiseSets.size(); i++) {
        final RawRiseSet rawRiseSet = rawRiseSets.get(i);
        printWriter.printf("%s\t%s\t%s", rawRiseSet.getDate(), format.format(rawRiseSet.getSunrise()),
                format.format(rawRiseSet.getSunset()));
        if (ArrayUtils.contains(daylightBandType, DaylightBandType.twilight)) {
            final RawRiseSet rawTwilight = rawTwilights.get(i);
            printWriter.printf("\t%s\t%s", format.format(rawTwilight.getSunrise()),
                    format.format(rawTwilight.getSunset()));
        }
        for (final DaylightBand band : bands) {
            final RiseSet riseSet = band.get(rawRiseSet.getDate());
            if (riseSet == null) {
                printWriter.print("\t\t");
            } else {
                printWriter.printf("\t%s\t%s",
                        riseSet.getSunrise().toLocalTime().format(DateTimeFormatter.ofPattern("HH:mm:ss")),
                        riseSet.getSunset().toLocalTime().format(DateTimeFormatter.ofPattern("HH:mm:ss")));
            }
        }
        printWriter.println();
    }
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.writers.HtmlWriter.java

public static List<String> renderDocumentToHtmlParagraphs(JCas jCas) {
    List<String> result = new ArrayList<>();

    // iterate over paragraphs
    for (Paragraph p : JCasUtil.select(jCas, Paragraph.class)) {
        StringWriter writer = new StringWriter();
        PrintWriter out = new PrintWriter(writer);
        // iterate over tokens
        for (Token t : JCasUtil.selectCovered(jCas, Token.class, p)) {
            // print token's preceding space if needed
            if (ArgumentPrinterUtils.hasSpaceBefore(t, jCas)) {
                out.print(" ");
            }/*from   w ww  .  j a v a2 s . co m*/

            // does an argument concept begin here?
            ArgumentComponent argumentConcept = ArgumentPrinterUtils.argAnnotationBegins(t, jCas);
            if (argumentConcept != null) {
                out.printf("<span class=\"component\">%s:</span> <span class=\"%s\">",
                        argumentConcept.getClass().getSimpleName().toLowerCase(),
                        argumentConcept.getClass().getSimpleName().toLowerCase());
            }

            Sentence sentence = ArgumentPrinterUtils.sentenceStartsOnToken(t);
            if (sentence != null) {
                out.printf("<span class=\"sentence\">S%d</span>",
                        ArgumentPrinterUtils.getSentenceNumber(sentence, jCas));
            }

            // print token
            out.print(t.getCoveredText());

            // does an argument concept end here?
            if (ArgumentPrinterUtils.argAnnotationEnds(t, jCas)) {
                out.print("</span>");
            }
        }

        result.add(writer.toString());
    }

    return result;
}

From source file:it.tidalwave.imageio.test.ImageReaderTestSupport.java

/*******************************************************************************************************************
 *
 *
 ******************************************************************************************************************/
public static void dumpRasterAsText(final @Nonnull Raster raster, final @Nonnull PrintWriter pw) {
    final int width = raster.getWidth();
    final int height = raster.getHeight();
    final int bandCount = raster.getNumBands();
    logger.fine("Dumping raster %d x %d x %d", width, height, bandCount);

    for (int y = 0; y < height; y++) {
        for (int b = 0; b < bandCount; b++) {
            pw.printf("y=%04d b=%1d : ", y, b);

            for (int x = 0; x < width; x++) {
                final int sample = raster.getSample(x, y, b) & 0xffff;
                pw.printf("%04x ", sample);
            }/* ww  w.  j a v a  2s . c  o  m*/

            pw.println();
        }
    }
}

From source file:de.tudarmstadt.ukp.dkpro.tc.svmhmm.util.SVMHMMUtils.java

/**
 * Creates a new file in the same directory as {@code featureVectorsFile} and replaces the first
 * token (outcome label) by its corresponding integer number from the bi-di map
 *
 * @param featureVectorsFile file//  www  . jav  a2s.  c  o m
 * @param labelsToIntegers   mapping
 * @return new file
 */
public static File replaceLabelsWithIntegers(File featureVectorsFile, BidiMap labelsToIntegers)
        throws IOException {
    File result = new File(featureVectorsFile.getParent(), "mappedLabelsToInt_" + featureVectorsFile.getName());
    PrintWriter pw = new PrintWriter(new FileOutputStream(result));

    for (String line : FileUtils.readLines(featureVectorsFile)) {
        // split on the first whitespaces, keep the rest
        String[] split = line.split("\\s", 2);
        String label = split[0];
        String remainingContent = split[1];

        // find the integer
        Integer intOutput = (Integer) labelsToIntegers.get(label);

        // print to the output stream
        pw.printf("%d %s%n", intOutput, remainingContent);
    }

    IOUtils.closeQuietly(pw);

    return result;
}

From source file:org.dkpro.tc.ml.svmhmm.util.SVMHMMUtils.java

/**
 * Creates a new file in the same directory as {@code featureVectorsFile} and replaces the first
 * token (outcome label) by its corresponding integer number from the bi-di map
 *
 * @param featureVectorsFile/*from w  ww  . j a  v  a 2  s  .c om*/
 *            file
 * @param labelsToIntegers
 *            mapping
 * @return new file
 */
public static File replaceLabelsWithIntegers(File featureVectorsFile, BidiMap labelsToIntegers)
        throws IOException {
    File result = new File(featureVectorsFile.getParent(), "mappedLabelsToInt_" + featureVectorsFile.getName());
    PrintWriter pw = new PrintWriter(new FileOutputStream(result));

    BufferedReader br = new BufferedReader(new FileReader(featureVectorsFile));

    String line = null;
    while ((line = br.readLine()) != null) {
        // split on the first whitespaces, keep the rest
        String[] split = line.split("\\s", 2);
        String label = split[0];
        String remainingContent = split[1];

        // find the integer
        Integer intOutput = (Integer) labelsToIntegers.get(label);

        // print to the output stream
        pw.printf("%d %s%n", intOutput, remainingContent);
    }

    IOUtils.closeQuietly(pw);
    IOUtils.closeQuietly(br);

    return result;
}

From source file:pdl.web.filter.JsonBasicAuthenticationEP.java

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException {
    response.addHeader("WWW-Authenticate", "Basic realm=\"" + getRealmName() + "\"");
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    PrintWriter writer = response.getWriter();
    writer.printf("Error(%s) - %s", HttpServletResponse.SC_UNAUTHORIZED, "Bad credentials");
}

From source file:org.apache.sling.typecleanup.TypeCleanupServlet.java

protected void printTypeCleanupInfo(SlingHttpServletResponse response, TypeCleanupInfo infos)
        throws IOException {
    PrintWriter w = response.getWriter();
    w.printf("%d resources traversed, %d obsolete resources\n", infos.getNbResourceParsed(),
            infos.getPaths().size());/*w w w .  j  a  v  a 2s .  c  o  m*/
    for (String obsoletePath : infos.getPaths()) {
        w.printf("%s,\n", obsoletePath);
    }
    if (infos.getIgnoredPaths().size() > 0) {
        w.printf("%d configured paths were ignored:\n");
        for (String ignoredPath : infos.getIgnoredPaths()) {
            w.printf("%s,\n", ignoredPath);
        }
    }
}