lib/openwfe/utils.rb in openwferu-0.9.12 vs lib/openwfe/utils.rb in openwferu-0.9.12.863

- old
+ new

@@ -28,12 +28,10 @@ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. #++ # -# $Id: utils.rb 3454 2006-10-08 16:51:00Z jmettraux $ -# # # "hecho en Costa Rica" and "made in Japan" # # john.mettraux@openwfe.org @@ -54,14 +52,10 @@ return nil if not xmlElt return xmlElt.elements[1] if not elementName - #xmlElt.elements.each do |elt| - # return elt if elt.name == elementName - #end - #return nil xmlElt.elements.detect { |elt| elt.name == elementName } end # # Used in tests, is equivalent to Perl's die() method. @@ -197,18 +191,40 @@ # # Returns an URI if the string is one, else returns nil. # No exception is thrown by this method. # def OpenWFE.parse_uri (string) + begin return URI::parse(string) rescue Exception => e end + nil end # + # Returns a URI instance if the given ref is a http, https, file or ftp + # URI. Returns nil else. + # The sister method parse_uri() is OK with things like mailto:, gopher:, ... + # + def OpenWFE.parse_known_uri (ref) + + uri = OpenWFE::parse_uri(ref.to_s) + + return nil unless uri + + return uri if uri.scheme == "file" + return uri if uri.scheme == "http" + return uri if uri.scheme == "https" + return uri if uri.scheme == "ftp" + # what else ... + + nil + end + + # # Returns true if the given string starts with the 'start' string. # def OpenWFE.starts_with (string, start) # # my favourite way of doing that would be by adding this @@ -376,29 +392,29 @@ return value unless rest return nil unless value - return lookup_attribute(value, rest) + lookup_attribute(value, rest) end # # This method is used within the InFlowWorkItem and the CsvTable classes. # def OpenWFE.has_attribute? (container, key) key, rest = pop_key(key) - if not rest + unless rest return container.has_key?(key) \ if container.respond_to?(:has_key?) return false end - return has_attribute?(rest, key) + has_attribute?(rest, key) end # # Returns a list of lines matching the pattern in the given file. # @@ -446,17 +462,17 @@ protected def pop_key (key) i = key.index(".") return narrow(key), nil unless i - return narrow(key[0..i-1]), key[i+1..-1] + [ narrow(key[0..i-1]), key[i+1..-1] ] end def narrow (key) return 0 if key == "0" i = key.to_i return i if i != 0 - return key + key end end