root/lang/perl/Text-Nyarlax/trunk/lib/Text/Nyarlax/Element.pm @ 730

Revision 730, 4.1 kB (checked in by nyarla, 6 years ago)

lang/perl/Text-Nyarlax: I divided Text::Nyarlax::Util into Text::Nyarlax::ElementUtil? and Text::Nyarlax::ParserUtil?.

Line 
1package Text::Nyarlax::Element;
2
3use strict;
4use warnings;
5
6our $VERSION = '0.1';
7
8use Carp;
9use Text::Nyarlax::ElementUtil qw( join_node );
10use base qw( Class::Accessor::Fast Exporter );
11
12__PACKAGE__->mk_ro_accessors( qw( name attribute ) );
13{
14    no warnings 'uninitialized';
15    *attr = __PACKAGE__->can('attribute');
16}
17
18our @EXPORT_OK = qw( element );
19
20sub new {
21    my ( $class, %args ) = @_;
22
23    my $name = $args{'name'}
24        or croak "Element name is not specified.";
25
26    my $attr = $args{'attribute'} || $args{'attr'} || {};
27    croak "Attribute is not HASH reference." if ( ref $attr ne 'HASH' );
28
29    my $self = bless {
30        name        => $name,
31        attribute   => $attr,
32        content     => [],
33    }, $class;
34
35    my $content = $args{'content'} || [];
36       $content = [ $content ] if ( ref $content ne 'ARRAY' );
37
38    $self->content( $content );
39
40    return $self;
41}
42
43sub parent {
44    my $self = shift;
45    if ( @_ ) {
46        my $parent = shift @_;
47        croak "Argument is not " . __PACKAGE__ . " object."
48            if ( ref $parent ne __PACKAGE__ );
49        $self->{'parent'} = $parent;
50    }
51    else {
52        $self->{'parent'};
53    }
54}
55
56sub content {
57    my $self = shift;
58    if ( @_ ) {
59        my $contents = shift @_;
60           $contents = [ $contents ] if ( ref $contents ne 'ARRAY' );
61        for my $content ( @{ $contents } ) {
62            $content->parent( $self ) if ( ref $content eq __PACKAGE__ );
63        }
64        $self->{'content'} = $contents;
65    }
66    else {
67        $self->{'content'};
68    }
69}
70
71sub shift_content {
72    my ( $self ) = @_;
73    shift @{ $self->{'content'} };
74}
75
76sub unshift_content {
77    my ( $self, @contents ) = @_;
78    for my $content ( @contents ) {
79        $content->parent( $self ) if ( ref $content eq __PACKAGE__ );
80    }
81    unshift @{ $self->{'content'} }, @contents;
82}
83
84sub pop_content {
85    my ( $self ) = @_;
86    pop @{ $self->{'content'} };
87}
88
89sub push_content {
90    my ( $self, @contents ) = @_;
91    for my $content ( @contents ) {
92        $content->parent( $self ) if ( ref $content eq __PACKAGE__ );
93    }
94    push @{ $self->{'content'} }, @contents;
95}
96
97sub join_content {
98    my ( $self, %args ) = @_;
99    $self->content( join_node( $self->content, %args ) );
100}
101
102sub element {
103    my $name = shift;
104    __PACKAGE__->new( name => $name );
105}
106
1071;
108__END__
109
110=head1 NAME
111
112Text::Nyarlax::Element - Text::Nyarlax element object
113
114=head1 SYNOPSIS
115
116    use Text::Nyarlax::Element;
117   
118    my $elm = Text::Nyarlax::Element->new(
119        name => 'Section',
120        attr => {
121            id => 'MyID',
122            class => 'MyClass',
123        },
124        content => [
125            'Foo',
126            'Bar',
127            'Baz'
128        ],
129    );
130
131=head1 DESCRIPTION
132
133Text::Nyarlax::Element is the object to compose the element tree.
134The element tree is generated by the parser of Text::Nyarlax.
135
136=head1 METHODS
137
138=head2 new
139
140The argument is three of name, attribute(attr) and content, and name is necessary.
141
142attribute has to be Hash and content has to be one character string,Text::Nyarlax::Element object
143or ARRAY which consists of a character string and Text::Nyarlax::element object.
144
145=head2 name
146
147get element name. readonly.
148
149=head2 attribute/attr
150
151get element attributes. return HASH reference.
152
153=head2 parent
154
155set/get the Text::Nyarlax::Element object which becomes a parent.
156
157=head2 content
158
159set/get the contents of Text::Nyarlax::Element object.
160
161When setting the contents, it's necessary to use ARRAY reference,
162and when getting the contents, ARRAY reference is returning.
163
164=head2 shift_content
165
166The contents are added at the first of obejct contents.
167
168=head2 unshift_content
169
170The first contents is taken out.
171
172=head2 push_content
173
174The contents are added at the last of obejct contents.
175
176=head2 pop_content
177
178The last contents is taken out.
179
180Naoki Okamura E<lt>thotep@nayrla.netE<gt>
181
182=head1 LICENSE
183
184This library is free software; you can redistribute it and/or modify
185it under the same terms as Perl itself.
186
187=head1 SEE ALSO
188
189L<Text::Nyarlax::Element::Content>
190
191=cut;
Note: See TracBrowser for help on using the browser.