Search in sources :

Example 66 with Writer

use of java.io.Writer in project cogtool by cogtool.

the class ProjectController method exportDesignToXML.

private boolean exportDesignToXML(Design design) {
    String defaultFilename = ((design != null) ? design.getName() : project.getName()) + ".xml";
    File exportFile = null;
    if (interaction != null && CogTool.exportCSVKludgeDir == null) {
        exportFile = interaction.selectXMLFile(false, defaultFilename);
    } else {
        exportFile = new File(CogTool.exportCSVKludgeDir, defaultFilename);
    }
    if (exportFile == null) {
        return false;
    }
    OutputStream oStream = null;
    String completionMsg;
    try {
        try {
            oStream = new FileOutputStream(exportFile);
            Writer xmlWriter = new BufferedWriter(new OutputStreamWriter(oStream, "UTF-8"));
            if (design == null) {
                ExportCogToolXML.exportXML(project, xmlWriter, "UTF-8");
                completionMsg = allDesignsExportedToXml;
            } else {
                Map<ITaskDesign, TaskApplication> designTAs = project.taskApplicationsForDesign(design);
                ExportCogToolXML.exportXML(design, designTAs, xmlWriter, "UTF-8");
                completionMsg = designExportedToXml;
            }
            xmlWriter.flush();
        } finally {
            if (oStream != null) {
                oStream.close();
            }
        }
    } catch (IllegalArgumentException e) {
        throw new RcvrIllegalStateException("Invalid argument exporting to XML", e);
    } catch (IllegalStateException e) {
        throw new RcvrIllegalStateException("Exporting to XML", e);
    } catch (IOException e) {
        throw new RcvrIOSaveException("Exporting to XML", e);
    } catch (Exception e) {
        throw new RecoverableException("Exporting to XML", e);
    }
    if (interaction != null) {
        interaction.setStatusMessage(completionMsg + " " + exportFile.getAbsolutePath());
    }
    return true;
}
Also used : RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) ITaskDesign(edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) RcvrIOSaveException(edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException) RcvrXMLParsingException(edu.cmu.cs.hcii.cogtool.util.RcvrXMLParsingException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RcvrUnimplementedFnException(edu.cmu.cs.hcii.cogtool.util.RcvrUnimplementedFnException) RcvrCannotUndoRedoException(edu.cmu.cs.hcii.cogtool.util.RcvrCannotUndoRedoException) IOException(java.io.IOException) RcvrImportException(edu.cmu.cs.hcii.cogtool.util.RcvrImportException) RcvrClipboardException(edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException) RcvrImageException(edu.cmu.cs.hcii.cogtool.util.RcvrImageException) RcvrOutOfMemoryException(edu.cmu.cs.hcii.cogtool.util.RcvrOutOfMemoryException) FileNotFoundException(java.io.FileNotFoundException) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) SAXException(org.xml.sax.SAXException) RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) RcvrParsingException(edu.cmu.cs.hcii.cogtool.util.RcvrParsingException) RecoverableException(edu.cmu.cs.hcii.cogtool.util.RecoverableException) RcvrIOSaveException(edu.cmu.cs.hcii.cogtool.util.RcvrIOSaveException) RcvrIOTempException(edu.cmu.cs.hcii.cogtool.util.RcvrIOTempException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) BufferedWriter(java.io.BufferedWriter) RcvrIllegalStateException(edu.cmu.cs.hcii.cogtool.util.RcvrIllegalStateException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) TaskApplication(edu.cmu.cs.hcii.cogtool.model.TaskApplication) File(java.io.File) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) FileWriter(java.io.FileWriter) RecoverableException(edu.cmu.cs.hcii.cogtool.util.RecoverableException)

Example 67 with Writer

use of java.io.Writer in project cw-omnibus by commonsguy.

the class ScheduledService method append.

