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.



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

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

Instance Method Details

#getDocsForAssetsXml(xAssets = nil) ⇒ Object



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

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

#getDocsForAssetsXmlPath(sPath = nil) ⇒ Object



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

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

#getJsondocForXmlAssetNode(oNodeAsset = nil) ⇒ Object



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

def getJsondocForXmlAssetNode(oNodeAsset=nil)
  if oNodeAsset.nil?
    raise RuntimeError, 'E_NIL_NODE'
  end
  oJsondoc = JsonDoc::Document.new
  oJsondoc.bIsStrict = false
  if oNodeAsset.attribute('id')
    sId = oNodeAsset.attribute('id').value
    if sId =~ /^(.+):([0-9]+)$/
      sObjectType = $1
      iObjectId   = $2.to_i
      oJsondoc.setProp(:__id__sObjectDomain,'Versionone')
      oJsondoc.setProp(:__id__sObjectType,sObjectType)
      oJsondoc.setProp(:__id__iObjectId,  iObjectId)
    end
  end
  if oNodeAsset.attribute('href').value
    sUrl = @sUrl \
      ? @sUrl + oNodeAsset.attribute('href').value
      : oNodeAsset.attribute('href').value
    oJsondoc.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 == ''
        oJsondoc.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
            oJsondoc.pushProp(yPropKey,oNodeRelationChild.attribute('idref').value)
          end
        else
          raise RuntimeError, "E_UNKNOWN_RELATION_CHILD_NAME #{oNodeRelationChild.name}"
        end
      end
      if oNodeRelation.children.count == 0
        oJsondoc.setProp(yPropKey,[])
      end
    else
      raise RuntimeError, "E_UNKNOWN_ASSET_NODE_NAME #{oNodeChild.name}"
    end
  end
  return oJsondoc
end