| 1 | ############################################################################### | ||
| 2 | # XML::Template::Element::Test | ||
| 3 | # | ||
| 4 | # Copyright (c) 2002-2003 Jonathan A. Waxman <jowaxman@law.upenn.edu> | ||
| 5 | # All rights reserved. | ||
| 6 | # | ||
| 7 | # This program is free software; you can redistribute it and/or | ||
| 8 | # modify it under the same terms as Perl itself. | ||
| 9 | ############################################################################### | ||
| 10 | package XML::Template::Element::Test; | ||
| 11 | |||
| 12 | # The base class for any element module is typically XML::Template::Element. | ||
| 13 | # If the element is a database element, the base class will typically be | ||
| 14 | # XML::Template::Element::DB. | ||
| 15 | # If the element needs to use iteration, add to the list of base classes | ||
| 16 | # XML::Template::Element::Iterator. | ||
| 17 | use base qw(XML::Template::Element); | ||
| 18 | |||
| 19 | use strict; | ||
| 20 | use XML::Template::Element; | ||
| 21 | |||
| 22 | |||
| 23 | =pod | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | XML::Template::Element::Test - A sample XML::Template plugin module for | ||
| 28 | the test namespace. | ||
| 29 | |||
| 30 | =head1 SYNOPSIS | ||
| 31 | |||
| 32 | This XML::Template plugin module implements the test namespace tagset. | ||
| 33 | It is provided as a sample plugin module to base real ones on. | ||
| 34 | |||
| 35 | =head1 CONSTRUCTORE | ||
| 36 | |||
| 37 | XML::Template::Element::Test inherits its constructor method, C<new>, from | ||
| 38 | L<XML::Template::Element>. | ||
| 39 | |||
| 40 | =head1 TEST TAGSET METHODS | ||
| 41 | |||
| 42 | =head2 test | ||
| 43 | |||
| 44 | This method implements the test tag. The following attributes are used: | ||
| 45 | |||
| 46 | =over 4 | ||
| 47 | |||
| 48 | =item attrib1 | ||
| 49 | |||
| 50 | Test attribute 1. | ||
| 51 | |||
| 52 | =item attrib2, attribs2 | ||
| 53 | |||
| 54 | Test attribute 2. | ||
| 55 | |||
| 56 | =back | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub test { | ||
| 61 | my $self = shift; | ||
| 62 | |||
| 63 | # If C<content> for this element is set in the configuration to | ||
| 64 | # C<xml>, the first argument will contain this element's content | ||
| 65 | # translated into Perl code. If C<content> is set to C<text>, the | ||
| 66 | # first argument will contain the unparsed content of this element. | ||
| 67 | my $code = shift; | ||
| 68 | # my $text = shift; | ||
| 69 | |||
| 70 | # The second argument is a reference to a hash containing attribute | ||
| 71 | # name/value pairs. If C<parse> for an attribbute is set to C<true>, | ||
| 72 | # the attribute value will have been parsed with the standard string | ||
| 73 | # parrser or by the parser given by C<parser> for the attribute. If | ||
| 74 | # C<parse> is set to C<false>, the values will be a string containing | ||
| 75 | # the unparsed attribute value. | ||
| 76 | my $attribs = shift; | ||
| 77 | |||
| 78 | # Get the attribute values using the C<get_attrib> method. The first | ||
| 79 | # parameter is the reference to the attributes hash. The second | ||
| 80 | # parameter is the name(s) of the attribute to get. This parameter can | ||
| 81 | # be a scalar or an anaonymous list containing the various allowable | ||
| 82 | # names of the attribute. C<get_attrib> will first look for the | ||
| 83 | # attributes associated with this element's namespace, then for | ||
| 84 | # attributes not associated with any attribute. Note that C<get_attrib> | ||
| 85 | # will remove the attribute from the attribute hash if it finds it. To | ||
| 86 | # prevent this, pass C<0> as the last argument. If the attribute does | ||
| 87 | # not exist, undef is returned. You should always test for this and | ||
| 88 | # proivide a default value. | ||
| 89 | my $attrib1 = $self->get_attrib ($attribs, 'attrib1') || 'undef'; | ||
| 90 | my $attrib2 = $self->get_attrib ($attribs, ['attrib2', 'attribs2']) | ||
| 91 | || "'default'"; | ||
| 92 | |||
| 93 | # The method C<generate_named_params> will generate a comma-separated | ||
| 94 | # list of named params for the attribute names and values. | ||
| 95 | my $attribs_named_params = $self->generate_named_params ($attribs); | ||
| 96 | |||
| 97 | # Get the element's namespace | ||
| 98 | my $namespace = $self->namespace (); | ||
| 99 | |||
| 100 | # Create the code for this element. | ||
| 101 | my $outcode = qq{ | ||
| 102 | # The code for the element should generally be enclosed in a do block to | ||
| 103 | # give a context for local variables. | ||
| 104 | do { | ||
| 105 | # Create a new variable context for any XML::Template variables that | ||
| 106 | # get created in this element's code. | ||
| 107 | \$vars->create_context (); | ||
| 108 | |||
| 109 | # Create a hash containing attribute name/value pairs. | ||
| 110 | # This is necessary for checking attribute value types and may be | ||
| 111 | # necessary for other purposes in the element code. | ||
| 112 | my \%attribs = ($attribs_named_params); | ||
| 113 | |||
| 114 | # Check the attribute value types against the types given in the | ||
| 115 | # configuration. | ||
| 116 | # \$process->{_parser}->check_attrib_types ('$namespace', 'test', \\\%attribs); | ||
| 117 | |||
| 118 | # If code was generated for the element's content, insert it. | ||
| 119 | $code | ||
| 120 | |||
| 121 | # Delete the variable context. | ||
| 122 | \$vars->delete_context (); | ||
| 123 | }; | ||
| 124 | }; | ||
| 125 | |||
| 126 | # Return the element's code. | ||
| 127 | return $outcode; | ||
| 128 | } | ||
| 129 | |||
| 130 | =pod | ||
| 131 | |||
| 132 | =head1 AUTHOR | ||
| 133 | |||
| 134 | Jonathan Waxman | ||
| 135 | <jowaxman@bbl.med.upenn.edu> | ||
| 136 | |||
| 137 | =head1 COPYRIGHT | ||
| 138 | |||
| 139 | Copyright (c) 2002-2003 Jonathan A. Waxman | ||
| 140 | All rights reserved. | ||
| 141 | |||
| 142 | This program is free software; you can redistribute it and/or | ||
| 143 | modify it under the same terms as Perl itself. | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | |||
| 148 | 1; |