Debian Codegraph @

rdfloader/xmlreader/readerUtils.go

raw symbols SCIP
1 package rdfloader
2
3 import (
4 "bufio"
5 "os"
6 "unicode"
7 )
8
9 const WHITESPACE uint64 = 1<<'\t' | 1<<'\n' | 1<<'\r' | 1<<' '
10
11 type XMLReader struct {
12 fileReader *bufio.Reader
13 fileObj *os.File
14 }
15
16 /*
17 An attribute is of the form schemaName:tagName="value" which exists inside an opening tag.
18 For example:-
19 If the opening tag is:
20 <rdf:RDF
21 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
22 xmlns:doap="http://usefulinc.com/ns/doap#">
23 Attributes are given by :-
24 1. SchemaName=xmlns, Name=rdf, Value=http://www.w3.org/1999/02/22-rdf-syntax-ns#
25 2. SchemaName=xmlns, Name=doap, Value=http://usefulinc.com/ns/doap#
26 */
27 type Attribute struct {
28 Name string
29 SchemaName string
30 Value string
31 }
32
33 type Pair struct {
34 First interface{}
35 Second interface{}
36 }
37
38 type Tag struct {
39 SchemaName string
40 Name string
41 Attrs []Attribute
42 }
43
44 type Block struct {
45 // A block is a valid sub-xml.
46 // for example:
47 // 1. <tag />
48 // 2. <tag attr="attr" />
49 // 3. <tag> value </tag>
50 // 4. <parent> <child> value </child> </parent>
51 OpeningTag Tag
52 Value string
53 Children []*Block
54 }
55
56 // returns next character in the file without affecting the file pointer
57 func (xmlReader *XMLReader) peekARune() (r rune, err error) {
58 singleByte, err := xmlReader.fileReader.Peek(1)
59 if err != nil {
60 return r, err
61 } else {
62 return rune(singleByte[0]), nil
63 }
64 }
65
66 // returns next character in the file which advances the file pointer.
67 func (xmlReader *XMLReader) readARune() (rune, error) {
68 singleByteArray := make([]byte, 1)
69 _, err := xmlReader.fileReader.Read(singleByteArray)
70 return rune(singleByteArray[0]), err
71 }
72
73 func (xmlReader *XMLReader) readTill(delim uint64) ([]rune, error) {
74 // reads the input file rune by rune till the target rune is found
75 // or eof is reached.
76 // Note: it doesn't include the target rune in the read word.
77 var buffer []rune
78 for {
79 r, err := xmlReader.fileReader.Peek(1)
80 if err == nil {
81 // checking if the read rune is same as any of the delimiters' mask
82 if (delim & (1 << r[0])) != 0 {
83 // current char is same as one of the delimiters.
84 return buffer, nil
85 }
86
87 // moving file pointer one character ahead.
88 xmlReader.readARune()
89
90 // current character is not one of the delimiters.
91 buffer = append(buffer, rune(r[0]))
92 } else {
93 return buffer, err
94 }
95 }
96 }
97
98 func (xmlReader *XMLReader) readTillString(delimiter string) ([]byte, error) {
99 // reads the input file rune by rune till the target rune is found
100 // or eof is reached.
101 // Note: it doesn't include the target rune in the read word.
102 var buffer []byte
103 for {
104 b, err := xmlReader.peekNBytes(len(delimiter))
105 if err != nil {
106 // flush the output when any error occurs
107 return []byte{}, err
108 }
109 // checking if the read rune is same as any of the delimiters' mask
110 if string(b) == delimiter {
111 return buffer, nil
112 }
113
114 // moving file pointer n characters ahead.
115 xmlReader.readARune()
116
117 // current string doesn't match the given delimiter.
118 buffer = append(buffer, b[0])
119 }
120 }
121
122 // read N bytes from the file without affecting the file pointer.
123 func (xmlReader *XMLReader) peekNBytes(n int) ([]byte, error) {
124 return xmlReader.fileReader.Peek(n)
125 }
126
127 // read N bytes from the file advancing the file pointer by N.
128 func (xmlReader *XMLReader) readNBytes(n int) ([]byte, error) {
129 var output []byte
130 for n > 0 {
131 buffer := make([]byte, n)
132 nBytesRead, err := xmlReader.fileReader.Read(buffer)
133 if err != nil {
134 return output, err
135 }
136 n -= nBytesRead
137 output = append(output, buffer[:nBytesRead]...)
138 }
139 return output, nil
140 }
141
142 // advance the file pointer until a non-blank character is found.
143 func (xmlReader *XMLReader) ignoreWhiteSpace() (nWS int, err error) {
144 // nWS: number of whitespaces which were stripped.
145 for {
146 char, err := xmlReader.peekARune()
147 if err != nil || !unicode.IsSpace(char) {
148 return nWS, err
149 }
150 nWS++
151 xmlReader.readARune()
152 }
153 }