| Class | Origami::Parser |
| In: |
sources/parser/parser.rb
|
| Parent: | Object |
| VERBOSE_QUIET | = | 0 | Do not output debug information. | |
| VERBOSE_INFO | = | 1 | Output some useful information. | |
| VERBOSE_DEBUG | = | 2 | Output debug information. | |
| VERBOSE_INSANE | = | 3 | Output every objects read | |
| PPK | = | Adobe::AddressBook::PPK.new(o.PPK) | ||
| User | = | Adobe::AddressBook::UserList.new(o.PPK.User) | ||
| AddressBook | = | Adobe::AddressBook::AddressList.new(o.PPK.AddressBook) |
| options | [RW] |
Creates a new PDF file Parser.
| options: | A hash of options modifying the parser behavior. |
# File sources/parser/parser.rb, line 123
123: def initialize(options = {})
124:
125: #Default options values
126: @options =
127: {
128: :verbosity => VERBOSE_INFO, # Verbose level.
129: :ignore_errors => true, # Try to keep on parsing when errors occur.
130: :callback => Proc.new {}, # Callback procedure whenever a structure is read.
131: :prompt_password => Proc.new { print "Password: "; gets.chomp } #Callback procedure to prompt password when document is encrypted.
132: }
133:
134: @options.update(options)
135: end
Parse the given file and returns a PDF object, or nil if the parsing process failed.
| filename: | The path to the PDF file to parse. |
# File sources/parser/parser.rb, line 141
141: def parse(filename)
142:
143: # Read PDF file contents
144: begin
145:
146: fd = File.open(filename, "r").binmode
147: stream = StringScanner.new(fd.read)
148:
149: info "...Start parsing file ..."
150:
151: info "...Reading header..."
152:
153: hdr = nil
154: @@file_types.each { |fileType|
155: begin
156: hdr = fileType::Header.parse(stream)
157: break
158: rescue Exception => e
159: next
160: end
161: }
162:
163: case hdr
164: when PDF::Header
165: pdf = PDF.new(false)
166: pdf.header = hdr
167: pdf.filename = filename
168: @options[:callback].call(pdf.header)
169:
170: parse_pdf_file(pdf, stream)
171:
172: info "...End parsing file..."
173: info
174:
175: return pdf
176:
177: when Adobe::AddressBook::Header
178: addrbk = Adobe::AddressBook.new
179: addrbk.header = hdr
180: addrbk.filename = filename
181: @options[:callback].call(addrbk.header)
182:
183: parse_addressbook(addrbk, stream)
184:
185: info "...End parsing file..."
186: info
187:
188: return addrbk
189:
190: else
191: raise InvalidHeader, "No file type was recognized"
192: end
193:
194: rescue SystemExit
195: raise
196:
197: rescue Exception => e
198: error "An error occured while parsing."
199:
200: debug "#{e.message} (#{e.class})"
201: #debug e.backtrace.join("\n")
202: debug
203:
204: raise
205: end
206:
207: end