Example usage for java.io InputStreamReader getEncoding

List of usage examples for java.io InputStreamReader getEncoding

Introduction

In this page you can find the example usage for java.io InputStreamReader getEncoding.

Prototype

public String getEncoding() 

Source Link

Document

Returns the name of the character encoding being used by this stream.

Usage

From source file:com.evolveum.midpoint.web.page.admin.reports.PageNewReport.java

private void importReportFromFilePerformed(AjaxRequestTarget target) {
    OperationResult result = new OperationResult(OPERATION_IMPORT_REPORT);

    FileUploadField file = (FileUploadField) get(
            createComponentPath(ID_MAIN_FORM, ID_INPUT, ID_INPUT_FILE, ID_FILE_INPUT));
    final FileUpload uploadedFile = file.getFileUpload();
    if (uploadedFile == null) {
        error(getString("PageNewReport.message.nullFile"));
        target.add(getFeedbackPanel());/*  w ww.  j  ava2 s. com*/

        return;
    }

    InputStream stream = null;
    File newFile = null;
    try {
        // Create new file
        MidPointApplication application = getMidpointApplication();
        WebApplicationConfiguration config = application.getWebApplicationConfiguration();
        File folder = new File(config.getImportFolder());
        if (!folder.exists() || !folder.isDirectory()) {
            folder.mkdir();
        }

        newFile = new File(folder, uploadedFile.getClientFileName());
        // Check new file, delete if it already exists
        if (newFile.exists()) {
            newFile.delete();
        }
        // Save file
        //            Task task = createSimpleTask(OPERATION_IMPORT_FILE);
        newFile.createNewFile();
        uploadedFile.writeTo(newFile);

        InputStreamReader reader = new InputStreamReader(new FileInputStream(newFile), "utf-8");
        //            reader.
        stream = new ReaderInputStream(reader, reader.getEncoding());
        byte[] reportIn = IOUtils.toByteArray(stream);

        setResponsePage(new PageReport(new ReportDto(Base64.encodeBase64(reportIn))));
    } catch (Exception ex) {
        result.recordFatalError("Couldn't import file.", ex);
        LoggingUtils.logException(LOGGER, "Couldn't import file", ex);
    } finally {
        if (stream != null) {
            IOUtils.closeQuietly(stream);
        }
        if (newFile != null) {
            FileUtils.deleteQuietly(newFile);
        }
    }

    showResult(result);
    target.add(getFeedbackPanel());
}

From source file:CSV.HandlingCSV.java

