|
1 # |
|
2 # Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of the License "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # Provides generic methods retrieving data from the XML file. |
|
16 # Internally uses DOM API. Uses XML Checker to validate the XML. |
|
17 # |
|
18 |
|
19 package genericparser; |
|
20 require Exporter; |
|
21 @ISA=qw(Exporter); |
|
22 |
|
23 @EXPORT=qw( |
|
24 |
|
25 getRootElement |
|
26 getAttrValue |
|
27 getElementValue |
|
28 getSiblingElements |
|
29 getNodeAttributes |
|
30 getChildElements |
|
31 getNodeFromTree |
|
32 getElementsTagName |
|
33 getElementName |
|
34 ); |
|
35 |
|
36 use strict; |
|
37 use XML::DOM; |
|
38 use XML::DOM::ValParser;#XML Validator |
|
39 |
|
40 my $validxml; # XML validation status |
|
41 $XML::Checker::FAIL = \&failhandler; # User defined fail handler |
|
42 |
|
43 # User defined fail handler for the XML checker |
|
44 sub failhandler |
|
45 { |
|
46 my ($code, $msg, @context) = @_; |
|
47 print "ERROR: $msg\n"; |
|
48 $validxml = 0; |
|
49 } |
|
50 |
|
51 #Returns the root element of the XML file |
|
52 sub getRootElement() { |
|
53 my ($xmlfile) = shift; |
|
54 die "ERROR: XML File does not exists in the specified path $xmlfile\n" if (!-f $xmlfile); |
|
55 my $DOMParser = new XML::DOM::Parser(); #DOM Parser |
|
56 #Set the SGML_SEARCH_PATH to the path where the DTD files are found ($ENV{EPOCROOT}epoc32\\tools). |
|
57 XML::Checker::Parser::set_sgml_search_path ("$ENV{EPOCROOT}epoc32/tools"); |
|
58 my $xmlValidator = new XML::Checker::Parser();#Validates XML against Schema |
|
59 $validxml = 1; |
|
60 $xmlValidator->parsefile($xmlfile); |
|
61 |
|
62 if($validxml) |
|
63 { |
|
64 my $document = $DOMParser->parsefile($xmlfile);#Parse XML file |
|
65 my $root = $document->getDocumentElement(); |
|
66 return $root; |
|
67 } |
|
68 |
|
69 return 0; |
|
70 } |
|
71 |
|
72 #Returns the attribute value of the element |
|
73 #Optional argument strictcaseflg does not convert the case of the attribute value |
|
74 sub getAttrValue(){ |
|
75 my ($elementname, $name, $strictcaseflg) = @_; |
|
76 my $attrVal = $elementname->getAttribute($name) ; |
|
77 if ($attrVal eq "") { |
|
78 return undef; |
|
79 } |
|
80 if(!defined $strictcaseflg) { |
|
81 return lc($attrVal); |
|
82 } |
|
83 else { |
|
84 return $attrVal; |
|
85 } |
|
86 } |
|
87 |
|
88 #Returns the element value |
|
89 #Optional argument strictcaseflg does not convert the case of the element value |
|
90 sub getElementValue(){ |
|
91 my ($elementname) = shift; |
|
92 my ($strictcaseflg)=shift; |
|
93 my $elementVal; |
|
94 if( !$elementname->hasChildNodes() ) |
|
95 { |
|
96 return undef; |
|
97 } |
|
98 if ($elementname->getNodeType == XML::DOM::ELEMENT_NODE) { |
|
99 $elementVal = $elementname->getFirstChild()->getData ; |
|
100 } |
|
101 |
|
102 if(!defined $strictcaseflg) { |
|
103 return lc($elementVal); |
|
104 } |
|
105 else { |
|
106 return $elementVal; |
|
107 } |
|
108 } |
|
109 |
|
110 #Returns the sibling elements for the given node |
|
111 sub getSiblingElements { |
|
112 my $child = shift; |
|
113 my @nodeList; |
|
114 while($child) { |
|
115 if($child->getNodeType eq XML::DOM::ELEMENT_NODE) { |
|
116 @nodeList=(@nodeList,$child); |
|
117 } |
|
118 $child = $child->getNextSibling; |
|
119 } |
|
120 return @nodeList; |
|
121 } |
|
122 |
|
123 #Returns the attribute list reference for the given node |
|
124 sub getNodeAttributes() { |
|
125 my $node = shift; |
|
126 my $attlist; |
|
127 if ($node->getNodeType() eq XML::DOM::ELEMENT_NODE) { |
|
128 $attlist = $node->getAttributes; |
|
129 } |
|
130 return $attlist; |
|
131 } |
|
132 |
|
133 #Returns the children for the given node element |
|
134 sub getChildElements { |
|
135 my $child = shift; |
|
136 my @childList; |
|
137 my @newChildList; |
|
138 |
|
139 @childList=$child->getChildNodes; |
|
140 foreach my $node (@childList) { |
|
141 if($node->getNodeType eq XML::DOM::ELEMENT_NODE) { |
|
142 @newChildList=(@newChildList,$node); |
|
143 } |
|
144 } |
|
145 return @newChildList; |
|
146 } |
|
147 |
|
148 #Returns the list of nodes that matches the specified node tree |
|
149 sub getNodeFromTree(){ |
|
150 |
|
151 my @resultNodes; |
|
152 my ($element, @nodeNames) = @_; |
|
153 my $nodeName; |
|
154 my @children = $element->getChildNodes(); |
|
155 |
|
156 foreach my $child (@children) { |
|
157 if ($child->getNodeType eq XML::DOM::ELEMENT_NODE) { |
|
158 if (($child->getNodeName) eq $nodeNames[0]) { |
|
159 if ($#nodeNames) { |
|
160 $nodeName = shift @nodeNames;#Pop unmatched node |
|
161 push @resultNodes,&getNodeFromTree($child, @nodeNames); |
|
162 unshift @nodeNames, $nodeName;#Put back the nodes to proper level |
|
163 } |
|
164 else { |
|
165 push @resultNodes,$child;#Push matched node to the destination |
|
166 } |
|
167 |
|
168 } |
|
169 |
|
170 } |
|
171 |
|
172 } |
|
173 |
|
174 return @resultNodes; |
|
175 } |
|
176 |
|
177 #Returns the list of elements whose node matches with the node name supplied |
|
178 sub getElementsTagName{ |
|
179 my ($node, $name) = @_; |
|
180 if ($node->getNodeType eq XML::DOM::ELEMENT_NODE) { |
|
181 my @taggedElements = $node->getElementsByTagName($name); |
|
182 return @taggedElements; |
|
183 } |
|
184 } |
|
185 |
|
186 #Returns the element name for the given node |
|
187 sub getElementName{ |
|
188 my $node = shift; |
|
189 if ($node->getNodeType eq XML::DOM::ELEMENT_NODE) { |
|
190 return lc($node->getNodeName); |
|
191 } |
|
192 } |
|
193 |
|
194 1; |