Ambrosia

 view release on metacpan or  search on metacpan

share/Managers/buildApp.pm  view on Meta::CPAN

{
    my $self = shift;
    $self->SUPER::_init(@_);

    $self->parser = XML::LibXML->new();
    $self->xslt = XML::LibXSLT->new();

    my $source = $self->source = $self->parser->parse_file(Context->param('data'));

    $self->application = ($source->getElementsByTagName('Application'))[0]->cloneNode(0);

    $self->application->addChild(($source->getElementsByTagName('Config'))[0]->cloneNode(1));
    $self->application->addChild(($source->getElementsByTagName('DataSource'))[0]->cloneNode(1));
    $self->application->addChild(($source->getElementsByTagName('Relations'))[0]->cloneNode(1));
    $self->application->addChild(($source->getElementsByTagName('MenuGroups'))[0]->cloneNode(1));
}

our $encoding;

sub prepare
{
    my $self = shift;

    return unless $self->validateDocument();

    Context->repository->set(quiet => 1);
    my $appName = config->ID;

    my %hDir = $self->makeDirectorys($appName);

    $encoding = config->Charset || 'utf-8';
    ############################################################################

    my $tmplPath = (config->TemplatePath || '.');
    $self->makeEntityCode($hDir{dirEntity},     $tmplPath . '/Templates/Common/Entity.xsl',      '.pm');
    $self->makeEntityCode($hDir{dirValidators}, $tmplPath . '/Templates/Common/Validator.xsl',   'Validator.pm');

    $self->makeManagerCode($hDir{dirManagers},   $tmplPath . '/Templates/Common/SaveManager.xsl', 'SaveManager.pm', 0, '@Type!="BIND" and @Type!="VIEW"');
    $self->makeManagerCode($hDir{dirManagers},   $tmplPath . '/Templates/Common/EditManager.xsl', 'EditManager.pm', 0, '@Type!="BIND" and @Type!="VIEW"');
    $self->makeManagerCode($hDir{dirManagers},   $tmplPath . '/Templates/Common/GetTreeManager.xsl', 'EditManager.pm', 1, '@Type!="BIND"');

    $self->makeManagerCode($hDir{dirManagers},   $tmplPath . '/Templates/Common/ListManager.xsl', 'ListManager.pm', 0, '@Type!="BIND"');
    $self->makeManagerCode($hDir{dirManagers},   $tmplPath . '/Templates/Common/TreeManager.xsl', 'TreeManager.pm', 1, '@Type!="BIND"');

    my $tpl_path = '';
    my $ex = '';
    if ( config->TemplateStyle->{htmltemplate} eq 'xslt' )
    {
        $tpl_path = 'XSLT';
        $ex = '.xsl';
    }
    elsif( config->TemplateStyle->{htmltemplate} eq 'tt' )
    {
        $tpl_path = 'TOOLKIT';
        $ex = '.ttkt.html';
    }
    if (  config->TemplateStyle->{jsframework} eq 'dojo' )
    {
        $tpl_path .= '+DOJO';
    }
    $self->makeEntityCode($hDir{dirTemplates}, $tmplPath . '/Templates/Templates/' . $tpl_path . '/edit_json.xsl', '_edit_json' . $ex, 1, '@Type!="BIND" and @Type!="VIEW"');
    copy($tmplPath . '/Templates/Templates/' . $tpl_path . '/message.xsl',
         $hDir{dirTemplates} . '/_message.xsl') or die "Copy failed: $! ['${tmplPath}/Templates/Templates/${tpl_path}/message.xsl']";

    copy($tmplPath . '/Templates/incUtils.xsl',
         $hDir{dirTemplates} . '/_inc_utils.xsl') or die "Copy failed: $! ['${tmplPath}/Templates/incUtils.xsl']";

    {#make main.xsl
        my $style_doc = $self->parser->parse_file($tmplPath . '/Templates/Templates/' . $tpl_path . '/main.xsl');
        ($style_doc->getElementsByLocalName('output'))[0]->setAttribute( encoding => $encoding );
        my $stylesheet = $self->xslt->parse_stylesheet($style_doc);
        my $results = $stylesheet->transform($self->source);
        my $fn = $hDir{dirTemplates} . '/main.xsl';
        if ( -e $fn )
        {
            rename $fn, $fn . '.bak';
        }
        #if ( open my $fh, ">:encoding($encoding)", $fn)
        if ( open my $fh, ">", $fn)
        {
            my $txt = $stylesheet->output_string($results);
            print $fh $txt;
            close $fh;
        }
    }
    {#make list.xsl
        my $style_doc = $self->parser->parse_file($tmplPath . '/Templates/Templates/' . $tpl_path . '/list_json.xsl');
        ($style_doc->getElementsByLocalName('output'))[0]->setAttribute( encoding => $encoding );
        my $stylesheet = $self->xslt->parse_stylesheet($style_doc);
        my $results = $stylesheet->transform($self->source);
        my $fn = $hDir{dirTemplates} . '/list_json.xsl';
        if ( -e $fn )
        {
            rename $fn, $fn . '.bak';
        }
        #if ( open my $fh, ">:encoding($encoding)", $fn)
        if ( open my $fh, ">", $fn)
        {
            my $txt = $stylesheet->output_string($results);
            print $fh $txt;
            close $fh;
        }
    }

    {
        if ( $self->application->getAttribute('Authorization') ne 'NO' )
        {#make authorize.xsl
            my $style_doc = $self->parser->parse_file($tmplPath . '/Templates/Templates/' . $tpl_path . '/authorize.xsl');
            ($style_doc->getElementsByLocalName('output'))[0]->setAttribute( encoding => $encoding );
            my $stylesheet = $self->xslt->parse_stylesheet($style_doc);
            my $results = $stylesheet->transform($self->source);
            my $fn = $hDir{dirTemplates} . '/authorize.xsl';
            if ( -e $fn )
            {
                rename $fn, $fn . '.bak';
            }
            #if ( open my $fh, ">:encoding($encoding)", $fn)
            if ( open my $fh, ">", $fn)
            {
                my $txt = $stylesheet->output_string($results);
                print $fh $txt;



( run in 1.395 second using v1.01-cache-2.11-cpan-97f6503c9c8 )