use std::env;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use xrust::item::{Item, Node, SequenceTrait};
use xrust::parser::ParseError;
use xrust::parser::xml::parse as xmlparse;
use xrust::parser::xpath::parse;
use xrust::transform::context::{ContextBuilder, StaticContextBuilder};
use xrust::trees::smite::RNode;
use xrust::xdmerror::{Error, ErrorKind};
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
println!("Usage: {} xpath xml", args[0]);
return;
}
let exprpath = Path::new(&args[1]);
let mut exprfile = match File::open(&exprpath) {
Err(why) => {
panic!(
"unable to open XPath expression file \"{}\" due to \"{}\"",
&args[1], why
)
}
Ok(f) => f,
};
let mut expr = String::new();
match exprfile.read_to_string(&mut expr) {
Err(why) => panic!("unable to read from \"{}\" due to \"{}\"", &args[1], why),
Ok(_) => {}
};
let xpath = parse::<RNode>(expr.trim(), None, None).expect("XPath expression not recognised");
let srcpath = Path::new(&args[2]);
let mut srcfile = match File::open(&srcpath) {
Err(why) => {
panic!(
"unable to open source document \"{}\" due to \"{}\"",
&args[2], why
)
}
Ok(f) => f,
};
let mut srcxml = String::new();
match srcfile.read_to_string(&mut srcxml) {
Err(why) => panic!("unable to read from \"{}\" due to \"{}\"", &args[2], why),
Ok(_) => {}
};
let root = RNode::new_document();
xmlparse(
root.clone(),
srcxml.as_str(),
Some(|_: &_| Err(ParseError::MissingNameSpace)),
)
.expect("unable to parse XML");
let context = ContextBuilder::new()
.context(vec![Item::Node(root)])
.build();
let mut stctxt = StaticContextBuilder::new()
.message(|_| Ok(()))
.fetcher(|_| Err(Error::new(ErrorKind::NotImplemented, "not implemented")))
.parser(|_| Err(Error::new(ErrorKind::NotImplemented, "not implemented")))
.build();
let result = context
.dispatch(&mut stctxt, &xpath)
.expect("failed to evaluate XPath");
println!("{}", result.to_xml());
}