Skip to content

vtk_fortran_xml_scanner ​

XML scanner: index the elements of an XML file without loading it.

The scanner reads the file once, in chunks, and records for each element its name, attributes, position in the hierarchy and the positions (in bytes, from 1) of its content in the file; the content itself is never stored, so the memory used does not depend on the data held in the file. The content is read afterwards, on demand, from the recorded positions. The scan can stop at the start tag of a given element (e.g. AppendedData of VTK files, whose content is raw binary).

The module depends only on PENF: it is meant to be moved into FoXy.

Supported XML: elements, attributes (single or double quoted, with the predefined entities), comments, processing instructions and declarations (skipped). CDATA sections are not supported.

Source: src/lib/vtk_fortran_xml_scanner.f90

Contents ​

Variables ​

NameTypeAttributesDescription
chunk_sizeinteger(kind=I8P)parameterSize of the chunks read from the file.

Derived Types ​

xml_attribute ​

Attribute of an XML element: name and (entities decoded) value.

Components ​

NameTypeAttributesDescription
namecharacter(len=:)allocatableAttribute name.
valuecharacter(len=:)allocatableAttribute value.

xml_element ​

Element of an XML file: name, attributes, hierarchy and positions of its content in the file.

The content of an element spans content_start:content_end; its text, the part before the first child element (where VTK puts the data of a DataArray), spans content_start:text_end. Both are empty for a self closing element.

Components ​

NameTypeAttributesDescription
namecharacter(len=:)allocatableElement name.
attributetype(xml_attribute)allocatableAttributes.
attributes_numberinteger(kind=I4P)Number of attributes.
parentinteger(kind=I4P)Index of the parent element (0 for the root).
levelinteger(kind=I4P)Depth in the hierarchy (1 for the root).
childinteger(kind=I4P)allocatableIndexes of the children elements.
children_numberinteger(kind=I4P)Number of children elements.
content_startinteger(kind=I8P)First byte of the content (after the start tag).
text_endinteger(kind=I8P)Last byte of the text (before the first child or end tag).
content_endinteger(kind=I8P)Last byte of the content (before the end tag).
is_self_closinglogicalThe element is self closing, ``.

Type-Bound Procedures ​

NameAttributesDescription
get_attributepass(self)Return the value of an attribute.
has_attributepass(self)Return .true. if an attribute is present.

xml_scanner ​

Index of the elements of an XML file.

Components ​

NameTypeAttributesDescription
elementtype(xml_element)allocatableElements, in the order of their start tags.
elements_numberinteger(kind=I4P)Number of elements.
is_stoppedlogicalThe scan stopped at the start tag of the stop_at element.

Type-Bound Procedures ​

NameAttributesDescription
find_childpass(self)Return the index of the n-th child element with a given name.
freepass(self)Free the index.
scanpass(self)Scan an XML file.
add_elementpass(self)Add an element.

stream_buffer ​

Buffered reader of a file opened for stream access.

Components ​

NameTypeAttributesDescription
unitinteger(kind=I4P)File unit.
file_sizeinteger(kind=I8P)File size in bytes.
firstinteger(kind=I8P)Position in the file of the first character of the buffer.
lengthinteger(kind=I8P)Number of valid characters in the buffer.
charscharacter(len=:)allocatableBuffer.

Type-Bound Procedures ​

NameAttributesDescription
char_atpass(self)Return the character at a position of the file.
findpass(self)Return the position of the next occurrence of a character.
loadpass(self)Load the chunk starting at a position of the file.

Subroutines ​

get_attribute ​

Return the value of an attribute (empty if the attribute is not present).

fortran
subroutine get_attribute(self, name, value, is_found)

Arguments

NameTypeIntentAttributesDescription
selfclass(xml_element)inXML element.
namecharacter(len=*)inAttribute name.
valuecharacter(len=:)outallocatableAttribute value.
is_foundlogicaloutoptionalThe attribute is present.

Call graph

free ​

Free the index.

Attributes: elemental

fortran
subroutine free(self)

Arguments

NameTypeIntentAttributesDescription
selfclass(xml_scanner)inoutXML scanner.

scan ​

Scan an XML file, indexing its elements.

The error is 0 on success, 1 if the file cannot be read, 2 if the XML is malformed (unterminated tag, comment or attribute value, end tag not matching the start tag, unclosed element).

fortran
subroutine scan(self, filename, error, stop_at)

Arguments