public void searchElements() {
    CSVReader reader;//  ww w  .j  av  a2  s  .  c  o m
    List<String[]> entradas = new ArrayList<>();
    try {
        try {
            if ((this.url == null) || (!this.verifyConnectUrl())) {
                File file = new File("download" + System.getProperty("file.separator") + doc_nome);
                if (!file.exists()) {
                    File diretoria = new File("download");
                    diretoria.mkdir();
                    file.createNewFile();
                }
                InputStream input = new FileInputStream(file);
                InputStreamReader r = new InputStreamReader(new FileInputStream(file));
                reader = new CSVReader(new InputStreamReader(input, r.getEncoding()), ';');
                if (reader.verifyReader()) {
                    entradas = reader.readAll();
                }
            } else {
                InputStream input;
                try {
                    input = new URL(url).openStream();
                    reader = new CSVReader(new InputStreamReader(input), ';');
                    if (reader.verifyReader()) {
                        entradas = reader.readAll();
                        File file = new File("download" + System.getProperty("file.separator") + doc_nome);
                        if (!file.exists()) {
                            File diretoria = new File("download");
                            diretoria.mkdir();
                            file.createNewFile();
                            novo = true;
                            try (CSVWriter scv = new CSVWriter(new FileWriter(file), ';')) {
                                scv.writeAll(entradas);
                                scv.flush();
                            }
                        } else {
                            File file2 = new File("horario_disciplinas.csv");
                            try (CSVWriter scv = new CSVWriter(new FileWriter(file2), ';')) {
                                scv.writeAll(entradas);
                                scv.flush();
                            }
                            if (FileUtils.contentEquals(file, file2)) {
                                file2.delete();
                            } else {
                                try (CSVWriter scv = new CSVWriter(new FileWriter(file), ';')) {
                                    file2.delete();
                                    scv.writeAll(entradas);
                                    scv.flush();
                                    novo = true;
                                }
                            }
                        }
                    }
                } catch (MalformedURLException ex) {
                    Logger.getLogger(HandlingCSV.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(HandlingCSV.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(HandlingCSV.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(HandlingCSV.class.getName()).log(Level.SEVERE, null, ex);
        }
    } catch (Exception ex) {
    }
    if (!entradas.isEmpty()) {
        ElementsCSV elemento;
        for (int i = 1; entradas.size() > i; i++) {
            if (entradas.get(i).length >= this.tamanho) {
                elemento = new ElementsCSV(Integer.valueOf(entradas.get(i)[valores[0]]),
                        Integer.valueOf(entradas.get(i)[valores[1]]),
                        Integer.valueOf(entradas.get(i)[valores[2]]),
                        Integer.valueOf(entradas.get(i)[valores[3]]),
                        Integer.valueOf(entradas.get(i)[valores[4]]), entradas.get(i)[valores[5]],
                        entradas.get(i)[valores[6]], entradas.get(i)[valores[7]], entradas.get(i)[valores[8]],
                        entradas.get(i)[valores[9]], entradas.get(i)[valores[10]]);
                elementos.add(elemento);
            }
        }
    }

    // tratamento para elementos contguos,no entanto, apurados como separados

    if (!elementos.isEmpty()) {
        for (int k = 0; k < elementos.size(); k++) {
            for (int j = 0; j < elementos.size(); j++) {
                if (elementos.get(k).getMaterialCode().equals(elementos.get(j).getMaterialCode())) {
                    if (elementos.get(k).getCdSubject().equals(elementos.get(j).getCdSubject())) {
                        if (elementos.get(k).getPersonCode().equals(elementos.get(k).getPersonCode())) {
                            if (elementos.get(k).getDayWeek() == elementos.get(j).getDayWeek()) {
                                if (elementos.get(k).getHourEnd() == elementos.get(j).getHourIni()) {
                                    if (elementos.get(k).getMinuteEnd() == elementos.get(j).getMinuteIni()) {
                                        elementos.get(k).setHourEnd(elementos.get(j).getHourEnd());
                                        elementos.get(k).setMinuteEnd(elementos.get(j).getMinuteEnd());
                                        elementos.remove(j);
                                        k = 0;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

From source file:at.gv.egiz.slbinding.SLUnmarshaller.java

/**
 * @param source a StreamSource wrapping a Reader (!) for the marshalled Object
 * @return the unmarshalled Object//  w ww . ja v  a 2 s  . c o  m
 * @throws XMLStreamException
 * @throws JAXBException
 */
public Object unmarshal(StreamSource source) throws XMLStreamException, JAXBException {
    Reader inputReader = source.getReader();

    /* Validate XML against XXE, XEE, and SSRF
     * 
     * This pre-validation step is required because com.sun.xml.stream.sjsxp-1.0.2 XML stream parser library does not 
     * support all XML parser features to prevent these types of attacks  
     */
    if (inputReader instanceof InputStreamReader) {
        try {
            //create copy of input stream
            InputStreamReader isReader = (InputStreamReader) inputReader;
            String encoding = isReader.getEncoding();
            byte[] backup = IOUtils.toByteArray(isReader, encoding);

            //validate input stream
            DOMUtils.validateXMLAgainstXXEAndSSRFAttacks(new ByteArrayInputStream(backup));

            //create new inputStreamReader for reak processing
            inputReader = new InputStreamReader(new ByteArrayInputStream(backup), encoding);

        } catch (XMLStreamException e) {
            log.error("XML data validation FAILED with msg: " + e.getMessage(), e);
            throw new XMLStreamException("XML data validation FAILED with msg: " + e.getMessage(), e);

        } catch (IOException e) {
            log.error("XML data validation FAILED with msg: " + e.getMessage(), e);
            throw new XMLStreamException("XML data validation FAILED with msg: " + e.getMessage(), e);

        }

    } else {
        log.error("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        log.error(
                "Reader is not of type InputStreamReader -> can not make a copy of the InputStream --> extended XML validation is not possible!!! ");
        log.error("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

    }

    /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     * parse XML with original functionality
     * 
     * This code implements the the original mocca XML processing by using 
     *  com.sun.xml.stream.sjsxp-1.0.2 XML stream parser library. Currently, this library is required to get full 
     *  security-layer specific XML processing. However, this lib does not fully support XXE, XEE and SSRF
     *  prevention mechanisms (e.g.: XMLInputFactory.SUPPORT_DTD flag is not used)    
     * 
     * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     */
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();

    //disallow DTD and external entities
    inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
    inputFactory.setProperty("javax.xml.stream.isSupportingExternalEntities", false);

    XMLEventReader eventReader = inputFactory.createXMLEventReader(inputReader);
    RedirectEventFilter redirectEventFilter = new RedirectEventFilter();
    XMLEventReader filteredReader = inputFactory.createFilteredReader(eventReader, redirectEventFilter);

    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    ReportingValidationEventHandler validationEventHandler = new ReportingValidationEventHandler();
    unmarshaller.setEventHandler(validationEventHandler);

    unmarshaller.setListener(new RedirectUnmarshallerListener(redirectEventFilter));
    unmarshaller.setSchema(slSchema);

    Object object;
    try {
        log.trace("Before unmarshal().");
        object = unmarshaller.unmarshal(filteredReader);
        log.trace("After unmarshal().");
    } catch (UnmarshalException e) {
        if (log.isDebugEnabled()) {
            log.debug("Failed to unmarshal security layer message.", e);
        } else {
            log.info("Failed to unmarshal security layer message."
                    + (e.getMessage() != null ? " " + e.getMessage() : ""));
        }

        if (validationEventHandler.getErrorEvent() != null) {
            ValidationEvent errorEvent = validationEventHandler.getErrorEvent();
            if (e.getLinkedException() == null) {
                e.setLinkedException(errorEvent.getLinkedException());
            }
        }
        throw e;
    }

    return object;

}

From source file:org.lockss.util.TestStreamUtil.java

public void testGetReader() {
    InputStream in = new StringInputStream("123");
    InputStreamReader rdr;
    Charset def = Charset.forName(Constants.DEFAULT_ENCODING);
    Charset utf = Charset.forName("UTF-8");
    rdr = (InputStreamReader) StreamUtil.getReader(in, null);
    assertTrue(def.aliases().contains(rdr.getEncoding()));
    rdr = (InputStreamReader) StreamUtil.getReader(in, "ISO-8859-1");
    assertTrue(def.aliases().contains(rdr.getEncoding()));
    rdr = (InputStreamReader) StreamUtil.getReader(in, "utf-8");
    assertTrue(utf.aliases().contains(rdr.getEncoding()));
    rdr = (InputStreamReader) StreamUtil.getReader(in, "NoSuchCharset");
    assertTrue(def.aliases().contains(rdr.getEncoding()));
}

From source file:org.pentaho.di.ui.trans.step.common.CsvInputAwareStepDialog.java

default String[] getFieldNamesImpl(final InputStreamReader reader, final CsvInputAwareMeta meta)
        throws KettleException {

    String[] fieldNames = new String[] {};
    if (reader == null || meta == null) {
        logError(BaseMessages.getString("Dialog.ErrorGettingFields.Message"));
        return fieldNames;
    }/*from  w ww.j a  va  2s  .c o m*/
    final String delimiter = getTransMeta().environmentSubstitute(meta.getDelimiter());
    final String enclosure = getTransMeta().environmentSubstitute(meta.getEnclosure());

    final EncodingType encodingType = EncodingType.guessEncodingType(reader.getEncoding());

    // Read a line of data to determine the number of rows...
    final String line = TextFileInputUtils.getLine(getLogChannel(), reader, encodingType,
            meta.getFileFormatTypeNr(), new StringBuilder(1000));
    if (!StringUtils.isBlank(line)) {
        fieldNames = CsvInput.guessStringsFromLine(getLogChannel(), line, delimiter, enclosure,
                meta.getEscapeCharacter());
    }
    if (Utils.isEmpty(fieldNames)) {
        logError(BaseMessages.getString("Dialog.ErrorGettingFields.Message"));
        return fieldNames;
    }

    // Massage field names
    for (int i = 0; i < fieldNames.length; i++) {
        fieldNames[i] = Const.trim(fieldNames[i]);
        if (!meta.hasHeader()) {
            final DecimalFormat df = new DecimalFormat("000");
            fieldNames[i] = "Field_" + df.format(i);
        } else if (!Utils.isEmpty(meta.getEnclosure()) && fieldNames[i].startsWith(meta.getEnclosure())
                && fieldNames[i].endsWith(meta.getEnclosure()) && fieldNames[i].length() > 1) {
            fieldNames[i] = fieldNames[i].substring(1, fieldNames[i].length() - 1);
        }
        // trim again, now that the enclosure characters have been removed
        fieldNames[i] = Const.trim(fieldNames[i]);
        fieldNames[i] = massageFieldName(fieldNames[i]);
    }
    return fieldNames;
}

From source file:canreg.client.dataentry.Import.java

public static boolean importFiles(Task<Object, Void> task, Document doc,
        List<canreg.client.dataentry.Relation> map, File[] files, CanRegServerInterface server,
        ImportOptions io) throws SQLException, RemoteException, SecurityException, RecordLockedException {
    int numberOfLinesRead = 0;
    Writer reportWriter = new BufferedWriter(new OutputStreamWriter(System.out));
    if (io.getReportFileName() != null && io.getReportFileName().trim().length() > 0) {
        try {//from ww  w. ja v a 2s  .c o  m
            reportWriter = new BufferedWriter(new FileWriter(io.getReportFileName()));
        } catch (IOException ex) {
            Logger.getLogger(Import.class.getName()).log(Level.WARNING, null, ex);
        }
    }
    boolean success = false;
    Set<String> noNeedToLookAtPatientVariables = new TreeSet<String>();
    noNeedToLookAtPatientVariables
            .add(canreg.common.Tools.toLowerCaseStandardized(io.getPatientIDVariableName()));
    noNeedToLookAtPatientVariables
            .add(canreg.common.Tools.toLowerCaseStandardized(io.getPatientRecordIDVariableName()));

    String[] lineElements;
    ResultCode worstResultCodeFound;
    //         CSVReader reader = null;
    CSVParser parser = null;
    CSVFormat format = CSVFormat.DEFAULT.withFirstRecordAsHeader().withDelimiter(io.getSeparators()[0]);

    int linesToRead = io.getMaxLines();

    try {
        // first we get the patients
        if (task != null) {
            task.firePropertyChange(PROGRESS, 0, 0);
            task.firePropertyChange(PATIENTS, 0, 0);
        }
        if (files[0] != null) {
            reportWriter
                    .write("Starting to import patients from " + files[0].getAbsolutePath() + Globals.newline);
            FileInputStream patientFIS = new FileInputStream(files[0]);
            InputStreamReader patientISR = new InputStreamReader(patientFIS, io.getFileCharsets()[0]);

            Logger.getLogger(Import.class.getName()).log(Level.CONFIG, "Name of the character encoding {0}",
                    patientISR.getEncoding());

            int numberOfRecordsInFile = canreg.common.Tools.numberOfLinesInFile(files[0].getAbsolutePath());

            numberOfLinesRead = 0;

            if (linesToRead > 0) {
                linesToRead = Math.min(numberOfRecordsInFile, linesToRead);
            } else {
                linesToRead = numberOfRecordsInFile;
            }
            parser = CSVParser.parse(files[0], io.getFileCharsets()[0], format);

            for (CSVRecord csvRecord : parser) {
                // We allow for null tasks...
                boolean savePatient = true;
                boolean deletePatient = false;
                int oldPatientDatabaseRecordID = -1;

                if (task != null) {
                    task.firePropertyChange(PROGRESS, ((numberOfLinesRead - 1) * 100 / linesToRead) / 3,
                            ((numberOfLinesRead) * 100 / linesToRead) / 3);
                    task.firePropertyChange(PATIENTS, ((numberOfLinesRead - 1) * 100 / linesToRead),
                            ((numberOfLinesRead) * 100 / linesToRead));
                }

                // Build patient part
                Patient patient = new Patient();
                for (int i = 0; i < map.size(); i++) {
                    Relation rel = map.get(i);
                    if (rel.getDatabaseTableVariableID() >= 0
                            && rel.getDatabaseTableName().equalsIgnoreCase("patient")) {
                        if (rel.getVariableType().equalsIgnoreCase("Number")) {
                            if (csvRecord.get(rel.getFileColumnNumber()).length() > 0) {
                                try {
                                    patient.setVariable(rel.getDatabaseVariableName(),
                                            Integer.parseInt(csvRecord.get(rel.getFileColumnNumber())));
                                } catch (NumberFormatException ex) {
                                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE,
                                            "Number format error in line: " + (numberOfLinesRead + 1 + 1)
                                                    + ". ",
                                            ex);
                                    success = false;
                                }
                            }
                        } else {
                            patient.setVariable(rel.getDatabaseVariableName(),
                                    csvRecord.get(rel.getFileColumnNumber()));
                        }
                    }
                    if (task != null) {
                        task.firePropertyChange(RECORD, i - 1 / map.size() * 50, i / map.size() * 50);
                    }
                }
                // debugOut(patient.toString());

                // debugOut(tumour.toString());
                // add patient to the database
                Object patientID = patient.getVariable(io.getPatientRecordIDVariableName());
                Patient oldPatientRecord = null;
                try {
                    oldPatientRecord = CanRegClientApp.getApplication().getPatientRecord((String) patientID,
                            false);
                } catch (DistributedTableDescriptionException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (RecordLockedException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (RemoteException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SecurityException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnknownTableException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                }

                if (oldPatientRecord != null) {
                    // deal with discrepancies                        
                    switch (io.getDiscrepancies()) {
                    case ImportOptions.REJECT:
                        savePatient = false;
                        break;
                    case ImportOptions.UPDATE:
                        String updateReport = updateRecord(oldPatientRecord, patient);
                        if (updateReport.length() > 0) {
                            reportWriter.write(patient.getVariable(io.getTumourIDVariablename())
                                    + Globals.newline + updateReport);
                        }
                        oldPatientDatabaseRecordID = (Integer) oldPatientRecord
                                .getVariable(Globals.PATIENT_TABLE_RECORD_ID_VARIABLE_NAME);
                        patient = oldPatientRecord;
                        savePatient = true;
                        break;
                    case ImportOptions.OVERWRITE:
                        // deleteTumour;
                        oldPatientDatabaseRecordID = (Integer) oldPatientRecord
                                .getVariable(Globals.PATIENT_TABLE_RECORD_ID_VARIABLE_NAME);
                        String overWriteReport = overwriteRecord(oldPatientRecord, patient);
                        if (overWriteReport.length() > 0) {
                            reportWriter.write(patient.getVariable(io.getTumourIDVariablename())
                                    + Globals.newline + overWriteReport);
                        }
                        patient = oldPatientRecord;
                        savePatient = true;
                        break;
                    }
                    // reportWriter.write(tumour.getVariable(io.getTumourIDVariablename()) + "Tumour already exists.\n");
                }

                if (task != null) {
                    task.firePropertyChange(RECORD, 50, 75);
                }

                if ((!io.isTestOnly())) {
                    if (deletePatient) {
                        server.deleteRecord(oldPatientDatabaseRecordID, Globals.PATIENT_TABLE_NAME);
                    }
                    if (savePatient) {
                        if (patient.getVariable(Globals.PATIENT_TABLE_RECORD_ID_VARIABLE_NAME) != null) {
                            server.editPatient(patient);
                        } else {
                            server.savePatient(patient);
                        }
                    }
                }

                if (task != null) {
                    task.firePropertyChange(RECORD, 100, 75);
                }

                numberOfLinesRead++;

                if (Thread.interrupted()) {
                    //We've been interrupted: no more importing.
                    reportWriter.flush();
                    throw new InterruptedException();
                }
            }
            parser.close();
            reportWriter.write("Finished reading patients." + Globals.newline + Globals.newline);
            reportWriter.flush();
        }
        if (task != null) {
            task.firePropertyChange(PATIENTS, 100, 100);
            task.firePropertyChange("progress", 33, 34);
        }

        // then we get the tumours            
        if (task != null) {
            task.firePropertyChange(TUMOURS, 0, 0);
        }

        if (files[1] != null) {
            reportWriter
                    .write("Starting to import tumours from " + files[1].getAbsolutePath() + Globals.newline);

            FileInputStream tumourFIS = new FileInputStream(files[1]);
            InputStreamReader tumourISR = new InputStreamReader(tumourFIS, io.getFileCharsets()[1]);

            Logger.getLogger(Import.class.getName()).log(Level.CONFIG, "Name of the character encoding {0}",
                    tumourISR.getEncoding());

            numberOfLinesRead = 0;

            int numberOfRecordsInFile = canreg.common.Tools.numberOfLinesInFile(files[1].getAbsolutePath());

            if (linesToRead > 0) {
                linesToRead = Math.min(numberOfRecordsInFile, linesToRead);
            } else {
                linesToRead = numberOfRecordsInFile;
            }

            format = CSVFormat.DEFAULT.withFirstRecordAsHeader().withDelimiter(io.getSeparators()[1]);

            parser = CSVParser.parse(files[1], io.getFileCharsets()[1], format);

            for (CSVRecord csvRecord : parser) {
                // We allow for null tasks...
                boolean saveTumour = true;
                boolean deleteTumour = false;

                if (task != null) {
                    task.firePropertyChange(PROGRESS, 33 + ((numberOfLinesRead - 1) * 100 / linesToRead) / 3,
                            33 + ((numberOfLinesRead) * 100 / linesToRead) / 3);
                    task.firePropertyChange(TUMOURS, ((numberOfLinesRead - 1) * 100 / linesToRead),
                            ((numberOfLinesRead) * 100 / linesToRead));
                }
                // Build tumour part
                Tumour tumour = new Tumour();
                for (int i = 0; i < map.size(); i++) {
                    Relation rel = map.get(i);
                    if (rel.getDatabaseTableVariableID() >= 0
                            && rel.getDatabaseTableName().equalsIgnoreCase("tumour")) {
                        if (rel.getVariableType().equalsIgnoreCase("Number")) {
                            if (csvRecord.get(rel.getFileColumnNumber()).length() > 0) {
                                try {
                                    tumour.setVariable(rel.getDatabaseVariableName(),
                                            Integer.parseInt(csvRecord.get(rel.getFileColumnNumber())));
                                } catch (NumberFormatException ex) {
                                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE,
                                            "Number format error in line: " + (numberOfLinesRead + 1 + 1)
                                                    + ". ",
                                            ex);
                                    success = false;
                                }
                            }
                        } else {
                            tumour.setVariable(rel.getDatabaseVariableName(),
                                    csvRecord.get(rel.getFileColumnNumber()));
                        }
                    }
                    if (task != null) {
                        task.firePropertyChange(RECORD, i - 1 / map.size() * 50, i / map.size() * 50);
                    }
                }

                // see if this tumour exists in the database already
                // TODO: Implement this using arrays and getTumourRexords instead
                Tumour tumour2 = null;
                try {
                    tumour2 = CanRegClientApp.getApplication().getTumourRecordBasedOnTumourID(
                            (String) tumour.getVariable(io.getTumourIDVariablename()), false);
                } catch (DistributedTableDescriptionException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (RecordLockedException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (RemoteException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SecurityException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnknownTableException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                }

                if (tumour2 != null) {
                    // deal with discrepancies
                    switch (io.getDiscrepancies()) {
                    case ImportOptions.REJECT:
                        saveTumour = false;
                        break;
                    case ImportOptions.UPDATE:
                        String updateReport = updateRecord(tumour2, tumour);
                        if (updateReport.length() > 0) {
                            reportWriter.write(tumour.getVariable(io.getTumourIDVariablename())
                                    + Globals.newline + updateReport);
                        }
                        tumour = tumour2;
                        saveTumour = true;
                        break;
                    case ImportOptions.OVERWRITE:
                        // deleteTumour;
                        deleteTumour = true;
                        saveTumour = true;
                        break;
                    }
                    // reportWriter.write(tumour.getVariable(io.getTumourIDVariablename()) + "Tumour already exists.\n");

                }

                Patient patient = null;
                try {
                    patient = CanRegClientApp.getApplication().getPatientRecord(
                            (String) tumour.getVariable(io.getPatientRecordIDTumourTableVariableName()), false);
                } catch (DistributedTableDescriptionException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (RecordLockedException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (RemoteException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SecurityException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnknownTableException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                }

                if (patient != null) {
                    if (io.isDoChecks() && saveTumour) {
                        // run the edits...
                        String message = "";
                        LinkedList<CheckResult> checkResults = canreg.client.CanRegClientApp.getApplication()
                                .performChecks(patient, tumour);

                        Map<Globals.StandardVariableNames, CheckResult.ResultCode> mapOfVariablesAndWorstResultCodes = new EnumMap<Globals.StandardVariableNames, CheckResult.ResultCode>(
                                Globals.StandardVariableNames.class);
                        worstResultCodeFound = CheckResult.ResultCode.OK;
                        for (CheckResult result : checkResults) {
                            if (result.getResultCode() != CheckResult.ResultCode.OK
                                    && result.getResultCode() != CheckResult.ResultCode.NotDone) {
                                if (!result.getResultCode().equals(CheckResult.ResultCode.Missing)) {
                                    message += result + "\t";
                                    worstResultCodeFound = CheckResult.decideWorstResultCode(
                                            result.getResultCode(), worstResultCodeFound);
                                    for (Globals.StandardVariableNames standardVariableName : result
                                            .getVariablesInvolved()) {
                                        CheckResult.ResultCode worstResultCodeFoundForThisVariable = mapOfVariablesAndWorstResultCodes
                                                .get(standardVariableName);
                                        if (worstResultCodeFoundForThisVariable == null) {
                                            mapOfVariablesAndWorstResultCodes.put(standardVariableName,
                                                    result.getResultCode());
                                        } else if (CheckResult.compareResultSets(result.getResultCode(),
                                                worstResultCodeFoundForThisVariable) > 0) {
                                            mapOfVariablesAndWorstResultCodes.put(standardVariableName,
                                                    result.getResultCode());
                                        }
                                    }
                                }
                            }
                            // Logger.getLogger(Import.class.getName()).log(Level.INFO, result.toString());
                        }
                        // always generate ICD10...
                        // ConversionResult[] conversionResult = canreg.client.CanRegClientApp.getApplication().performConversions(Converter.ConversionName.ICDO3toICD10, patient, tumour);
                        // tumour.setVariable(io.getICD10VariableName(), conversionResult[0].getValue());

                        if (worstResultCodeFound != CheckResult.ResultCode.Invalid
                                && worstResultCodeFound != CheckResult.ResultCode.Missing) {
                            // generate ICD10 codes
                            ConversionResult[] conversionResult = canreg.client.CanRegClientApp.getApplication()
                                    .performConversions(Converter.ConversionName.ICDO3toICD10, patient, tumour);
                            tumour.setVariable(io.getICD10VariableName(), conversionResult[0].getValue());
                            // generate ICCC codes
                            ConversionResult[] conversionResultICCC = canreg.client.CanRegClientApp
                                    .getApplication()
                                    .performConversions(Converter.ConversionName.ICDO3toICCC3, patient, tumour);
                            tumour.setVariable(io.getICCCVariableName(), conversionResultICCC[0].getValue());
                        } else {
                            tumour.setVariable(io.getTumourRecordStatus(), "0");
                        }

                        if (worstResultCodeFound == CheckResult.ResultCode.OK) {
                            // message += "Cross-check conclusion: Valid";
                        } else {
                            reportWriter.write(tumour.getVariable(io.getTumourIDVariablename()) + "\t" + message
                                    + Globals.newline);
                            // System.out.println(tumour.getVariable(io.getTumourIDVariablename()) + " " + message);
                        }
                        tumour.setVariable(io.getTumourCheckStatus(),
                                CheckResult.toDatabaseVariable(worstResultCodeFound));

                    } else {
                        // try to generate ICD10, if missing, anyway
                        String icd10 = (String) tumour.getVariable(io.getICD10VariableName());
                        if (icd10 == null || icd10.trim().length() == 0) {
                            ConversionResult[] conversionResult = canreg.client.CanRegClientApp.getApplication()
                                    .performConversions(Converter.ConversionName.ICDO3toICD10, patient, tumour);
                            tumour.setVariable(io.getICD10VariableName(), conversionResult[0].getValue());
                        }
                        // try to generate ICCC3, if missing, anyway
                        String iccc = (String) tumour.getVariable(io.getICCCVariableName());
                        if (iccc == null || iccc.trim().length() == 0) {
                            ConversionResult[] conversionResult = canreg.client.CanRegClientApp.getApplication()
                                    .performConversions(Converter.ConversionName.ICDO3toICCC3, patient, tumour);
                            tumour.setVariable(io.getICCCVariableName(), conversionResult[0].getValue());
                        }
                    }
                } else {
                    reportWriter.write(tumour.getVariable(io.getTumourIDVariablename()) + "\t"
                            + "No patient matches this Tumour." + Globals.newline);
                    tumour.setVariable(io.getTumourRecordStatus(), "0");
                    tumour.setVariable(io.getTumourCheckStatus(),
                            CheckResult.toDatabaseVariable(ResultCode.Missing));
                }
                if (task != null) {
                    task.firePropertyChange(RECORD, 50, 75);
                }
                if (!io.isTestOnly()) {
                    if (deleteTumour) {
                        server.deleteRecord(
                                (Integer) tumour2.getVariable(Globals.TUMOUR_TABLE_RECORD_ID_VARIABLE_NAME),
                                Globals.TUMOUR_TABLE_NAME);
                    }
                    if (saveTumour) {
                        // if tumour has no record ID we save it
                        if (tumour.getVariable(Globals.TUMOUR_TABLE_RECORD_ID_VARIABLE_NAME) == null) {
                            server.saveTumour(tumour);
                        } // if not we edit it
                        else {
                            server.editTumour(tumour);
                        }
                    }
                }
                if (task != null) {
                    task.firePropertyChange(RECORD, 75, 100);
                }
                //Read next line of data

                numberOfLinesRead++;

                if (Thread.interrupted()) {
                    //We've been interrupted: no more importing.
                    reportWriter.flush();
                    throw new InterruptedException();
                }
            }
            parser.close();
            reportWriter.write("Finished reading tumours." + Globals.newline + Globals.newline);
            reportWriter.flush();
        }
        if (task != null) {
            task.firePropertyChange(TUMOURS, 100, 100);
        }
        // then at last we get the sources
        if (task != null) {
            task.firePropertyChange(SOURCES, 0, 0);
            task.firePropertyChange(PROGRESS, 66, 66);
        }
        if (files[2] != null) {
            reportWriter
                    .write("Starting to import sources from " + files[2].getAbsolutePath() + Globals.newline);

            FileInputStream sourceFIS = new FileInputStream(files[2]);
            InputStreamReader sourceISR = new InputStreamReader(sourceFIS, io.getFileCharsets()[2]);

            Logger.getLogger(Import.class.getName()).log(Level.CONFIG, "Name of the character encoding {0}",
                    sourceISR.getEncoding());

            numberOfLinesRead = 0;

            int numberOfRecordsInFile = canreg.common.Tools.numberOfLinesInFile(files[2].getAbsolutePath());

            if (linesToRead > 0) {
                linesToRead = Math.min(numberOfRecordsInFile, linesToRead);
            } else {
                linesToRead = numberOfRecordsInFile;
            }

            format = CSVFormat.DEFAULT.withFirstRecordAsHeader().withDelimiter(io.getSeparators()[2]);

            parser = CSVParser.parse(files[2], io.getFileCharsets()[2], format);

            for (CSVRecord csvRecord : parser) {
                // We allow for null tasks...
                if (task != null) {
                    task.firePropertyChange(PROGRESS, 67 + ((numberOfLinesRead - 1) * 100 / linesToRead) / 3,
                            67 + ((numberOfLinesRead) * 100 / linesToRead) / 3);
                    task.firePropertyChange(SOURCES, ((numberOfLinesRead - 1) * 100 / linesToRead),
                            ((numberOfLinesRead) * 100 / linesToRead));
                }

                // Build source part
                Source source = new Source();
                for (int i = 0; i < map.size(); i++) {
                    Relation rel = map.get(i);
                    if (rel.getDatabaseTableVariableID() >= 0
                            && rel.getDatabaseTableName().equalsIgnoreCase(Globals.SOURCE_TABLE_NAME)) {
                        if (rel.getVariableType().equalsIgnoreCase("Number")) {
                            if (csvRecord.get(rel.getFileColumnNumber()).length() > 0) {
                                try {
                                    source.setVariable(rel.getDatabaseVariableName(),
                                            Integer.parseInt(csvRecord.get(rel.getFileColumnNumber())));
                                } catch (NumberFormatException ex) {
                                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE,
                                            "Number format error in line: " + (numberOfLinesRead + 1 + 1)
                                                    + ". ",
                                            ex);
                                    success = false;
                                }
                            }
                        } else {
                            source.setVariable(rel.getDatabaseVariableName(),
                                    csvRecord.get(rel.getFileColumnNumber()));
                        }
                    }
                    if (task != null) {
                        task.firePropertyChange(RECORD, i - 1 / map.size() * 50, i / map.size() * 50);
                    }
                }

                Tumour tumour = null;
                try {
                    tumour = CanRegClientApp.getApplication().getTumourRecordBasedOnTumourID(
                            (String) source.getVariable(io.getTumourIDSourceTableVariableName()), false);
                } catch (DistributedTableDescriptionException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (RecordLockedException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (RemoteException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SecurityException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnknownTableException ex) {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                }
                if (task != null) {
                    task.firePropertyChange(RECORD, 50, 75);
                }
                boolean addSource = true;

                if (tumour != null) {
                    Set<Source> sources = tumour.getSources();
                    Object sourceRecordID = source.getVariable(io.getSourceIDVariablename());
                    // look for source in sources
                    for (Source oldSource : sources) {
                        if (oldSource.getVariable(io.getSourceIDVariablename()).equals(sourceRecordID)) {
                            // deal with discrepancies
                            switch (io.getDiscrepancies()) {
                            case ImportOptions.REJECT:
                                addSource = false;
                                break;
                            case ImportOptions.UPDATE:
                                String updateReport = updateRecord(oldSource, source);
                                if (updateReport.length() > 0) {
                                    reportWriter.write(tumour.getVariable(io.getTumourIDVariablename())
                                            + Globals.newline + updateReport);
                                }
                                source = oldSource;
                                addSource = false;
                                break;
                            case ImportOptions.OVERWRITE:
                                // deleteTumour;
                                sources.remove(oldSource);
                                addSource = true;
                                break;
                            }
                        }
                    }
                    if (addSource) {
                        sources.add(source);
                    }
                    tumour.setSources(sources);
                    if (!io.isTestOnly()) {
                        server.editTumour(tumour);
                    }
                } else {
                    Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null,
                            "No tumour for source record.");
                }
                if (task != null) {
                    task.firePropertyChange(RECORD, 75, 100);
                }
                //Read next line of data

                numberOfLinesRead++;

                if (Thread.interrupted()) {
                    //We've been interrupted: no more importing.
                    reportWriter.flush();
                    throw new InterruptedException();
                }
            }
            reportWriter.write("Finished reading sources." + Globals.newline + Globals.newline);
            reportWriter.flush();
            parser.close();
        }
        if (task != null) {
            task.firePropertyChange(SOURCES, 100, 100);
            task.firePropertyChange(PROGRESS, 100, 100);
            while (!task.isProgressPropertyValid()) {
                // wait untill progress has been updated...
            }
        }
        reportWriter.write("Finished" + Globals.newline);
        reportWriter.flush();
        success = true;
    } catch (IOException ex) {
        Logger.getLogger(Import.class.getName()).log(Level.SEVERE,
                "Error in line: " + (numberOfLinesRead + 1 + 1) + ". ", ex);
        success = false;
    } catch (InterruptedException ex) {
        Logger.getLogger(Import.class.getName()).log(Level.INFO,
                "Interupted on line: " + (numberOfLinesRead + 1) + ". ", ex);
        success = true;
    } catch (IndexOutOfBoundsException ex) {
        Logger.getLogger(Import.class.getName()).log(Level.SEVERE,
                "String too short error in line: " + (numberOfLinesRead + 1 + 1) + ". ", ex);
        success = false;
    } finally {
        if (parser != null) {
            try {
                parser.close();
            } catch (IOException ex) {
                Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        try {
            reportWriter.flush();
            reportWriter.close();
        } catch (IOException ex) {
            Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    if (task != null) {
        task.firePropertyChange(PROGRESS, 100, 100);
        task.firePropertyChange("finished", null, null);
    }
    return success;
}

From source file:org.pentaho.di.ui.trans.steps.parallelgzipcsv.ParGzipCsvInputDialog.java

private void getCSV() {
    InputStream inputStream = null;
    try {/*from www  . ja v a 2 s  .co m*/
        ParGzipCsvInputMeta meta = new ParGzipCsvInputMeta();
        getInfo(meta);

        String filename = transMeta.environmentSubstitute(meta.getFilename());

        FileObject fileObject = KettleVFS.getFileObject(filename);
        if (!(fileObject instanceof LocalFile)) {
            // We can only use NIO on local files at the moment, so that's what we limit ourselves to.
            //
            throw new KettleException(
                    BaseMessages.getString(PKG, "ParGzipCsvInput.Log.OnlyLocalFilesAreSupported"));
        }

        wFields.table.removeAll();

        inputStream = new GZIPInputStream(KettleVFS.getInputStream(fileObject));
        InputStreamReader reader = new InputStreamReader(inputStream);
        EncodingType encodingType = EncodingType.guessEncodingType(reader.getEncoding());

        // Read a line of data to determine the number of rows...
        //
        String line = TextFileInput.getLine(log, reader, encodingType, TextFileInputMeta.FILE_FORMAT_MIXED,
                new StringBuilder(1000));

        // Split the string, header or data into parts...
        //
        String[] fieldNames = Const.splitString(line, meta.getDelimiter());

        if (!meta.isHeaderPresent()) {
            // Don't use field names from the header...
            // Generate field names F1 ... F10
            //
            DecimalFormat df = new DecimalFormat("000");
            for (int i = 0; i < fieldNames.length; i++) {
                fieldNames[i] = "Field_" + df.format(i);
            }
        } else {
            if (!Const.isEmpty(meta.getEnclosure())) {
                for (int i = 0; i < fieldNames.length; i++) {
                    if (fieldNames[i].startsWith(meta.getEnclosure())
                            && fieldNames[i].endsWith(meta.getEnclosure()) && fieldNames[i].length() > 1) {
                        fieldNames[i] = fieldNames[i].substring(1, fieldNames[i].length() - 1);
                    }
                }
            }
        }

        // Trim the names to make sure...
        //
        for (int i = 0; i < fieldNames.length; i++) {
            fieldNames[i] = Const.trim(fieldNames[i]);
        }

        // Update the GUI
        //
        for (int i = 0; i < fieldNames.length; i++) {
            TableItem item = new TableItem(wFields.table, SWT.NONE);
            item.setText(1, fieldNames[i]);
            item.setText(2, ValueMeta.getTypeDesc(ValueMetaInterface.TYPE_STRING));
        }
        wFields.removeEmptyRows();
        wFields.setRowNums();
        wFields.optWidth(true);

        // Now we can continue reading the rows of data and we can guess the
        // Sample a few lines to determine the correct type of the fields...
        //
        String shellText = BaseMessages.getString(PKG, "ParGzipCsvInputDialog.LinesToSample.DialogTitle");
        String lineText = BaseMessages.getString(PKG, "ParGzipCsvInputDialog.LinesToSample.DialogMessage");
        EnterNumberDialog end = new EnterNumberDialog(shell, 100, shellText, lineText);
        int samples = end.open();
        if (samples >= 0) {
            getInfo(meta);

            TextFileCSVImportProgressDialog pd = new TextFileCSVImportProgressDialog(shell, meta, transMeta,
                    reader, samples, true);
            String message = pd.open();
            if (message != null) {
                wFields.removeAll();

                // OK, what's the result of our search?
                getData(meta);
                wFields.removeEmptyRows();
                wFields.setRowNums();
                wFields.optWidth(true);

                EnterTextDialog etd = new EnterTextDialog(shell,
                        BaseMessages.getString(PKG, "ParGzipCsvInputDialog.ScanResults.DialogTitle"),
                        BaseMessages.getString(PKG, "ParGzipCsvInputDialog.ScanResults.DialogMessage"), message,
                        true);
                etd.setReadOnly();
                etd.open();
            }
        }
    } catch (IOException e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "ParGzipCsvInputDialog.IOError.DialogTitle"),
                BaseMessages.getString(PKG, "ParGzipCsvInputDialog.IOError.DialogMessage"), e);
    } catch (KettleException e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "System.Dialog.Error.Title"),
                BaseMessages.getString(PKG, "ParGzipCsvInputDialog.ErrorGettingFileDesc.DialogMessage"), e);
    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {
            // Ignore errors
        }
    }
}

From source file:org.pentaho.di.ui.trans.steps.csvinput.CsvInputDialog.java

private void getCSV() {
    InputStream inputStream = null;
    try {// ww w  . j ava  2  s .c om
        CsvInputMeta meta = new CsvInputMeta();
        getInfo(meta);

        String filename = transMeta.environmentSubstitute(meta.getFilename());
        String delimiter = transMeta.environmentSubstitute(meta.getDelimiter());
        String enclosure = transMeta.environmentSubstitute(meta.getEnclosure());

        FileObject fileObject = KettleVFS.getFileObject(filename);
        if (!(fileObject instanceof LocalFile)) {
            // We can only use NIO on local files at the moment, so that's what we
            // limit ourselves to.
            //
            throw new KettleException(BaseMessages.getString(PKG, "CsvInput.Log.OnlyLocalFilesAreSupported"));
        }

        wFields.table.removeAll();

        inputStream = KettleVFS.getInputStream(fileObject);

        String realEncoding = transMeta.environmentSubstitute(meta.getEncoding());
        InputStreamReader reader;
        if (Const.isEmpty(realEncoding)) {
            reader = new InputStreamReader(inputStream);
        } else {
            reader = new InputStreamReader(inputStream, realEncoding);
        }

        EncodingType encodingType = EncodingType.guessEncodingType(reader.getEncoding());

        // Read a line of data to determine the number of rows...
        //
        String line = TextFileInput.getLine(log, reader, encodingType, TextFileInputMeta.FILE_FORMAT_MIXED,
                new StringBuilder(1000));

        // Split the string, header or data into parts...
        //
        String[] fieldNames = CsvInput.guessStringsFromLine(log, line, delimiter, enclosure,
                meta.getEscapeCharacter());

        if (!meta.isHeaderPresent()) {
            // Don't use field names from the header...
            // Generate field names F1 ... F10
            //
            DecimalFormat df = new DecimalFormat("000");
            for (int i = 0; i < fieldNames.length; i++) {
                fieldNames[i] = "Field_" + df.format(i);
            }
        } else {
            if (!Const.isEmpty(meta.getEnclosure())) {
                for (int i = 0; i < fieldNames.length; i++) {
                    if (fieldNames[i].startsWith(meta.getEnclosure())
                            && fieldNames[i].endsWith(meta.getEnclosure()) && fieldNames[i].length() > 1) {
                        fieldNames[i] = fieldNames[i].substring(1, fieldNames[i].length() - 1);
                    }
                }
            }
        }

        // Trim the names to make sure...
        //
        for (int i = 0; i < fieldNames.length; i++) {
            fieldNames[i] = Const.trim(fieldNames[i]);
        }

        // Update the GUI
        //
        for (int i = 0; i < fieldNames.length; i++) {
            TableItem item = new TableItem(wFields.table, SWT.NONE);
            item.setText(1, fieldNames[i]);
            item.setText(2, ValueMeta.getTypeDesc(ValueMetaInterface.TYPE_STRING));
        }
        wFields.removeEmptyRows();
        wFields.setRowNums();
        wFields.optWidth(true);

        // Now we can continue reading the rows of data and we can guess the
        // Sample a few lines to determine the correct type of the fields...
        //
        String shellText = BaseMessages.getString(PKG, "CsvInputDialog.LinesToSample.DialogTitle");
        String lineText = BaseMessages.getString(PKG, "CsvInputDialog.LinesToSample.DialogMessage");
        EnterNumberDialog end = new EnterNumberDialog(shell, 100, shellText, lineText);
        int samples = end.open();
        if (samples >= 0) {
            getInfo(meta);

            TextFileCSVImportProgressDialog pd = new TextFileCSVImportProgressDialog(shell, meta, transMeta,
                    reader, samples, true);
            String message = pd.open();
            if (message != null) {
                wFields.removeAll();

                // OK, what's the result of our search?
                getData(meta, false);
                wFields.removeEmptyRows();
                wFields.setRowNums();
                wFields.optWidth(true);

                EnterTextDialog etd = new EnterTextDialog(shell,
                        BaseMessages.getString(PKG, "CsvInputDialog.ScanResults.DialogTitle"),
                        BaseMessages.getString(PKG, "CsvInputDialog.ScanResults.DialogMessage"), message, true);
                etd.setReadOnly();
                etd.open();

                // asyncUpdatePreview();
            }
        }
    } catch (IOException e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "CsvInputDialog.IOError.DialogTitle"),
                BaseMessages.getString(PKG, "CsvInputDialog.IOError.DialogMessage"), e);
    } catch (KettleException e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "System.Dialog.Error.Title"),
                BaseMessages.getString(PKG, "CsvInputDialog.ErrorGettingFileDesc.DialogMessage"), e);
    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {
            // Ignore close errors
        }
    }
}

From source file:tufts.vue.URLResource.java

private Properties scrapeHTMLmetaData(URLConnection connection, int maxSearchBytes) throws java.io.IOException {
    Properties metaData = new Properties();

    InputStream byteStream = connection.getInputStream();

    if (DEBUG.DND && DEBUG.META) {
        System.err.println("Getting headers from " + connection);
        System.err.println("Headers: " + connection.getHeaderFields());
    }/*w w  w.  j a  v a  2s . co  m*/

    // note: be sure to call getContentType and don't rely on getting it from the HeaderFields map,
    // as sometimes it's set by the OS for a file:/// URL when there are no header fields (no http server)
    // (actually, this is set by java via a mime type table based on file extension, or a guess based on the stream)
    if (DEBUG.DND)
        System.err.println("*** getting contentType & encoding...");
    final String contentType = connection.getContentType();
    final String contentEncoding = connection.getContentEncoding();
    final int contentLength = connection.getContentLength();

    if (DEBUG.DND)
        System.err.println("*** contentType [" + contentType + "]");
    if (DEBUG.DND)
        System.err.println("*** contentEncoding [" + contentEncoding + "]");
    if (DEBUG.DND)
        System.err.println("*** contentLength [" + contentLength + "]");

    setProperty("url.contentType", contentType);
    setProperty("url.contentEncoding", contentEncoding);
    if (contentLength >= 0)
        setProperty("url.contentLength", contentLength);

    //if (contentType.toLowerCase().startsWith("text/html") == false) {
    if (!isHTML()) { // we only currently handle HTML
        if (DEBUG.Enabled)
            System.err.println("*** contentType [" + contentType + "] not HTML; skipping title extraction");
        return metaData;
    }

    if (DEBUG.DND)
        System.err.println("*** scanning for HTML meta-data...");

    try {
        final BufferedInputStream bufStream = new BufferedInputStream(byteStream, maxSearchBytes);
        bufStream.mark(maxSearchBytes);

        final byte[] byteBuffer = new byte[maxSearchBytes];
        int bytesRead = 0;
        int len = 0;
        // BufferedInputStream still won't read thru a block, so we need to allow
        // a few reads here to get thru a couple of blocks, so we can get up to
        // our maxbytes (e.g., a common return chunk count is 1448 bytes, presumably related to the MTU)
        do {
            int max = maxSearchBytes - bytesRead;
            len = bufStream.read(byteBuffer, bytesRead, max);
            System.out.println("*** read " + len);
            if (len > 0)
                bytesRead += len;
            else if (len < 0)
                break;
        } while (len > 0 && bytesRead < maxSearchBytes);
        if (DEBUG.DND)
            System.out.println("*** Got total chars: " + bytesRead);
        String html = new String(byteBuffer, 0, bytesRead);
        if (DEBUG.DND && DEBUG.META)
            System.out.println("*** HTML-STRING[" + html + "]");

        // first, look for a content encoding, so we can search for and get the title
        // on a properly encoded character stream

        String charset = null;

        Matcher cm = Content_Charset_Regex.matcher(html);
        if (cm.lookingAt()) {
            charset = cm.group(1);
            if (DEBUG.DND)
                System.err.println("*** found HTML specified charset [" + charset + "]");
            setProperty("charset", charset);
        }

        if (charset == null && contentEncoding != null) {
            if (DEBUG.DND || true)
                System.err.println("*** no charset found: using contentEncoding charset " + contentEncoding);
            charset = contentEncoding;
        }

        final String decodedHTML;

        if (charset != null) {
            bufStream.reset();
            InputStreamReader decodedStream = new InputStreamReader(bufStream, charset);
            //InputStreamReader decodedStream = new InputStreamReader(new ByteArrayInputStream(byteBuffer), charset);
            if (true || DEBUG.DND)
                System.out.println("*** decoding bytes into characters with official encoding "
                        + decodedStream.getEncoding());
            setProperty("contentEncoding", decodedStream.getEncoding());
            char[] decoded = new char[bytesRead];
            int decodedChars = decodedStream.read(decoded);
            decodedStream.close();
            if (true || DEBUG.DND)
                System.err.println("*** " + decodedChars + " characters decoded using " + charset);
            decodedHTML = new String(decoded, 0, decodedChars);
        } else
            decodedHTML = html; // we'll just have to go with the default platform charset...

        // these needed to be left open till the decodedStream was done, which
        // although it should never need to read beyond what's already buffered,
        // some internal java code has checks that make sure the underlying stream
        // isn't closed, even it it isn't used.
        byteStream.close();
        bufStream.close();

        Matcher m = HTML_Title_Regex.matcher(decodedHTML);
        if (m.lookingAt()) {
            String title = m.group(1);
            if (true || DEBUG.DND)
                System.err.println("*** found title [" + title + "]");
            metaData.put("title", title.trim());
        }

    } catch (Throwable e) {
        System.err.println("scrapeHTMLmetaData: " + e);
        if (DEBUG.DND)
            e.printStackTrace();
    }

    if (DEBUG.DND || DEBUG.Enabled)
        System.err.println("*** scrapeHTMLmetaData returning [" + metaData + "]");
    return metaData;
}

From source file:org.pentaho.di.ui.trans.steps.textfileinput.TextFileInputDialog.java

private List<String> getFirst(int nrlines, boolean skipHeaders) throws KettleException {
    TextFileInputMeta meta = new TextFileInputMeta();
    getInfo(meta);//from   w  w w .  j  av a 2s. com
    FileInputList textFileList = meta.getTextFileList(transMeta);

    InputStream fi;
    CompressionInputStream f = null;
    StringBuilder lineStringBuilder = new StringBuilder(256);
    int fileFormatType = meta.getFileFormatTypeNr();

    List<String> retval = new ArrayList<String>();

    if (textFileList.nrOfFiles() > 0) {
        FileObject file = textFileList.getFile(0);
        try {
            fi = KettleVFS.getInputStream(file);

            CompressionProvider provider = CompressionProviderFactory.getInstance()
                    .createCompressionProviderInstance(meta.getFileCompression());
            f = provider.createInputStream(fi);

            InputStreamReader reader;
            if (meta.getEncoding() != null && meta.getEncoding().length() > 0) {
                reader = new InputStreamReader(f, meta.getEncoding());
            } else {
                reader = new InputStreamReader(f);
            }
            EncodingType encodingType = EncodingType.guessEncodingType(reader.getEncoding());

            int linenr = 0;
            int maxnr = nrlines + (meta.hasHeader() ? meta.getNrHeaderLines() : 0);

            if (skipHeaders) {
                // Skip the header lines first if more then one, it helps us position
                if (meta.isLayoutPaged() && meta.getNrLinesDocHeader() > 0) {
                    int skipped = 0;
                    String line = TextFileInput.getLine(log, reader, encodingType, fileFormatType,
                            lineStringBuilder);
                    while (line != null && skipped < meta.getNrLinesDocHeader() - 1) {
                        skipped++;
                        line = TextFileInput.getLine(log, reader, encodingType, fileFormatType,
                                lineStringBuilder);
                    }
                }

                // Skip the header lines first if more then one, it helps us position
                if (meta.hasHeader() && meta.getNrHeaderLines() > 0) {
                    int skipped = 0;
                    String line = TextFileInput.getLine(log, reader, encodingType, fileFormatType,
                            lineStringBuilder);
                    while (line != null && skipped < meta.getNrHeaderLines() - 1) {
                        skipped++;
                        line = TextFileInput.getLine(log, reader, encodingType, fileFormatType,
                                lineStringBuilder);
                    }
                }
            }

            String line = TextFileInput.getLine(log, reader, encodingType, fileFormatType, lineStringBuilder);
            while (line != null && (linenr < maxnr || nrlines == 0)) {
                retval.add(line);
                linenr++;
                line = TextFileInput.getLine(log, reader, encodingType, fileFormatType, lineStringBuilder);
            }
        } catch (Exception e) {
            throw new KettleException(
                    BaseMessages.getString(PKG, "TextFileInputDialog.Exception.ErrorGettingFirstLines",
                            "" + nrlines, file.getName().getURI()),
                    e);
        } finally {
            try {
                if (f != null) {
                    f.close();
                }
            } catch (Exception e) {
                // Ignore errors
            }
        }
    }

    return retval;
}