View on
MetaCPAN
Moritz Sinn > XML-ParseDTD-0.1.4 > XML::ParseDTD

Download:
XML-ParseDTD-0.1.4.tar.gz

Dependencies

Annotate this POD (1)

CPAN RT

New  2
Open  0
View/Report Bugs
Module Version: 0.1.4   Source  

NAME ^

XML::ParseDTD - parses a XML DTD and provides methods to access the information stored in the DTD.

DEPENDENCIES ^

Perl Version

        5.004

Standard Modules

        Carp 1.01

Nonstandard Modules

        LWP::UserAgent 0.01
        Cache::Cache 1.02

SYNOPSIS ^

    use XML::ParseDTD;
    $dtd = XML::ParseDTD->new($dtd);
    $bool = $dtd->child_allowed($tag, $childtag);
    $bool = $dtd->child_list_allowed($tag, @childtags);
    $bool = $dtd->attr_allowed($tag, $attribute);
    $bool = $dtd->attr_list_allowed($tag, @attributes);
    $bool = $dtd->is_empty($tag);
    $bool = $dtd->is_defined($tag);
    $bool = $dtd->is_fixed($tag, $attribute);
    $bool = $dtd->attr_value_allowed($tag, $attribute, $value);
    $bool = $dtd->attr_list_value_allowed($tag, \%attribute_value);
    @tags = $dtd->get_document_tags();
    $regexp = $dtd->get_child_regexp($tag);
    @attributes = $dtd->get_attributes($tag);
    @req_attributes = $dtd->get_req_attributes($tag);
    $value = $dtd->get_allowed_attr_values($tag, $attribute);
    $default_value = $dtd->get_attr_def_value($tag, $attribute);
    $dtd->clear_cache();
    $errormessage = $dtd->errstr;
    $errornumber = $dtd->err;

DESCRIPTION ^

ParseDTD.pm is a Perl 5 object class which provides methods to access the information stored in a XML DTD.

This module basically tells you which tags are known by the dtd, which child tags a certain tag might have, which tags are defined as a empty tag, which attributes a certain tag might have, which values are allowed for a certain attribute, which attributes are required, which attributes are fixed, which attributes have which default value ... well i would say it tells you all except the entity definitions (they're on the ToDo list) that is defined in the dtd (at least all that i know of, but i'm not so much into that topic, so please make me aware if i missed something). All this information can be accessed in 2 diffrent ways: 1. you can simply get it 2. you can pass certain data and the module then tells you whether thats ok or not.

This package uses Cache::SharedMemoryCache to cache every parsed DTD, so next time the data structure representing the dtd can be just taken out of memory. Thus the dtd is not refetched and not parsed again which saves quite some time and work. You can easily modify the module so that is uses Cache::FileCache if you prefer, but i think SharedMemory is faster.

Everytime the constructor is called it first checks whether the given dtd is already in memory, if so it compares the last modified date to the date stored in memory and then decides whether it should refetch it or not. If the dtd lays on the local filesystem this operation doesn't produce any reasonable overhead, but if the dtd is fetched out of the internet it might make sense to not check the last modified header every time. You can configure how often it should be checked, by default it is checked averaged every third time. But since most dtds don't change it is mostly save to not check it at all.

Internally the parsed DTD data is simply stored in 6 hash structures. Because of this and because of the caching the module should be very fast.

USING XML::ParseDTD ^

The Constructor

new ($dtd_url, [ %conf ])

This method is the constructor. The first argument must be the path to a xml dtd, it should be a valid URL using the file or http protocol. Here are some examples:

The configuration hash can be used to influence the modules behaviour. The options known are:

Note: You shouldn't set any option to 0 since it will not be interpreted, that means the default setting will be used instead.

Check Methods

child_allowed ($tag, $childtag)

Checks whether the given tag can contain the given childtag.

Returns 1 (true) or 0 (false).

child_list_allowed ($tag, @childtags)

Checks whether its ok if the given tag contains the given childtags in the given order. This means that the method will return ails if a certain tag is not allowed, a required tag is not given or the order is not allowed.

Returns 1 (true) or 0 (false).

attr_allowed ($tag, $attribute)

Checks whether the given attribute is allowed for the given tag.

Returns 1 (true) or 0 (false).

attr_list_allowed ($tag, @attributes)

Checks whether its ok if the given tag has set given attributes. This means that the method will return fails if a certain attribute is not allowed or a required attribute is not given.

Returns 1 (true) or 0 (false).

is_empty ($tag)

Checks whether the given tag is a empty tag, that means whether it can't contain any elements or data.

Returns 1 (true) or 0 (false).

is_any ($tag)

Checks whether the given tag has content model ANY.

Returns 1 (true) or 0 (false).

is_defined ($tag)

Checks whether the given tag is defined in the dtd, that means whether it is allowed in the document.

Returns 1 (true) or 0 (false).

is_fixed ($tag, $attribute)

Checks whether the given attribute for the given tag is a fixed attribute, that means if its value is predefined by the dtd.

If so, you can use get_allowed_attr_values to get the predefined value.

Returns 1 (true) or 0 (false)

attr_value_allowed ($tag, $attribute, $value)

Checks whether the given attribute for the given tag might be set to the given value.

Returns 1 (true) or 0 (false).

attr_list_value_allowed ($tag, \%attribute_value)

Calls attr_list_allowed for the attribute names, if everything is fine it calls attr_value_allowed for each value.

Returns 1 (true) or 0 (false).

Get Methods

get_document_tags

Returns a list of all tags which are defined in the dtd, that means which are allowed in the document.

get_child_regexp ($tag)

Returns the regular expression, which defines which combinations of child elements are valid for the given tag, as a string.

get_attributes ($tag)

Returns a list of all attributes which are allowed for the given tag.

get_req_attributes ($tag)

Returns a list of all required attributes for the given tag.

get_allowed_attr_values ($tag,$attribute)

Returns the allowed values for the given attribute for the given tag.

If only one certain string is allowed to be set as value, this string is returned. If the value must be one string out of a list of strings, a reference to this list is returned. If the value must be of a certain datatype such as PCDATA, ID or NMTOKEN, a reference to a hash with only one element is returned. The key is the name of the datatype and the value is a regular expression string which describes the datatype.

undef is returned if nothing is defined as attribute value, that normally means that the attribute is not known for the given tag, but you can call errstr to get more information.

get_attr_def_value ($tag,$attribute)

Returns the default value defined for the given attribute of the given tag. In most cases no default value is defined, that means that undef is returned. But undef is also returned if the tag does not exist or if the attribute is not allowed for the given tag. To get more information why undef was returned, you should call errstr.

Other Methods

clear_cache ()

Clears the cache, that means that the dtd will be refetched and reparsed next time.

errstr ()

Returns the message of the last occured error.

err ()

Returns the number of the last occured error.

BUGS ^

Send bug reports to: bug-XML-ParseDTD@rt.cpan.org (if that doesn't work feel free to send directly to moritz@freesources.org). Or use the webinterface at http://rt.cpan.org/NoAuth/Bugs.html?Dist=XML-ParseDTD.

Thanks!

AUTHOR ^

(c) 2003, Moritz Sinn. This module is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License (see http://www.gnu.org/licenses/gpl.txt) as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

    This module is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

I am always interested in knowing how my work helps others, so if you put this module to use in any of your own code then please send me the URL. If you make modifications to the module because it doesn't work the way you need, please send me a copy so that I can roll desirable changes into the main release.

Address comments, suggestions, and bug reports to moritz@freesources.org.

syntax highlighting: