Class: RibitData::PageParser

Inherits:
Object
  • Object
show all
Defined in:
lib/ribit/ribitdata.rb

Instance Method Summary collapse

Constructor Details

#initialize(contentDirectory) ⇒ PageParser

Returns a new instance of PageParser.



784
785
786
787
# File 'lib/ribit/ribitdata.rb', line 784

def initialize( contentDirectory )
  @contentDir = contentDirectory
  @logger = RibitLogger.new( PageParser )
end

Instance Method Details

#page_modifiable?(element) ⇒ Boolean

Returns true if page can be modified

Returns:

  • (Boolean)


838
839
840
841
842
843
# File 'lib/ribit/ribitdata.rb', line 838

def page_modifiable?( element )
  dataEle = element.elements['data']

  # page can't be modified if it refers to a file
  return dataEle.attributes['file-ref'] == nil
end

#parse_page(category, fileName) ⇒ Object

Parses an XML file and returns instance of Page



809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
# File 'lib/ribit/ribitdata.rb', line 809

def parse_page( category, fileName )
  @logger.debug( "Parsing page data from file #{fileName}" )
  @file = File.new( fileName )
  begin    
    doc = REXML::Document.new( @file )
    id = File.basename( fileName, ".xml")
    #  looped only once
    doc.elements.each do |pageEle|
      name = getText( pageEle.elements['name'] )
      page = Page.new( id, name, category )
      page.title = getText( pageEle.elements['title'] )
      page.data = getText( pageEle.elements['data'] )
      
      pageEle.elements.each("meta/property" ) do |properties|
        name = properties.attributes["name"]
        value = properties.attributes["value"]
        page.( name, value )
      end
  
      @logger.debug( "Read the page ID=#{page.full_id}, name=#{page.full_name}" )
      return page
    end
  ensure
    @file.close
  end

end

#parse_pages(category) ⇒ Object



790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# File 'lib/ribit/ribitdata.rb', line 790

def parse_pages( category )
  @logger.debug( "Parsing pages for category #{category.name}" )
  categoryDir = File.join( @contentDir, category.id )
  @logger.debug( "Looking *.xml files from #{categoryDir}" )
  pages = Array.new
  # under category dir all those files with <number>.xml are page files
  Dir[File.join( categoryDir, '*.xml')].each do |fileName|
    @logger.debug( "Found #{fileName}" )
    if ( /^[0-9]+\.xml$/.match( File.basename( fileName ) ) )
      # yes, the name matches
      pages.push( parse_page( category, fileName ) )
    end
  
  end
  
  return pages
end