Class: VersiononeSdk::ParserXmlAssets

Inherits:
Object
  • Object
show all
Defined in:
lib/versionone_sdk/parser_xml_assets.rb

Instance Method Summary collapse

Constructor Details

#initialize(dOptions = {}) ⇒ ParserXmlAssets

Returns a new instance of ParserXmlAssets.



7
8
9
# File 'lib/versionone_sdk/parser_xml_assets.rb', line 7

def initialize(dOptions={})
  @sUrl = dOptions.has_key?(:url) ? dOptions[:url] : ''
end

Instance Method Details

#getDocForAssetXml(xAsset = nil) ⇒ Object



28
29
30
31
32
# File 'lib/versionone_sdk/parser_xml_assets.rb', line 28

def getDocForAssetXml(xAsset=nil)
  oXml   = Nokogiri::XML::Document.parse(xAsset)
  oAsset = self.getJsondocForXmlAssetNode( oXml.root )
  return oAsset
end

#getDocsForAssetsXml(xAssets = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/versionone_sdk/parser_xml_assets.rb', line 16

def getDocsForAssetsXml(xAssets=nil)
  oXml   = Nokogiri::XML::Document.parse(xAssets)
  oBlock = oXml.xpath("//Assets/Asset")
  aDocs  = []
  oBlock.map do |oNodeAsset|
    oAsset = self.getJsondocForXmlAssetNode( oNodeAsset )
    unless oAsset.nil?
      aDocs.push(oAsset)
    end
  end
  return aDocs
end

#getDocsForAssetsXmlPath(sPath = nil) ⇒ Object



10
11
12
13
14
15
# File 'lib/versionone_sdk/parser_xml_assets.rb', line 10

def getDocsForAssetsXmlPath(sPath=nil)
  if File.exists?(sPath)
    return self.getDocsForAssetsXml(IO.read(sPath))
  end
  return []
end

#getJsondocForXmlAssetNode(oNodeAsset = nil) ⇒ Object



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
# File 'lib/versionone_sdk/parser_xml_assets.rb', line 33

def getJsondocForXmlAssetNode(oNodeAsset=nil)
  if oNodeAsset.nil?
    raise RuntimeError, 'E_NIL_NODE'
  end
  oAsset = VersiononeSdk::Asset.new
  oAsset.bIsStrict = false
  if oNodeAsset.attribute('id')
    sId = oNodeAsset.attribute('id').value
    if sId =~ /^(.+):([0-9]+)$/
      sObjectType = $1
      iObjectId   = $2.to_i
      oAsset.setProp(:__id__sObjectDomain,'Versionone')
      oAsset.setProp(:__id__sObjectType,sObjectType)
      oAsset.setProp(:__id__iObjectId,  iObjectId)
    end
  end
  if oNodeAsset.attribute('href').value
    sUrl = @sUrl \
      ? @sUrl + oNodeAsset.attribute('href').value
      : oNodeAsset.attribute('href').value
    oAsset.setProp(:__id__sObjectUrl,sUrl)
  end
  oNodeAsset.children.each do |oNodeChild|
    if oNodeChild.name == 'Attribute'
      if oNodeChild.attribute('name')
        yPropKey = oNodeChild.attribute('name').to_s.to_sym
        sPropVal = oNodeChild.text
        sPropVal = nil if sPropVal == ''
        oAsset.setProp(yPropKey,sPropVal)
      end
    elsif oNodeChild.name == 'Relation'
      yPropKey      = oNodeChild.attribute('name').to_s.to_sym
      oNodeRelation = oNodeChild
      oNodeRelation.children.each do |oNodeRelationChild|
        if oNodeRelationChild.name == 'Asset'
          if oNodeRelationChild.attribute('idref').value
            oAsset.pushProp(yPropKey,oNodeRelationChild.attribute('idref').value)
          end
        else
          raise RuntimeError, "E_UNKNOWN_RELATION_CHILD_NAME #{oNodeRelationChild.name}"
        end
      end
      if oNodeRelation.children.count == 0
        oAsset.setProp(yPropKey,[])
      end
    else
      raise RuntimeError, "E_UNKNOWN_ASSET_NODE_NAME #{oNodeChild.name}"
    end
  end
  oAsset.inflate
  return oAsset
end