use of org.apache.commons.io.LineIterator in project pratilipi by Pratilipi.
the class PageContentApi method get.
@Get
public Response get(GetRequest request) throws UnexpectedServerException {
String title = null;
StringBuilder content = new StringBuilder();
try {
File file = _getFile(_getFileName(request.pageName, request.language));
if (// Fall-back to English
file == null && request.language != Language.ENGLISH)
file = _getFile(_getFileName(request.pageName, Language.ENGLISH));
if (// File doesn't exist in specified language and English
file == null)
return new Response();
LineIterator it = FileUtils.lineIterator(file, "UTF-8");
if (it.hasNext())
title = it.nextLine().trim();
while (it.hasNext()) content.append(it.nextLine() + "<br/>");
LineIterator.closeQuietly(it);
} catch (IOException e) {
logger.log(Level.SEVERE, "Exception while reading from data file.", e);
throw new UnexpectedServerException();
}
return new Response(title, content.toString());
}
use of org.apache.commons.io.LineIterator in project jackrabbit-oak by apache.
the class FileLineDifferenceIteratorTest method lineItr.
private static LineIterator lineItr(String seq) {
Iterable<String> seqItr = Splitter.on(',').trimResults().split(seq);
String lines = Joiner.on(StandardSystemProperty.LINE_SEPARATOR.value()).join(seqItr);
return new LineIterator(new StringReader(lines));
}
use of org.apache.commons.io.LineIterator in project ice by JBEI.
the class BulkCSVUpload method getBulkUploadDataFromFile.
// NOTE: this also validates the part data (with the exception of the actual files)
List<PartWithSample> getBulkUploadDataFromFile(InputStream inputStream) throws IOException {
List<PartWithSample> partDataList = new LinkedList<>();
// initialize parser to null; when not-null in the loop below, then the header has been parsed
CSVParser parser = null;
HashMap<Integer, HeaderValue> headers = null;
// parse CSV file
try {
LineIterator it = IOUtils.lineIterator(inputStream, "UTF-8");
int index = 0;
while (it.hasNext()) {
String line = it.nextLine().trim();
// check if first time parsing (first line)
if (parser == null) {
// to indicate the type of parser to use (tab or comma separated)
if (line.contains("\t") && !line.contains(","))
parser = new CSVParser('\t');
else
parser = new CSVParser();
// get column headers
String[] fieldStrArray = parser.parseLine(line);
headers = processColumnHeaders(fieldStrArray);
continue;
}
// skip any empty lines (holes) in the csv file
if (StringUtils.isBlank(line) || line.replaceAll(",", "").trim().isEmpty())
continue;
// at this point we must have headers since that should be the first item in the file
if (headers == null)
throw new IOException("Could not parse file headers");
// parser != null; process line contents with available headers
String[] valuesArray = parser.parseLine(line);
PartData partData = new PartData(addType);
PartSample partSample = null;
if (subType != null) {
partData.getLinkedParts().add(new PartData(subType));
}
// for each column
for (int i = 0; i < valuesArray.length; i += 1) {
HeaderValue headerForColumn = headers.get(i);
// process sample information
if (headerForColumn.isSampleField()) {
// todo : move to another method
if (partSample == null)
partSample = new PartSample();
setPartSampleData(((SampleHeaderValue) headerForColumn).getSampleField(), partSample, valuesArray[i]);
} else {
EntryHeaderValue entryHeaderValue = (EntryHeaderValue) headerForColumn;
EntryField field = entryHeaderValue.getEntryField();
PartData data;
String value = valuesArray[i];
boolean isSubType = entryHeaderValue.isSubType();
if (isSubType)
data = partData.getLinkedParts().get(0);
else
data = partData;
// get the data for the field
switch(field) {
case ATT_FILENAME:
ArrayList<AttachmentInfo> attachments = data.getAttachments();
if (attachments == null) {
attachments = new ArrayList<>();
data.setAttachments(attachments);
}
attachments.clear();
attachments.add(new AttachmentInfo(value));
break;
case SEQ_FILENAME:
data.setSequenceFileName(value);
break;
case SEQ_TRACE_FILES:
// todo
break;
case EXISTING_PART_NUMBER:
Entry entry = DAOFactory.getEntryDAO().getByPartNumber(value);
if (entry == null)
throw new IOException("Could not locate part number \"" + value + "\" for linking");
PartData toLink = entry.toDataTransferObject();
data.getLinkedParts().add(toLink);
break;
default:
partData = EntryUtil.setPartDataFromField(partData, value, field, isSubType);
}
}
}
// validate
List<EntryField> fields = EntryUtil.validates(partData);
if (!fields.isEmpty()) {
invalidFields.clear();
invalidFields.addAll(fields);
return null;
}
partData.setIndex(index);
PartWithSample partWithSample = new PartWithSample(partSample, partData);
partDataList.add(partWithSample);
index += 1;
}
} finally {
IOUtils.closeQuietly(inputStream);
}
return partDataList;
}
use of org.apache.commons.io.LineIterator in project pratilipi by Pratilipi.
the class DataAccessorMockImpl method getPratilipiListTitle.
@Override
public String getPratilipiListTitle(String listName, Language lang) {
String fileName = "list." + lang.getCode() + "." + listName;
String listTitle = null;
try {
InputStream inputStream = DataAccessor.class.getResource(CURATED_DATA_FOLDER + "/" + fileName).openStream();
LineIterator it = IOUtils.lineIterator(inputStream, "UTF-8");
listTitle = it.nextLine().trim();
LineIterator.closeQuietly(it);
} catch (NullPointerException | IOException e) {
logger.log(Level.SEVERE, "Exception while reading from " + listName + " .", e);
}
return listTitle;
}
use of org.apache.commons.io.LineIterator in project stanbol by apache.
the class DataFileResourceLoader method getLines.
public List<String> getLines(String resource) throws IOException {
List<String> lines = new ArrayList<String>();
LineIterator it = IOUtils.lineIterator(openResource(resource), "UTF-8");
while (it.hasNext()) {
String line = it.nextLine();
if (line != null && !line.isEmpty() && line.charAt(0) != '#') {
lines.add(line);
}
}
return lines;
}
Aggregations