Example usage for java.io StringWriter StringWriter

List of usage examples for java.io StringWriter StringWriter

Introduction

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

Prototype

public StringWriter(int initialSize) 

Source Link

Document

Create a new string writer using the specified initial string-buffer size.

Usage

From source file:Main.java

public static String getXMLContent(XMLEventReader reader, StartElement element, boolean decodeCharacters)
        throws XMLStreamException {
    String rootElementName = getLocalName(element);

    StringWriter buffer = new StringWriter(1024);

    StartElement pendingElement = null;
    String pendingElementName = null;

    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();

        if (pendingElement != null) {
            boolean skip = false;

            if (event.isEndElement() && pendingElementName.equals(getLocalName(event.asEndElement()))) {
                writeAsEncodedUnicode(pendingElement, buffer, true); // empty tag
                skip = true; // skip this end tag
            } else {
                writeAsEncodedUnicode(pendingElement, buffer, false);
            }/* ww w  . j a v  a 2s  .  c o m*/

            pendingElement = null;
            pendingElementName = null;

            if (skip)
                continue;
        }

        if (event.isEndElement()) {
            EndElement endElement = event.asEndElement();
            String name = getLocalName(endElement);

            if (rootElementName.equals(name))
                return buffer.toString();

            writeAsEncodedUnicode(endElement, buffer);
        } else if (event.isStartElement()) {
            pendingElement = event.asStartElement();
            pendingElementName = getLocalName(pendingElement);
        } else if (event.isCharacters() && decodeCharacters) {
            buffer.append(event.asCharacters().getData());
        } else {
            event.writeAsEncodedUnicode(buffer);
        }
    }

    throw new XMLStreamException(format("Missing closing tag for '" + rootElementName + "' element", element));
}

From source file:Main.java

/**
 * Pulled from apache lang commons project (StringUtils).
 * /*from  w w  w.ja  v a2 s. c om*/
 * <p>Worker method for the {@link #escapeJavaScript(String)} method.</p>
 * 
 * @param str String to escape values in, may be null
 * @param escapeSingleQuotes escapes single quotes if <code>true</code>
 * @return the escaped string
 */
private static String escapeJavaStyleString(String str, boolean escapeSingleQuotes) {
    if (str == null) {
        return null;
    }
    try {
        StringWriter writer = new StringWriter(str.length() * 2);
        escapeJavaStyleString(writer, str, escapeSingleQuotes);
        return writer.toString();
    } catch (IOException ioe) {
        // this should never ever happen while writing to a StringWriter
        ioe.printStackTrace();
        return null;
    }
}

From source file:Main.java

static String escape(String input) {
    StringWriter output = new StringWriter(input.length() * 2);
    for (int i = 0; i < input.length(); i++) {
        char c = input.charAt(i);
        if (c == '"') {
            output.append("&quot;");
        } else if (c == '\'') {
            output.append("&apos;");
        } else if (c == '<') {
            output.append("&lt;");
        } else if (c == '>') {
            output.append("&gt;");
        } else if (c == '&') {
            output.append("&amp;");
        } else {//  w w  w.  ja v a  2  s . com
            output.append(c);
        }
    }
    return output.toString();
}

From source file:Main.java

public static String dumpException(Throwable e) {
    StringWriter sw = new StringWriter(160);
    sw.write(e.getClass().getName());/*from   w  w w . j a v a  2s.  com*/
    sw.write(":\n");
    e.printStackTrace(new PrintWriter(sw));
    return sw.toString();
}

From source file:Main.java

/**
 * Get a string representation of the current stack state of all the active threads.
 *//*from w  w  w .j  a  va2 s.co  m*/
public static String getStackTraces() {
    StringWriter stringWriter = new StringWriter(5000);
    printStackTraces(new PrintWriter(stringWriter));
    return stringWriter.toString();
}