W3C home > Mailing lists > Public > uri@w3.org > September 2007

Re: URI Reference creation

From: Sebastian Pipping <webmaster@hartwork.org>
Date: Wed, 05 Sep 2007 04:21:54 +0200
Message-ID: <46DE12C2.6020201@hartwork.org>
To: Mike Brown <mike@skew.org>
CC: uri@w3.org

I felt like starting some pseudocode for reference creation
my own. The code below aims to convert an absolute URI to
relative given a base URI if possible (meaning if scheme
and host are equal to those in the base URI).
Or was there pseudocode already and I forgot about it?

Here is what I currently have:



== Variables ==
Base - [IN] Base to cut off if possible
A    - [IN] Absolute URI that we would like relative instead
T    - [OUT] Transformation result (relative if possible, absolute if necessary)


== Example ==
BASE = http://ex/x/y/z?one
A    = http://ex/r?two
T    = ../../r?two


== Algorithm ==
if (A.scheme != Base.scheme) then
   T.scheme    = A.scheme;
   T.authority = A.authority;
   T.path      = A.path;
   T.query     = A.query;
   T.fragment  = A.fragment;
else
   undef(T.scheme);
   if (A.authority != Base.authority) then
      T.authority = A.authority;
      T.path      = A.path;
      T.query     = A.query;
      T.fragment  = A.fragment;
   else
      undef(last(Base.path));
      T.path = "";
      while (first(A.path) == first(Base.path)) do
         A.path++;
         Base.path++;
      endwhile;
      bool pathNaked = true;
      while defined(first(Base.path)) do
         Base.path++;
         T.path += "../";
         pathNaked = false;
      endwhile;
      while defined(first(A.path)) do
         if pathNaked then
            if (first(A.path) contains ":") then
               T.path += "./";
            endif;
         endif;
         T.path += first(A.path);
         pathNaked = false;
         A.path++;
         if defined(first(A.path)) then
            T.path += + "/";
         endif;
      endwhile;
      T.query    = A.query;
      T.fragment = A.fragment;
   endif;
endif;


I hope it's more useful than braindead, I'm not too awake anymore ;-)



Sebastian
Received on Wednesday, 5 September 2007 02:22:11 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 6 January 2015 21:25:11 UTC