|
| 1 | +//! # XML2RDF Converter |
| 2 | +//! |
| 3 | +//! This is a Rust-based tool that converts XML data into RDF format. It uses the `xml-rs` crate |
| 4 | +//! for XML parsing and the `oxrdf` crate to construct RDF triples and graphs. |
| 5 | +//! |
| 6 | +//! ## Features |
| 7 | +//! - Parses XML input and converts it to RDF triples |
| 8 | +//! - Supports specifying a custom namespace for generated RDF nodes |
| 9 | +//! - Outputs RDF data to a specified file, oxrdf::Graph or stdout |
| 10 | +//! |
| 11 | +//! ## Usage |
| 12 | +//! Run the XML2RDF converter from the command line. For detailed usage information, run: |
| 13 | +//! ``` |
| 14 | +//! xml2rdf --help |
| 15 | +//! ``` |
| 16 | +//! |
| 17 | +//! ## Example |
| 18 | +//! To convert a XML file to RDF format with a specified namespace and output file: |
| 19 | +//! ``` |
| 20 | +//! xml2rdf convert --namespace http://example.com/ns# --xml data.xml --output-file output.nt |
| 21 | +//! ``` |
| 22 | +//! This will take `data.xml`, apply the specified namespace, and save the RDF output in `output.nt`. |
| 23 | +
|
| 24 | +use clap::{Parser, Subcommand}; |
| 25 | +use csv2rdf::*; |
| 26 | + |
| 27 | +/// Command-line interface for XML2RDF Converter |
| 28 | +/// |
| 29 | +/// This struct defines the command-line interface (CLI) for interacting with the XML2RDF converter. |
| 30 | +#[derive(Parser)] |
| 31 | +#[command(version, about = "Converts XML data into RDF format.")] |
| 32 | +struct Cli { |
| 33 | + /// CLI command selection |
| 34 | + #[command(subcommand)] |
| 35 | + command: Option<Commands>, |
| 36 | +} |
| 37 | + |
| 38 | +/// Supported Commands |
| 39 | +/// |
| 40 | +/// Contains the available commands for the XML2RDF converter. |
| 41 | +#[derive(Subcommand)] |
| 42 | +enum Commands { |
| 43 | + /// Convert XML to RDF format. |
| 44 | + /// |
| 45 | + /// The `convert` command parses a XML file, converts it to RDF triples using `xml-rs` for parsing |
| 46 | + /// and `oxrdf` to construct the graph, and saves the output. |
| 47 | + Convert { |
| 48 | + /// Namespace for RDF graph generation. |
| 49 | + /// |
| 50 | + /// A custom namespace to prefix RDF resources created from XML keys and values. |
| 51 | + #[arg(short, long, default_value = "https://decisym.ai/csv2rdf/data")] |
| 52 | + namespace: String, |
| 53 | + |
| 54 | + /// Path to input XML file(s). |
| 55 | + /// |
| 56 | + /// Provide the path to one or more XML files that will be parsed and converted. |
| 57 | + #[arg(short, long, num_args = 1..)] |
| 58 | + input: Vec<String>, |
| 59 | + |
| 60 | + /// Path to output file. |
| 61 | + /// |
| 62 | + /// Optional: Specify the path to save the generated RDF data. If not provided, data will be written |
| 63 | + /// to stdout |
| 64 | + #[arg(short, long)] |
| 65 | + output: Option<String>, |
| 66 | + }, |
| 67 | +} |
| 68 | + |
| 69 | +fn main() { |
| 70 | + let cli = Cli::parse(); |
| 71 | + |
| 72 | + match &cli.command { |
| 73 | + Some(Commands::Convert { |
| 74 | + namespace, |
| 75 | + input, |
| 76 | + output, |
| 77 | + }) => { |
| 78 | + let mut w: Box<dyn writer::RdfWriter> = if let Some(file) = output { |
| 79 | + match writer::FileWriter::to_file(file.clone()) { |
| 80 | + Err(e) => { |
| 81 | + eprintln!("Error opening file for writing: {e}"); |
| 82 | + return; |
| 83 | + } |
| 84 | + Ok(v) => Box::new(v), |
| 85 | + } |
| 86 | + } else { |
| 87 | + Box::new(writer::FileWriter::to_stdout()) |
| 88 | + }; |
| 89 | + |
| 90 | + match convert::parse_csv(input.clone(), w.as_mut(), namespace) { |
| 91 | + Ok(_) => {} |
| 92 | + Err(e) => eprintln!("Error writing: {}", e), |
| 93 | + } |
| 94 | + } |
| 95 | + None => {} |
| 96 | + } |
| 97 | +} |
0 commit comments