Search in sources :

Example 61 with CharArrayWriter

use of java.io.CharArrayWriter in project engineblock by engineblock.

the class BufferAppender method start.

@Override
public void start() {
    buffer = new CharArrayWriter(0);
    super.start();
}
Also used : CharArrayWriter(java.io.CharArrayWriter)

Example 62 with CharArrayWriter

use of java.io.CharArrayWriter in project ceylon-compiler by ceylon.

the class JavaPositionsRetriever method getJavaSourceCodeWithCeylonPositions.

public String getJavaSourceCodeWithCeylonPositions() {
    final CharArrayWriter writer = new CharArrayWriter();
    Pretty printer = new Pretty(writer, true) {

        int previousCeylonPosition = -1;

        int previousPositionInString = 0;

        private void outputCeylonPosition(JCTree tree) {
            try {
                int currentCeylonPosition = tree.getPreferredPosition();
                int currentPositionInString = writer.size();
                if (previousCeylonPosition != currentCeylonPosition || previousPositionInString != currentPositionInString) {
                    if (currentCeylonPosition != -1 && currentCeylonPosition != 0) {
                        writer.write("/* " + formatCeylonPosition(currentCeylonPosition) + " */");
                    }
                    previousCeylonPosition = currentCeylonPosition;
                    previousPositionInString = writer.size();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void visitTopLevel(JCCompilationUnit tree) {
            outputCeylonPosition(tree);
            super.visitTopLevel(tree);
        }

        @Override
        public void visitImport(JCImport tree) {
            outputCeylonPosition(tree);
            super.visitImport(tree);
        }

        @Override
        public void visitClassDef(JCClassDecl tree) {
            outputCeylonPosition(tree);
            super.visitClassDef(tree);
        }

        @Override
        public void visitMethodDef(JCMethodDecl tree) {
            outputCeylonPosition(tree);
            super.visitMethodDef(tree);
        }

        @Override
        public void visitVarDef(JCVariableDecl tree) {
            outputCeylonPosition(tree);
            super.visitVarDef(tree);
        }

        @Override
        public void visitSkip(JCSkip tree) {
            outputCeylonPosition(tree);
            super.visitSkip(tree);
        }

        @Override
        public void visitBlock(JCBlock tree) {
            outputCeylonPosition(tree);
            super.visitBlock(tree);
            tree.endpos = currentPosition - 1;
        }

        @Override
        public void visitDoLoop(JCDoWhileLoop tree) {
            outputCeylonPosition(tree);
            super.visitDoLoop(tree);
        }

        @Override
        public void visitWhileLoop(JCWhileLoop tree) {
            outputCeylonPosition(tree);
            super.visitWhileLoop(tree);
        }

        @Override
        public void visitForLoop(JCForLoop tree) {
            outputCeylonPosition(tree);
            super.visitForLoop(tree);
        }

        @Override
        public void visitForeachLoop(JCEnhancedForLoop tree) {
            outputCeylonPosition(tree);
            super.visitForeachLoop(tree);
        }

        @Override
        public void visitLabelled(JCLabeledStatement tree) {
            outputCeylonPosition(tree);
            super.visitLabelled(tree);
        }

        @Override
        public void visitSwitch(JCSwitch tree) {
            outputCeylonPosition(tree);
            super.visitSwitch(tree);
        }

        @Override
        public void visitCase(JCCase tree) {
            outputCeylonPosition(tree);
            super.visitCase(tree);
        }

        @Override
        public void visitSynchronized(JCSynchronized tree) {
            outputCeylonPosition(tree);
            super.visitSynchronized(tree);
        }

        @Override
        public void visitTry(JCTry tree) {
            outputCeylonPosition(tree);
            super.visitTry(tree);
        }

        @Override
        public void visitCatch(JCCatch tree) {
            outputCeylonPosition(tree);
            super.visitCatch(tree);
        }

        @Override
        public void visitConditional(JCConditional tree) {
            outputCeylonPosition(tree);
            super.visitConditional(tree);
        }

        @Override
        public void visitIf(JCIf tree) {
            outputCeylonPosition(tree);
            super.visitIf(tree);
        }

        @Override
        public void visitExec(JCExpressionStatement tree) {
            outputCeylonPosition(tree);
            super.visitExec(tree);
        }

        @Override
        public void visitBreak(JCBreak tree) {
            outputCeylonPosition(tree);
            super.visitBreak(tree);
        }

        @Override
        public void visitContinue(JCContinue tree) {
            outputCeylonPosition(tree);
            super.visitContinue(tree);
        }

        @Override
        public void visitReturn(JCReturn tree) {
            outputCeylonPosition(tree);
            super.visitReturn(tree);
        }

        @Override
        public void visitThrow(JCThrow tree) {
            outputCeylonPosition(tree);
            super.visitThrow(tree);
        }

        @Override
        public void visitAssert(JCAssert tree) {
            outputCeylonPosition(tree);
            super.visitAssert(tree);
        }

        @Override
        public void visitApply(JCMethodInvocation tree) {
            outputCeylonPosition(tree);
            super.visitApply(tree);
        }

        @Override
        public void visitNewClass(JCNewClass tree) {
            outputCeylonPosition(tree);
            super.visitNewClass(tree);
        }

        @Override
        public void visitNewArray(JCNewArray tree) {
            outputCeylonPosition(tree);
            super.visitNewArray(tree);
        }

        @Override
        public void visitParens(JCParens tree) {
            outputCeylonPosition(tree);
            super.visitParens(tree);
        }

        @Override
        public void visitAssign(JCAssign tree) {
            outputCeylonPosition(tree);
            super.visitAssign(tree);
        }

        @Override
        public void visitAssignop(JCAssignOp tree) {
            outputCeylonPosition(tree);
            super.visitAssignop(tree);
        }

        @Override
        public void visitUnary(JCUnary tree) {
            outputCeylonPosition(tree);
            super.visitUnary(tree);
        }

        @Override
        public void visitBinary(JCBinary tree) {
            outputCeylonPosition(tree);
            super.visitBinary(tree);
        }

        @Override
        public void visitTypeCast(JCTypeCast tree) {
            outputCeylonPosition(tree);
            super.visitTypeCast(tree);
        }

        @Override
        public void visitTypeTest(JCInstanceOf tree) {
            outputCeylonPosition(tree);
            super.visitTypeTest(tree);
        }

        @Override
        public void visitIndexed(JCArrayAccess tree) {
            outputCeylonPosition(tree);
            super.visitIndexed(tree);
        }

        @Override
        public void visitSelect(JCFieldAccess tree) {
            outputCeylonPosition(tree);
            super.visitSelect(tree);
        }

        @Override
        public void visitIdent(JCIdent tree) {
            outputCeylonPosition(tree);
            super.visitIdent(tree);
        }

        @Override
        public void visitLiteral(JCLiteral tree) {
            outputCeylonPosition(tree);
            super.visitLiteral(tree);
        }

        @Override
        public void visitTypeIdent(JCPrimitiveTypeTree tree) {
            outputCeylonPosition(tree);
            super.visitTypeIdent(tree);
        }

        @Override
        public void visitTypeArray(JCArrayTypeTree tree) {
            outputCeylonPosition(tree);
            super.visitTypeArray(tree);
        }

        @Override
        public void visitTypeApply(JCTypeApply tree) {
            outputCeylonPosition(tree);
            super.visitTypeApply(tree);
        }

        @Override
        public void visitTypeParameter(JCTypeParameter tree) {
            outputCeylonPosition(tree);
            super.visitTypeParameter(tree);
        }

        @Override
        public void visitWildcard(JCWildcard tree) {
            outputCeylonPosition(tree);
            super.visitWildcard(tree);
        }

        @Override
        public void visitTypeBoundKind(TypeBoundKind tree) {
            outputCeylonPosition(tree);
            super.visitTypeBoundKind(tree);
        }

        @Override
        public void visitErroneous(JCErroneous tree) {
            outputCeylonPosition(tree);
            super.visitErroneous(tree);
        }

        @Override
        public void visitLetExpr(LetExpr tree) {
            outputCeylonPosition(tree);
            super.visitLetExpr(tree);
        }

        @Override
        public void visitModifiers(JCModifiers mods) {
            outputCeylonPosition(mods);
            super.visitModifiers(mods);
        }

        @Override
        public void visitAnnotation(JCAnnotation tree) {
            outputCeylonPosition(tree);
            super.visitAnnotation(tree);
        }

        @Override
        public void visitTree(JCTree tree) {
            outputCeylonPosition(tree);
            super.visitTree(tree);
        }
    };
    printer.visitTopLevel(unit);
    return writer.toString();
}
Also used : JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) JCTypeApply(com.sun.tools.javac.tree.JCTree.JCTypeApply) JCAssert(com.sun.tools.javac.tree.JCTree.JCAssert) JCPrimitiveTypeTree(com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCIf(com.sun.tools.javac.tree.JCTree.JCIf) JCEnhancedForLoop(com.sun.tools.javac.tree.JCTree.JCEnhancedForLoop) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) JCNewArray(com.sun.tools.javac.tree.JCTree.JCNewArray) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) JCCase(com.sun.tools.javac.tree.JCTree.JCCase) JCThrow(com.sun.tools.javac.tree.JCTree.JCThrow) JCImport(com.sun.tools.javac.tree.JCTree.JCImport) JCWildcard(com.sun.tools.javac.tree.JCTree.JCWildcard) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) LetExpr(com.sun.tools.javac.tree.JCTree.LetExpr) JCErroneous(com.sun.tools.javac.tree.JCTree.JCErroneous) JCSynchronized(com.sun.tools.javac.tree.JCTree.JCSynchronized) JCParens(com.sun.tools.javac.tree.JCTree.JCParens) JCDoWhileLoop(com.sun.tools.javac.tree.JCTree.JCDoWhileLoop) JCContinue(com.sun.tools.javac.tree.JCTree.JCContinue) JCInstanceOf(com.sun.tools.javac.tree.JCTree.JCInstanceOf) TypeBoundKind(com.sun.tools.javac.tree.JCTree.TypeBoundKind) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCUnary(com.sun.tools.javac.tree.JCTree.JCUnary) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCCatch(com.sun.tools.javac.tree.JCTree.JCCatch) JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JCWhileLoop(com.sun.tools.javac.tree.JCTree.JCWhileLoop) JCReturn(com.sun.tools.javac.tree.JCTree.JCReturn) JCLabeledStatement(com.sun.tools.javac.tree.JCTree.JCLabeledStatement) JCAssign(com.sun.tools.javac.tree.JCTree.JCAssign) JCSkip(com.sun.tools.javac.tree.JCTree.JCSkip) JCConditional(com.sun.tools.javac.tree.JCTree.JCConditional) JCExpressionStatement(com.sun.tools.javac.tree.JCTree.JCExpressionStatement) CharArrayWriter(java.io.CharArrayWriter) JCTypeCast(com.sun.tools.javac.tree.JCTree.JCTypeCast) JCArrayTypeTree(com.sun.tools.javac.tree.JCTree.JCArrayTypeTree) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JCTree(com.sun.tools.javac.tree.JCTree) JCBinary(com.sun.tools.javac.tree.JCTree.JCBinary) JCArrayAccess(com.sun.tools.javac.tree.JCTree.JCArrayAccess) IOException(java.io.IOException) JCForLoop(com.sun.tools.javac.tree.JCTree.JCForLoop) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Pretty(com.sun.tools.javac.tree.Pretty) JCTry(com.sun.tools.javac.tree.JCTree.JCTry) JCSwitch(com.sun.tools.javac.tree.JCTree.JCSwitch) JCLiteral(com.sun.tools.javac.tree.JCTree.JCLiteral) JCAssignOp(com.sun.tools.javac.tree.JCTree.JCAssignOp) JCBreak(com.sun.tools.javac.tree.JCTree.JCBreak)

