| 1 | #/*==================================================================== | ||
| 2 | # * Babel Objects, Version 1.0 | ||
| 3 | # * ==================================================================== | ||
| 4 | # * | ||
| 5 | # * Copyright (c) 2000 The Babel Objects Network. All rights reserved. | ||
| 6 | # * | ||
| 7 | # * This source file is subject to version 1.1 of The Babel Objects | ||
| 8 | # * License, that is bundled with this package in the file LICENSE, | ||
| 9 | # * and is available through the world wide web at : | ||
| 10 | # * | ||
| 11 | # * http://www.BabelObjects.Org/law/license/1.1.txt | ||
| 12 | # * | ||
| 13 | # * If you did not receive a copy of the Babel Objects license and are | ||
| 14 | # * unable to obtain it through the world wide web, please send a note | ||
| 15 | # * to license@BabelObjects.Org so we can mail you a copy immediately. | ||
| 16 | # * | ||
| 17 | # * -------------------------------------------------------------------- | ||
| 18 | # * | ||
| 19 | # * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | ||
| 20 | # * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
| 21 | # * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| 22 | # * DISCLAIMED. IN NO EVENT SHALL THE BABEL OBJECTS NETWORK OR | ||
| 23 | # * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 24 | # * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| 25 | # * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
| 26 | # * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
| 27 | # * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
| 28 | # * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | ||
| 29 | # * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 30 | # * SUCH DAMAGE. | ||
| 31 | # * | ||
| 32 | # * ==================================================================== | ||
| 33 | # * | ||
| 34 | # * This software consists of voluntary contributions made by many | ||
| 35 | # * individuals on behalf of The Babel Objects Network. For more | ||
| 36 | # * information on The Babel Objects Network, please see | ||
| 37 | # * <http://www.BabelObjects.org/>. | ||
| 38 | # * | ||
| 39 | # */ | ||
| 40 | |||
| 41 | package BabelObjects::Util::Facility::XMLFacility; | ||
| 42 | |||
| 43 | use BabelObjects::Util::Dvlpt::Log; | ||
| 44 | use XML::DOM; | ||
| 45 | |||
| 46 | use strict; | ||
| 47 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD); | ||
| 48 | |||
| 49 | require Exporter; | ||
| 50 | require AutoLoader; | ||
| 51 | |||
| 52 | @ISA = qw(Exporter AutoLoader); | ||
| 53 | # Items to export into callers namespace by default. Note: do not export | ||
| 54 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 55 | # Do not simply export all your public functions/methods/constants. | ||
| 56 | @EXPORT = qw( | ||
| 57 | |||
| 58 | ); | ||
| 59 | $VERSION = '1.00'; | ||
| 60 | |||
| 61 | my $aLog; | ||
| 62 | |||
| 63 | sub new { | ||
| 64 | my $proto = shift; | ||
| 65 | my $args = shift; | ||
| 66 | |||
| 67 | my $class = ref($proto) || $proto; | ||
| 68 | |||
| 69 | my $self = { | ||
| 70 | }; | ||
| 71 | |||
| 72 | bless ($self, $class); | ||
| 73 | |||
| 74 | $aLog = new BabelObjects::Util::Dvlpt::Log(); | ||
| 75 | #$aLog->log("\n-- BabelObjects::Util::Facility::XMLFacility --"); | ||
| 76 | |||
| 77 | return $self; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub getValueByPath { | ||
| 81 | my $self = shift; | ||
| 82 | my $node = shift; | ||
| 83 | my $path = shift; | ||
| 84 | |||
| 85 | $_ = $path; | ||
| 86 | |||
| 87 | my (@tags) = /(.*)\/(.*)/; | ||
| 88 | |||
| 89 | foreach (@tags) { | ||
| 90 | #$aLog->log("Tag name to search : $_"); | ||
| 91 | $node = $self->getElementByTagName($node, $_); | ||
| 92 | } | ||
| 93 | |||
| 94 | return $node->getFirstChild->getData; | ||
| 95 | } | ||
| 96 | |||
| 97 | # return the first node under $node with the parameter named $name provided | ||
| 98 | sub getElementByTagName { | ||
| 99 | my $self = shift; | ||
| 100 | my $node = shift; | ||
| 101 | my $name = shift; | ||
| 102 | |||
| 103 | #$aLog->log("getElementByTagName"); | ||
| 104 | #$aLog->log(" Node = $node"); | ||
| 105 | #$aLog->log(" Name = $name"); | ||
| 106 | |||
| 107 | my $nodes = $node->getElementsByTagName ("*", 0); | ||
| 108 | my $n = $nodes->getLength; | ||
| 109 | |||
| 110 | for (my $i = 0; $i < $n; $i++) { | ||
| 111 | my $aNode = $nodes->item($i); | ||
| 112 | #$aLog->log(" Type = ".$aNode->getNodeType); | ||
| 113 | if ($aNode->getNodeType == ELEMENT_NODE) { | ||
| 114 | #$aLog->log(" Current Name = ".$aNode->getNodeName); | ||
| 115 | if ($aNode->getNodeName eq $name) { | ||
| 116 | #$aLog->log(" Name result = ".$aNode->getNodeName); | ||
| 117 | return $aNode; | ||
| 118 | } else { | ||
| 119 | my $nodeResult = $self->getElementByTagName($aNode, $name); | ||
| 120 | if ($nodeResult) { | ||
| 121 | return $nodeResult; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | } | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | # returns all the child nodes in the same level under 'node' and named 'name' | ||
| 129 | sub getElementsByTagName { | ||
| 130 | my $self = shift; | ||
| 131 | my $node = shift; | ||
| 132 | my $name = shift; | ||
| 133 | |||
| 134 | my @nodes; | ||
| 135 | |||
| 136 | #$aLog->log("getElementsByTagName"); | ||
| 137 | #$aLog->log(" Node = $node"); | ||
| 138 | #$aLog->log(" Name = $name"); | ||
| 139 | |||
| 140 | my $childs = $node->getChildNodes(); | ||
| 141 | my $n = $childs->getLength; | ||
| 142 | |||
| 143 | for (my $i = 0; $i < $n; $i++) { | ||
| 144 | my $aNode = $childs->item($i); | ||
| 145 | #$aLog->log("Node $i = ".$aNode); | ||
| 146 | if ($aNode->getNodeType == ELEMENT_NODE) { | ||
| 147 | #$aLog->log("Current Name = ".$aNode->getNodeName); | ||
| 148 | if ($aNode->getNodeName eq $name) { | ||
| 149 | #$aLog->log(" Name result = ".$aNode->getNodeName); | ||
| 150 | push(@nodes, $aNode); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | return @nodes | ||
| 156 | } | ||
| 157 | |||
| 158 | # returns all the nodes named 'name' under the branch | ||
| 159 | sub getElementsByPath { | ||
| 160 | my $self = shift; | ||
| 161 | my $node = shift; | ||
| 162 | my $path = shift; | ||
| 163 | my $regExp = shift || "([^\/]+)\/[^\/]+\/([^\/]+)"; | ||
| 164 | |||
| 165 | #my (@tags) = $path =~ /([^\/]+)/g; | ||
| 166 | my (@tags) = $path =~ /$regExp/g; | ||
| 167 | my $name = pop(@tags); | ||
| 168 | |||
| 169 | foreach (@tags) { | ||
| 170 | #$aLog->log("Tag name to search : $_"); | ||
| 171 | $node = $self->getElementByTagName($node, $_); | ||
| 172 | } | ||
| 173 | |||
| 174 | #$aLog->log(" Tag name to search : $name"); | ||
| 175 | #return $self->getElementByTagName($node, $name); | ||
| 176 | return $node->getElementsByTagName($name); | ||
| 177 | } | ||
| 178 | |||
| 179 | # prints the xml tree | ||
| 180 | sub dump { | ||
| 181 | my $self = shift; | ||
| 182 | my $node = shift; | ||
| 183 | my $spaces = shift; | ||
| 184 | |||
| 185 | my $nodes = $node->getElementsByTagName ("*", 0); | ||
| 186 | my $n = $nodes->getLength; | ||
| 187 | |||
| 188 | for (my $i = 0; $i < $n; $i++) { | ||
| 189 | my $aNode = $nodes->item($i); | ||
| 190 | #$aLog->log(" Node = $aNode"); | ||
| 191 | #$aLog->log(" Type = ".$aNode->getNodeType); | ||
| 192 | #$aLog->log(" Name = ".$aNode->getNodeName); | ||
| 193 | if ($aNode->getNodeType == ELEMENT_NODE) { | ||
| 194 | print $spaces."<".$aNode->getNodeName.">\n"; | ||
| 195 | if ( ($aNode->getFirstChild) | ||
| 196 | && ($aNode->getFirstChild->getNodeType == TEXT_NODE) | ||
| 197 | && ($aNode->getFirstChild->getData ne "") ) { | ||
| 198 | $_ = $aNode->getFirstChild->getData; | ||
| 199 | s/\t*|\s*|\n//g; | ||
| 200 | if ($_ ne "") { | ||
| 201 | print $spaces." ".$_."\n"; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | $self->dump($aNode, $spaces." "); | ||
| 205 | print $spaces."</".$aNode->getNodeName.">\n"; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | 1; | ||
| 211 | __END__ |