Search code examples
javaparsingcommand-linecommand-line-argumentspicocli

Is it possible to convert OutStream to a string?


I have a method that uses DOM parsing to build an XML file and it takes an OutputStream as an argument. I'm trying to run the program from the command line, but the command line option only accepts strings.

I can run it by inputting System.out as an argument and running the the program, but that's it.

Here's a snippet of code:

public class WriteSourceTranslatedToTXML extends GetSourceSentences {

    public static String makeTranslated(OutputStream output, String out) throws ParserConfigurationException, IOException, SAXException, URISyntaxException {

        System.out.println("---creating XML file---");
        Document doc = new Document();
        doc.setRootElement(new Element("txml"));

        // Built XML here and inserted sentences
        xmlOutputter.setFormat(Format.getPrettyFormat());
        xmlOutputter.output(doc, output);

        // Write to file
        XMLOutputter xout = new XMLOutputter();
        String xml = xout.outputString(doc.getRootElement().getContent());

        try(FileOutputStream fileOutputStream =
               new FileOutputStream(out)) {
                   xmlOutputter.output(doc, fileOutputStream);

And here is the code for the command line:

@Command(name = "fileCli", description = "Performs file manipulation operations", mixinStandardHelpOptions = true, version = "File Client 1.0")

public class TranslateTXML implements Callable<String> {

    @Option(names = "-o", description = "output path")
    private String output;
    if (output != null) {
        WriteSourceTranslatedToTXML.makeTranslated(output); // Red line under output
        System.out.println("translated made");
        System.out.println("------");
        System.out.println("File \"translated.txml\" has been outputted to designated path");

How can I do it?


Solution

  • I used a different method by which Java builds the XML file.

    The new method call does not require an OutputStream argument, but simply a string, which I set as the filepath.

    public static void createXml(String directory) throws IOException, TransformerException, ParserConfigurationException {
    
        BufferedReader sourceReader = new BufferedReader(new FileReader(db));
    
        .
        .
    
        // Use DocumentBuilder and construct the XML file using NodeList Collection
        .
        .
    
        DOMSource source = new DOMSource(doc);
    
        // Write to console or file
        StreamResult console = new StreamResult(System.out);
        StreamResult file = new StreamResult(new File(directory));
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
    
        // For pretty print
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    
        // Write data
        transformer.transform(source, console);
        transformer.transform(source, file);
        sourceReader.close();
    

    So by using DOM parser, Transformer, and StreamResult, I can build the .xml file and store it in a Stream with StreamResult (which requires a String parameter for output). Voilà!