Example usage for java.io StringWriter append

List of usage examples for java.io StringWriter append

Introduction

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

Prototype

public StringWriter append(char c) 

Source Link

Document

Appends the specified character to this writer.

Usage

From source file:com.jaspersoft.jasperserver.rest.services.RESTJobSummary.java

private String generateSummeryReport(JobSummary[] summaries) throws ServiceException {
    try {//from  ww  w .j a v a  2  s.  c  o  m
        StringWriter sw = new StringWriter();

        sw.append("<jobs>");

        for (int i = 0; i < summaries.length; i++) {
            restUtils.getMarshaller(JobSummary.class).marshal(summaries[i], sw);
            if (log.isDebugEnabled()) {
                log.debug("finished marshaling job: " + summaries[i].getId());
            }
        }
        sw.append("</jobs>");
        return sw.toString();
    } catch (JAXBException e) {
        throw new ServiceException(e.getLocalizedMessage());
    }
}

From source file:com.espertech.esper.epl.expression.core.ExprNumberSetRange.java

public void toPrecedenceFreeEPL(StringWriter writer) {
    this.getChildNodes()[0].toEPL(writer, ExprPrecedenceEnum.MINIMUM);
    writer.append(":");
    this.getChildNodes()[1].toEPL(writer, ExprPrecedenceEnum.MINIMUM);
}

From source file:com.virtualparadigm.packman.processor.JPackageManager.java

private static void marshallToFile(Collection<Package> packages, String filePath) {
    try {/*from ww w  .j a va 2 s.  co m*/
        Marshaller marshaller = jaxbContext.createMarshaller();

        // removes the xml header:
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        StringWriter stringWriter = new StringWriter();
        for (Package installedPackage : packages) {
            marshaller.marshal(
                    new JAXBElement<Package>(new QName(null, "package"), Package.class, installedPackage),
                    stringWriter);

            stringWriter.append("\n");
        }

        FileUtils.writeStringToFile(new File(filePath), stringWriter.toString(), "UTF-8");
    } catch (Exception e) {
        logger.error("", e);
    }
}

From source file:org.modeshape.web.jcr.rest.model.RestException.java

/**
 * Creates a new exception, based on an existing {@link Throwable}
 *
 * @param t a {@code non-null} {@link Exception}
 *//*from   w w w .j a  va  2  s .  c  o m*/
public RestException(Throwable t) {
    this.message = t.getMessage();
    StringWriter stringWriter = new StringWriter();
    for (StackTraceElement trace : t.getStackTrace()) {
        stringWriter.append(trace.toString());
    }
    this.stackTrace = stringWriter.toString();
}

From source file:org.javelin.sws.ext.bind.internal.metadata.PeelingFormatter.java

@Override
public String print(T object, Locale locale) {
    // TODO: object should not be null!

    P value = this.metadata.getValue(object);
    if (this.metadata.isCollectionProperty()) {
        @SuppressWarnings("unchecked")
        Collection<P> col = (Collection<P>) value;
        if (col.size() == 0)
            return "";
        StringWriter sb = new StringWriter();
        for (P el : col) {
            if (el != null)
                sb.append(' ').append(
                        this.nestedFormatter == null ? el.toString() : this.nestedFormatter.print(el, locale));
        }/*www . j  a  va 2s  .  c o m*/
        return sb.toString().substring(1);
    } else {
        return this.nestedFormatter == null ? value.toString() : this.nestedFormatter.print(value, locale);
    }
}

From source file:com.espertech.esper.epl.expression.ops.ExprBitWiseNode.java

public void toPrecedenceFreeEPL(StringWriter writer) {
    getChildNodes()[0].toEPL(writer, getPrecedence());
    writer.append(_bitWiseOpEnum.getComputeDescription());
    getChildNodes()[1].toEPL(writer, getPrecedence());
}

From source file:com.espertech.esper.epl.db.DatabasePollingViewableFactory.java

/**
 * Lexes the sample SQL and inserts a "where 1=0" where-clause.
 * @param querySQL to inspect using lexer
 * @return sample SQL with where-clause inserted
 * @throws ExprValidationException to indicate a lexer problem
 */// w w w.j av  a2 s  .com
