Example usage for java.io PrintStream print

List of usage examples for java.io PrintStream print

Introduction

In this page you can find the example usage for java.io PrintStream print.

Prototype

public void print(Object obj) 

Source Link

Document

Prints an object.

Usage

From source file:Main.java

protected static void print(PrintStream out, Node node) {
    if (node == null)
        return;//from ww  w  .ja va 2 s.c  o  m
    short type = node.getNodeType();
    switch (type) {
    case Node.DOCUMENT_NODE: {
        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        // out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
        NodeList nodelist = node.getChildNodes();
        int size = nodelist.getLength();
        for (int i = 0; i < size; i++)
            print(out, nodelist.item(i));
        break;
    }

    case Node.DOCUMENT_TYPE_NODE: {
        DocumentType docType = (DocumentType) node;
        out.print("<!DOCTYPE " + getDocumentTypeData(docType) + ">\n");
        break;
    }

    case Node.ELEMENT_NODE: {
        out.print('<');
        out.print(node.getNodeName());
        NamedNodeMap map = node.getAttributes();
        if (map != null) {
            int size = map.getLength();
            for (int i = 0; i < size; i++) {
                Attr attr = (Attr) map.item(i);
                out.print(' ');
                out.print(attr.getNodeName());
                out.print("=\"");
                out.print(normalize(attr.getNodeValue()));
                out.print('"');
            }
        }

        if (!node.hasChildNodes())
            out.print("/>");
        else {
            out.print('>');
            NodeList nodelist = node.getChildNodes();
            int numChildren = nodelist.getLength();
            for (int i = 0; i < numChildren; i++)
                print(out, nodelist.item(i));

            out.print("</");
            out.print(node.getNodeName());
            out.print('>');
        }
        break;
    }

    case Node.ENTITY_REFERENCE_NODE: {
        NodeList nodelist = node.getChildNodes();
        if (nodelist != null) {
            int size = nodelist.getLength();
            for (int i = 0; i < size; i++)
                print(out, nodelist.item(i));

        }
        break;
    }

    case Node.CDATA_SECTION_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }

    case Node.TEXT_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }

    case Node.PROCESSING_INSTRUCTION_NODE: {
        out.print("<?");
        out.print(node.getNodeName());
        String s = node.getNodeValue();
        if (s != null && s.length() > 0) {
            out.print(' ');
            out.print(s);
        }
        out.print("?>");
        break;
    }

    case Node.COMMENT_NODE: {
        out.print("<!--");
        out.print(node.getNodeValue());
        out.print("-->");
        break;
    }

    default: {
        out.print(normalize(node.getNodeValue()));
        break;
    }
    }
    out.flush();
}

From source file:MailHandlerDemo.java

/**
 * Gets a formatting string describing the given handler.
 *
 * @param prefix the output prefix.//from   w  ww  .  j  av a 2s  .  c o m
 * @param err the error stream.
 * @param h the handler.
 * @return the formatted string.
 */
private static String toString(String prefix, PrintStream err, Handler h) {
    StringBuilder buf = new StringBuilder();
    buf.append(h.getClass().getName());
    try {
        if (h instanceof MailHandler) {
            MailHandler mh = (MailHandler) h;
            buf.append(", ").append(mh.getSubject());
        }
    } catch (SecurityException error) {
        err.print(prefix + ": ");
        error.printStackTrace(err);
    }

    try {
        buf.append(", ").append(h.getFormatter());
    } catch (SecurityException error) {
        err.print(prefix + ": ");
        error.printStackTrace(err);
    }

    try {
        if (h instanceof MailHandler) {
            MailHandler mh = (MailHandler) h;
            buf.append(", ").append(Arrays.toString(mh.getAttachmentFormatters()));
        }
    } catch (SecurityException error) {
        err.print(prefix + ": ");
        error.printStackTrace(err);
    }

    try {
        buf.append(", ").append(h.getLevel());
    } catch (SecurityException error) {
        err.print(prefix + ": ");
        error.printStackTrace(err);
    }

    try {
        buf.append(", ").append(h.getFilter());
    } catch (SecurityException error) {
        err.print(prefix + ": ");
        error.printStackTrace(err);
    }

    try {
        buf.append(", ").append(h.getErrorManager());
    } catch (SecurityException error) {
        err.print(prefix + ": ");
        error.printStackTrace(err);
    }

    buf.append(", ").append(toString(h.getClass().getClassLoader()));
    return buf.toString();
}

From source file:com.zimbra.cs.imap.ImapMessage.java

private static void nparams(PrintStream ps, MimeCompoundHeader header) {
    boolean first = true;
    for (Iterator<Map.Entry<String, String>> it = header.parameterIterator(); it.hasNext(); first = false) {
        Map.Entry<String, String> param = it.next();
        ps.print(first ? '(' : ' ');
        aSTRING(ps, param.getKey());/*from  w w  w  .  ja v a 2  s  .c  om*/
        ps.write(' ');
        nstring2047(ps, param.getValue());
    }
    ps.print(first ? "NIL" : ")");
}

From source file:com.opengamma.analytics.financial.model.finitedifference.applications.PDEUtilityTools.java

