xmlIntWrapFort.f90 7.8 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

PRIVATE :: init_from_command_line
20
CONTAINS
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

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")
        CALL xmlSetAttributeValue(xpath(:i-1),xpath(i+1:ii-1))
        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
     

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
SUBROUTINE xmlInitInterface()

   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()
64 65 66
   IF(errorStatus.NE.0) THEN
      CALL juDFT_error("Could not initialize XML interface.",calledby="xmlInitInterface")
   END IF
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

END SUBROUTINE xmlInitInterface

SUBROUTINE xmlParseSchema(schemaFilename)

   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)
91
   IF(errorStatus.NE.0) THEN
92
      CALL juDFT_error("XML Schema file not parsable: "//TRIM(ADJUSTL(schemaFilename)),calledby="xmlParseSchema")
93
   END IF
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

END SUBROUTINE xmlParseSchema

SUBROUTINE xmlParseDoc(docFilename)

   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)
118
   IF(errorStatus.NE.0) THEN
119
      CALL juDFT_error("XML document file not parsable: "//TRIM(ADJUSTL(docFilename)),calledby="xmlParseDoc")
120
   END IF
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

END SUBROUTINE xmlParseDoc

SUBROUTINE xmlValidateDoc()

   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()
142
   IF(errorStatus.NE.0) THEN
143
      CALL juDFT_error("XML document cannot be validated against Schema.",calledby="xmlValidateDoc")
144
   END IF
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

END SUBROUTINE xmlValidateDoc

SUBROUTINE xmlInitXPath()

   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()
166 167 168
   IF(errorStatus.NE.0) THEN
      CALL juDFT_error("Could not initialize XPath.",calledby="xmlInitXPath")
   END IF
169
   CALL init_from_command_line()
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
END SUBROUTINE xmlInitXPath

FUNCTION xmlGetNumberOfNodes(xPath)

   USE iso_c_binding
   USE m_types

   IMPLICIT NONE

   INTEGER :: xmlGetNumberOfNodes
   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

   xmlGetNumberOfNodes = getNumberOfXMLNodes(TRIM(ADJUSTL(xPath))//C_NULL_CHAR)

END FUNCTION xmlGetNumberOfNodes

FUNCTION xmlGetAttributeValue(xPath)

   USE iso_c_binding
   USE m_types

   IMPLICIT NONE

   CHARACTER(LEN=255) :: xmlGetAttributeValue

   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

218 219
  

220 221 222 223
   c_string = getXMLAttributeValue(TRIM(ADJUSTL(xPath))//C_NULL_CHAR)

   CALL C_F_POINTER(c_string, valueFromC, [ 255 ])
   IF (.NOT.c_associated(c_string)) THEN
224 225 226
      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")
227
   END IF
228 229 230 231 232 233

   value = ''
   i = 1
   DO WHILE ((valueFromC(i).NE.C_NULL_CHAR).AND.(i.LE.255))
      value(i:i) = valueFromC(i)
      i = i + 1
234
   END DO
235
   length = i-1
236

237
   xmlGetAttributeValue = value(1:length)
238 239 240

END FUNCTION xmlGetAttributeValue

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

SUBROUTINE xmlSetAttributeValue(xPath,VALUE)

   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

 END SUBROUTINE xmlSetAttributeValue

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
SUBROUTINE xmlFreeResources()

   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()
291 292 293 294
   IF(errorStatus.NE.0) THEN
      CALL juDFT_error("Could not free XML resources.",calledby="xmlFreeResources")
      STOP 'Error!'
   END IF
295 296 297 298

END SUBROUTINE xmlFreeResources

END MODULE m_xmlIntWrapFort