has
'_data_dir'
=> (
isa
=>
'Str'
,
is
=>
'rw'
);
has
'_rng'
=> (
isa
=>
'XML::LibXML::RelaxNG'
,
is
=>
'rw'
);
has
'_xml_parser'
=> (
isa
=>
"XML::LibXML"
,
is
=>
'rw'
);
has
'_stylesheet'
=> (
isa
=>
"XML::LibXSLT::StylesheetWrapper"
,
is
=>
'rw'
);
our
$VERSION
=
'0.1.2'
;
sub
_init
{
my
(
$self
,
$args
) =
@_
;
my
$data_dir
=
$args
->{
'data_dir'
} ||
XML::Grammar::Fiction::ConfigData->config(
'extradata_install_path'
)->[0];
$self
->_data_dir(
$data_dir
);
my
$rngschema
=
XML::LibXML::RelaxNG->new(
location
=>
File::Spec->catfile(
$self
->_data_dir(),
"screenplay-xml.rng"
),
);
$self
->_rng(
$rngschema
);
$self
->_xml_parser(XML::LibXML->new());
my
$xslt
= XML::LibXSLT->new();
my
$style_doc
=
$self
->_xml_parser()->parse_file(
File::Spec->catfile(
$self
->_data_dir(),
"screenplay-xml-to-html.xslt"
),
);
$self
->_stylesheet(
$xslt
->parse_stylesheet(
$style_doc
));
return
0;
}
sub
translate_to_html
{
my
(
$self
,
$args
) =
@_
;
my
$source_dom
=
$self
->_xml_parser()->parse_file(
$args
->{source}->{file})
;
my
$ret_code
;
eval
{
$ret_code
=
$self
->_rng()->validate(
$source_dom
);
};
if
(
defined
(
$ret_code
) && (
$ret_code
== 0))
{
}
else
{
confess
"RelaxNG validation failed [\$ret_code == $ret_code ; $@]"
;
}
my
$stylesheet
=
$self
->_stylesheet();
my
$results
=
$stylesheet
->transform(
$source_dom
);
my
$medium
=
$args
->{output};
if
(
$medium
eq
"string"
)
{
return
$stylesheet
->output_string(
$results
);
}
elsif
(
$medium
eq
"xml"
)
{
return
$results
;
}
else
{
confess
"Unknown medium"
;
}
}
1;