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_TRACE =
Output every objects read
3
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#defer_type_cast(reference, type) ⇒ Object
:nodoc:.
-
#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:.
- #pos ⇒ Object
- #pos=(offset) ⇒ Object
- #target_data ⇒ Object
- #target_filename ⇒ Object
- #target_filesize ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Parser
:nodoc:
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/origami/parser.rb', line 53 def initialize( = {}) #:nodoc: # Type information for indirect objects. @deferred_casts = {} #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() @logger = @options[:logger] @data = nil ::String.disable_colorization(false) if @options[:colorize_log] end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
51 52 53 |
# File 'lib/origami/parser.rb', line 51 def @options end |
Instance Method Details
#defer_type_cast(reference, type) ⇒ Object
:nodoc:
190 191 192 |
# File 'lib/origami/parser.rb', line 190 def defer_type_cast(reference, type) #:nodoc: @deferred_casts[reference] = type end |
#parse(stream) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/origami/parser.rb', line 86 def parse(stream) data = if stream.respond_to? :read StringScanner.new(stream.read.force_encoding('binary')) elsif stream.is_a? ::String @filename = stream StringScanner.new(File.binread(@filename)) elsif stream.is_a? StringScanner stream else raise TypeError end @data = data @data.pos = 0 end |
#parse_object(pos = @data.pos) ⇒ Object
:nodoc:
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/origami/parser.rb', line 103 def parse_object(pos = @data.pos) #:nodoc: @data.pos = pos begin obj = Object.parse(@data, self) return if obj.nil? if Origami::OPTIONS[:enable_type_propagation] and @deferred_casts.key?(obj.reference) types = @deferred_casts[obj.reference] types = [ types ] unless types.is_a?(::Array) # Promote object if a compatible type is found. if cast_type = types.find{|type| type < obj.class} obj = obj.cast_to(cast_type, self) end end trace "Read #{obj.type} object#{ if obj.class != obj.native_type " (" + obj.native_type.to_s.split('::').last + ")" end }, #{obj.reference}" @options[:callback].call(obj) obj rescue UnterminatedObjectError error $!. obj = $!.obj Object.skip_until_next_obj(@data) @options[:callback].call(obj) obj rescue error "Breaking on: #{(@data.peek(10) + "...").inspect} at offset 0x#{@data.pos.to_s(16)}" error "Last exception: [#{$!.class}] #{$!.}" 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:
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/origami/parser.rb', line 172 def parse_trailer(pos = @data.pos) #:nodoc: @data.pos = pos begin info "...Parsing trailer..." trailer = Trailer.parse(@data, self) @options[:callback].call(trailer) trailer rescue debug "Exception caught while parsing trailer : " + $!. warn "Unable to parse trailer!" raise end end |
#parse_xreftable(pos = @data.pos) ⇒ Object
:nodoc:
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/origami/parser.rb', line 152 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 debug "Exception caught while parsing xref table : " + $!. 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 |
#pos ⇒ Object
74 75 76 77 78 |
# File 'lib/origami/parser.rb', line 74 def pos raise RuntimeError, "Cannot get position, parser has no loaded data." if @data.nil? @data.pos end |
#pos=(offset) ⇒ Object
80 81 82 83 84 |
# File 'lib/origami/parser.rb', line 80 def pos=(offset) raise RuntimeError, "Cannot set position, parser has no loaded data." if @data.nil? @data.pos = offset end |
#target_data ⇒ Object
202 203 204 |
# File 'lib/origami/parser.rb', line 202 def target_data @data.string.dup if @data end |
#target_filename ⇒ Object
194 195 196 |
# File 'lib/origami/parser.rb', line 194 def target_filename @filename end |
#target_filesize ⇒ Object
198 199 200 |
# File 'lib/origami/parser.rb', line 198 def target_filesize @data.string.size if @data end |