xmlIntWrapFort.f90 8.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
!--------------------------------------------------------------------------------
! Copyright (c) 2016 Peter Grünberg Institut, Forschungszentrum Jülich, Germany
! This file is part of FLEUR and available as free software under the conditions
! of the MIT license as expressed in the LICENSE file in more detail.
!--------------------------------------------------------------------------------

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Wrapper routines for XML IO - Fortran side
!
!                                   GM'16
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
14 15
MODULE m_xmlIntWrapFort

16 17
USE m_juDFT

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
private

TYPE t_xml
   integer:: id
 contains
   PROCEDURE,NOPASS :: InitInterface
   PROCEDURE,NOPASS :: ParseSchema
   PROCEDURE,NOPASS :: ParseDoc 
   PROCEDURE,NOPASS :: ValidateDoc 
   PROCEDURE,NOPASS :: InitXPath
   PROCEDURE,NOPASS :: GetNumberOfNodes  
   PROCEDURE,NOPASS :: SetAttributeValue  
   PROCEDURE,NOPASS :: FreeResources
end type
   public t_xml

CONTAINS
35 36 37 38 39 40 41 42 43 44 45 46 47

SUBROUTINE init_from_command_line()
  IMPLICIT NONE
  CHARACTER(len=1000)::xpath
  INTEGER:: i,ii
  
  IF (judft_was_argument("-xmlXPath")) THEN
     xpath=judft_string_for_argument("-xmlXPath")
     DO WHILE(INDEX(xpath,"=")>0)
        i=INDEX(xpath,"=")
        ii=INDEX(xpath,":")
        IF (ii==0) ii=LEN(TRIM(xpath))+1
        IF (i>100.OR.ii-i>100) CALL judft_error("Too long xmlXPath argument",calledby="xmlIntWarpFort.f90")
48
        CALL SetAttributeValue(xpath(:i-1),xpath(i+1:ii-1))
49 50 51 52 53 54 55 56 57 58 59
        WRITE(*,*) "Set from command line:",TRIM(xpath(:i-1)),"=",TRIM(xpath(i+1:ii-1))
        IF (ii+1<len(xpath))THEN
           xpath=xpath(ii+1:)
        ELSE
           xpath=""
        ENDIF
     END DO
  END IF
END SUBROUTINE init_from_command_line
     

60
SUBROUTINE InitInterface()
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

   USE iso_c_binding
   USE m_types

   IMPLICIT NONE

   INTEGER :: errorStatus

   interface
      function initializeXMLInterface() bind(C, name="initializeXMLInterface")
         use iso_c_binding
         INTEGER(c_int) initializeXMLInterface
      end function initializeXMLInterface
   end interface

   errorStatus = 0
   errorStatus = initializeXMLInterface()
78 79 80
   IF(errorStatus.NE.0) THEN
      CALL juDFT_error("Could not initialize XML interface.",calledby="xmlInitInterface")
   END IF
81

82
END SUBROUTINE InitInterface
83

84
SUBROUTINE ParseSchema(schemaFilename)
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

   USE iso_c_binding
   USE m_types

   IMPLICIT NONE

   CHARACTER(LEN=200,KIND=c_char), INTENT(IN) :: schemaFilename

   INTEGER :: errorStatus

   interface
      function parseXMLSchema(schemaFilename) bind(C, name="parseXMLSchema")
         use iso_c_binding
         INTEGER(c_int) parseXMLSchema
         character(kind=c_char) :: schemaFilename(*)
      end function parseXMLSchema
   end interface

   errorStatus = 0
   errorStatus = parseXMLSchema(schemaFilename)
105
   IF(errorStatus.NE.0) THEN