NameTypeIntentAttributesDescription
selfclass(xml_scanner)inoutXML scanner.
filenamecharacter(len=*)inFile name.
errorinteger(kind=I4P)outError status.
stop_atcharacter(len=*)inoptionalStop the scan after the start tag of the element with this name.

Call graph

add_element ​

Add an element, parsing its start tag (the text between '<' and '>').

fortran
subroutine add_element(self, tag, id, iostat)

Arguments

NameTypeIntentAttributesDescription
selfclass(xml_scanner)inoutXML scanner.
tagcharacter(len=*)inStart tag text.
idinteger(kind=I4P)outIndex of the new element.
iostatinteger(kind=I4P)outStatus: non-zero if the tag is malformed.

Call graph

load ​

Load the chunk starting at a position of the file.

fortran
subroutine load(self, pos)

Arguments

NameTypeIntentAttributesDescription
selfclass(stream_buffer)inoutFile buffer.
posinteger(kind=I8P)inPosition in the file.

Call graph

read_tag ​

Read the text of a tag starting at '<', up to the '>' closing it (outside quoted attribute values).

fortran
subroutine read_tag(buffer, lt, tag, gt)

Arguments

NameTypeIntentAttributesDescription
buffertype(stream_buffer)inoutFile buffer.
ltinteger(kind=I8P)inPosition of '<'.
tagcharacter(len=:)outallocatableText of the tag, between ''.
gtinteger(kind=I8P)outPosition of '>', 0 if the tag is not terminated.

Call graph

add_attribute ​

Add an attribute to an element.

fortran
subroutine add_attribute(element, name, value)

Arguments

NameTypeIntentAttributesDescription
elementtype(xml_element)inoutXML element.
namecharacter(len=*)inAttribute name.
valuecharacter(len=*)inAttribute value.

Call graph

add_child ​

Add a child index to an element.

fortran
subroutine add_child(element, child)

Arguments

NameTypeIntentAttributesDescription
elementtype(xml_element)inoutXML element.
childinteger(kind=I4P)inIndex of the child.

Call graph

Functions ​

has_attribute ​

Return .true. if an attribute is present.

Attributes: pure

Returns: logical

fortran
function has_attribute(self, name) result(is_present)

Arguments

NameTypeIntentAttributesDescription
selfclass(xml_element)inXML element.
namecharacter(len=*)inAttribute name.

find_child ​

Return the index of the n-th (default first) child element with a given name, 0 if there is none.

With parent=0 the root elements are searched.

Attributes: pure

Returns: integer(kind=I4P)

fortran
function find_child(self, parent, name, n) result(id)

Arguments

NameTypeIntentAttributesDescription
selfclass(xml_scanner)inXML scanner.
parentinteger(kind=I4P)inIndex of the parent element, 0 for the root elements.
namecharacter(len=*)inElement name.
ninteger(kind=I4P)inoptionalOccurrence of the element among the children with that name.

Call graph

char_at ​

Return the character at a position of the file (a blank beyond its end).

Returns: character(len=1)

fortran
function char_at(self, pos) result(c)

Arguments

NameTypeIntentAttributesDescription
selfclass(stream_buffer)inoutFile buffer.
posinteger(kind=I8P)inPosition in the file.

Call graph

find ​

Return the position of the next occurrence of a character from a position of the file, 0 if there is none.

Returns: integer(kind=I8P)

fortran
function find(self, c, pos) result(found)

Arguments

NameTypeIntentAttributesDescription
selfclass(stream_buffer)inoutFile buffer.
ccharacter(len=1)inCharacter searched.
posinteger(kind=I8P)inPosition in the file where the search starts.

Call graph

decode_entities ​

Decode the predefined XML entities of a string (< > & " ').

Attributes: pure

Returns: character(len=:)

fortran
function decode_entities(string) result(decoded)

Arguments

NameTypeIntentAttributesDescription
stringcharacter(len=*)inString.

Call graph

is_space ​

Return .true. if a character is XML white space (blank, tab, line feed, carriage return).

Attributes: elemental

Returns: logical

fortran
function is_space(c) result(is_blank)

Arguments

NameTypeIntentAttributesDescription
ccharacter(len=1)inCharacter.

Call graph

skip_spaces ​

Return the position of the first character not white space from a position of a string (len+1 if none).

Attributes: pure

Returns: integer(kind=I4P)

fortran
function skip_spaces(string, c) result(next)

Arguments

NameTypeIntentAttributesDescription
stringcharacter(len=*)inString.
cinteger(kind=I4P)inStart position.

Call graph