Search in sources :

Example 76 with CharArrayReader

use of java.io.CharArrayReader in project openmrs-core by openmrs.

the class ObsServiceTest method saveObs_shouldDeleteThePreviousFileWhenAComplexObservationIsUpdatedWithANewComplexValue.

/**
 * @see ObsService#saveObs(Obs,String)
 */
@Test
public void saveObs_shouldDeleteThePreviousFileWhenAComplexObservationIsUpdatedWithANewComplexValue() {
    String changeMessage = "Testing TRUNK-4538";
    executeDataSet(COMPLEX_OBS_XML);
    ObsService os = Context.getObsService();
    ConceptService cs = Context.getConceptService();
    AdministrationService as = Context.getAdministrationService();
    // make sure the file isn't there to begin with
    File complexObsDir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_COMPLEX_OBS_DIR));
    final File createdFile = new File(complexObsDir, "nameOfFile.txt");
    if (createdFile.exists())
        createdFile.delete();
    // the complex data to put onto an obs that will be saved
    Reader input = new CharArrayReader("This is a string to save to a file".toCharArray());
    ComplexData complexData = new ComplexData("nameOfFile.txt", input);
    // must fetch the concept instead of just new Concept(8473) because the attributes on concept are checked
    // this is a concept mapped to the text handler
    Concept questionConcept = cs.getConcept(8474);
    Obs obsToSave = new Obs(new Person(1), questionConcept, new Date(), new Location(1));
    obsToSave.setComplexData(complexData);
    os.saveObs(obsToSave, null);
    File updatedFile = new File(complexObsDir, "nameOfUpdatedFile.txt");
    if (updatedFile.exists())
        updatedFile.delete();
    // the complex data to put onto an obs that will be updated
    Reader updatedInput = new CharArrayReader("This is a string to save to a file which uploaded to update an obs".toCharArray());
    ComplexData updatedComplexData = new ComplexData("nameOfUpdatedFile.txt", updatedInput);
    obsToSave.setComplexData(updatedComplexData);
    try {
        os.saveObs(obsToSave, changeMessage);
        Assert.assertFalse(createdFile.exists());
    } finally {
        // we always have to delete this inside the same unit test because it is outside the
        // database and hence can't be "rolled back" like everything else
        updatedFile.delete();
    }
}
Also used : Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) CharArrayReader(java.io.CharArrayReader) Reader(java.io.Reader) Date(java.util.Date) CharArrayReader(java.io.CharArrayReader) ComplexData(org.openmrs.obs.ComplexData) File(java.io.File) Person(org.openmrs.Person) Location(org.openmrs.Location) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 77 with CharArrayReader

use of java.io.CharArrayReader in project openmrs-core by openmrs.

the class ObsServiceTest method saveObs_shouldNotOverwriteFileWhenUpdatingAComplexObs.

/**
 * @throws IOException
 * @see ObsService#saveObs(Obs,String)
 */
@Test
public void saveObs_shouldNotOverwriteFileWhenUpdatingAComplexObs() throws IOException {
    executeDataSet(COMPLEX_OBS_XML);
    ObsService os = Context.getObsService();
    ConceptService cs = Context.getConceptService();
    AdministrationService as = Context.getAdministrationService();
    // Create the file that was supposedly put there by another obs
    File complexObsDir = OpenmrsUtil.getDirectoryInApplicationDataDirectory(as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_COMPLEX_OBS_DIR));
    File previouslyCreatedFile = new File(complexObsDir, "nameOfFile.txt");
    FileUtils.writeByteArrayToFile(previouslyCreatedFile, "a string to save to a file".getBytes());
    // the file we'll be creating...defining it here so we can delete it in a finally block
    File newComplexFile = null;
    try {
        long oldFileSize = previouslyCreatedFile.length();
        // now add a new file to this obs and update it
        // ...then make sure the original file is still there
        // the complex data to put onto an obs that will be saved
        Reader input2 = new CharArrayReader("diff string to save to a file with the same name".toCharArray());
        ComplexData complexData = new ComplexData("nameOfFile.txt", input2);
        // must fetch the concept instead of just new Concept(8473) because the attributes on concept are checked
        // this is a concept mapped to the text handler
        Concept questionConcept = cs.getConcept(8474);
        Obs obsToSave = new Obs(new Person(1), questionConcept, new Date(), new Location(1));
        obsToSave.setComplexData(complexData);
        os.saveObs(obsToSave, null);
        // make sure the old file still appears now after the save
        Assert.assertEquals(oldFileSize, previouslyCreatedFile.length());
        String valueComplex = obsToSave.getValueComplex();
        String filename = valueComplex.substring(valueComplex.indexOf("|") + 1).trim();
        newComplexFile = new File(complexObsDir, filename);
        // make sure the file appears now after the save
        Assert.assertTrue(newComplexFile.length() > oldFileSize);
    } finally {
        // clean up the files we created
        newComplexFile.delete();
        try {
            previouslyCreatedFile.delete();
        } catch (Exception e) {
        // pass
        }
    }
}
Also used : Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) CharArrayReader(java.io.CharArrayReader) Reader(java.io.Reader) Date(java.util.Date) ParseException(java.text.ParseException) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) CharArrayReader(java.io.CharArrayReader) ComplexData(org.openmrs.obs.ComplexData) File(java.io.File) Person(org.openmrs.Person) Location(org.openmrs.Location) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest) Test(org.junit.Test)

