Example usage for org.apache.solr.internal.csv CSVParser getLine

List of usage examples for org.apache.solr.internal.csv CSVParser getLine

Introduction

In this page you can find the example usage for org.apache.solr.internal.csv CSVParser getLine.

Prototype

public String[] getLine() throws IOException 

Source Link

Document

Parses from the current point in the stream til the end of the current line.

Usage

From source file:org.alfresco.solr.query.MimetypeGroupingQParserPlugin.java

License:Open Source License

private static synchronized void initMap(String mappingFile) {
    String solrHome = SolrResourceLoader.locateSolrHome().toString();
    File file = new File(solrHome, mappingFile);

    CSVParser parser;
    try {/*from  w  w  w.  j  a va2 s  .co m*/
        parser = new CSVParser(new FileReader(file), CSVStrategy.DEFAULT_STRATEGY);
        // parse the fieldnames from the header of the file
        parser.getLine();

        // read the rest of the CSV file
        for (;;) {
            String[] vals = null;

            vals = parser.getLine();

            if (vals == null)
                break;

            mappings.put(vals[1], vals[2]);

            ArrayList<String> grouped = reverseMappings.get(vals[2]);
            if (grouped == null) {
                grouped = new ArrayList<>();
                reverseMappings.put(vals[2], grouped);
            }
            if (!grouped.contains(vals[1])) {
                grouped.add(vals[1]);
            }
        }
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

From source file:org.phenotips.vocabulary.internal.CSVFileService.java

License:Open Source License

/**
 * Parses the CSF input with first line as a headers row into the collection of SolrInputDocuments.
 *
 * @param location the string url address from where to get the data to parse
 * @param headerToFieldMap the map between the headers in the data input stream and the correct field names
 * @param strategy represents the strategy for a CSV, mainly the separator character
 *///from  ww  w. ja v a  2 s.c  o m
public CSVFileService(String location, Map<String, String> headerToFieldMap, CSVStrategy strategy) {
    URL url;
    try {
        url = new URL(location);
    } catch (MalformedURLException ex) {
        throw new SolrException(SolrException.ErrorCode.UNSUPPORTED_MEDIA_TYPE, ex);
    }

    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream()));

        String[] headers;

        String zeroLine = in.readLine();

        CSVParser headParser = new CSVParser(new StringReader(zeroLine), strategy);

        try {
            headers = headParser.getLine();
        } catch (IOException e) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
        }

        // get correct field names for velocity
        transformHeaders(headers, headerToFieldMap);
        Reader reader = new InputStreamReader(url.openConnection().getInputStream());
        parseLines(reader, strategy, headers);
    } catch (NullPointerException ex) {
        this.logger.error("NullPointer: {}", ex.getMessage());
    } catch (IOException ex) {
        this.logger.error("IOException: {}", ex.getMessage());
    }

}

From source file:org.phenotips.vocabulary.internal.CSVFileService.java

License:Open Source License

private void parseLines(Reader reader, CSVStrategy strategy, String[] headers) {
    String[] pieces;// www .  j a v a  2  s. c  o  m

    try {
        CSVParser parser = new CSVParser(reader, strategy);

        while ((pieces = parser.getLine()) != null) {
            // Ignore the whole line if begins with tab symbol
            if (pieces.length != headers.length || "".equals(pieces[0])) {
                continue;
            }

            SolrInputDocument crtTerm = new SolrInputDocument();
            int counter = 0;
            for (String term : pieces) {
                if (!"".equals(term)) {
                    crtTerm.addField(headers[counter], term);
                }
                counter++;
            }

            this.solrDocuments.add(crtTerm);
        }
    } catch (NullPointerException ex) {
        this.logger.error("NullPointer: {} ", ex.getMessage());
    } catch (IOException ex) {
        this.logger.error(" IOException: {}", ex.getMessage());
    }
}