Skip to content

Commit

Permalink
Use the file name as node ID
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaskrause committed Aug 6, 2024
1 parent bedf582 commit f817efd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
28 changes: 19 additions & 9 deletions src/importer/saltxml/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,23 +223,21 @@ impl<'a, 'input> DocumentMapper<'a, 'input> {
.iter()
.filter(|n| SaltType::from_node(n) == SaltType::MediaDs)
{
let element_id = get_element_id(media_node)
let orig_element_id = get_element_id(media_node)
.context("Missing element ID for media/audio data source")?;

if let Some(SaltObject::Url(anno_value)) =
get_feature_by_qname(media_node, "salt", "SAUDIO_REFERENCE")
{
updates.add_event(UpdateEvent::AddNode {
node_name: element_id.clone(),
node_type: "file".to_string(),
})?;
// Parse the file URL with the input file location as base path
let base_dir = Url::from_directory_path(self.input_directory.canonicalize()?).ok();
let referenced_url = Url::options()
.base_url(base_dir.as_ref())
.parse(&anno_value)?;

let file_path = if referenced_url.scheme() == "file" {
let mut element_id = orig_element_id;
let mut file_path = referenced_url.to_string();
if referenced_url.scheme() == "file" {
// Resolve this file URL against the input direcotry and
// store it relative to the current working directory.
let referenced_path = Path::new(referenced_url.path());
Expand All @@ -248,10 +246,22 @@ impl<'a, 'input> DocumentMapper<'a, 'input> {
&std::env::current_dir()?,
)
.unwrap_or_else(|| referenced_path.to_path_buf());
referenced_path.to_string_lossy().to_string()
} else {
referenced_url.to_string()

file_path = referenced_path.to_string_lossy().to_string();
// Use the file name as element ID
if let Some(file_name) = referenced_path.file_name() {
element_id = format!(
"{}/{}",
self.document_node_name,
file_name.to_string_lossy()
);
}
};
updates.add_event(UpdateEvent::AddNode {
node_name: element_id.clone(),
node_type: "file".to_string(),
})?;

self.media_files
.insert(element_id.clone(), file_path.clone());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ expression: actual
<node id="dialog.demo/dialog.demo#phon0">
<data key="k2">datasource</data>
</node>
<node id="dialog.demo/dialog.demo#audio1">
<node id="dialog.demo/dialog.demo/dialog.demo.webm">
<data key="k1">tests/data/import/salt/dialog.demo/dialog.demo/dialog.demo.webm</data>
<data key="k2">file</data>
</node>
Expand Down Expand Up @@ -2367,7 +2367,7 @@ expression: actual
</edge>
<edge id="e482" source="dialog.demo/dialog.demo#phon0" target="dialog.demo/dialog.demo" label="PartOf/annis/">
</edge>
<edge id="e483" source="dialog.demo/dialog.demo#audio1" target="dialog.demo/dialog.demo" label="PartOf/annis/">
<edge id="e483" source="dialog.demo/dialog.demo/dialog.demo.webm" target="dialog.demo/dialog.demo" label="PartOf/annis/">
</edge>
<edge id="e484" source="dialog.demo/dialog.demo#sTok1" target="dialog.demo/dialog.demo" label="PartOf/annis/">
</edge>
Expand Down

0 comments on commit f817efd

Please sign in to comment.