Re: URI Reference creation

Updated version with domainRootMode (see example below)
and empty path segment (e.g. abc//def) fix.


== 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/x/r?two

=== domainRootMode ===
T    = /x/r?two

=== not domainRootMode ===
T    = ../r?two


== Algorithm v0.2 ==
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
      if domainRootMode then
         undef(T.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 ":")
                     or (first(A.path) == "") 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;
endif;


Feedback is highly appreciated.



Sebastian

Received on Saturday, 8 September 2007 19:31:21 UTC