private void append(File f, RandomEvent event) {
    try {
        FileOutputStream fos = new FileOutputStream(f, true);
        Writer osw = new OutputStreamWriter(fos);
        osw.write(event.when.toString());
        osw.write(" : ");
        osw.write(Integer.toHexString(event.value));
        osw.write('\n');
        osw.flush();
        fos.flush();
        fos.getFD().sync();
        fos.close();
        Log.d(getClass().getSimpleName(), "logged to " + f.getAbsolutePath());
    } catch (IOException e) {
        Log.e(getClass().getSimpleName(), "Exception writing to file", e);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 68 with Writer

use of java.io.Writer in project cw-omnibus by commonsguy.

the class ScheduledService method append.

private void append(File f, RandomEvent event) {
    try {
        FileOutputStream fos = new FileOutputStream(f, true);
        Writer osw = new OutputStreamWriter(fos);
        osw.write(event.when.toString());
        osw.write(" : ");
        osw.write(Integer.toHexString(event.value));
        osw.write('\n');
        osw.flush();
        fos.flush();
        fos.getFD().sync();
        fos.close();
        Log.d(getClass().getSimpleName(), "logged to " + f.getAbsolutePath());
    } catch (IOException e) {
        Log.e(getClass().getSimpleName(), "Exception writing to file", e);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 69 with Writer

use of java.io.Writer in project cucumber-jvm by cucumber.

the class URLOutputStreamTest method write_to_file_without_existing_parent_directory.

@Test
public void write_to_file_without_existing_parent_directory() throws IOException, URISyntaxException {
    Path filesWithoutParent = Files.createTempDirectory("filesWithoutParent");
    String baseURL = filesWithoutParent.toUri().toURL().toString();
    URL urlWithoutParentDirectory = new URL(baseURL + "/non/existing/directory");
    Writer w = new UTF8OutputStreamWriter(new URLOutputStream(urlWithoutParentDirectory));
    w.write("Hellesøy");
    w.close();
    File testFile = new File(urlWithoutParentDirectory.toURI());
    assertEquals("Hellesøy", FixJava.readReader(openUTF8FileReader(testFile)));
}
Also used : Path(java.nio.file.Path) File(java.io.File) URL(java.net.URL) Writer(java.io.Writer) Test(org.junit.Test)

Example 70 with Writer

use of java.io.Writer in project cucumber-jvm by cucumber.

the class URLOutputStreamTest method can_http_put.

@Test
public void can_http_put() throws IOException, ExecutionException, InterruptedException {
    final BlockingQueue<String> data = new LinkedBlockingDeque<String>();
    Rest r = new Rest(webbit);
    r.PUT("/.cucumber/stepdefs.json", new HttpHandler() {

        @Override
        public void handleHttpRequest(HttpRequest req, HttpResponse res, HttpControl ctl) throws Exception {
            data.offer(req.body());
            res.end();
        }
    });
    Writer w = new UTF8OutputStreamWriter(new URLOutputStream(new URL(Utils.toURL("http://localhost:9873/.cucumber"), "stepdefs.json")));
    w.write("Hellesøy");
    w.flush();
    w.close();
    assertEquals("Hellesøy", data.poll(1000, TimeUnit.MILLISECONDS));
}
Also used : HttpRequest(org.webbitserver.HttpRequest) HttpHandler(org.webbitserver.HttpHandler) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) HttpResponse(org.webbitserver.HttpResponse) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL) Rest(org.webbitserver.rest.Rest) HttpControl(org.webbitserver.HttpControl) Writer(java.io.Writer) Test(org.junit.Test)

Aggregations

Writer (java.io.Writer)1259 OutputStreamWriter (java.io.OutputStreamWriter)512 IOException (java.io.IOException)414 StringWriter (java.io.StringWriter)300 File (java.io.File)269 FileOutputStream (java.io.FileOutputStream)196 BufferedWriter (java.io.BufferedWriter)178 FileWriter (java.io.FileWriter)174 PrintWriter (java.io.PrintWriter)159 OutputStream (java.io.OutputStream)120 Test (org.junit.Test)109 InputStreamReader (java.io.InputStreamReader)71 ByteArrayOutputStream (java.io.ByteArrayOutputStream)64 BufferedReader (java.io.BufferedReader)62 Reader (java.io.Reader)62 HashMap (java.util.HashMap)59 Map (java.util.Map)59 ArrayList (java.util.ArrayList)58 InputStream (java.io.InputStream)54 Properties (java.util.Properties)39