Debian Codegraph @

rdfloader/parser/parser.go

raw symbols SCIP
1 package parser
2
3 import (
4 "fmt"
5 xmlreader "github.com/spdx/gordf/rdfloader/xmlreader"
6 "github.com/spdx/gordf/uri"
7 "strings"
8 "sync"
9 )
10
11 const RDFNS = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
12
13 type Parser struct {
14 setTriples map[string]*Triple
15 setNodes map[string]*Node
16 Triples []*Triple
17 writeLock sync.RWMutex
18 nodesWriteLock sync.RWMutex
19 SchemaDefinition map[string]uri.URIRef
20 blankNodeGetter BlankNodeGetter
21 rdfNS uri.URIRef
22 wg sync.WaitGroup
23 }
24
25 func parseHeaderBlock(rootBlock xmlreader.Block) (map[string]uri.URIRef, error) {
26 // returns all the schema definitions in the root block.
27 // a schema definition is of the form xmlns:SchemaName="URI",
28
29 namespaceURI := map[string]uri.URIRef{}
30
31 // boolean to indicate if we got any uri same as parser.RDFNS
32 anyRDFURI := false
33
34 for _, attr := range rootBlock.OpeningTag.Attrs {
35 if attr.SchemaName == "xmlns" {
36 uriref, err := uri.NewURIRef(attr.Value)
37 if err != nil {
38 return namespaceURI, fmt.Errorf("schema URI %v doesn't confirm to URL rules", attr.Value)
39 }
40 if strings.TrimSuffix(uriref.String(), "#") == strings.TrimSuffix(RDFNS, "#") {
41 anyRDFURI = true
42 }
43 namespaceURI[attr.Name] = uriref
44 } else if attr.SchemaName == "" && attr.Name == "xmlns" {
45 uriref, err := uri.NewURIRef(attr.Value)
46 if err != nil {
47 return namespaceURI, err
48 }
49 namespaceURI[attr.SchemaName] = uriref
50 }
51 }
52
53 // rdfAbbrevPresent: true if user has mapped "rdf" to another uri
54 _, rdfAbbrevPresent := namespaceURI["rdf"]
55 if !anyRDFURI && !rdfAbbrevPresent {
56 rdfURI, _ := uri.NewURIRef(RDFNS)
57 namespaceURI["rdf"] = rdfURI
58 }
59 return namespaceURI, nil
60 }
61
62 func (parser *Parser) getRDFAttributeIndex(tag xmlreader.Tag, attrName string) (index int, err error) {
63 /*
64 From all the attribute of the given tag, return the index of the attribute rdf:attrName
65 */
66 index = -1
67 for i, attr := range tag.Attrs {
68 attrUri, err := parser.uriFromPair(attr.SchemaName, attr.Name)
69 if err != nil {
70 break
71 }
72 if attrUri == parser.rdfNS.AddFragment(attrName) {
73 // current attribute is a rdf:attrName tag,
74 index = i
75 break
76 }
77 }
78 return
79 }
80
81 func getLastURI(tag xmlreader.Tag, lastURI string) string {
82 for _, attr := range tag.Attrs {
83 if attr.SchemaName == "" && attr.Name == "xmlns" {
84 return attr.Value
85 }
86 }
87 return lastURI
88 }
89
90 func New() (parser *Parser) {
91 // creates a new parser object
92 rdfNS, _ := uri.NewURIRef(RDFNS)
93 return &Parser{
94 setTriples: map[string]*Triple{},
95 setNodes: map[string]*Node{},
96 Triples: []*Triple{},
97 writeLock: sync.RWMutex{},
98 nodesWriteLock: sync.RWMutex{},
99 SchemaDefinition: map[string]uri.URIRef{"": uri.URIRef{}},
100 blankNodeGetter: BlankNodeGetter{-1},
101 wg: sync.WaitGroup{},
102 rdfNS: rdfNS,
103 }
104 }
105
106 func (parser *Parser) parseBlock(currBlock *xmlreader.Block, node *Node, lastURI string, errp *error) {
107 /*
108 1. What is a block?
109 Ans: A rdf block is made up of
110 1. Root Node (IRI Ref or BlankNode) :: Subject
111 2. Link (IRI Ref) :: Object
112 3. anotherBlock (Literal or IRI Ref or Blank Node) :: Predicate
113
114 2. Example of a Block.
115 Sample RDF/XML input with non-blank subject and literal predicate.
116 <spdx:License rdf:about="http://spdx.org/licenses/Apache-2.0">
117 <spdx:licenseId>Apache-2.0</spdx:licenseId>
118 </spdx:License>
119 Output Components:
120 Subject: http://spdx.org/licenses/Apache-2.0 (IRI Ref)
121 Object: spdx:licenseId (IRI Ref)
122 Predicate: Apacha-2.0 (Literal)
123 If the rdf:about attribute of the subject is removed, it will become a blank node.
124
125 3. What is a node *Node?
126 Ans: effectively, node representation of the block parameter.
127 node := parser.nodeFromTag(block)
128
129 4. Parameter errp.
130 Pointer to an error variable.
131 used to report errors in a concurrent environment.
132 why pointer? Because go func() cannot return anything.
133 */
134 node = parser.resolveNode(node)
135 defer parser.wg.Done()
136 lastURI = getLastURI(currBlock.OpeningTag, lastURI)
137 if len(currBlock.Children) == 0 {
138 // adding only one triple which identifies the type of the current block.
139 predicateURI := parser.rdfNS.AddFragment("type")
140 openingTagUri, newErr := parser.uriFromPair(currBlock.OpeningTag.SchemaName, currBlock.OpeningTag.Name)
141 if newErr != nil {
142 *errp = newErr
143 return
144 }
145 parser.appendTriple(&Triple{
146 Subject: node,
147 Predicate: &Node{IRI, predicateURI.String()},
148 Object: &Node{IRI, openingTagUri.String()},
149 })
150 return
151 }
152 for _, predicateBlock := range currBlock.Children {
153 // predicateURI can't be a blank node. It has to be a URI Reference
154 // according to https://www.w3.org/TR/rdf-concepts/#dfn-predicate
155 predicateURI, newErr := parser.uriFromPair(predicateBlock.OpeningTag.SchemaName, predicateBlock.OpeningTag.Name)
156 if newErr != nil {
157 *errp = fmt.Errorf("error creating a reference URI link for the predicate block. %v", newErr)
158 return
159 }
160 predicateNode := &Node{NodeType: IRI, ID: predicateURI.String()}
161
162 openingTagUri, newErr := parser.uriFromPair(currBlock.OpeningTag.SchemaName, currBlock.OpeningTag.Name)
163 if newErr != nil {
164 *errp = newErr
165 return
166 }
167
168 // (node) -> rdf:type -> (openingTagURI)
169 predicateURI = parser.rdfNS.AddFragment("type")
170 parser.appendTriple(&Triple{
171 Subject: node,
172 Predicate: &Node{IRI, predicateURI.String()},
173 Object: &Node{IRI, openingTagUri.String()},
174 })
175 if len(predicateBlock.Children) == 0 {
176 // no children.
177 currentTriple := &Triple{
178 Subject: node,
179 Predicate: predicateNode,
180 Object: nil,
181 }
182 resIdx, newErr := parser.getRDFAttributeIndex(predicateBlock.OpeningTag, "resource")
183 *errp = newErr
184 if *errp != nil {
185 return
186 }
187 nodeidIdx, newErr := parser.getRDFAttributeIndex(predicateBlock.OpeningTag, "nodeID")
188 *errp = newErr
189 if *errp != nil {
190 return
191 }
192
193 switch {
194 case resIdx != -1:
195 // rdf:resource attribute is present
196 currentTriple.Object = &Node{
197 NodeType: RESOURCELITERAL,
198 ID: predicateBlock.OpeningTag.Attrs[resIdx].Value,
199 }
200 case nodeidIdx != -1:
201 // we have a reference to another block via rdf:nodeID
202 currentTriple.Object = &Node{
203 NodeType: NODEIDLITERAL,
204 ID: parser.blankNodeGetter.GetFromId(predicateBlock.OpeningTag.Attrs[nodeidIdx].Value).ID,
205 }
206 default:
207 // it is a literal node without any special attributes
208 resIdx, newErr = parser.getRDFAttributeIndex(predicateBlock.OpeningTag, "nodeID")
209 currentTriple.Object = &Node{
210 NodeType: LITERAL,
211 ID: predicateBlock.Value,
212 }
213 }
214
215 // registering a new Triple:
216 parser.appendTriple(currentTriple)
217 }
218
219 // the predicate block has children
220 for _, objectBlock := range predicateBlock.Children {
221 objectNode, newErr := parser.nodeFromTag(objectBlock.OpeningTag, lastURI)
222 if newErr != nil {
223 *errp = newErr
224 return
225 }
226
227 parser.appendTriple(&Triple{
228 Subject: node,
229 Predicate: predicateNode,
230 Object: objectNode,
231 })
232 parser.wg.Add(1)
233 go parser.parseBlock(objectBlock, objectNode, lastURI, errp)
234 if *errp != nil {
235 return
236 }
237 }
238 }
239 }
240
241 func (parser *Parser) Parse(rootBlock xmlreader.Block) (err error) {
242 // set all the schema definitions in the root block.
243 schemaDefinition, err := parseHeaderBlock(rootBlock)
244 if err != nil {
245 return err
246 }
247 parser.SchemaDefinition = schemaDefinition
248
249 // root tag is set now.
250 var childNode *Node
251 xmlns := schemaDefinition[""]
252 xmlnsString := xmlns.String()
253 for _, child := range rootBlock.Children {
254 childNode, err = parser.nodeFromTag(child.OpeningTag, xmlnsString)
255 if err != nil {
256 return err
257 }
258 parser.wg.Add(1)
259 go parser.parseBlock(child, childNode, xmlnsString, &err)
260 if err != nil {
261 return err
262 }
263 }
264 parser.wg.Wait() // wait for all the go routines to finish executing.
265 return err
266 }