| 1 | package Rinchi::DOM; | ||
| 2 | |||
| 3 | 1 | ▶ | use 5.008001; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use XML::DOM; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | |||
| 8 | require Exporter; | ||
| 9 | |||
| 10 | our @ISA = qw(Exporter); | ||
| 11 | |||
| 12 | # Items to export into callers namespace by default. Note: do not export | ||
| 13 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 14 | # Do not simply export all your public functions/methods/constants. | ||
| 15 | |||
| 16 | # This allows declaration use Rinchi::DOM ':all'; | ||
| 17 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||
| 18 | # will save memory. | ||
| 19 | our %EXPORT_TAGS = ( 'all' => [ qw( | ||
| 20 | |||
| 21 | ) ] ); | ||
| 22 | |||
| 23 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 24 | |||
| 25 | our @EXPORT = qw( | ||
| 26 | |||
| 27 | ); | ||
| 28 | |||
| 29 | our $VERSION = '0.01'; | ||
| 30 | |||
| 31 | =head1 NAME | ||
| 32 | |||
| 33 | Rinchi::DOM - DOM Interface. | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | use Rinchi::DOM; | ||
| 38 | use Rinchi::Fortran::Preprocessor; | ||
| 39 | |||
| 40 | my @args = ( | ||
| 41 | 'test.pl', | ||
| 42 | '-I/usr/include', | ||
| 43 | '-Uccc', | ||
| 44 | ); | ||
| 45 | |||
| 46 | my $closed = 0; | ||
| 47 | |||
| 48 | my $rd = new Rinchi::DOM; | ||
| 49 | my $fpp = new Rinchi::Fortran::Preprocessor; | ||
| 50 | |||
| 51 | my $document = $rd->process_to_DOM($fpp, 'test_src/bisect.f90',\@args); | ||
| 52 | |||
| 53 | |||
| 54 | =head1 DESCRIPTION | ||
| 55 | |||
| 56 | This module provides XML::DOM suuport for Rinchi preprocessors. | ||
| 57 | |||
| 58 | =head2 EXPORT | ||
| 59 | |||
| 60 | None by default. | ||
| 61 | |||
| 62 | =head1 METHODS | ||
| 63 | |||
| 64 | =over 4 | ||
| 65 | |||
| 66 | =item new | ||
| 67 | |||
| 68 | Constructor for Rinchi::DOM. | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | use vars qw( $_RD_document | ||
| 73 | $_RD_cur_node | ||
| 74 | $_RD_cdata_value | ||
| 75 | $_RD_in_cdata | ||
| 76 | ); | ||
| 77 | |||
| 78 | =item new | ||
| 79 | |||
| 80 | Constructor for Rinchi::DOM. | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | sub new { | ||
| 85 | my ($class, %args) = @_; | ||
| 86 | my $self = bless \%args, $class; | ||
| 87 | |||
| 88 | $self; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub _startElementHandler() { | ||
| 92 | my ($tagName, $hasChild, %attrs) = @_; | ||
| 93 | |||
| 94 | my $elem = $_RD_document->createElement($tagName); | ||
| 95 | foreach my $name (sort keys %attrs) { | ||
| 96 | $elem->setAttribute($name, $attrs{$name}); | ||
| 97 | } | ||
| 98 | $_RD_cur_node->appendChild($elem); | ||
| 99 | $_RD_cur_node = $elem; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub _endElementHandler() { | ||
| 103 | my ($tagName) = @_; | ||
| 104 | |||
| 105 | $_RD_cur_node = $_RD_cur_node->getParentNode(); | ||
| 106 | } | ||
| 107 | |||
| 108 | sub _characterDataHandler() { | ||
| 109 | my ($cdata) = @_; | ||
| 110 | |||
| 111 | if($_RD_in_cdata) { | ||
| 112 | $_RD_cdata_value .= $cdata; | ||
| 113 | } else { | ||
| 114 | my $text = $_RD_document->createTextNode($cdata); | ||
| 115 | $_RD_cur_node->appendChild($text); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | sub _processingInstructionHandler() { | ||
| 120 | my ($target,$data) = @_; | ||
| 121 | |||
| 122 | my $pi = $_RD_document->createProcessingInstruction($target, $data); | ||
| 123 | $_RD_cur_node->appendChild($pi); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub _commentHandler() { | ||
| 127 | my ($string) = @_; | ||
| 128 | |||
| 129 | my $comment = $_RD_document->createComment($string); | ||
| 130 | $_RD_cur_node->appendChild($comment); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub _startCdataHandler() { | ||
| 134 | |||
| 135 | $_RD_cdata_value = ""; | ||
| 136 | $_RD_in_cdata = 1; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub _endCdataHandler() { | ||
| 140 | |||
| 141 | my $cdata = $_RD_document->createCDATASection($_RD_cdata_value); | ||
| 142 | $_RD_cur_node->appendChild($cdata); | ||
| 143 | $_RD_in_cdata = 0; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub _xmlDeclHandler() { | ||
| 147 | my ($version, $encoding, $standalone) = @_; | ||
| 148 | |||
| 149 | my $xmldecl = $_RD_document->createXMLDecl($version, $encoding, $standalone); | ||
| 150 | $_RD_document->setXMLDecl($xmldecl); | ||
| 151 | } | ||
| 152 | |||
| 153 | =item sub process_to_DOM($processor, $path, [\@args]) | ||
| 154 | |||
| 155 | my $document = $rdom->process_to_DOM($processor, 'some_file.fpp' ,\@args); | ||
| 156 | |||
| 157 | Where $processor is as preprocessor such as Rinchi::CPlusPlus::Proprocessor or | ||
| 158 | Rinchi::Fortran::Proprocessor, $path is the path to the file to be parsed and | ||
| 159 | $args is an optional reference to an array of arguments. | ||
| 160 | |||
| 161 | Parse the given file after passing the arguments if given. Print the new source | ||
| 162 | to standard output. | ||
| 163 | |||
| 164 | =cut | ||
| 165 | |||
| 166 | sub process_to_DOM($$$) { | ||
| 167 | my ($self, $pp, $path, $args) = @_; | ||
| 168 | |||
| 169 | $_RD_document = XML::DOM::Document->new(); | ||
| 170 | $_RD_cur_node = $_RD_document; | ||
| 171 | |||
| 172 | $pp->setHandlers('Start' => \&_startElementHandler, | ||
| 173 | 'End' => \&_endElementHandler, | ||
| 174 | 'Char' => \&_characterDataHandler, | ||
| 175 | 'Proc' => \&_processingInstructionHandler, | ||
| 176 | 'Comment' => \&_commentHandler, | ||
| 177 | 'CdataStart' => \&_startCdataHandler, | ||
| 178 | 'CdataEnd' => \&_endCdataHandler, | ||
| 179 | 'XMLDecl' => \&_xmlDeclHandler, | ||
| 180 | ); | ||
| 181 | |||
| 182 | if (defined($args) and ref($args) eq 'ARRAY') { | ||
| 183 | $pp->process_file($path,$args); | ||
| 184 | } else { | ||
| 185 | $pp->process_file($path); | ||
| 186 | } | ||
| 187 | |||
| 188 | return $_RD_document; | ||
| 189 | } | ||
| 190 | |||
| 191 | # Preloaded methods go here. | ||
| 192 | |||
| 193 | 1; | ||
| 194 | __END__ | ||