Class: Origami::Parser
- Inherits:
-
Object
- Object
- Origami::Parser
- Defined in:
- lib/origami/parser.rb
Overview
:nodoc:
Direct Known Subclasses
Defined Under Namespace
Classes: ParsingError
Constant Summary collapse
- VERBOSE_QUIET =
Do not output debug information.
0
- VERBOSE_INFO =
Output some useful information.
1
- VERBOSE_DEBUG =
Output debug information.
2
- VERBOSE_INSANE =
Output every objects read
3
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Parser
constructor
:nodoc:.
- #parse(stream) ⇒ Object
-
#parse_object(pos = @data.pos) ⇒ Object
:nodoc:.
-
#parse_trailer(pos = @data.pos) ⇒ Object
:nodoc:.
-
#parse_xreftable(pos = @data.pos) ⇒ Object
:nodoc:.
- #target_data ⇒ Object
- #target_filename ⇒ Object
- #target_filesize ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Parser
:nodoc:
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/origami/parser.rb', line 125 def initialize( = {}) #:nodoc: #Default options values @options = { :verbosity => VERBOSE_INFO, # Verbose level. :ignore_errors => true, # Try to keep on parsing when errors occur. :callback => Proc.new {}, # Callback procedure whenever a structure is read. :logger => STDERR, # Where to output parser messages. :colorize_log => true # Colorize parser output? } @options.update() end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
123 124 125 |
# File 'lib/origami/parser.rb', line 123 def @options end |
Instance Method Details
#parse(stream) ⇒ Object
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 |
# File 'lib/origami/parser.rb', line 140 def parse(stream) data = if stream.respond_to? :read if ''.respond_to? :force_encoding StringScanner.new(stream.read.force_encoding('binary')) # 1.9 compat else StringScanner.new(stream.read) end elsif stream.is_a? ::String @filename = stream if ''.respond_to? :force_encoding StringScanner.new(File.open(stream, "r", :encoding => 'binary').binmode.read) else StringScanner.new(File.open(stream, "r").binmode.read) end elsif stream.is_a? StringScanner stream else raise TypeError end @logger = @options[:logger] @data = data @data.pos = 0 end |
#parse_object(pos = @data.pos) ⇒ Object
:nodoc:
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/origami/parser.rb', line 166 def parse_object(pos = @data.pos) #:nodoc: @data.pos = pos begin obj = Object.parse(@data) return if obj.nil? trace "Read #{obj.type} object#{ if obj.type != obj.real_type " (" + obj.real_type.to_s.split('::').last + ")" end }, #{obj.reference}" @options[:callback].call(obj) obj rescue UnterminatedObjectError => e error e. obj = e.obj Object.skip_until_next_obj(@data) @options[:callback].call(obj) obj rescue Exception => e error "Breaking on: #{(@data.peek(10) + "...").inspect} at offset 0x#{@data.pos.to_s(16)}" error "Last exception: [#{e.class}] #{e.}" if not @options[:ignore_errors] error "Manually fix the file or set :ignore_errors parameter." raise end debug 'Skipping this indirect object.' raise if not Object.skip_until_next_obj(@data) retry end end |
#parse_trailer(pos = @data.pos) ⇒ Object
:nodoc:
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/origami/parser.rb', line 224 def parse_trailer(pos = @data.pos) #:nodoc: @data.pos = pos begin info "...Parsing trailer..." trailer = Trailer.parse(@data) @options[:callback].call(trailer) trailer rescue Exception => e debug "Exception caught while parsing trailer : " + e. warn "Unable to parse trailer!" abort("Manually fix the file or set :ignore_errors parameter.") if not @options[:ignore_errors] raise end end |
#parse_xreftable(pos = @data.pos) ⇒ Object
:nodoc:
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/origami/parser.rb', line 205 def parse_xreftable(pos = @data.pos) #:nodoc: @data.pos = pos begin info "...Parsing xref table..." xreftable = XRef::Section.parse(@data) @options[:callback].call(xreftable) xreftable rescue Exception => e debug "Exception caught while parsing xref table : " + e. warn "Unable to parse xref table! Xrefs might be stored into an XRef stream." @data.pos -= 'trailer'.length unless @data.skip_until(/trailer/).nil? nil end end |
#target_data ⇒ Object
252 253 254 |
# File 'lib/origami/parser.rb', line 252 def target_data @data.string.dup if @data end |
#target_filename ⇒ Object
244 245 246 |
# File 'lib/origami/parser.rb', line 244 def target_filename @filename end |
#target_filesize ⇒ Object
248 249 250 |
# File 'lib/origami/parser.rb', line 248 def target_filesize @data.string.size if @data end |