/**
 * Prints out the values of the function f(x,y) where x takes the values x_1 to x_N and y takes the values y_1 to y_M, along with the x and y values, with x as the top row and
 * y as the left column. This format can be used by the Excel 3D surface plotter.
 * @param name Optional name for the output
 * @param data The data/*from   w  w w.  j  av  a 2s.  c  o  m*/
 * @param x x-values
 * @param y y-values
 * @param out output
 */
public static void printSurface(final String name, final double[][] data, final double[] x, final double[] y,
        final PrintStream out) {
    ArgumentChecker.notNull(data, "null data");
    ArgumentChecker.notNull(x, "null x");
    ArgumentChecker.notNull(y, "null y");
    ArgumentChecker.notNull(out, "null printStream");
    final int n = data.length;
    final int m = data[0].length;
    ArgumentChecker.isTrue(n == y.length, "Size of data is {} {}, but length of y is {}", n, m, y.length);
    ArgumentChecker.isTrue(m == x.length, "Size of data is {} {}, but length of x is {}", n, m, x.length);

    out.println(name);
    for (int j = 0; j < m; j++) {
        out.print("\t" + x[j]);
    }
    out.print("\n");
    for (int i = 0; i < n; i++) {
        out.print(y[i]);
        for (int j = 0; j < m; j++) {
            out.print("\t" + data[i][j]);
        }
        out.print("\n");
    }
    out.print("\n");
}

From source file:org.dbpedia.spotlight.evaluation.SpotterEvaluatorPrecisionRecall.java

private static void printSpots(PrintStream writer, Text text,
        List<SurfaceFormOccurrence> extractedSurfaceFormOccurrences) {
    StringBuffer line = new StringBuffer();
    line.append(text.text().hashCode());
    line.append("\t");
    for (SurfaceFormOccurrence sfo : extractedSurfaceFormOccurrences) {
        line.append(sfo.surfaceForm());//from  w  w  w  .  j a v a  2  s.  c  o  m
        line.append(":");
        line.append(sfo.textOffset());
        line.append(",");
    }
    line.append("\n");
    writer.print(line.toString());
}

From source file:com.jrummyapps.busybox.signing.ZipSigner.java

/**
 * Write a .SF file with a digest the specified manifest.
 *//*from   w  w  w  .ja  va 2 s  . co  m*/
private static byte[] writeSignatureFile(Manifest manifest, OutputStream out)
        throws IOException, GeneralSecurityException {
    final Manifest sf = new Manifest();
    final Attributes main = sf.getMainAttributes();
    main.putValue("Manifest-Version", MANIFEST_VERSION);
    main.putValue("Created-By", CREATED_BY);

    final MessageDigest md = MessageDigest.getInstance("SHA1");
    final PrintStream print = new PrintStream(new DigestOutputStream(new ByteArrayOutputStream(), md), true,
            "UTF-8");

    // Digest of the entire manifest
    manifest.write(print);
    print.flush();
    main.putValue("SHA1-Digest-Manifest", base64encode(md.digest()));

    final Map<String, Attributes> entries = manifest.getEntries();
    for (final Map.Entry<String, Attributes> entry : entries.entrySet()) {
        // Digest of the manifest stanza for this entry.
        print.print("Name: " + entry.getKey() + "\r\n");
        for (final Map.Entry<Object, Object> att : entry.getValue().entrySet()) {
            print.print(att.getKey() + ": " + att.getValue() + "\r\n");
        }
        print.print("\r\n");
        print.flush();

        final Attributes sfAttr = new Attributes();
        sfAttr.putValue("SHA1-Digest", base64encode(md.digest()));
        sf.getEntries().put(entry.getKey(), sfAttr);
    }

    final ByteArrayOutputStream sos = new ByteArrayOutputStream();
    sf.write(sos);

    String value = sos.toString();
    String done = value.replace("Manifest-Version", "Signature-Version");

    out.write(done.getBytes());

    print.close();
    sos.close();

    return done.getBytes();
}

From source file:net.imagini.cassandra.DumpSSTables.SSTableExport.java

/**
 * Get portion of the columns and serialize in loop while not more columns
 * left in the row//from   w  ww .  j a  va  2s .  co  m
 * 
 * @param row
 *            SSTableIdentityIterator row representation with Column Family
 * @param key
 *            Decorated Key for the required row
 * @param out
 *            output stream
 * @throws CharacterCodingException
 */
private static void serializeRow(SSTableIdentityIterator row, DecoratedKey key, PrintStream out)
        throws CharacterCodingException {
    ColumnFamily columnFamily = row.getColumnFamily();
    boolean isSuperCF = columnFamily.isSuper();
    CFMetaData cfMetaData = columnFamily.metadata();
    AbstractType<?> comparator = columnFamily.getComparator();

    // out.println("{");
    // writeKey(out, "key");
    // writeJSON(out, ByteBufferUtil.string(key.key));
    // out.print(ByteBufferUtil.string(key.key));
    // out.print(":");

    out.print(ByteBufferUtil.string(key.key) + " ");
    writeKey(out, "metadata");
    writeMeta(out, columnFamily);

    // writeKey(out, "columns");
    if (isSuperCF) {
        out.println("WARNING: SUPERCOLUMN");
        throw new UnsupportedOperationException("This program does not support supercolumns");
    }

    out.println();
    // serializeColumns(row, out, comparator, cfMetaData);
    createMapOutputs(row, out, comparator, cfMetaData, ByteBufferUtil.string(key.key));
}