Example 78 with CharArrayReader

use of java.io.CharArrayReader in project Java-Tutorial by gpcodervn.

the class PushbackReaderExample method main.

public static void main(String[] args) throws Exception {
    char[] ary = { '1', '-', '-', '2', '-', '3', '4', '-', '-', '-', '5', '6' };
    CharArrayReader reader = new CharArrayReader(ary);
    PushbackReader push = new PushbackReader(reader);
    int i;
    while ((i = push.read()) != -1) {
        // Tìm thấy ký tự '-'
        if (i == '-') {
            int j;
            // Đọc tiếp một ký tự nữa
            if ((j = push.read()) == '-') {
                System.out.print("#*");
            } else {
                // Đẩy trở lại (Pushes back) ký tự này lên luồng.
                // Giống như lùi con trỏ trở lại 1 vị trí.
                push.unread(j);
                System.out.print((char) i);
            }
        } else {
            System.out.print((char) i);
        }
    }
}
Also used : CharArrayReader(java.io.CharArrayReader) PushbackReader(java.io.PushbackReader)

Example 79 with CharArrayReader

use of java.io.CharArrayReader in project validator by validator.

the class ParserPerfHarnessNew method runLoop.

public long runLoop() throws SAXException, IOException {
    long times = 0;
    while (System.currentTimeMillis() < endTime) {
        InputSource inputSource = new InputSource(new CharArrayReader(testData));
        inputSource.setEncoding("utf-8");
        reader.parse(inputSource);
        times++;
    }
    return times;
}
Also used : InputSource(org.xml.sax.InputSource) CharArrayReader(java.io.CharArrayReader)

Example 80 with CharArrayReader

use of java.io.CharArrayReader in project tetrad by cmu-phil.

the class TabularDataTransferHandler method importData.

public boolean importData(JComponent c, Transferable t) {
    if (c instanceof TabularDataJTable) {
        try {
            TabularDataJTable tabularData = (TabularDataJTable) c;
            String s = (String) t.getTransferData(DataFlavor.stringFlavor);
            int startRow = tabularData.getSelectedRow();
            int startCol = tabularData.getSelectedColumn();
            if (startRow == 0) {
                startRow = 1;
            }
            if (startCol < getNumLeadingCols()) {
                startCol = getNumLeadingCols();
            }
            if (!checkRanges(s, startCol, tabularData)) {
                return false;
            }
            boolean shouldAsk = false;
            boolean shiftDown = true;
            BufferedReader preReader = new BufferedReader(new CharArrayReader(s.toCharArray()));
            String preLine = preReader.readLine();
            StringTokenizer preTokenizer = new StringTokenizer(preLine, "\t");
            int numTokens = preTokenizer.countTokens();
            for (int col = startCol; col < startCol + numTokens; col++) {
                Object value = tabularData.getValueAt(startRow, col);
                if (!"".equals(value) && !(null == value)) {
                    shouldAsk = true;
                }
                if (startRow - getNumLeadingRows() >= tabularData.getDataSet().getNumRows() || startCol - getNumLeadingCols() >= tabularData.getDataSet().getNumColumns()) {
                    shouldAsk = false;
                    shiftDown = false;
                }
            }
            if (shouldAsk) {
                String[] choices = new String[] { "Shift corresponding cells down to make room", "Replace corresponding cells" };
                Object choice = JOptionPane.showInputDialog(JOptionUtils.centeringComp(), "How should the clipboard contents be pasted?", "Paste Contents", JOptionPane.INFORMATION_MESSAGE, null, choices, choices[0]);
                // Null means the user cancelled the input.
                if (choice == null) {
                    return false;
                }
                shiftDown = choice.equals(choices[0]);
            }
            doPaste(s, startRow, startCol, shiftDown, tabularData);
        } catch (UnsupportedFlavorException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }
    return false;
}
Also used : StringTokenizer(java.util.StringTokenizer) CharArrayReader(java.io.CharArrayReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException)

Aggregations

CharArrayReader (java.io.CharArrayReader)102 IOException (java.io.IOException)45 BufferedReader (java.io.BufferedReader)22 Reader (java.io.Reader)19 File (java.io.File)11 InputStreamReader (java.io.InputStreamReader)10 Test (org.junit.Test)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 StringReader (java.io.StringReader)8 FileReader (java.io.FileReader)7 PreparedStatement (java.sql.PreparedStatement)7 BufferedWriter (java.io.BufferedWriter)5 CharArrayWriter (java.io.CharArrayWriter)5 FileWriter (java.io.FileWriter)5 InputStream (java.io.InputStream)5 ResultSet (java.sql.ResultSet)5 InputSource (org.xml.sax.InputSource)5 PipedReader (java.io.PipedReader)4 CharBuffer (java.nio.CharBuffer)4 Signature (java.security.Signature)4