Example usage for java.io StringWriter write

List of usage examples for java.io StringWriter write

Introduction

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

Prototype

public void write(String str) 

Source Link

Document

Write a string.

Usage

From source file:org.apache.maven.plugin.javadoc.AbstractFixJavadocMojo.java

/**
 * Add a default Javadoc for the given field, i.e.:
 * <br/>/*from www .  ja  va 2s  .  c  o m*/
 * <code>
 * <font color="#808080">1</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font>
 * <font color="#3f5fbf">&#47;&#42;&#42;&nbsp;Constant&nbsp;</font><font color="#7f7f9f">&lt;code&gt;</font>
 * <font color="#3f5fbf">MY_STRING_CONSTANT=&#34;value&#34;</font>
 * <font color="#7f7f9f">&lt;/code&gt;&nbsp;</font><font color="#3f5fbf">&#42;&#47;</font><br />
 * <font color="#808080">2</font>&nbsp;<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font>
 * <font color="#7f0055"><b>public&nbsp;static&nbsp;final&nbsp;</b></font>
 * <font color="#000000">String&nbsp;MY_STRING_CONSTANT&nbsp;=&nbsp;</font>
 * <font color="#2a00ff">&#34;value&#34;</font><font color="#000000">;</font>
 * </code>
 *
 * @param stringWriter not null
 * @param field        not null
 * @param indent       not null
 * @throws IOException if any
 */
private void addDefaultFieldComment(final StringWriter stringWriter, final JavaField field, final String indent)
        throws IOException {
    StringBuilder sb = new StringBuilder();

    sb.append(indent).append(START_JAVADOC).append(" ");
    sb.append("Constant <code>").append(field.getName());

    if (StringUtils.isNotEmpty(field.getInitializationExpression())) {
        String qualifiedName = field.getType().getJavaClass().getFullyQualifiedName();

        if (qualifiedName.equals(Byte.TYPE.toString()) || qualifiedName.equals(Short.TYPE.toString())
                || qualifiedName.equals(Integer.TYPE.toString()) || qualifiedName.equals(Long.TYPE.toString())
                || qualifiedName.equals(Float.TYPE.toString()) || qualifiedName.equals(Double.TYPE.toString())
                || qualifiedName.equals(Boolean.TYPE.toString())
                || qualifiedName.equals(Character.TYPE.toString())) {
            sb.append("=");
            sb.append(field.getInitializationExpression().trim());
        }

        if (qualifiedName.equals(String.class.getName())) {
            StringBuilder value = new StringBuilder();
            String[] lines = getLines(field.getInitializationExpression());
            for (String line : lines) {
                StringTokenizer token = new StringTokenizer(line.trim(), "\"\n\r");
                while (token.hasMoreTokens()) {
                    String s = token.nextToken();

                    if (s.trim().equals("+")) {
                        continue;
                    }
                    if (s.trim().endsWith("\\")) {
                        s += "\"";
                    }
                    value.append(s);
                }
            }

            sb.append("=\"");
            // reduce the size
            if (value.length() < 40) {
                sb.append(value.toString()).append("\"");
            } else {
                sb.append(value.toString().substring(0, 39)).append("\"{trunked}");
            }
        }
    }

    sb.append("</code> ").append(END_JAVADOC);
    sb.append(EOL);

    stringWriter.write(sb.toString());
}

From source file:org.apache.maven.plugin.javadoc.AbstractFixJavadocMojo.java

/**
 * Process the given {@link JavaClass}, ie add missing javadoc tags depending user parameters.
 *
 * @param javaClass not null//from  w w w.  j  a  v a2  s  .co  m
 * @throws IOException            if any
 * @throws MojoExecutionException if any
 */
private void processFix(JavaClass javaClass) throws IOException, MojoExecutionException {
    // Skipping inner classes
    if (javaClass.isInner()) {
        return;
    }

    File javaFile = new File(javaClass.getSource().getURL().getFile());
    // the original java content in memory
    final String originalContent = StringUtils.unifyLineSeparators(FileUtils.fileRead(javaFile, encoding));

    if (getLog().isDebugEnabled()) {
        getLog().debug("Fixing " + javaClass.getFullyQualifiedName());
    }

    final StringWriter stringWriter = new StringWriter();
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new StringReader(originalContent));

        String line;
        int lineNumber = 0;
        while ((line = reader.readLine()) != null) {
            lineNumber++;
            final String indent = autodetectIndentation(line);

            // fixing classes
            if (javaClass.getComment() == null && javaClass.getAnnotations() != null
                    && javaClass.getAnnotations().length != 0) {
                if (lineNumber == javaClass.getAnnotations()[0].getLineNumber()) {
                    fixClassComment(stringWriter, originalContent, javaClass, indent);

                    takeCareSingleComment(stringWriter, originalContent, javaClass);
                }
            } else {
                if (lineNumber == javaClass.getLineNumber()) {
                    fixClassComment(stringWriter, originalContent, javaClass, indent);

                    takeCareSingleComment(stringWriter, originalContent, javaClass);
                }
            }

            // fixing fields
            if (javaClass.getFields() != null) {
                for (int i = 0; i < javaClass.getFields().length; i++) {
                    JavaField field = javaClass.getFields()[i];

                    if (lineNumber == field.getLineNumber()) {
                        fixFieldComment(stringWriter, javaClass, field, indent);
                    }
                }
            }

            // fixing methods
            if (javaClass.getMethods() != null) {
                for (int i = 0; i < javaClass.getMethods().length; i++) {
                    JavaMethod method = javaClass.getMethods()[i];

                    if (lineNumber == method.getLineNumber()) {
                        fixMethodComment(stringWriter, originalContent, method, indent);

                        takeCareSingleComment(stringWriter, originalContent, method);
                    }
                }
            }

            stringWriter.write(line);
            stringWriter.write(EOL);
        }
    } finally {
        IOUtil.close(reader);
    }

    if (getLog().isDebugEnabled()) {
        getLog().debug("Saving " + javaClass.getFullyQualifiedName());
    }

    if (outputDirectory != null
            && !outputDirectory.getAbsolutePath().equals(getProjectSourceDirectory().getAbsolutePath())) {
        String path = StringUtils.replace(javaFile.getAbsolutePath().replaceAll("\\\\", "/"),
                project.getBuild().getSourceDirectory().replaceAll("\\\\", "/"), "");
        javaFile = new File(outputDirectory, path);
        javaFile.getParentFile().mkdirs();
    }
    writeFile(javaFile, encoding, stringWriter.toString());
}