List of usage examples for org.apache.poi.openxml4j.opc.internal FileHelper getFilename
public static String getFilename(File file)
From source file:org.tridas.io.formats.heidelberg.HeidelbergReader.java
License:Apache License
@Override protected void parseFile(String[] argFileString, IMetadataFieldSet argDefaultFields) throws InvalidDendroFileException { log.debug("Parsing: " + argFileString); defaults = (HeidelbergToTridasDefaults) argDefaultFields; // first lets see if we look like a heidelberg file checkFile(argFileString);//from w w w. jav a 2s . com if (this.getOriginalFilename() != null) { File file = new File(this.getOriginalFilename()); defaults.getStringDefaultValue(TridasExtraField.ORIGINAL_FILENAME) .setValue(FileHelper.getFilename(file)); } int fileLength = argFileString.length; int lineNum = 0; HeidelbergSeries currSeries = null; String commentsCache = ""; while (lineNum < fileLength) { currentLineNum = lineNum; // update line num String line = argFileString[lineNum]; if (line.startsWith("HEADER:")) { lineNum++;// get off header line currentLineNum = lineNum; // update line num line = argFileString[lineNum]; ArrayList<String> header = new ArrayList<String>(); FHStartsOrEndsWith seriesstarts = null; while (!line.toUpperCase().startsWith("DATA:")) { header.add(line); currentLineNum = ++lineNum; // update line num line = argFileString[lineNum]; if (line.toLowerCase().startsWith("seriesstart")) { String[] split = line.split("="); if (split.length == 2) { seriesstarts = FHStartsOrEndsWith.fromCode(split[1]); } } } currSeries = new HeidelbergSeries(); extractHeader(header.toArray(new String[0]), currSeries); currSeries.nextValueIs = seriesstarts; } else if (line.toUpperCase().startsWith("DATA:")) { // see what kind of data is here FHDataFormat dataFormat = null; try { dataFormat = FHDataFormat.valueOf(line.substring(line.indexOf(":") + 1).trim()); } catch (IllegalArgumentException e) { throw new InvalidDendroFileException( "The data format '" + line.substring(line.indexOf(":") + 1).trim() + "' [" + line.substring(line.indexOf(":") + 1).trim().length() + " chars" + "] is not recognised"); } lineNum++; line = argFileString[lineNum]; currentLineNum = lineNum; // update line num ArrayList<String> data = new ArrayList<String>(); while (lineNum < fileLength) { line = argFileString[lineNum]; if (line.startsWith("HEADER")) { break; } if (line.trim().equals("")) { currentLineNum = ++lineNum; continue; } data.add(line); currentLineNum = ++lineNum; // update line num } if (currSeries == null) { currSeries = new HeidelbergSeries(); } currSeries.dataFormat = dataFormat; extractData(data.toArray(new String[0]), currSeries); // Add any comments lines that have been cached if (commentsCache != null) { currSeries.fileMetadata.put("comment[x", commentsCache); commentsCache = null; } series.add(currSeries); currSeries = null; } else { log.error("Found unknown line. Treating as comment"); lineNum++; // Cache the line to add to the next series as comments if (commentsCache == null) { commentsCache = ""; } commentsCache += line.trim() + "\n"; } } populateHeaderInformation(); populateDataInformation(); // Loop through all the series to check dates are logical for (HeidelbergSeries thisSeries : series) { SafeIntYear startYear = null; SafeIntYear endYear = null; Integer yearCount = thisSeries.dataVals.size(); Integer length = null; if (thisSeries.defaults.getStringDefaultValue(DefaultFields.LENGTH).getValue() != null) { try { length = Integer .valueOf(thisSeries.defaults.getStringDefaultValue(DefaultFields.LENGTH).getValue()); } catch (Exception e) { length = yearCount; } } else { length = yearCount; } // Check that DateBegin, DateEnd and number of values agree if (thisSeries.defaults.getIntegerDefaultValue(DefaultFields.DATE_BEGIN).getValue() != null) { startYear = new SafeIntYear( thisSeries.defaults.getIntegerDefaultValue(DefaultFields.DATE_BEGIN).getValue().toString(), true); } if (thisSeries.defaults.getIntegerDefaultValue(DefaultFields.DATE_END).getValue() != null) { endYear = new SafeIntYear( thisSeries.defaults.getIntegerDefaultValue(DefaultFields.DATE_END).getValue().toString(), true); } if (startYear != null && endYear != null) { // Check the difference is the same as the yearCount if (endYear.diff(startYear) != length - 1) { /*thisSeries.defaults.addConversionWarning( new ConversionWarning( WarningType.AMBIGUOUS, I18n.getText("heidelberg.inconsistentDates")) );*/ throw new InvalidDendroFileException(I18n.getText("heidelberg.inconsistentDates")); } } else if (startYear != null && endYear == null) { // Use start year to set endYear based on the length thisSeries.defaults.getIntegerDefaultValue(DefaultFields.DATE_END) .setValue(Integer.parseInt(startYear.add(length - 1).toString())); } else if (startYear == null && endYear != null) { // Set startYear based on endYear and length thisSeries.defaults.getIntegerDefaultValue(DefaultFields.DATE_BEGIN) .setValue(Integer.parseInt(endYear.add(1 - length).toString())); } else { // both start and end year are null addWarning( new ConversionWarning(WarningType.NULL_VALUE, I18n.getText("heidelberg.noStartOrEndDate"))); } } }
From source file:org.tridas.io.formats.sheffield.SheffieldReader.java
License:Apache License
@SuppressWarnings("unchecked") @Override// w w w. j a v a 2 s. c o m protected void parseFile(String[] argFileString, IMetadataFieldSet argDefaultFields) throws InvalidDendroFileException { defaults = (SheffieldToTridasDefaults) argDefaultFields; // Check the file is valid checkFile(argFileString); if (this.getOriginalFilename() != null) { File file = new File(this.getOriginalFilename()); defaults.getStringDefaultValue(TridasExtraField.ORIGINAL_FILENAME) .setValue(FileHelper.getFilename(file)); } ArrayList<TridasValue> ringWidthValues = new ArrayList<TridasValue>(); SafeIntYear startYear = null; for (int lineNum = 1; lineNum <= 22; lineNum++) { String lineString = argFileString[lineNum - 1]; // Line 1 - Series title if (lineNum == 1) { if (lineString.length() > 64) { addWarning( new ConversionWarning(WarningType.NOT_STRICT, I18n.getText("sheffield.lineOneTooBig"))); } if (SheffieldFile.containsSpecialChars(lineString)) { addWarning(new ConversionWarning(WarningType.NOT_STRICT, I18n.getText("sheffield.specialCharWarning"))); } defaults.getStringDefaultValue(DefaultFields.SERIES_TITLE).setValue(lineString); } // Line 2 - Number of rings else if (lineNum == 2) { try { int ringCount = Integer.valueOf(lineString); defaults.getIntegerDefaultValue(DefaultFields.RING_COUNT).setValue(ringCount); } catch (NumberFormatException e) { addWarning(new ConversionWarning(WarningType.INVALID, I18n.getText("fileio.invalidDataValue"))); } } // Line 3 - Date type else if (lineNum == 3) { if (!lineString.equalsIgnoreCase("A") && (!lineString.equalsIgnoreCase("R"))) { addWarning( new ConversionWarning(WarningType.INVALID, I18n.getText("sheffield.invalidDateType"))); continue; } dateType = SheffieldDateType.fromCode(lineString); GenericDefaultValue<SheffieldDateType> dateTypeField = (GenericDefaultValue<SheffieldDateType>) defaults .getDefaultValue(DefaultFields.SHEFFIELD_DATE_TYPE); dateTypeField.setValue(SheffieldDateType.fromCode(lineString)); } // Line 4 - Start date else if (lineNum == 4) { try { int yearNum = Integer.valueOf(lineString.trim()); // Handle offset for absolute dates if (dateType == SheffieldDateType.ABSOLUTE) { if (yearNum >= 10001) { yearNum = yearNum - 10000; } else { yearNum = yearNum - 10001; } } GenericDefaultValue<SafeIntYear> startYearField = (GenericDefaultValue<SafeIntYear>) defaults .getDefaultValue(DefaultFields.START_YEAR); startYear = new SafeIntYear(yearNum); startYearField.setValue(startYear); } catch (NumberFormatException e) { addWarning(new ConversionWarning(WarningType.INVALID, I18n.getText("fileio.invalidStartYear"))); } } // Line 5 - Data type else if (lineNum == 5) { GenericDefaultValue<SheffieldDataType> dataTypeField = (GenericDefaultValue<SheffieldDataType>) defaults .getDefaultValue(DefaultFields.SHEFFIELD_DATA_TYPE); dataTypeField.setValue(SheffieldDataType.fromCode(lineString)); } // Line 6 - sapwood number or number of timbers else if (lineNum == 6) { Integer val = 0; try { val = Integer.parseInt(lineString); GenericDefaultValue<SheffieldDataType> dataTypeField = (GenericDefaultValue<SheffieldDataType>) defaults .getDefaultValue(DefaultFields.SHEFFIELD_DATA_TYPE); if (dataTypeField.getValue().equals(SheffieldDataType.ANNUAL_RAW_RING_WIDTH)) { defaults.getIntegerDefaultValue(DefaultFields.SAPWOOD_COUNT).setValue(val); } else { // Field contains number of timbers/chronologies. This is not required // as it can be taken from the 'count' of the value tags } } catch (NumberFormatException e) { addWarning(new ConversionWarning(WarningType.INVALID, I18n.getText("fileio.invalidDataValue"), "Sapwood count")); continue; } } // Line 7 - edge code or chronology type else if (lineNum == 7) { GenericDefaultValue<SheffieldDataType> dataTypeField = (GenericDefaultValue<SheffieldDataType>) defaults .getDefaultValue(DefaultFields.SHEFFIELD_DATA_TYPE); if (dataTypeField.getValue().equals(SheffieldDataType.ANNUAL_RAW_RING_WIDTH)) { // Raw data so this field is for edge code GenericDefaultValue<SheffieldEdgeCode> edgeCodeField = (GenericDefaultValue<SheffieldEdgeCode>) defaults .getDefaultValue(DefaultFields.SHEFFIELD_EDGE_CODE); if (SheffieldEdgeCode.fromCode(lineString.trim()) != null) { edgeCodeField.setValue(SheffieldEdgeCode.fromCode(lineString.trim())); } else { addWarning(new ConversionWarning(WarningType.INVALID, I18n.getText("sheffield.invalidEdgeCode"))); continue; } } else { // This field is for chronology type GenericDefaultValue<SheffieldChronologyType> chronologyTypeField = (GenericDefaultValue<SheffieldChronologyType>) defaults .getDefaultValue(DefaultFields.SHEFFIELD_CHRONOLOGY_TYPE); if (SheffieldChronologyType.fromCode(lineString.trim()) != null) { chronologyTypeField.setValue(SheffieldChronologyType.fromCode(lineString.trim())); } else { addWarning(new ConversionWarning(WarningType.INVALID, I18n.getText("sheffield.invalidChronologyType"))); continue; } } } // Line 8 - comment else if (lineNum == 8) { if (lineString.length() > 64) { addWarning(new ConversionWarning(WarningType.NOT_STRICT, I18n.getText("sheffield.lineNTooBig", String.valueOf(lineString.length())))); } defaults.getStringDefaultValue(DefaultFields.SERIES_COMMENT).setValue(lineString); } // Line 9 - UK Grid coords else if (lineNum == 9) { // We could add PROJ4 or Jcoord libs to convert these but not sure its worth it // For now just add as genericField if (!lineString.equals("?")) { defaults.getStringDefaultValue(DefaultFields.UK_COORDS).setValue(lineString); try { SpatialUtils.getLocationGeometryFromBNG(lineString); } catch (NumberFormatException e) { addWarning(new ConversionWarning(WarningType.INVALID, "British National Grid coordinates invalid", String.valueOf(lineString.length()))); } } } // Line 10 - Lat/Long coords else if (lineNum == 10) { if (!lineString.equals("?")) { Double northing; Double easting; String[] coords = lineString.split(" "); if (coords.length != 2) { addWarning(new ConversionWarning(WarningType.NOT_STRICT, I18n.getText("sheffield.errorParsingCoords"))); continue; } if (lineString.contains("^")) { // Convert old style coordinates try { northing = convertCoordsToDD(coords[0], NorthingEasting.NORTH_SOUTH); easting = convertCoordsToDD(coords[1], NorthingEasting.EAST_WEST); } catch (ConversionWarningException e) { addWarning(e.getWarning()); continue; } } else { try { northing = Double.valueOf(coords[0]); easting = Double.valueOf(coords[1]); } catch (NumberFormatException e) { addWarning(new ConversionWarning(WarningType.NOT_STRICT, I18n.getText("sheffield.errorParsingCoords"))); continue; } } if (northing != null && easting != null) { defaults.getDoubleDefaultValue(DefaultFields.LATITUDE).setValue(northing); defaults.getDoubleDefaultValue(DefaultFields.LONGITUDE).setValue(easting); } } } // Line 11 - Pith else if (lineNum == 11) { GenericDefaultValue<ComplexPresenceAbsence> pithField = (GenericDefaultValue<ComplexPresenceAbsence>) defaults .getDefaultValue(DefaultFields.PITH); if (lineString.equalsIgnoreCase("C")) { pithField.setValue(ComplexPresenceAbsence.COMPLETE); } else if (lineString.equalsIgnoreCase("?")) { pithField.setValue(ComplexPresenceAbsence.UNKNOWN); } else { pithField.setValue(ComplexPresenceAbsence.ABSENT); // Sheffield format includes some extra info about pith that does not // map // to TRiDaS so this info will be stored as a generic field defaults.getStringDefaultValue(DefaultFields.PITH_DESCRIPTION) .setValue(SheffieldPithCode.fromCode(lineString).toString()); } } // Line 12 - Cross-section code else if (lineNum == 12) { GenericDefaultValue<SheffieldShapeCode> shapeField = (GenericDefaultValue<SheffieldShapeCode>) defaults .getDefaultValue(DefaultFields.SHEFFIELD_SHAPE_CODE); if (SheffieldShapeCode.fromCode(lineString) != null) { shapeField.setValue(SheffieldShapeCode.fromCode(lineString)); } else { addWarning(new ConversionWarning(WarningType.INVALID, I18n.getText("fileio.invalidDataValue"), "Cross-section code")); continue; } } // Line 13 - Major dimension else if (lineNum == 13) { Double dim; try { dim = Double.parseDouble(lineString); defaults.getDoubleDefaultValue(DefaultFields.MAJOR_DIM).setValue(dim); } catch (NumberFormatException e) { addWarning(new ConversionWarning(WarningType.INVALID, I18n.getText("fileio.invalidDataValue"), "Major dimension")); continue; } } // Line 14 - Minor dimension else if (lineNum == 14) { Double dim; try { dim = Double.parseDouble(lineString); defaults.getDoubleDefaultValue(DefaultFields.MINOR_DIM).setValue(dim); } catch (NumberFormatException e) { addWarning(new ConversionWarning(WarningType.INVALID, I18n.getText("fileio.invalidDataValue"), "Minor dimension")); continue; } } // Line 15 - Unmeasured inner rings else if (lineNum == 15) { Integer ringCount = 0; if ((lineString.equalsIgnoreCase("N"))) { defaults.getIntegerDefaultValue(DefaultFields.UNMEAS_INNER_RINGS).setValue(0); } else { try { ringCount = Integer.parseInt(lineString.substring(1)); defaults.getIntegerDefaultValue(DefaultFields.UNMEAS_INNER_RINGS).setValue(ringCount); } catch (NumberFormatException e) { addWarning(new ConversionWarning(WarningType.INVALID, I18n.getText("fileio.invalidDataValue"), "Unmeasured inner rings")); continue; } } } // Line 16 - Unmeasured outer rings else if (lineNum == 16) { Integer ringCount = 0; if ((lineString.equalsIgnoreCase("N"))) { defaults.getIntegerDefaultValue(DefaultFields.UNMEAS_OUTER_RINGS).setValue(0); } else { try { ringCount = Integer.parseInt(lineString.substring(1)); defaults.getIntegerDefaultValue(DefaultFields.UNMEAS_OUTER_RINGS).setValue(ringCount); } catch (NumberFormatException e) { addWarning(new ConversionWarning(WarningType.INVALID, I18n.getText("fileio.invalidDataValue"), "Unmeasured outer rings")); continue; } } } // Line 17 - Group/phase else if (lineNum == 17) { if (lineString.length() >= 14) { addWarning( new ConversionWarning(WarningType.NOT_STRICT, I18n.getText("sheffield.line17TooBig"))); } else if (!lineString.equals("?")) { defaults.getStringDefaultValue(DefaultFields.GROUP_PHASE).setValue(lineString); } } // Line 18 - Short title else if (lineNum == 18) { if (lineString.length() > 8) { addWarning( new ConversionWarning(WarningType.NOT_STRICT, I18n.getText("sheffield.line18TooBig"))); } defaults.getStringDefaultValue(DefaultFields.SHORT_TITLE).setValue(lineString); } // Line 19 - Period else if (lineNum == 19) { GenericDefaultValue<SheffieldPeriodCode> periodField = (GenericDefaultValue<SheffieldPeriodCode>) defaults .getDefaultValue(DefaultFields.SHEFFIELD_PERIOD_CODE); if (SheffieldPeriodCode.fromCode(lineString) != null) { periodField.setValue(SheffieldPeriodCode.fromCode(lineString)); } else { addWarning(new ConversionWarning(WarningType.INVALID, I18n.getText("fileio.invalidDataValue"), "Period code")); continue; } } // Line 20 - Species code else if (lineNum == 20) { if (!lineString.equals("?")) { defaults.getStringDefaultValue(DefaultFields.TAXON_CODE).setValue(lineString); } } // Line 21 - Interpretation and anatomical notes // TODO - Parse value.remarks from these notes else if (lineNum == 21) { if (!lineString.equals("?")) { String[] notesArray = lineString.split("~"); if (notesArray.length % 3 != 0) { // Notes array does not split into threes addWarning(new ConversionWarning(WarningType.INVALID, I18n.getText("sheffield.interpInvalid"))); continue; } for (int i = 0; i < notesArray.length; i = i + 3) { if (!notesArray[i].equalsIgnoreCase("I") && !notesArray[i].equalsIgnoreCase("A")) { // Each note must begin with an I (interpretation) or an A // (anatomy) addWarning(new ConversionWarning(WarningType.INVALID, I18n.getText("sheffield.interpNotIorA"))); continue; } } for (int i = 1; i < notesArray.length; i = i + 3) { try { Integer.parseInt(notesArray[i]); } catch (NumberFormatException e) { // The second field of each note must be a year or ring number addWarning(new ConversionWarning(WarningType.INVALID, I18n.getText("sheffield.interpNoNumber"))); continue; } } defaults.getStringDefaultValue(DefaultFields.INTERPRETATION_NOTES).setValue(lineString); } } // Line 22 - Variable type else if (lineNum == 22) { GenericDefaultValue<SheffieldVariableCode> variableField = (GenericDefaultValue<SheffieldVariableCode>) defaults .getDefaultValue(DefaultFields.SHEFFIELD_VARIABLE_TYPE); if (SheffieldVariableCode.fromCode(lineString) != null) { variableField.setValue(SheffieldVariableCode.fromCode(lineString)); } else { addWarning(new ConversionWarning(WarningType.INVALID, I18n.getText("fileio.invalidDataValue"), "Data variable code")); continue; } } } int lineNum = 23; // Extract actual values for (int i = 22; i < argFileString.length; i++) { TridasValue v = new TridasValue(); if (!argFileString[i].trim().equals("H") && !argFileString[i].trim().equals("R") && !argFileString[i].trim().equals("F")) { v.setValue(argFileString[i].trim()); ringWidthValues.add(v); log.debug("value = " + String.valueOf(argFileString[i])); } else { lineNum = i + 1; break; } } // Set last year if (startYear != null) { SafeIntYear endYear = startYear.add(ringWidthValues.size() - 1); GenericDefaultValue<SafeIntYear> endYearField = (GenericDefaultValue<SafeIntYear>) defaults .getDefaultValue(DefaultFields.END_YEAR); endYearField.setValue(endYear); } // See if we can get counts if (argFileString[lineNum - 1].trim().equals("H")) { for (int i = lineNum; i < argFileString.length; i++) { TridasValue v = null; try { v = ringWidthValues.get(i - lineNum); } catch (Exception e) { break; } if (!argFileString[i].trim().equals("H") && !argFileString[i].trim().equals("R") && !argFileString[i].trim().equals("F")) { Integer count; try { count = Integer.parseInt(argFileString[i]); } catch (NumberFormatException e) { break; } v.setCount(count); log.debug("count = " + String.valueOf(count)); } else { lineNum = i; break; } } } // Check ring count matches number of values in file if (defaults.getIntegerDefaultValue(DefaultFields.RING_COUNT).getValue() != ringWidthValues.size()) { addWarning(new ConversionWarning(WarningType.INVALID, I18n.getText("fileio.valueCountMismatch"))); defaults.getIntegerDefaultValue(DefaultFields.RING_COUNT).setValue(ringWidthValues.size()); } GenericDefaultValue<SheffieldDataType> dataTypeField = (GenericDefaultValue<SheffieldDataType>) defaults .getDefaultValue(DefaultFields.SHEFFIELD_DATA_TYPE); if (dataTypeField.getValue().equals(SheffieldDataType.ANNUAL_RAW_RING_WIDTH)) { // Now build up our measurementSeries TridasMeasurementSeries series = defaults.getMeasurementSeriesWithDefaults(); // Add values to nested value(s) tags TridasValues valuesGroup = defaults.getTridasValuesWithDefaults(); valuesGroup.setValues(ringWidthValues); ArrayList<TridasValues> valuesGroupList = new ArrayList<TridasValues>(); valuesGroupList.add(valuesGroup); // Add all the data to the series series.setValues(valuesGroupList); this.series = series; } else { // Now build up our measurementSeries TridasDerivedSeries series = defaults.getDefaultTridasDerivedSeries(); // Add values to nested value(s) tags TridasValues valuesGroup = defaults.getTridasValuesWithDefaults(); valuesGroup.setValues(ringWidthValues); ArrayList<TridasValues> valuesGroupList = new ArrayList<TridasValues>(); valuesGroupList.add(valuesGroup); // Add all the data to the series series.setValues(valuesGroupList); this.series = series; } }