Example 63 with CharArrayWriter

use of java.io.CharArrayWriter in project jersey by jersey.

the class WadlGeneratorJAXBGrammarGenerator method buildModelAndSchemas.

/**
     * Build the JAXB model and generate the schemas based on tha data
     *
     * @param extraFiles additional files.
     * @return class to {@link QName} resolver.
     */
private Resolver buildModelAndSchemas(final Map<String, ApplicationDescription.ExternalGrammar> extraFiles) {
    // Lets get all candidate classes so we can create the JAX-B context
    // include any @XmlSeeAlso references.
    final Set<Class> classSet = new HashSet<>(seeAlsoClasses);
    for (final TypeCallbackPair pair : nameCallbacks) {
        final GenericType genericType = pair.genericType;
        final Class<?> clazz = genericType.getRawType();
        if (clazz.getAnnotation(XmlRootElement.class) != null) {
            classSet.add(clazz);
        } else if (SPECIAL_GENERIC_TYPES.contains(clazz)) {
            final Type type = genericType.getType();
            if (type instanceof ParameterizedType) {
                final Type parameterType = ((ParameterizedType) type).getActualTypeArguments()[0];
                if (parameterType instanceof Class) {
                    classSet.add((Class) parameterType);
                }
            }
        }
    }
    // Create a JAX-B context, and use this to generate us a bunch of
    // schema objects
    JAXBIntrospector introspector = null;
    try {
        final JAXBContext context = JAXBContext.newInstance(classSet.toArray(new Class[classSet.size()]));
        final List<StreamResult> results = new ArrayList<>();
        context.generateSchema(new SchemaOutputResolver() {

            int counter = 0;

            @Override
            public Result createOutput(final String namespaceUri, final String suggestedFileName) {
                final StreamResult result = new StreamResult(new CharArrayWriter());
                result.setSystemId("xsd" + (counter++) + ".xsd");
                results.add(result);
                return result;
            }
        });
        for (final StreamResult result : results) {
            final CharArrayWriter writer = (CharArrayWriter) result.getWriter();
            final byte[] contents = writer.toString().getBytes("UTF8");
            extraFiles.put(result.getSystemId(), new ApplicationDescription.ExternalGrammar(// I don't think there is a specific media type for XML Schema
            MediaType.APPLICATION_XML_TYPE, contents));
        }
        // Create an introspector
        //
        introspector = context.createJAXBIntrospector();
    } catch (final JAXBException e) {
        LOGGER.log(Level.SEVERE, "Failed to generate the schema for the JAX-B elements", e);
    } catch (final IOException e) {
        LOGGER.log(Level.SEVERE, "Failed to generate the schema for the JAX-B elements due to an IO error", e);
    }
    if (introspector != null) {
        final JAXBIntrospector copy = introspector;
        return new Resolver() {

            public QName resolve(final Class type) {
                Object parameterClassInstance = null;
                try {
                    final Constructor<?> defaultConstructor = AccessController.doPrivileged(new PrivilegedExceptionAction<Constructor<?>>() {

                        @SuppressWarnings("unchecked")
                        @Override
                        public Constructor<?> run() throws NoSuchMethodException {
                            final Constructor<?> constructor = type.getDeclaredConstructor();
                            constructor.setAccessible(true);
                            return constructor;
                        }
                    });
                    parameterClassInstance = defaultConstructor.newInstance();
                } catch (final InstantiationException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
                    LOGGER.log(Level.FINE, null, ex);
                } catch (final PrivilegedActionException ex) {
                    LOGGER.log(Level.FINE, null, ex.getCause());
                }
                if (parameterClassInstance == null) {
                    return null;
                }
                try {
                    return copy.getElementName(parameterClassInstance);
                } catch (final NullPointerException e) {
                    // annotation is passed as a parameter of #getElementName method.
                    return null;
                }
            }
        };
    } else {
        // No resolver created
        return null;
    }
}
Also used : ArrayList(java.util.ArrayList) JAXBContext(javax.xml.bind.JAXBContext) CharArrayWriter(java.io.CharArrayWriter) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) ParameterizedType(java.lang.reflect.ParameterizedType) SchemaOutputResolver(javax.xml.bind.SchemaOutputResolver) HashSet(java.util.HashSet) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) GenericType(javax.ws.rs.core.GenericType) StreamResult(javax.xml.transform.stream.StreamResult) SchemaOutputResolver(javax.xml.bind.SchemaOutputResolver) PrivilegedActionException(java.security.PrivilegedActionException) Constructor(java.lang.reflect.Constructor) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) ApplicationDescription(org.glassfish.jersey.server.wadl.internal.ApplicationDescription) InvocationTargetException(java.lang.reflect.InvocationTargetException) JAXBIntrospector(javax.xml.bind.JAXBIntrospector) MediaType(javax.ws.rs.core.MediaType) GenericType(javax.ws.rs.core.GenericType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 64 with CharArrayWriter

use of java.io.CharArrayWriter in project tomcat by apache.

the class AbstractAccessLogValve method log.

@Override
public void log(Request request, Response response, long time) {
    if (!getState().isAvailable() || !getEnabled() || logElements == null || condition != null && null != request.getRequest().getAttribute(condition) || conditionIf != null && null == request.getRequest().getAttribute(conditionIf)) {
        return;
    }
    /**
         * XXX This is a bit silly, but we want to have start and stop time and
         * duration consistent. It would be better to keep start and stop
         * simply in the request and/or response object and remove time
         * (duration) from the interface.
         */
    long start = request.getCoyoteRequest().getStartTime();
    Date date = getDate(start + time);
    CharArrayWriter result = charArrayWriters.pop();
    if (result == null) {
        result = new CharArrayWriter(128);
    }
    for (int i = 0; i < logElements.length; i++) {
        logElements[i].addElement(result, date, request, response, time);
    }
    log(result);
    if (result.size() <= maxLogMessageBufferSize) {
        result.reset();
        charArrayWriters.push(result);
    }
}
Also used : Date(java.util.Date) CharArrayWriter(java.io.CharArrayWriter)

Example 65 with CharArrayWriter

use of java.io.CharArrayWriter in project hazelcast by hazelcast.

the class MultiLineDiagnosticsLogWriterTest method assertLongValue.

private void assertLongValue(long value) {
    CharArrayWriter out = new CharArrayWriter();
    MultiLineDiagnosticsLogWriter writer = new MultiLineDiagnosticsLogWriter();
    writer.init(new PrintWriter(out));
    writer.writeLong(value);
    String expected = String.format(Locale.US, "%,d", value);
    assertEquals(expected, out.toString());
}
Also used : CharArrayWriter(java.io.CharArrayWriter) PrintWriter(java.io.PrintWriter)

Aggregations

CharArrayWriter (java.io.CharArrayWriter)116 PrintWriter (java.io.PrintWriter)28 IndentingPrintWriter (com.android.internal.util.IndentingPrintWriter)21 IOException (java.io.IOException)16 BufferedReader (java.io.BufferedReader)7 ArrayList (java.util.ArrayList)7 CharArrayReader (java.io.CharArrayReader)6 Test (org.junit.Test)5 File (java.io.File)4 Reader (java.io.Reader)4 StringReader (java.io.StringReader)4 HashMap (java.util.HashMap)4 FeatureVector (com.airbnb.aerosolve.core.FeatureVector)3 ModelHeader (com.airbnb.aerosolve.core.ModelHeader)3 ModelRecord (com.airbnb.aerosolve.core.ModelRecord)3 BufferedWriter (java.io.BufferedWriter)3 InputStreamReader (java.io.InputStreamReader)3 StreamResult (javax.xml.transform.stream.StreamResult)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2