1
|
|
|
require 'jsondoc' |
2
|
|
|
require 'nokogiri' |
3
|
|
|
require 'versionone_sdk/asset' |
4
|
|
|
|
5
|
|
|
module VersiononeSdk |
6
|
|
|
class ParserXmlAssets |
7
|
|
|
def initialize(dOptions={}) |
8
|
|
|
@sUrl = dOptions.key?(:url) ? dOptions[:url] : '' |
9
|
|
|
end |
10
|
|
|
def getDocsForAssetsXmlPath(sPath=nil) |
11
|
|
|
if File.exists?(sPath) |
12
|
|
|
return self.getDocsForAssetsXml(IO.read(sPath)) |
13
|
|
|
end |
14
|
|
|
return [] |
15
|
|
View Code Duplication |
end |
|
|
|
|
16
|
|
|
def getDocsForAssetsXml(xAssets=nil) |
17
|
|
|
oXml = Nokogiri::XML::Document.parse(xAssets) |
18
|
|
|
oBlock = oXml.xpath("//Assets/Asset") |
19
|
|
|
aDocs = [] |
20
|
|
|
oBlock.map do |oNodeAsset| |
21
|
|
|
oAsset = self.getJsondocForXmlAssetNode( oNodeAsset ) |
22
|
|
|
unless oAsset.nil? |
23
|
|
|
aDocs.push(oAsset) |
24
|
|
|
end |
25
|
|
|
end |
26
|
|
|
return aDocs |
27
|
|
View Code Duplication |
end |
|
|
|
|
28
|
|
|
def getDocForAssetXml(xAsset=nil) |
29
|
|
|
oXml = Nokogiri::XML::Document.parse(xAsset) |
30
|
|
|
oAsset = self.getJsondocForXmlAssetNode( oXml.root ) |
31
|
|
|
return oAsset |
32
|
|
|
end |
33
|
|
|
def getJsondocForXmlAssetNode(oNodeAsset=nil) |
34
|
|
|
if oNodeAsset.nil? |
35
|
|
|
raise RuntimeError, 'E_NIL_NODE' |
36
|
|
|
end |
37
|
|
|
oAsset = VersiononeSdk::Asset.new |
38
|
|
|
oAsset.bIsStrict = false |
39
|
|
|
sOidtokSrc = nil |
40
|
|
|
sObjectType = nil |
41
|
|
|
iObjectId = nil |
42
|
|
|
if oNodeAsset.attribute('id') |
43
|
|
|
sOidtokSrc = oNodeAsset.attribute('id').value |
44
|
|
|
if sOidtokSrc =~ /^(.+):([0-9]+)$/ |
45
|
|
|
sObjectType = $1 |
46
|
|
|
iObjectId = $2.to_i |
47
|
|
|
oAsset.setProp(:_sObjectDomain__id,'Versionone') |
48
|
|
|
oAsset.setProp(:_sObjectType__id,sObjectType) |
49
|
|
|
oAsset.setProp(:_iObjectId__id, iObjectId) |
50
|
|
|
end |
51
|
|
|
end |
52
|
|
|
if oNodeAsset.attribute('href').value |
53
|
|
|
sUrl = @sUrl \ |
54
|
|
|
? @sUrl + oNodeAsset.attribute('href').value |
55
|
|
|
: oNodeAsset.attribute('href').value |
56
|
|
|
oAsset.setProp(:_sObjectUrl__id,sUrl) |
57
|
|
|
end |
58
|
|
|
oNodeAsset.children.each do |oNodeChild| |
59
|
|
|
if oNodeChild.name == 'Attribute' || oNodeChild.name == 'Relation' |
60
|
|
|
if oNodeChild.attribute('name') |
61
|
|
|
yPropKey = oNodeChild.attribute('name').to_s.to_sym |
62
|
|
|
xxPropVal = getAssetNodeChildPropVal( oNodeChild ) |
63
|
|
|
oAsset.setProp(yPropKey,xxPropVal) |
64
|
|
|
end |
65
|
|
|
elsif oNodeChild.name == 'Message' && oNodeChild.text == 'Not Found' |
66
|
|
|
raise RuntimeError, "E_ASSET_NOT_FOUND #{oNodeAsset.attribute('href').value}" |
67
|
|
|
else |
68
|
|
|
raise RuntimeError, "E_UNKNOWN_ASSET_NODE_NAME #{oNodeChild.name}" |
69
|
|
|
end |
70
|
|
|
end |
71
|
|
|
oAsset.inflate |
72
|
|
|
return oAsset |
73
|
|
|
end |
74
|
|
|
|
75
|
|
|
def getAssetNodeChildPropVal(oNodeChild=nil) |
76
|
|
|
xxPropVal = nil |
77
|
|
|
aPropVals = [] |
78
|
|
|
if oNodeChild.children.length > 0 |
79
|
|
|
oNodeChild.children.each do |oNodeChildGrand| |
80
|
|
|
dAttributes = oNodeChildGrand.attributes |
81
|
|
|
xxPropVal = dAttributes.key?('idref') \ |
82
|
|
|
? oNodeChildGrand.attribute('idref').value \ |
83
|
|
|
: oNodeChildGrand.text |
84
|
|
|
xxPropVal = nil if xxPropVal == '' |
85
|
|
|
aPropVals.push(xxPropVal) |
86
|
|
|
end |
87
|
|
|
if aPropVals.length > 1 |
88
|
|
|
xxPropVal = aPropVals |
89
|
|
|
elsif aPropVals.length == 1 |
90
|
|
|
xxPropVal == aPropVals[0] |
91
|
|
|
else |
92
|
|
|
xxPropVal = nil |
93
|
|
|
end |
94
|
|
|
else |
95
|
|
|
xxPropVal = oNodeChild.text |
96
|
|
|
end |
97
|
|
|
xxPropVal = nil if xxPropVal == '' |
98
|
|
|
return xxPropVal |
99
|
|
|
end |
100
|
|
|
end |
101
|
|
|
end |
102
|
|
|
|