Skip to content

Commit

Permalink
Merge pull request #920 from NASA-PDS/issue_902
Browse files Browse the repository at this point in the history
Fix URL handling on Windows OS
  • Loading branch information
jordanpadams authored Jun 6, 2024
2 parents 7fc118c + 8eb18e3 commit bc5a110
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 48 deletions.
48 changes: 4 additions & 44 deletions src/main/java/gov/nasa/pds/tools/util/ImageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.io.File;
import java.io.RandomAccessFile;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;

Expand All @@ -16,25 +14,16 @@

public class ImageUtil {
private static final Logger LOG = LoggerFactory.getLogger(ImageUtil.class);
private URL target = null;

private static int JPEG_FIRST_SHORT = 0xffd8;
private static int JPEG_LAST_SHORT = 0xffd9;

private static int PNG_SIGNATURE_FIRST_INT = 0x89504e47;
private static int PNG_SIGNATURE_SECOND_INT = 0x0d0a1a0a;

public ImageUtil(URL target) {
this.target = target;
public ImageUtil() {
}

/**
* Returns the URL of the target.
*
*/
public URL getTarget() {
return (this.target);
}

/**
* Check if a JPEG file is valid by inspecting the first 2 bytes for 0xffd8 and last 2 bytes for
Expand All @@ -49,19 +38,6 @@ public boolean isJPEG(String jpegBase, URL parentURL) throws Exception {

LOG.debug("isJPEG:jpegBase {}", jpegBase);

// Get the location of the input file.
URI uri = null;
try {
uri = getTarget().toURI();
} catch (URISyntaxException e) {
// Should never happen
// but if it does, print an error message.
// Observed in DEV that if the file name contains spaces, the getURI() results
// in the URISyntaxException exception.
LOG.error("isJPEG:Cannot build URI for target {}", getTarget());
throw new Exception("isJPEG:Cannot build URI for target [" + getTarget() + "]");
}

// Note: The function FilenameUtils.getPath() doesn't seem to work correctly.
// It returns the path without the leading slash '/':
//
Expand All @@ -79,13 +55,10 @@ public boolean isJPEG(String jpegBase, URL parentURL) throws Exception {
// File(uri.getPath()).getParent()" which will only get the top level directory.
// It would be a bug if the file is in a sub directory of the top level
// directory.
String parent = parentURL.getFile();

LOG.debug("isJPEG:,parent,jpegBase {},{}", parent, jpegBase);


// try using Paths and URI internal consistency to make this work for #902
// Build the full pathname of the file.
String jpegRef = Paths.get(parent, jpegBase).toString();
LOG.debug("isJPEG:parent,jpegBase,jpegRef [{}],[{}],[{}]", parent, jpegBase, jpegRef);
String jpegRef = Paths.get(parentURL.toURI().resolve(jpegBase)).toString();

File jpegFile = new File(jpegRef);
RandomAccessFile raf = new RandomAccessFile(jpegFile, "r");
Expand Down Expand Up @@ -143,19 +116,6 @@ public boolean isPNG(String pngBase, URL parentURL) throws Exception {

LOG.debug("isPNG:pngBase {}", pngBase);

// Get the location of the input file.
URI uri = null;
try {
uri = getTarget().toURI();
} catch (URISyntaxException e) {
// Should never happen
// but if it does, print an error message.
// Observed in DEV that if the file name contains spaces, the getURI() results
// in the URISyntaxException exception.
LOG.error("isPNG:Cannot build URI for target {}", getTarget());
throw new Exception("isPNG:Cannot build URI for target [" + getTarget() + "]");
}

// Get the parent from parentURL instead of using "new
// File(uri.getPath()).getParent()" which will only get the top level directory.
// It would be a bug if the file is in a sub directory of the top level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -819,10 +819,11 @@ private void handleJPEG(ValidationTarget target, URL fileRef, TinyNodeImpl fileO

if (this.imageUtil == null) {
// Save imageUtil so it can be reused.
this.imageUtil = new ImageUtil(fileRef);
this.imageUtil = new ImageUtil();
}

jpegValidateFlag = this.imageUtil.isJPEG(jpegName, new URL(parent, directory));
jpegValidateFlag = this.imageUtil.isJPEG(jpegName, new URL(parent,
directory.endsWith("/") || directory.isBlank() ? directory : (directory + "/")));

// Report a warning if the JPEG file is not compliant.
if (!jpegValidateFlag) {
Expand Down Expand Up @@ -863,10 +864,11 @@ private void handlePNG(ValidationTarget target, URL fileRef, TinyNodeImpl fileOb

if (this.imageUtil == null) {
// Save imageUtil so it can be reused.
this.imageUtil = new ImageUtil(fileRef);
this.imageUtil = new ImageUtil();
}

validateFlag = this.imageUtil.isPNG(pngName, new URL(parent, directory));
validateFlag = this.imageUtil.isPNG(pngName, new URL(parent,
directory.endsWith("/") || directory.isBlank() ? directory : (directory + "/")));

// Report a warning if the PNG file is not compliant.
if (!validateFlag) {
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/features/developer.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Scenario Outline: Execute validate command for tests below.
# Validate#905
|"NASA-PDS/validate#905 Success no duplicates in non-observational;" | "github905" | 0 | "0 errors expected" | "totalErrors" | "src/test/resources" | "target/test" | "-r {reportDir}/report_github905.json -s json --skip-context-validation -t {resourceDir}/github905/dsn_0159-science.2008-02-29.xml {resourceDir}/github905/dsn_0159-science.2009-05-18.xml" | "report_github905.json" |

# Validate#902
|"NASA-PDS/validate#902 Success with windows file URL" | "github902" | 0 | "0 errors expected" | "totalErrors" | "src/test/resources" | "target/test" | "-r {reportDir}/report_github902.json -s json --skip-context-validation -t {resourceDir}/github902/s_00168901_thm.xml" | "report_github902.json" |

# Validate#873
|"NASA-PDS/validate#873 Success files same name different paths" | "github873" | 0 | "0 errors expected" | "totalErrors" | "src/test/resources" | "target/test" | "-r {reportDir}/report_github873.json -s json --skip-context-validation -R pds4.bundle -t {resourceDir}/github873" | "report_github873.json" |

Expand Down
Binary file added src/test/resources/github902/s_00168901_thm.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions src/test/resources/github902/s_00168901_thm.lbl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
PDS_VERSION_ID = PDS3

/* File characteristics */
RECORD_TYPE = UNDEFINED

/* Pointers to objects in file */
^DOCUMENT = "S_00168901_THM.JPG"

/* Identification data elements */
DATA_SET_ID = "MRO-M-SHARAD-5-RADARGRAM-V2.0"
PRODUCT_ID = "S_00168901_THM"
INSTRUMENT_HOST_ID = "MRO"
INSTRUMENT_HOST_NAME = "MARS RECONNAISSANCE ORBITER"
INSTRUMENT_ID = "SHARAD"
INSTRUMENT_NAME = "SHALLOW RADAR"
TARGET_NAME = "MARS"
ORBIT_NUMBER = 1689
MRO:START_SUB_SPACECRAFT_LONGITUDE = 230.67676 <DEGREE>
MRO:START_SUB_SPACECRAFT_LATITUDE = 64.239113 <DEGREE>
MRO:STOP_SUB_SPACECRAFT_LONGITUDE = 229.39072 <DEGREE>
MRO:STOP_SUB_SPACECRAFT_LATITUDE = 59.864001 <DEGREE>
START_TIME = 2006-12-06T02:08:22.929
STOP_TIME = 2006-12-06T02:09:48.723
SPACECRAFT_CLOCK_START_COUNT = "UNK"
SPACECRAFT_CLOCK_STOP_COUNT = "UNK"
PRODUCT_CREATION_TIME = 2021-05-19T16:15:43.494
PRODUCT_VERSION_ID = "1"
PRODUCER_FULL_NAME = "BRUCE A. CAMPBELL"
PRODUCER_INSTITUTION_NAME = "SMITHSONIAN INSTITUTION"

/* JPEG information */
OBJECT = DOCUMENT
DOCUMENT_NAME = "JPEG BROWSE"
DOCUMENT_FORMAT = JPG
DOCUMENT_TOPIC_TYPE = "BROWSE IMAGE"
INTERCHANGE_FORMAT = BINARY
PUBLICATION_DATE = 2021-05-19
DESCRIPTION = "Browse version of TIFF radargram image."
END_OBJECT = DOCUMENT

END
176 changes: 176 additions & 0 deletions src/test/resources/github902/s_00168901_thm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="https://pds.nasa.gov/pds4/pds/v1/PDS4_PDS_1I00.sch" schematypens="http://purl.oclc.org/dsdl/schematron"?>
<?xml-model href="https://pds.nasa.gov/pds4/mission/mro/v1/PDS4_MRO_1I00_1300.sch" schematypens="http://purl.oclc.org/dsdl/schematron"?>
<?xml-model href="https://pds.nasa.gov/pds4/geom/v1/PDS4_GEOM_1I00_1960.sch" schematypens="http://purl.oclc.org/dsdl/schematron" ?>

<Product_Browse
xmlns="http://pds.nasa.gov/pds4/pds/v1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mro="http://pds.nasa.gov/pds4/mission/mro/v1"
xmlns:geom="http://pds.nasa.gov/pds4/geom/v1"
xsi:schemaLocation=
"http://pds.nasa.gov/pds4/pds/v1 https://pds.nasa.gov/pds4/pds/v1/PDS4_PDS_1I00.xsd
http://pds.nasa.gov/pds4/mission/mro/v1 https://pds.nasa.gov/pds4/mission/mro/v1/PDS4_MRO_1I00_1300.xsd
http://pds.nasa.gov/pds4/geom/v1 https://pds.nasa.gov/pds4/geom/v1/PDS4_GEOM_1I00_1960.xsd">

<Identification_Area>
<logical_identifier>urn:nasa:pds:mro_sharad_us_radargrams:browse:s_00168901_thm</logical_identifier>
<version_id>1.0</version_id>
<title>MRO SHARAD US Radargrams Browse THM Products</title>
<information_model_version>1.18.0.0</information_model_version>
<product_class>Product_Browse</product_class>
<Citation_Information>
<author_list>Campbell, B. A.</author_list>
<publication_year>2024</publication_year>
<description>JPEG Browse Product for MRO SHARAD US Radargram</description>
</Citation_Information>
<Modification_History>
<Modification_Detail>
<modification_date>2024-05-17T10:59:43.592Z</modification_date>
<version_id>1.0</version_id>
<description>PDS4 XML Label Creation</description>
</Modification_Detail>
</Modification_History>
</Identification_Area>

<Context_Area>
<Time_Coordinates>
<start_date_time>2006-12-06T02:08:22.929Z</start_date_time>
<stop_date_time>2006-12-06T02:09:48.723Z</stop_date_time>
</Time_Coordinates>

<Investigation_Area>
<name>MARS RECONNAISSANCE ORBITER</name>
<type>Mission</type>
<Internal_Reference>
<lid_reference>urn:nasa:pds:context:investigation:mission.mars_reconnaissance_orbiter</lid_reference>
<reference_type>browse_to_investigation</reference_type>
</Internal_Reference>
</Investigation_Area>

<Observing_System>
<Observing_System_Component>
<name>MARS RECONNAISSANCE ORBITER</name>
<type>Host</type>
<Internal_Reference>
<lid_reference>urn:nasa:pds:context:instrument_host:spacecraft.mro</lid_reference>
<reference_type>is_instrument_host</reference_type>
</Internal_Reference>
</Observing_System_Component>
<Observing_System_Component>
<name>SHALLOW RADAR</name>
<type>Instrument</type>
<Internal_Reference>
<lid_reference>urn:nasa:pds:context:instrument:sharad.mro</lid_reference>
<reference_type>is_instrument</reference_type>
</Internal_Reference>
</Observing_System_Component>
</Observing_System>

<Target_Identification>
<name>MARS</name>
<type>Planet</type>
<Internal_Reference>
<lid_reference>urn:nasa:pds:context:target:planet.mars</lid_reference>
<reference_type>browse_to_target</reference_type>
</Internal_Reference>
</Target_Identification>

<Mission_Area>
<mro:MRO_Parameters>
<mro:product_version_id>1</mro:product_version_id>
<mro:orbit_number>1689</mro:orbit_number>
<mro:producer_institution_name>SMITHSONIAN INSTITUTION</mro:producer_institution_name>
</mro:MRO_Parameters>
</Mission_Area>

<Discipline_Area>
<geom:Geometry>
<geom:Geometry_Orbiter>
<geom:geometry_start_time_utc>2006-12-06T02:08:22.929Z</geom:geometry_start_time_utc>
<geom:geometry_stop_time_utc>2006-12-06T02:09:48.723Z</geom:geometry_stop_time_utc>
<geom:Surface_Geometry>
<geom:Surface_Geometry_Start_Stop>
<geom:start_subspacecraft_latitude unit="deg">64.239113</geom:start_subspacecraft_latitude>
<geom:stop_subspacecraft_latitude unit="deg">59.864001</geom:stop_subspacecraft_latitude>
<geom:start_subspacecraft_longitude unit="deg">230.67676</geom:start_subspacecraft_longitude>
<geom:stop_subspacecraft_longitude unit="deg">229.39072</geom:stop_subspacecraft_longitude>
</geom:Surface_Geometry_Start_Stop>
</geom:Surface_Geometry>
</geom:Geometry_Orbiter>
</geom:Geometry>
</Discipline_Area>

</Context_Area>

<Reference_List>
<Internal_Reference>
<lid_reference>urn:nasa:pds:mro_sharad_us_radargrams:browse:s_00168901_tiff</lid_reference>
<reference_type>browse_to_browse</reference_type>
<comment>US RDR Browse Images (TIFF).</comment>
</Internal_Reference>
<Internal_Reference>
<lid_reference>urn:nasa:pds:mro_sharad_us_radargrams:data:s_00168901_geom</lid_reference>
<reference_type>browse_to_data</reference_type>
<comment>US RDR Geometry information.</comment>
</Internal_Reference>
<Internal_Reference>
<lid_reference>urn:nasa:pds:mro_sharad_us_radargrams:data:s_00168901_rgram</lid_reference>
<reference_type>browse_to_data</reference_type>
<comment>SHARAD US RDR.</comment>
</Internal_Reference>
<Internal_Reference>
<lid_reference>urn:nasa:pds:mro_sharad_us_radargrams:document:rgram_processing</lid_reference>
<reference_type>browse_to_document</reference_type>
</Internal_Reference>
<Internal_Reference>
<lid_reference>urn:nasa:pds:mro_sharad_us_radargrams:document:edr_to_usrdr</lid_reference>
<reference_type>browse_to_document</reference_type>
</Internal_Reference>
<Source_Product_External>
<external_source_product_identifier>MGS-M-MOLA-5-MEGDR-L3-V1.0:MEGA90N000EB.IMG</external_source_product_identifier>
<reference_type>data_to_derived_source_product</reference_type>
<curating_facility>PDS GEO</curating_facility>
<description>Model of the Martian areoid used to vertically datum this simulation.</description>
</Source_Product_External>
<Source_Product_External>
<external_source_product_identifier>MGS-M-MOLA-5-MEGDR-L3-V1.0</external_source_product_identifier>
<reference_type>data_to_derived_source_product</reference_type>
<doi>10.17189/1519460</doi>
<curating_facility>PDS GEO</curating_facility>
<description>MOLA Mission Experiment Gridded Data Records</description>
</Source_Product_External>
<Source_Product_External>
<external_source_product_identifier>MRO-M-SPICE-6-V1.0</external_source_product_identifier>
<reference_type>data_to_derived_source_product</reference_type>
<doi>10.17189/1520100</doi>
<curating_facility>PDS NAIF</curating_facility>
<description>Mars Reconnaissance Orbiter SPICE data archive</description>
</Source_Product_External>
</Reference_List>

<File_Area_Browse>
<File>
<file_name>s_00168901_thm.jpg</file_name>
<creation_date_time>2021-05-19T16:15:43.494Z</creation_date_time>
</File>
<Encoded_Image>
<name>Low resolution browse image</name>
<offset unit="byte">0</offset>
<encoding_standard_id>JPEG</encoding_standard_id>
</Encoded_Image>
</File_Area_Browse>

<File_Area_Browse>
<File>
<file_name>s_00168901_thm.lbl</file_name>
</File>
<Stream_Text>
<offset unit="byte">0</offset>
<parsing_standard_id>PDS3</parsing_standard_id>
<description>Original PDS3 label</description>
<record_delimiter>Carriage-Return Line-Feed</record_delimiter>
</Stream_Text>
</File_Area_Browse>

</Product_Browse>

0 comments on commit bc5a110

Please sign in to comment.