for (int frameIdx = 0; frameIdx < prog.getProgramFrameCount(); frameIdx++) {
ProgramFrame frame = prog.getProgramFrameAt(frameIdx);
// Trim text fields and remove all empty fields from the day program
for (int fieldIdx = frame.getProgramFieldCount() - 1; fieldIdx >= 0; fieldIdx--) {
ProgramField field = frame.getProgramFieldAt(fieldIdx);
// Trim text fields
if (field.getType().getFormat() == ProgramFieldType.TEXT_FORMAT) {
String oldText = field.getTextData();
if (oldText != null) {
String newText = oldText.trim();
if (oldText.length() != newText.length()) {
field.setTextData(newText);
}
}
}
// Remove empty fields
byte[] data = field.getBinaryData();
if ((data == null) || (data.length == 0)) {
frame.removeProgramFieldAt(fieldIdx);
}
}
// Check the mandatory fields (start time and title)
ProgramField startTimeField = frame.getProgramFieldOfType(ProgramFieldType.START_TIME_TYPE);
if (startTimeField == null) {
throw new PreparationException("Program frame has no start time. "
+ "ID: " + frame.getId() + ", Day program: " + date + ", " + country + ", " + channel);
}
ProgramField titleField = frame.getProgramFieldOfType(ProgramFieldType.TITLE_TYPE);
if (titleField == null) {
frame.addProgramField(ProgramField.create(ProgramFieldType.TITLE_TYPE, "[Unknown title]"));
System.out.println("Program frame has no title. "
+ "ID: " + frame.getId() + ", Day program: " + date + ", " + country + ", " + channel);
}
// Try to create a short description
ProgramField shortDescField = frame.getProgramFieldOfType(ProgramFieldType.SHORT_DESCRIPTION_TYPE);
if (shortDescField == null) {
ProgramField descField = frame.getProgramFieldOfType(ProgramFieldType.DESCRIPTION_TYPE);
if (descField != null) {
// Generate a short description from the description
String shortDesc = descField.getTextData();
if (shortDesc.length() > MutableProgram.MAX_SHORT_INFO_LENGTH) {
int lastSpacePos = shortDesc.lastIndexOf(' ', MutableProgram.MAX_SHORT_INFO_LENGTH);
if (lastSpacePos == -1) {
shortDesc = shortDesc.substring(0, MutableProgram.MAX_SHORT_INFO_LENGTH);
} else {