106
      CALL juDFT_error("XML Schema file not parsable: "//TRIM(ADJUSTL(schemaFilename)),calledby="xmlParseSchema")
107
   END IF
108

109
END SUBROUTINE ParseSchema
110

111
SUBROUTINE ParseDoc(docFilename)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

   USE iso_c_binding
   USE m_types

   IMPLICIT NONE

   CHARACTER(LEN=200,KIND=c_char), INTENT(IN) :: docFilename

   INTEGER :: errorStatus

   interface
      function parseXMLDocument(docFilename) bind(C, name="parseXMLDocument")
         use iso_c_binding
         INTEGER(c_int) parseXMLDocument
         character(kind=c_char) :: docFilename(*)
      end function parseXMLDocument
   end interface

   errorStatus = 0
   errorStatus = parseXMLDocument(docFilename)
132
   IF(errorStatus.NE.0) THEN
133
      CALL juDFT_error("XML document file not parsable: "//TRIM(ADJUSTL(docFilename)),calledby="xmlParseDoc")
134
   END IF
135

136
END SUBROUTINE ParseDoc
137

138
SUBROUTINE ValidateDoc()
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

   USE iso_c_binding
   USE m_types

   IMPLICIT NONE

   INTEGER :: errorStatus

   interface
      function validateXMLDocument() bind(C, name="validateXMLDocument")
         use iso_c_binding
         INTEGER(c_int) validateXMLDocument
      end function validateXMLDocument
   end interface

   errorStatus = 0
   errorStatus = validateXMLDocument()
156
   IF(errorStatus.NE.0) THEN
157
      CALL juDFT_error("XML document cannot be validated against Schema.",calledby="xmlValidateDoc")
158
   END IF
159

160
END SUBROUTINE ValidateDoc
161

162
SUBROUTINE InitXPath()
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

   USE iso_c_binding
   USE m_types

   IMPLICIT NONE

   INTEGER :: errorStatus

   interface
      function initializeXPath() bind(C, name="initializeXPath")
         use iso_c_binding
         INTEGER(c_int) initializeXPath
      end function initializeXPath
   end interface

   errorStatus = 0
   errorStatus = initializeXPath()
180
   IF(errorStatus.NE.0) THEN
181
      CALL juDFT_error("Could not initialize XPath.",calledby="InitXPath")
182
   END IF
183
   CALL init_from_command_line()
184
END SUBROUTINE InitXPath
185

186
FUNCTION GetNumberOfNodes(xPath)
187 188 189 190 191 192

   USE iso_c_binding
   USE m_types

   IMPLICIT NONE

193
   INTEGER :: GetNumberOfNodes
194 195 196 197 198 199 200 201 202 203
   CHARACTER(LEN=*,KIND=c_char), INTENT(IN) :: xPath

   interface
      function getNumberOfXMLNodes(xPathExpression) bind(C, name="getNumberOfXMLNodes")
         use iso_c_binding
         INTEGER(c_int) getNumberOfXMLNodes
         character(kind=c_char) :: xPathExpression(*)
      end function getNumberOfXMLNodes
   end interface

204
   GetNumberOfNodes = getNumberOfXMLNodes(TRIM(ADJUSTL(xPath))//C_NULL_CHAR)
205

206
END FUNCTION GetNumberOfNodes
207

208
FUNCTION GetAttributeValue(xPath)
209 210 211 212 213 214

   USE iso_c_binding
   USE m_types

   IMPLICIT NONE

215
   CHARACTER(LEN=:),ALLOCATABLE :: GetAttributeValue
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

   CHARACTER(LEN=*, KIND=c_char), INTENT(IN) :: xPath

   CHARACTER (LEN=1, KIND=c_char), POINTER, DIMENSION (:) :: valueFromC => null()
   CHARACTER*255 :: value
   INTEGER :: length, errorStatus, i
   TYPE(c_ptr) :: c_string

   interface
      function getXMLAttributeValue(xPathExpression) bind(C, name="getXMLAttributeValue")
         use iso_c_binding
         CHARACTER(KIND=c_char) :: xPathExpression(*)
         TYPE(c_ptr) :: getXMLAttributeValue
      end function getXMLAttributeValue
   end interface

232 233
  

234 235 236 237
   c_string = getXMLAttributeValue(TRIM(ADJUSTL(xPath))//C_NULL_CHAR)

   CALL C_F_POINTER(c_string, valueFromC, [ 255 ])
   IF (.NOT.c_associated(c_string)) THEN
238 239 240
      WRITE(*,*) 'Error in trying to obtain attribute value from XPath:'
      WRITE(*,*) TRIM(ADJUSTL(xPath))
      CALL juDFT_error("Attribute value could not be obtained.",calledby="xmlGetAttributeValue")
241
   END IF
Daniel Wortmann's avatar
Daniel Wortmann committed
242 243 244 245 246 247

   value = ''
   i = 1
   DO WHILE ((valueFromC(i).NE.C_NULL_CHAR).AND.(i.LE.255))
      value(i:i) = valueFromC(i)
      i = i + 1
248
   END DO
Daniel Wortmann's avatar
Daniel Wortmann committed
249
   length = i-1
250

251
   GetAttributeValue = trim(adjustl(value(1:length)))
252

253
END FUNCTION GetAttributeValue
254

255

256
SUBROUTINE SetAttributeValue(xPath,VALUE)
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

   USE iso_c_binding
   USE m_types

   IMPLICIT NONE
  
   CHARACTER(LEN=*, KIND=c_char), INTENT(IN) :: xPath
   CHARACTER(len=*, KIND=c_char), INTENT(IN) :: value

   INTEGER :: errorStatus

   INTERFACE
      FUNCTION setXMLAttributeValue(xPathExpression,valueExpression) BIND(C, name="setXMLAttributeValue")
         use iso_c_binding
         CHARACTER(KIND=c_char) :: xPathExpression(*)
         CHARACTER(KIND=c_char) :: valueExpression(*)
         INTEGER(c_int) :: setXMLAttributeValue
       END FUNCTION setXMLAttributeValue
    END INTERFACE

    errorStatus = setXMLAttributeValue(TRIM(ADJUSTL(xPath))//C_NULL_CHAR,TRIM(ADJUSTL(VALUE))//C_NULL_CHAR)
    IF (errorStatus.ne.0) THEN
      WRITE(*,*) 'Error in trying to setting attribute value from XPath:'
      WRITE(*,*) TRIM(ADJUSTL(xPath))
      WRITE(*,*) TRIM(ADJUSTL(VALUE))
      CALL juDFT_error("Attribute value could not be set.",calledby="xmlSetAttributeValue")
   END IF

285
 END SUBROUTINE SetAttributeValue
286

287
SUBROUTINE FreeResources()
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

   USE iso_c_binding
   USE m_types

   IMPLICIT NONE

   INTEGER :: errorStatus

   interface
      function freeXMLResources() bind(C, name="freeXMLResources")
         use iso_c_binding
         INTEGER freeXMLResources
      end function freeXMLResources
   end interface

   errorStatus = 0
   errorStatus = freeXMLResources()
305 306 307 308
   IF(errorStatus.NE.0) THEN
      CALL juDFT_error("Could not free XML resources.",calledby="xmlFreeResources")
      STOP 'Error!'
   END IF
309

310
END SUBROUTINE FreeResources
311 312

END MODULE m_xmlIntWrapFort