1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
package org.onap.aai.graphgraph.reader;
import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.tg.TinkerGraphFactory;
import org.onap.aai.graphgraph.dto.Edge;
import org.springframework.core.io.ClassPathResource;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.*;
import static org.onap.aai.graphgraph.reader.DummySchemaReader.getProperties;
public class Parser {
static Graph g = TinkerGraphFactory.createTinkerGraph();
static int counter = 10000;
private static List<String> nodeNames = new ArrayList<>();
static List <Edge> edges = new LinkedList<>();
public static List<String> getNodes(){
Set<String> hs = new HashSet<>();
hs.addAll(nodeNames);
nodeNames.clear();
nodeNames.addAll(hs);
return nodeNames;
}
public static void parse() throws IOException, SAXException, ParserConfigurationException {
//File fXmlFile = new File("src/main/resources/aai_schema_v11.xsd");
File fXmlFile = new ClassPathResource("aai_schema_v11.xsd").getFile();
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList elements = doc.getElementsByTagName("xs:element");
for (int i = 0; i < elements.getLength(); i++) {
Node node = elements.item(i);
handleNode(node, null);
}
}
private static boolean isType(Node c) {
String type = c.getAttributes().getNamedItem("type").getNodeValue();
return type.equals("xs:string")
|| type.equals("xs:int")
|| type.equals("xs:boolean")
|| type.equals("xs:unsignedInt");
}
static void handleNode(Node c, Node parent){
if (c.getNodeName().equals("xs:complexType") || c.getNodeName().equals("xs:sequence")){
for (int i = 0; i < c.getChildNodes().getLength(); i++)
handleNode(c.getChildNodes().item(i), parent);
return;
}
if(c.getAttributes() == null)
return;
if(c.getAttributes().getNamedItem("name") == null && c.getAttributes().getNamedItem("ref") == null)
return;
if(c.getAttributes().getNamedItem("type") != null)
if(isType(c))
return;
String vertexId = determineVertexId(c);
nodeNames.add(vertexId);
Vertex current;
if (g.getVertex(vertexId) == null) {
current = g.addVertex(vertexId);
}else
current = g.getVertex(vertexId);
NodeList childNodes = getChildren(c);
for (int i = 0; i < childNodes.getLength(); i++)
handleNode(childNodes.item(i), c);
if(parent != null) {
g.addEdge(counter++, g.getVertex(determineVertexId(parent)), current, "parent");
edges.add(new Edge(g.getVertex(determineVertexId(parent)).getId().toString(), current.getId().toString(), "parent", getProperties("edgeshort")));
}
}
private static NodeList getChildren(Node n) {
NodeList childNodes = n.getChildNodes();
return childNodes;
}
private static String determineVertexId(Node n) {
Node nameAttrib = n.getAttributes().getNamedItem("name");
return nameAttrib != null ? nameAttrib.getNodeValue() : n.getAttributes().getNamedItem("ref").getNodeValue().split(":")[1];
}
}
|