protected static String lexSampleSQL(String querySQL) throws ExprValidationException {
    querySQL = querySQL.replaceAll("\\s\\s+|\\n|\\r", " ");
    StringReader reader = new StringReader(querySQL);
    CharStream input;
    try {
        input = new NoCaseSensitiveStream(reader);
    } catch (IOException ex) {
        throw new ExprValidationException("IOException lexing query SQL '" + querySQL + '\'', ex);
    }

    int whereIndex = -1;
    int groupbyIndex = -1;
    int havingIndex = -1;
    int orderByIndex = -1;
    List<Integer> unionIndexes = new ArrayList<Integer>();

    EsperEPL2GrammarLexer lex = new EsperEPL2GrammarLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lex);
    List tokenList = tokens.getTokens();

    for (int i = 0; i < tokenList.size(); i++) {
        Token token = (Token) tokenList.get(i);
        if ((token == null) || token.getText() == null) {
            break;
        }
        String text = token.getText().toLowerCase().trim();
        if (text.equals("where")) {
            whereIndex = token.getCharPositionInLine() + 1;
        }
        if (text.equals("group")) {
            groupbyIndex = token.getCharPositionInLine() + 1;
        }
        if (text.equals("having")) {
            havingIndex = token.getCharPositionInLine() + 1;
        }
        if (text.equals("order")) {
            orderByIndex = token.getCharPositionInLine() + 1;
        }
        if (text.equals("union")) {
            unionIndexes.add(token.getCharPositionInLine() + 1);
        }
    }

    // If we have a union, break string into subselects and process each
    if (unionIndexes.size() != 0) {
        StringWriter changedSQL = new StringWriter();
        int lastIndex = 0;
        for (int i = 0; i < unionIndexes.size(); i++) {
            int index = unionIndexes.get(i);
            String fragment;
            if (i > 0) {
                fragment = querySQL.substring(lastIndex + 5, index - 1);
            } else {
                fragment = querySQL.substring(lastIndex, index - 1);
            }
            String lexedFragment = lexSampleSQL(fragment);

            if (i > 0) {
                changedSQL.append("union ");
            }
            changedSQL.append(lexedFragment);
            lastIndex = index - 1;
        }

        // last part after last union
        String fragment = querySQL.substring(lastIndex + 5, querySQL.length());
        String lexedFragment = lexSampleSQL(fragment);
        changedSQL.append("union ");
        changedSQL.append(lexedFragment);

        return changedSQL.toString();
    }

    // Found a where clause, simplest cases
    if (whereIndex != -1) {
        StringWriter changedSQL = new StringWriter();
        String prefix = querySQL.substring(0, whereIndex + 5);
        String suffix = querySQL.substring(whereIndex + 5, querySQL.length());
        changedSQL.write(prefix);
        changedSQL.write("1=0 and ");
        changedSQL.write(suffix);
        return changedSQL.toString();
    }

    // No where clause, find group-by
    int insertIndex;
    if (groupbyIndex != -1) {
        insertIndex = groupbyIndex;
    } else if (havingIndex != -1) {
        insertIndex = havingIndex;
    } else if (orderByIndex != -1) {
        insertIndex = orderByIndex;
    } else {
        StringWriter changedSQL = new StringWriter();
        changedSQL.write(querySQL);
        changedSQL.write(" where 1=0 ");
        return changedSQL.toString();
    }

    try {
        StringWriter changedSQL = new StringWriter();
        String prefix = querySQL.substring(0, insertIndex - 1);
        changedSQL.write(prefix);
        changedSQL.write("where 1=0 ");
        String suffix = querySQL.substring(insertIndex - 1, querySQL.length());
        changedSQL.write(suffix);
        return changedSQL.toString();
    } catch (Exception ex) {
        String text = "Error constructing sample SQL to retrieve metadata for JDBC-drivers that don't support metadata, consider using the "
                + SAMPLE_WHERECLAUSE_PLACEHOLDER + " placeholder or providing a sample SQL";
        log.error(text, ex);
        throw new ExprValidationException(text, ex);
    }
}

From source file:org.broadleafcommerce.openadmin.web.resource.MessagesResourceResolver.java

protected String replaceResourceContents(String contents) throws IOException {
    InputStream inputStream = null;

    try {//from  w  w  w  .  j a va 2s.  com
        Properties prop = new Properties();
        String propFileName = getOpenAdminMessagesProperties();

        inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);

        if (inputStream != null) {
            prop.load(inputStream);
        } else {
            throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
        }

        StringWriter propertiesObject = new StringWriter();
        propertiesObject.append("{");

        // get the property value and print it out
        for (Map.Entry<Object, Object> property : prop.entrySet()) {
            propertiesObject.append(property.getKey() + ": \"" + property.getValue() + "\", ");
        }

        contents = contents.replace("//BLC-ADMIN-JS-MESSAGES", propertiesObject.getBuffer().toString()
                .substring(0, propertiesObject.getBuffer().toString().length() - 2) + "}");

    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }
    return contents;
}

From source file:com.espertech.esper.epl.expression.core.ExprNumberSetCronParam.java

public void toPrecedenceFreeEPL(StringWriter writer) {
    if (this.getChildNodes().length != 0) {
        this.getChildNodes()[0].toEPL(writer, getPrecedence());
        writer.append(" ");
    }/*from   ww  w .ja v  a2s .  com*/
    writer.append(cronOperator.getSyntax());
}

From source file:net.officefloor.tutorial.inherithttpserver.InheritHttpServerTest.java

/**
 * Undertakes the {@link HttpRequest} and ensures the responding page is as
 * expected.//from  w  w  w. j a va2s.com
 * 
 * @param url
 *            URL.
 * @param fileNameContainingExpectedContent
 *            Name of file containing the expected content.
 */
private void doTest(String url, String fileNameContainingExpectedContent) throws IOException {

    // Undertake the request
    HttpResponse response = this.client.execute(new HttpGet("http://localhost:7878/" + url));
    assertEquals("Incorrect response status for URL " + url, 200, response.getStatusLine().getStatusCode());
    String content = EntityUtils.toString(response.getEntity());

    // Obtain the expected content
    InputStream contentInputStream = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream(fileNameContainingExpectedContent);
    assertNotNull("Can not find file " + fileNameContainingExpectedContent, contentInputStream);
    Reader reader = new InputStreamReader(contentInputStream);
    StringWriter expected = new StringWriter();
    for (int character = reader.read(); character != -1; character = reader.read()) {
        expected.append((char) character);
    }
    reader.close();

    // Ensure the context is as expected
    assertEquals("Incorrect content for URL " + url, expected.toString(), content);
}