| 1 | package AI::Categorizer::Document::XML; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use AI::Categorizer::Document; | ||
| 5 | use base qw(AI::Categorizer::Document); | ||
| 6 | use XML::SAX; | ||
| 7 | |||
| 8 | __PACKAGE__->contained_objects | ||
| 9 | ( | ||
| 10 | xml_handler => 'AI::Categorizer::Document::XML::Handler', | ||
| 11 | ); | ||
| 12 | |||
| 13 | ### Constructors | ||
| 14 | |||
| 15 | sub parse { | ||
| 16 | my ($self, %args) = @_; | ||
| 17 | |||
| 18 | # it is a string which contains the content of XML | ||
| 19 | my $body= $args{content}; | ||
| 20 | |||
| 21 | # it is a hash which includes a pair of <elementName, weight> | ||
| 22 | my $elementWeight= $args{elementWeight}; | ||
| 23 | |||
| 24 | # construct Handler which receive event of element, data, comment, processing_instruction | ||
| 25 | # And convert their values into a sequence of string and save it into buffer | ||
| 26 | my $xmlHandler = $self->create_contained_object('xml_handler', weights => $elementWeight); | ||
| 27 | |||
| 28 | # construct parser | ||
| 29 | my $xmlParser= XML::SAX::ParserFactory->parser(Handler => $xmlHandler); | ||
| 30 | |||
| 31 | # let's start parsing XML, where the methids of Handler will be called | ||
| 32 | $xmlParser->parse_string($body); | ||
| 33 | |||
| 34 | # extract the converted string from Handler | ||
| 35 | $body= $xmlHandler->getContent; | ||
| 36 | |||
| 37 | # Now, construct Document Object and return it | ||
| 38 | return { body => $body }; | ||
| 39 | } | ||
| 40 | |||
| 41 | ########################################################################## | ||
| 42 | package AI::Categorizer::Document::XML::Handler; | ||
| 43 | use strict; | ||
| 44 | use base qw(XML::SAX::Base); | ||
| 45 | |||
| 46 | # Input: a hash which is weights of elements | ||
| 47 | # Output: object of this class | ||
| 48 | # Description: this is constructor | ||
| 49 | sub new{ | ||
| 50 | my ($class, %args) = @_; | ||
| 51 | |||
| 52 | # call super class such as XML::SAX::Base | ||
| 53 | my $self = $class->SUPER::new; | ||
| 54 | |||
| 55 | # save weights of elements which is a hash for pairs <elementName, weight> | ||
| 56 | # weight is times duplication of corresponding element | ||
| 57 | # It is provided by caller(one of parameters) at construction, and | ||
| 58 | # we must save it in order to use doing duplication at end_element | ||
| 59 | $self->{weightHash} = $args{weights}; | ||
| 60 | |||
| 61 | # It is storage to store the data produced by Text, CDataSection and etc. | ||
| 62 | $self->{content} = ''; | ||
| 63 | |||
| 64 | # This array is used to store the data for every element from root to the current visiting element. | ||
| 65 | # Thus, data of 0~($levelPointer-1)th in the array is only valid. | ||
| 66 | # The array which store the starting location(index) of the content for an element, | ||
| 67 | # From it, we can know all the data produced by an element at the end_element | ||
| 68 | # It is needed at the duplication of the data produced by the specific element | ||
| 69 | $self->{locationArray} = []; | ||
| 70 | |||
| 71 | return $self; | ||
| 72 | } | ||
| 73 | |||
| 74 | # Input: None | ||
| 75 | # Output: None | ||
| 76 | # Description: | ||
| 77 | # it is called whenever the parser meets the document | ||
| 78 | # it will be called at once | ||
| 79 | # Currently, confirm if the content buffer is an empty | ||
| 80 | sub start_document{ | ||
| 81 | my ($self, $doc)= @_; | ||
| 82 | |||
| 83 | # The level(depth) of the last called element in XML tree | ||
| 84 | # Calling of start_element is the preorder of the tree traversal. | ||
| 85 | # The level is the level of current visiting element in tree. | ||
| 86 | # the first element is 0-level | ||
| 87 | $self->{levelPointer} = 0; | ||
| 88 | |||
| 89 | # all data will be saved into here, initially, it is an empty | ||
| 90 | $self->{content} = ""; | ||
| 91 | |||
| 92 | #$self->SUPER::start_document($doc); | ||
| 93 | } | ||
| 94 | |||
| 95 | # Input: None | ||
| 96 | # Output: None | ||
| 97 | # Description: | ||
| 98 | # it is called whenever the parser ends the document | ||
| 99 | # it will be called at once | ||
| 100 | # Nothing to do | ||
| 101 | sub end_document{ | ||
| 102 | my ($self, $doc)= @_; | ||
| 103 | |||
| 104 | #$self->SUPER::end_document($doc); | ||
| 105 | } | ||
| 106 | |||
| 107 | # Input | ||
| 108 | # LocalName: $el->{LocalName} | ||
| 109 | # NamespaceURI: $el->{NamespaceURI} | ||
| 110 | # Name $el->{Name} | ||
| 111 | # Prefix $el->{Prefix} | ||
| 112 | # Attributes $el->{Attributes} | ||
| 113 | # for each attribute | ||
| 114 | # LocalName: $el->{LocalName} | ||
| 115 | # NamespaceURI: $el->{NamespaceURI} | ||
| 116 | # Name $el->{Name} | ||
| 117 | # Prefix $el->{Prefix} | ||
| 118 | # Value $el->{Value} | ||
| 119 | # Output: None | ||
| 120 | # Description: | ||
| 121 | # it is called whenever the parser meets the element | ||
| 122 | sub start_element{ | ||
| 123 | my ($self, $el)= @_; | ||
| 124 | |||
| 125 | # find the last location of the content | ||
| 126 | # its meaning is to append the new data at this location | ||
| 127 | my $location= length $self->{content}; | ||
| 128 | |||
| 129 | # save the last location of the current content | ||
| 130 | # so that at end_element the starting location of data of this element can be known | ||
| 131 | $self->{locationArray}[$self->{levelPointer}] = $location; | ||
| 132 | |||
| 133 | # for the next element, increase levelPointer | ||
| 134 | $self->{levelPointer}++; | ||
| 135 | |||
| 136 | #$self->SUPER::start_document($el); | ||
| 137 | } | ||
| 138 | |||
| 139 | # Input: None | ||
| 140 | # Output: None | ||
| 141 | # Description: | ||
| 142 | # it is called whenever the parser ends the element | ||
| 143 | sub end_element{ | ||
| 144 | my ($self, $el)= @_; | ||
| 145 | |||
| 146 | $self->{levelPointer}--; | ||
| 147 | my $location= $self->{locationArray}[$self->{levelPointer}]; | ||
| 148 | |||
| 149 | # find the name of element | ||
| 150 | my $elementName= $el->{Name}; | ||
| 151 | |||
| 152 | # set the default weight | ||
| 153 | my $weight= 1; | ||
| 154 | |||
| 155 | # check if user give the weight to duplicate data | ||
| 156 | $weight= $self->{weightHash}{$elementName} if exists $self->{weightHash}{$elementName}; | ||
| 157 | |||
| 158 | # 0 - remove all the data to be related to this element | ||
| 159 | if($weight == 0){ | ||
| 160 | $self->{content} = substr($self->{content}, 0, $location); | ||
| 161 | return; | ||
| 162 | } | ||
| 163 | |||
| 164 | # 1 - dont duplicate | ||
| 165 | if($weight == 1){ | ||
| 166 | return; | ||
| 167 | } | ||
| 168 | |||
| 169 | # n - duplicate data by n times | ||
| 170 | # get new content | ||
| 171 | my $newContent= substr($self->{content}, $location); | ||
| 172 | |||
| 173 | # start to copy | ||
| 174 | for(my $i=1; $i<$weight;$i++){ | ||
| 175 | $self->{content} .= $newContent; | ||
| 176 | } | ||
| 177 | |||
| 178 | #$self->SUPER::end_document($el); | ||
| 179 | } | ||
| 180 | |||
| 181 | # Input: a hash which consists of pair <Data, Value> | ||
| 182 | # Output: None | ||
| 183 | # Description: | ||
| 184 | # it is called whenever the parser meets the text which comes from Text, CDataSection and etc | ||
| 185 | # Value must be saved into content buffer. | ||
| 186 | sub characters{ | ||
| 187 | my ($self, $args)= @_; | ||
| 188 | |||
| 189 | # save "data plus new line" into content | ||
| 190 | $self->{content} .= "$args->{Data}\n"; | ||
| 191 | } | ||
| 192 | |||
| 193 | # Input: a hash which consists of pair <Data, Value> | ||
| 194 | # Output: None | ||
| 195 | # Description: | ||
| 196 | # it is called whenever the parser meets the comment | ||
| 197 | # Currently, it will be ignored | ||
| 198 | sub comment{ | ||
| 199 | my ($self, $args)= @_; | ||
| 200 | } | ||
| 201 | |||
| 202 | # Input: a hash which consists of pair <Data, Value> and <Target, Value> | ||
| 203 | # Output: None | ||
| 204 | # Description: | ||
| 205 | # it is called whenever the parser meets the processing_instructing | ||
| 206 | # Currently, it will be ignored | ||
| 207 | sub processing_instruction{ | ||
| 208 | my ($self, $args)= @_; | ||
| 209 | } | ||
| 210 | |||
| 211 | # Input: None | ||
| 212 | # Output: the converted data, that is, content | ||
| 213 | # Description: | ||
| 214 | # return the content | ||
| 215 | sub getContent{ | ||
| 216 | my ($self)= @_; | ||
| 217 | return $self->{content}; | ||
| 218 | } | ||
| 219 | |||
| 220 | 1; | ||
| 221 | __END__ |