Module: XML::Smart

Defined in:
lib/xml/smart.rb,
lib/xml/smart_dom.rb,
lib/xml/smart_qname.rb,
lib/xml/smart_domtext.rb,
lib/xml/smart_domother.rb,
lib/xml/smart_domelement.rb,
lib/xml/smart_domnodeset.rb,
lib/xml/smart_domattribute.rb,
lib/xml/smart_domnamespace.rb,
lib/xml/smart_domattributeset.rb,
lib/xml/smart_domnamespaceset.rb,
lib/xml/smart_processinginstruction.rb

Defined Under Namespace

Classes: Dom, Error, ProcessingInstruction, QName

Constant Summary collapse

VERSION =
LIBXML_VERSION =
MUTEX =
Mutex.new
COPY =
0
MOVE =
1

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.modify(name, default = nil, &block) ⇒ Object

Raises:



157
158
159
160
161
162
163
164
165
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
# File 'lib/xml/smart.rb', line 157

def self::modify(name,default=nil,&block)
  raise Error, 'first parameter has to be a filename or filehandle' unless name.is_a?(String) || name.is_a?(IO) || name.is_a?(Tempfile)
  raise Error, 'a block is mandatory' unless block_given?
  dom = io = nil
  begin
    if name.is_a?(String) && File.exists?(name)
      MUTEX.synchronize do
        io = ::Kernel::open(name,'r+')
        io.flock(File::LOCK_EX)
      end  
      dom = Dom.new Nokogiri::XML::parse(io){|config| config.noblanks.noent.nsclean.strict }, name
      io.rewind
    elsif name.is_a?(String) && !File.exists?(name)
      MUTEX.synchronize do
        io = ::Kernel::open(name,'w')
        io.flock(File::LOCK_EX)
      end  
      dom = Smart::string(default,name)
    elsif name.is_a?(IO) || name.is_a?(Tempfile)
      MUTEX.synchronize do
        io = name
        io.flock(File::LOCK_EX)
      end
      dom = Dom.new Nokogiri::XML::parse(io){|config| config.noblanks.noent.nsclean.strict }
      io.rewind
    end
    block.call(dom)
    dom.save_as(io)
  rescue => e
    puts e.message
    raise Error, "could not open #{name}"
  ensure
    if io
      io.flush
      io.truncate(io.pos)
      io.flock(File::LOCK_UN)
      io.close if name.is_a?(String)
    end
  end
  nil
end

.open(name, default = nil) ⇒ Object

Raises:



199
200
201
202
203
204
205
206
207
208
209
# File 'lib/xml/smart.rb', line 199

def self::open(name,default=nil)
  raise Error, 'first parameter has to be a filename or filehandle' unless name.is_a?(String) || name.is_a?(IO) || name.is_a?(Tempfile)
  raise Error, 'second parameter has to be an xml string' unless default.is_a?(String) || default.nil?
  dom = Smart::open_unprotected(name,default,true)
  if dom && block_given?
    yield dom
    nil
  else  
    dom
  end
end

.open_unprotected(name, default = nil, lock = false) ⇒ Object

Raises:



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/xml/smart.rb', line 211

def self::open_unprotected(name,default=nil,lock=false)
  raise Error, 'first parameter has to be a filename or filehandle' unless name.is_a?(String) || name.is_a?(IO) || name.is_a?(Tempfile)
  raise Error, 'second parameter has to be an xml string' unless default.is_a?(String) || default.nil?
  dom = begin
    filename = nil
    io = if name.is_a?(String) 
      filename = name
      ::Kernel::open(name) 
    else
      filename = name.path
      name
    end
    begin
      io.flock(File::LOCK_EX) if lock
      Dom.new Nokogiri::XML::parse(io){|config| config.noblanks.noent.nsclean.strict }, filename
    ensure
      io.flock(File::LOCK_UN)
    end
  rescue => e
    if default.nil?
      puts e.message
      raise Error, "could not open #{name}"
    else
      Smart::string(default)
    end
  end
  if block_given?
    yield dom
    nil
  else  
    dom
  end
end

.string(str, basepath = nil) ⇒ Object

Raises:



245
246
247
248
249
250
251
252
253
254
# File 'lib/xml/smart.rb', line 245

def self::string(str,basepath=nil)
  raise Error, 'first parameter has to be stringable (:to_s)' unless str.is_a?(String)
  dom = Dom.new Nokogiri::XML::parse(str.to_s){|config| config.noblanks.noent.nsclean.strict }, basepath
  if block_given?
    yield dom
    nil
  else  
    dom
  end
end

Instance Method Details

#initialize(name, default = nil) ⇒ Object



155
# File 'lib/xml/smart.rb', line 155

def initialize(name,default=nil); open(name,default); end