Class: ParseDecision::Parser
- Inherits:
-
Object
- Object
- ParseDecision::Parser
- Defined in:
- lib/parse_decision/parser.rb
Overview
The Parser class runs the show. This class is called by the controller object
Instance Method Summary collapse
- #configureForFileMode(fname) ⇒ Object
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
- #noCmdLineArg ⇒ Object
- #parse(srcpath, destpath) ⇒ Object
-
#parseCfg(cfg) ⇒ Object
Parse files based on the configuration.
-
#parseFile(fname) ⇒ Object
Parse an XML decision file.
- #parseFileWithCmdLineArg(arg) ⇒ Object
- #parseFileWithSwitch(arg) ⇒ Object
-
#setOutdir(dir) ⇒ Object
Set directory where generated files are placed.
-
#validateCfg(cfg) ⇒ Object
Validate the configuration.
-
#version ⇒ Object
Return the application’s version string.
Constructor Details
#initialize ⇒ Parser
Returns a new instance of Parser.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/parse_decision/parser.rb', line 26 def initialize() $LOG.debug "Parser::initialize" @cfg = Config.new.load @plugins = [Plugin::Application.new, Plugin::PpmXpath.new, Plugin::PreDecisionGuideline.new, Plugin::ProductXpath.new, Plugin::Product.new, ] @context = PDContext.new end |
Instance Method Details
#configureForFileMode(fname) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/parse_decision/parser.rb', line 132 def configureForFileMode(fname) $LOG.debug "Parser::configureForFileMode( #{fname} )" mode = :default fileTypeFound = false # Open the file and read line by line looking for indications of which mode to use. df = File.open(fname).each do |ln| # Search for 'normal' decision file. if(ln.include?("<PARAMS>")) fileTypeFound = true puts "Decision file type = :default (not webdecision)" if @context.verbose break end # Search for web decision file. if(ln.include?("Next Decision")) fileTypeFound = true mode = :webdecision puts "Decision file type = :webdecision" if @context.verbose break end # Exit file search if mode has been determined. if(true == fileTypeFound) break end end # do file # If the file is a web decision, reset the plugins. if(mode == :webdecision) @context.parseMode = mode @plugins = [Plugin::Application.new, Plugin::WebProduct.new, ] end end |
#noCmdLineArg ⇒ Object
186 187 188 |
# File 'lib/parse_decision/parser.rb', line 186 def noCmdLineArg() $LOG.debug "Parser::noCmdLineArg" end |
#parse(srcpath, destpath) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/parse_decision/parser.rb', line 73 def parse(srcpath, destpath) path = Pathname.new srcpath destpath = Pathname.pwd if destpath.nil? parseCfg({ file: path.basename.to_s, srcdir: path.dirname.to_s, outdir: destpath.to_s }) end |
#parseCfg(cfg) ⇒ Object
Parse files based on the configuration.
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 |
# File 'lib/parse_decision/parser.rb', line 81 def parseCfg(cfg) $LOG.debug "Parser::parseCfg( cfg )" if( !validateCfg(cfg) ) puts "ERROR: Invalid options." return end if( !File.exists?(@context.file) ) @context.file = File.join( @context.srcdir, @context.file ) if( !File.exists?(@context.file) ) puts "ERROR: unable to locate src file: #{@context.file}" return end end if( !File.exists?(@context.outdir) ) FileUtils.mkdir_p( @context.outdir ) puts "Output dir created: #{@context.outdir}" if @context.verbose end parseFile(@context.file) # Copy the decision log to the output dir. FileUtils.cp(@context.file, @context.outdir) end |
#parseFile(fname) ⇒ Object
Parse an XML decision file.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/parse_decision/parser.rb', line 110 def parseFile(fname) $LOG.debug "Parser::parseFile( #{fname} )" puts "Parsing file: #{fname}" if @context.verbose # Determine the mode and configure plugins based on the file data. configureForFileMode(fname) line_count = 1 # Open the file and read line by line df = File.open(fname).each do |ln| puts line_count.to_s if $DEBUG line_count += 1 @plugins.each do |plug| puts " --> #{plug.class}" if $DEBUG break if ( true == plug.execute(@context, ln)) end # plugins.each end # do file puts "Lines parsed: #{line_count}" if @context.verbose end |
#parseFileWithCmdLineArg(arg) ⇒ Object
177 178 179 |
# File 'lib/parse_decision/parser.rb', line 177 def parseFileWithCmdLineArg(arg) $LOG.debug "Parser::parseFileWithCmdLineArg( #{arg} )" end |
#parseFileWithSwitch(arg) ⇒ Object
172 173 174 |
# File 'lib/parse_decision/parser.rb', line 172 def parseFileWithSwitch(arg) $LOG.debug "Parser::parseFileWithSwitch( #{arg} )" end |
#setOutdir(dir) ⇒ Object
Set directory where generated files are placed.
182 183 184 |
# File 'lib/parse_decision/parser.rb', line 182 def setOutdir(dir) @context.outdir = dir end |
#validateCfg(cfg) ⇒ Object
Validate the configuration.
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 |
# File 'lib/parse_decision/parser.rb', line 46 def validateCfg(cfg) if(!(cfg.key?(:file) && (nil != cfg[:file]))) puts "Missing --file option." return false end if(!(cfg.key?(:outdir) && (nil != cfg[:outdir])) ) puts "Missing --outdir option." return false end @context.file = cfg[:file] @context.outdir = cfg[:outdir] if(cfg.key?(:srcdir) && cfg[:srcdir] != nil) @context.srcdir = cfg[:srcdir] end if(cfg.key?(:verbose) && cfg[:verbose] != nil) @context.verbose = cfg[:verbose] end return true end |
#version ⇒ Object
Return the application’s version string.
40 41 42 |
# File 'lib/parse_decision/parser.rb', line 40 def version() return ParseDecision::VERSION end |