From source file:com.sangupta.jerry.print.ConsoleTableWriter.java

/**
 * Output the data of the table as a JSON
 * /*from www . j  av a2  s.com*/
 * @param table
 *            the {@link ConsoleTable} to output
 * 
 * @param out
 *            the {@link PrintStream} to write to
 */
public static void writeJson(ConsoleTable table, PrintStream out) {
    if (table == null) {
        throw new IllegalArgumentException("ConsoleTable cannot be null");
    }

    if (out == null) {
        throw new IllegalArgumentException("PrintStream to write to cannot be null");
    }

    if (table.headerRow == null) {
        throw new IllegalStateException("Header row must be present for conversion to JSON");
    }

    List<String> names = new ArrayList<String>();
    for (int index = 0; index < table.headerRow.numColumns(); index++) {
        String name = table.headerRow.column(index);

        name = StringUtils.convertToJsonPropertyName(name);
        names.add(name);
    }

    // now start iterating over each row
    out.print("[\n");
    for (int rowIndex = 0; rowIndex < table.rows.size(); rowIndex++) {
        ConsoleTableRow row = table.rows.get(rowIndex);

        if (rowIndex > 0) {
            out.print(", ");
        }
        out.print("{\n");
        for (int index = 0; index < table.headerRow.numColumns(); index++) {
            if (index > 0) {
                out.print(", ");
            }
            out.print("\"");
            out.print(names.get(index));
            out.print("\" : \"");
            out.print(row.column(index));
            out.print("\"\n");
        }
        out.print("}\n");
    }
    out.print("]");
}

From source file:com.zimbra.cs.redolog.util.RedoLogVerify.java

private static void hexdump(PrintStream out, byte[] data, int offset, int length, long offsetOffsetBy,
        long badBytePos) {
    int end = Math.min(offset + length, data.length);
    int bytesPerLine = 16;
    while (offset < end) {
        int bytes = Math.min(bytesPerLine, end - offset); // bytes for this line
        long offsetLineStart = offset + offsetOffsetBy;
        long offsetLineEnd = offsetLineStart + bytes;
        out.printf("%08x: ", offsetLineStart);
        for (int i = 0; i < bytesPerLine; i++) {
            if (i < bytes)
                out.printf("%02x", ((int) data[offset + i]) & 0x000000ff);
            else//from   ww w  .j  ava2s .c  om
                out.print("  ");
            out.print(" ");
            if (i == 7)
                out.print(" ");
        }
        out.print(" ");
        for (int i = 0; i < bytesPerLine; i++) {
            if (i < bytes) {
                int ch = ((int) data[offset + i]) & 0x000000ff;
                if (ch >= 33 && ch <= 126) // printable ASCII range
                    out.printf("%c", (char) ch);
                else
                    out.print(".");
            } else {
                out.print(" ");
            }
        }

        if (offsetLineStart <= badBytePos && badBytePos < offsetLineEnd)
            out.print(" **");
        out.println();

        offset += bytes;
    }
}

From source file:com.sangupta.jerry.print.ConsoleTableWriter.java

/**
 * Output the data as an XML.//from  w  w  w  . j  a va  2  s .c  o m
 * 
 * @param table
 *            the {@link ConsoleTable} to output
 * 
 * @param out
 *            the {@link PrintWriter} to write to
 * 
 * @param parentXmlTag
 *            the uber XML tag to wrap items into
 * 
 * @param rowTag
 *            the tag to use for wrapping each row of data
 */
public static void writeXml(ConsoleTable table, PrintStream out, String parentXmlTag, String rowTag) {
    if (table == null) {
        throw new IllegalArgumentException("ConsoleTable cannot be null");
    }

    if (out == null) {
        throw new IllegalArgumentException("PrintStream to write to cannot be null");
    }

    if (table.headerRow == null) {
        throw new IllegalStateException("Header row must be present for conversion to XML");
    }

    List<String> names = new ArrayList<String>();
    for (int index = 0; index < table.headerRow.numColumns(); index++) {
        String name = table.headerRow.column(index);

        name = StringUtils.convertToJsonPropertyName(name);
        names.add(name);
    }

    // now start itearting over each row
    out.print("<" + parentXmlTag + ">\n");
    for (int rowIndex = 0; rowIndex < table.rows.size(); rowIndex++) {
        ConsoleTableRow row = table.rows.get(rowIndex);
        out.print("<" + rowTag + ">\n");
        for (int index = 0; index < table.headerRow.numColumns(); index++) {
            out.print("<");
            out.print(names.get(index));
            out.print(">");

            out.print(row.column(index));

            out.print("</");
            out.print(names.get(index));
            out.print(">\n");

        }
        out.print("</" + rowTag + ">\n");
    }
    out.print("</" + parentXmlTag + ">\n");
}