WSST::SchemaParserManager - SchemaParserManager class of WSST


WSST documentation Contained in the WSST distribution.

Index


Code Index:

NAME

Top

WSST::SchemaParserManager - SchemaParserManager class of WSST

DESCRIPTION

Top

SchemaParserManager is a "Singleton" class. This class manages schema parsers.

METHODS

Top

new

Constructor.

get_schema_parser

Returns schema parser object for the specified filepath.

instance

Returns "Singleton" instance.

init

Initialize this class.

SEE ALSO

Top

http://code.google.com/p/wsst/

AUTHORS

Top

Mitsuhisa Oshikawa <mitsuhisa [at] gmail.com> Yusuke Kawasaki <u-suke [at] kawa.net>

COPYRIGHT AND LICENSE

Top


WSST documentation Contained in the WSST distribution.

package WSST::SchemaParserManager;

use strict;
use File::Basename qw(fileparse);
use WSST::SchemaParser;
use WSST::SchemaParser::YAML;

our $VERSION = '0.1.1';

my $SINGLETON_INSTANCE = undef;

sub new {
    my $class = shift;
    
    my $self = {
        parsers => [],
        parser_type_map => {},
    };
    
    return bless($self, $class);
}

sub get_schema_parser {
    my $self = shift;
    my $path = shift;
    my ($fname, $dir, $ext) = fileparse($path, qr/\.[^.]*/);
    return $self->{parser_type_map}->{$ext}
        if $self->{parser_type_map}->{$ext};
    $ext =~ s/^\.//;
    $ext = uc($ext);
    my $cls = "WSST::SchemaParser::$ext";
    eval "require $cls;";
    if ($@) {
        die "parser not found: $path";
    }
    return $cls->new();
}

sub instance {
    my $class = shift;
    
    unless ($SINGLETON_INSTANCE) {
        $class->init();
    }
    
    return $SINGLETON_INSTANCE;
}

sub init {
    my $class = shift;
    
    my $self = $SINGLETON_INSTANCE = $class->new();

    foreach my $key (sort keys %WSST::SchemaParser::) {
        next if $key !~ /^(.+)::$/;
        my $cls = "WSST::SchemaParser::$1";
        my $obj = $cls->new();
        foreach my $type (@{$obj->types}) {
            $self->{parser_type_map}->{$type} = $obj;
        }
    }
}

1;