Debian Codegraph @

rdfloader/parser/parser_utils.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 )
9
10 type Triple struct {
11 Subject, Predicate, Object *Node
12 }
13
14 func (parser *Parser) appendTriple(triple *Triple) {
15 // does what it say.
16 // appends the triples to the parser.
17 // uses a lock for mutex to prevent race condition.
18
19 // writelock is a type of RWMutex.
20 // RW is Readers-Writer Lock. That is, at a time, more than on e readers
21 // can read from the data structure but at a time only one writer can
22 // access the data structure.
23 parser.writeLock.Lock()
24 if _, exists := parser.setTriples[triple.Hash()]; !exists {
25 // append to the map and triples' set if it doesn't already exist in the map.
26 parser.setTriples[triple.Hash()] = triple
27 parser.Triples = append(parser.Triples, triple)
28 }
29 parser.writeLock.Unlock()
30 }
31
32 func (parser *Parser) resolveNode(node *Node) *Node {
33 parser.nodesWriteLock.Lock()
34 defer parser.nodesWriteLock.Unlock()
35 existingNode := parser.setNodes[node.String()]
36 if existingNode != nil {
37 return existingNode
38 } else {
39 parser.setNodes[node.String()] = node
40 }
41 return node
42 }
43
44 func (parser *Parser) uriFromPair(schemaName, name string) (mergedUri uri.URIRef, err error) {
45 // returns the uri representation of a pair of strings.
46 // name:schemaName is an example of pair.
47 // pairs such as rdf:RDF, where, rdf must be a valid xmlns schema name.
48
49 // base must be a valid schema name defined in the root tag.
50 baseURI, ok := parser.SchemaDefinition[schemaName]
51 if !ok {
52 return uri.URIRef{}, fmt.Errorf("undefined schema name: %v", schemaName)
53 }
54
55 // adding the relative fragment to the base uri.
56 return baseURI.AddFragment(name), nil
57 }
58
59 func (parser *Parser) convertRdfIdToRdfAbout(tag xmlreader.Tag) (error) {
60 idx, err := parser.getRDFAttributeIndex(tag, "ID")
61 if err != nil {
62 return err
63 }
64 if idx == -1 {
65 return nil
66 }
67 // we've found a rdf:ID attribute. converting it into rdf:about attribute.
68 // converting rdf:ID="val" to rdf:about="#val"
69 tag.Attrs[idx].Name = "about"
70 tag.Attrs[idx].Value = "#" + tag.Attrs[idx].Value
71 return nil
72 }
73
74 func (parser *Parser) nodeFromTag(openingTag xmlreader.Tag, lastURI string) (node *Node, err error) {
75 // returns the node object from the opening tag of any block.
76 // https://www.w3.org/TR/rdf-syntax-grammar/figure1.png has sample image having 5 nodes.
77 // one of them is a blank node.
78
79 // description of the entire function:
80 // if the opening tag has an attribute of rdf:about,
81 // the node will represented by the value of rdf:about attribute
82 // else, it is a blank node.
83
84 err = parser.convertRdfIdToRdfAbout(openingTag)
85 if err != nil {
86 return nil, err
87 }
88
89 // checking if any of the attributes is a rdf:about attribute
90 index, err := parser.getRDFAttributeIndex(openingTag, "about")
91 if err != nil {
92 return
93 }
94
95 var currentNode Node
96 if index != -1 {
97 // we found a rdf:about tag.
98 currentNode.NodeType = IRI
99 currentNode.ID = openingTag.Attrs[index].Value
100 if strings.HasPrefix(currentNode.ID, "#") {
101 // the predicate uri is a relative uri. it must be resolve using the base lastURI
102 baseURI, err := uri.NewURIRef(lastURI)
103 if err != nil {
104 return nil, err
105 }
106 resolvedURI := baseURI.AddFragment(currentNode.ID)
107 currentNode.ID = resolvedURI.String()
108 }
109 return &currentNode, nil
110 }
111
112 // we don't have rdf:about attribute, returning a new blank node.
113 rdfNodeIDIndex, err := parser.getRDFAttributeIndex(openingTag, "nodeID")
114 if err != nil {
115 return nil, err
116 }
117 if rdfNodeIDIndex == -1 {
118 currentNode = parser.blankNodeGetter.Get()
119 } else {
120 currentNode = parser.blankNodeGetter.GetFromId(openingTag.Attrs[rdfNodeIDIndex].Value)
121 }
122 return &currentNode, nil
123 }
124
125 func (triple *Triple) Hash() string {
126 return fmt.Sprintf("{%v; %v; %v}", triple.Subject, triple.Predicate, triple.Object)
127 }