coffeepot.bean.wr.reader.FixedLengthReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of coffeepot-bean-wr Show documentation
Show all versions of coffeepot-bean-wr Show documentation
Coffeepot-bean-wr is a simple Java framework for marshalling Java beans to flat file and unmarshal flat file to Java Beans
/*
* Copyright 2015 Jeandeson O. Merelis.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package coffeepot.bean.wr.reader;
/*
* #%L
* coffeepot-bean-wr
* %%
* Copyright (C) 2013 - 2015 Jeandeson O. Merelis
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import coffeepot.bean.wr.mapper.FieldModel;
import coffeepot.bean.wr.mapper.ObjectMapper;
import coffeepot.bean.wr.mapper.ObjectMapperFactory;
import coffeepot.bean.wr.types.FormatType;
import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;
import java.util.List;
/**
*
* @author Jeandeson O. Merelis
*/
public class FixedLengthReader extends AbstractReader {
private final ObjectMapperFactory mapperFactory = new ObjectMapperFactory(FormatType.FIXED_LENGTH);
private int idStart;
private int idLength;
private boolean objectById;
public FixedLengthReader(Reader reader) {
super(reader);
}
@Override
public ObjectMapperFactory getObjectMapperFactory() {
return mapperFactory;
}
@Override
protected void beforeUnmarshal() {
objectById = !mapperFactory.getIdsMap().isEmpty();
if (objectById) {
ObjectMapper om = mapperFactory.getIdsMap().values().iterator().next();
Iterator it = om.getFields().iterator();
int pos = 0;
while (it.hasNext()) {
FieldModel f = it.next();
if (f.isId()) {
idStart = pos;
idLength = f.getLength();
break;
}
pos = pos + f.getLength();
}
}
}
private String[] currentRecord;
@Override
protected void clear() {
super.clear();
currentLine = null;
}
@Override
protected void beforeFill(ObjectMapper mapper) {
List fields = filterFieldsByVersion(mapper);
currentRecord = new String[fields.size()];
int idx = 0;
int pos = 0;
int endIdx;
Iterator it = fields.iterator();
while (it.hasNext()) {
FieldModel f = it.next();
if (f.isNestedObject()) {
idx++;
continue;
}
endIdx = pos + f.getLength();
currentRecord[idx] = currentLine.substring(pos, endIdx);
pos = endIdx;
idx++;
}
}
@Override
protected String getIdValue(boolean fromNext) {
if (objectById) {
if (idResolver != null) {
String id = idResolver.call(fromNext ? nextLine : currentLine);
if (id != null) {
return id.trim();
}
}
String s = fromNext ? nextLine.substring(idStart, idStart + idLength) : currentLine.substring(idStart, idStart + idLength);
return s.trim();
} else {
return null;
}
}
@Override
protected String getValueByIndex(int idx) {
return currentRecord[idx];
}
@Override
protected void readLine() throws IOException {
currentLine = nextLine;
nextLine = readNextLine();
}
@Override
protected boolean isCurrentRecordNull() {
return currentLine == null;
}
@Override
protected boolean hasNext() {
return nextLine != null;
}
protected String readNextLine() throws IOException {
String line = null;
while (true) {
line = getLine();
if (line == null) {
return null;
}
String s = line.trim();
if (!s.isEmpty()) {
break;
}
}
if (recordInitializator != null && removeRecordInitializator) {
if (line.startsWith(recordInitializator)) {
line = line.substring(recordInitializator.length());
}
}
return line;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy