Eötvös Quantum Utilities  v4.9.146
Providing the Horsepowers in the Quantum Realm
XML.f90
Go to the documentation of this file.
1 !======================================================================
2 ! Module supporting the inport of XML files.
3 ! This file is part of the EQuUs package. (for details see: http://eqt.elte.hu/equus/home)
4 ! Copyright (C) 2016 Peter Rakyta, Ph.D.
5 !
6 ! This program is free software: you can redistribute it and/or modify
7 ! it under the terms of the GNU General Public License as published by
8 ! the Free Software Foundation, either version 3 of the License, or
9 ! (at your option) any later version.
10 !
11 ! This program is distributed in the hope that it will be useful,
12 ! but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ! GNU General Public License for more details.
15 !
16 ! You should have received a copy of the GNU General Public License
17 ! along with this program. If not, see http://www.gnu.org/licenses/.
18 !
19 !======================================================================
20 #ifndef MAX_PROP_LENGTH
21 #define MAX_PROP_LENGTH 100
22 #endif
23 
24 
25 
26 MODULE equus_xml
27 
28 ! DESCRIPTION:
31 type, public :: attrib
32 ! The name of the property
33  character(len=MAX_PROP_LENGTH) :: name
34 ! The value of the property
35  character(len=MAX_PROP_LENGTH) :: value
36 end type
37 
38 
39 ! DESCRIPTION:
42 type, public :: xmlnode
43 ! The localname of the XML node
44  character(len=MAX_PROP_LENGTH) :: localname
45 ! The level of the subnode relative to the root
46  integer :: subnodelevel
47 ! The array of the attributes
48  TYPE( attrib ), pointer :: attrs(:) => null()
49 ! An array of XML subnodes
50  TYPE( xmlnode ), pointer :: subnodes(:) => null()
51 ! pointer to the parent node
52  TYPE( xmlnode ), pointer :: parentnode => null()
53 ! The value stored in the XML node
54  character(len=:), pointer :: value => null()
55 end type
56 
57 
58 
59 !****************************************************************
60  CONTAINS
61 !****************************************************************
62 
63 !-------------------------------------------------------------------------------------------------
64 ! DESCRIPTION:
70  function hassubnodes( cXMLnode )
71 
72  TYPE(xmlnode) :: cxmlnode
73  logical :: hassubnodes
74 
75  ! converting subnodes/value
76  if (ASSOCIATED(cxmlnode%subnodes)) then
77  hassubnodes = .true.
78  else
79  hassubnodes = .false.
80  end if
81 
82  end function hassubnodes
83 
84 
85 
86 
87 !-------------------------------------------------------------------------------------------------
88 ! DESCRIPTION:
93  subroutine deallocatenode(node)
94  TYPE( xmlnode ) :: node
95 
96  if (ASSOCIATED(node%subnodes)) then
97  deallocate(node%subnodes)
98  end if
99 
100  if (ASSOCIATED(node%attrs)) then
101  deallocate(node%attrs)
102  end if
103 
104  if (ASSOCIATED(node%parentnode)) then
105  deallocate(node%parentnode)
106  end if
107 
108  if (ASSOCIATED(node%value)) then
109  deallocate(node%value)
110  end if
111 
112  end subroutine deallocatenode
113 
114 !-------------------------------------------------------------------------------------------------
115 ! DESCRIPTION:
121  subroutine copynode(source, dest)
122  TYPE( xmlnode ) :: source
123  TYPE( xmlnode ) :: dest
124 
125  if (ASSOCIATED(source%attrs)) then
126  dest%attrs => source%attrs
127  end if
128 
129  if (ASSOCIATED(source%subnodes)) then
130  dest%subnodes => source%subnodes
131  end if
132 
133  if (ASSOCIATED(source%parentnode)) then
134  dest%parentnode => source%parentnode
135  end if
136 
137  if (ASSOCIATED(source%value)) then
138  dest%value => source%value
139  end if
140 
141  dest%subnodelevel = source%subnodelevel
142  dest%localname = source%localname
143 
144 
145 
146  end subroutine copynode
147 
148 !-------------------------------------------------------------------------------------------------
149 ! DESCRIPTION:
154  subroutine printnodewithsubnodes(node)
155  TYPE( xmlnode ) :: node
156  integer idx
157 
158 
159  call printnode(node)
160 
161  if (ASSOCIATED(node%subnodes)) then
162  else
163  print *, "The node has no subnodes."
164  end if
165 
166  end subroutine printnodewithsubnodes
167 
168 
169 
170 !-------------------------------------------------------------------------------------------------
171 ! DESCRIPTION:
176  subroutine printnode(node)
177  TYPE( xmlnode ) :: node
178 
179  integer idx
180 
181  print *, " "
182  print *, "**********************"
183  print *, "Printing content of an XML node"
184  print *, "localname: ", trim(node%localname)
185  print *, "subnodelevel: ", node%subnodelevel
186 
187  if (ASSOCIATED(node%attrs)) then
188  print *, "attributes:"
189  do idx = 1, SIZE(node%attrs, 1)
190  print *, idx, " ", trim(node%attrs(idx)%name), ": ", trim(node%attrs(idx)%value)
191  end do
192  end if
193 
194  if (ASSOCIATED(node%value)) then
195  print *, "The value stored in the node: ", trim(node%value)
196  end if
197 
198  if (ASSOCIATED(node%subnodes)) then
199  print *, "Number of subnodes: ", SIZE(node%subnodes, 1)
200  do idx = 1, SIZE(node%subnodes, 1)
201  call printnodewithsubnodes(node%subnodes(idx))
202  end do
203  end if
204 
205 
206  end subroutine printnode
207 
208 
209 !-------------------------------------------------------------------------------------------------
210 ! DESCRIPTION:
216  subroutine addsubnode(node, subnode)
217  TYPE( xmlnode ) :: subnode
218  TYPE( xmlnode ) :: node
219  TYPE( xmlnode ), pointer :: subnodes_tmp(:)
220 
221  integer elementNum
222 
223  if (.not.ASSOCIATED(node%subnodes)) then
224  allocate(node%subnodes(1))
225  call copynode(subnode, node%subnodes(1))
226  return
227  end if
228 
229 
230  elementnum = SIZE( node%subnodes, 1)
231  allocate( subnodes_tmp(elementnum) )
232 
233  do idx = 1, elementnum
234  call copynode(node%subnodes(idx), subnodes_tmp(idx))
235  end do
236 
237  deallocate( node%subnodes )
238  allocate( node%subnodes(elementnum+1) )
239  do idx = 1, elementnum
240  call copynode(subnodes_tmp(idx), node%subnodes(idx))
241  end do
242 
243  deallocate( subnodes_tmp )
244 
245  call copynode(subnode, node%subnodes(elementnum+1))
246 
247 ! XML node containing subnodes should not contain any value
248  node%value => null()
249 
250 
251 
252  end subroutine addsubnode
253 
254 
255 
256 
257 
258 
259 END MODULE
subroutine addsubnode(node, subnode)
Definition: XML.f90:217
subroutine deallocatenode(node)
Definition: XML.f90:94
subroutine printnode(node)
Definition: XML.f90:177
logical function hassubnodes(cXMLnode)
Definition: XML.f90:71
subroutine copynode(source, dest)
Definition: XML.f90:122
subroutine printnodewithsubnodes(node)
Definition: XML.f90:155