Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

THtmlAnchor.java

00001 /*
00002 *  Name:      THtmlAnchor.java
00003 *  Author:    Francisco Vides Fernández
00004 *  Summary:   Html anchor class
00005 *  Date:      $Date: 2003/10/02 22:25:06 $
00006 *  Revision:  $Revision: 1.2 $
00007 *
00008 *  Copyright (C) 2001-2003  Francisco Vides Fernandez <pax@dedalo-ing.com>
00009 *
00010 *  This program is free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  This program is distributed in the hope that it will be useful,
00016 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 *  GNU General Public License for more details.
00019 *
00020 *  You should have received a copy of the GNU General Public License
00021 *  along with this program; if not, write to the Free Software
00022 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 *  MA 02111-1307, USA.
00024 */
00025 
00027 package org.mpcl.text.html;
00028 
00029 import java.util.ArrayList;
00030 import java.util.HashMap;
00031 import java.util.Iterator;
00032 import java.util.List;
00033 
00034 
00035 public class THtmlAnchor
00036 {
00037 
00038   public static final int _kiAbsolute = 1;
00039 
00040   public static final int _kiRelative = 2;
00041 
00042   public static final String  _kyHttpPrefix = "http://";
00043 
00045   protected int  iType;
00046 
00048   protected HashMap tHrefValues;
00049 
00054   protected HashMap tHrefMultipleValues;
00055 
00057   protected HashMap tValues;
00058 
00060   protected String  yUrl;
00061 
00063   protected String  yHrefValues;
00064 
00066   protected String  yText;
00067 
00069   protected String  yValues;
00070 
00071 
00072   //
00073   //  C O N S T R U C T O R S
00074   //
00075 
00076   public THtmlAnchor()
00077   {
00078     iType               = _kiRelative;
00079     tHrefMultipleValues = null;
00080     tHrefValues         = new HashMap();
00081     tValues             = new HashMap();
00082     yHrefValues         = null;
00083     yText               = null;;
00084     yUrl                = null;
00085     yValues             = null;
00086   }
00087 
00088   public THtmlAnchor (String yURL)
00089   {
00090     iType               = _kiRelative;
00091     tHrefMultipleValues = null;
00092     tHrefValues         = new HashMap();
00093     tValues             = new HashMap();
00094     yHrefValues         = null;
00095     yText               = yURL;
00096     yValues             = null;
00097     setUrl (yURL);
00098   }
00099 
00100   public THtmlAnchor (String yURL, int iLINK_TYPE)
00101   {
00102 
00103     switch (iLINK_TYPE)
00104     {
00105       case _kiAbsolute:
00106       {
00107         iType = iLINK_TYPE;
00108         break;
00109       }
00110       case _kiRelative:
00111       {
00112         iType = iLINK_TYPE;
00113         break;
00114       }
00115       default:
00116       {
00117         iType = _kiRelative;
00118       }
00119     }
00120     tHrefMultipleValues = null;
00121     tHrefValues         = new HashMap();
00122     tValues             = new HashMap();
00123     yHrefValues         = null;
00124     yText               = yURL;
00125     yValues             = null;
00126     setUrl (yURL);
00127 
00128   }  // THtmlAnchor()
00129 
00134   public void addHrefValue (String yKEY, String yVALUE)
00135   {
00136 
00137     yHrefValues = null;
00138     if ( !tHrefValues.containsKey (yKEY) )
00139     {
00140       if ( ( tHrefMultipleValues == null )            ||
00141            ( !tHrefMultipleValues.containsKey (yKEY) ) )
00142       {
00143         tHrefValues.put (yKEY, yVALUE);
00144       }
00145       else
00146       {
00147         ((List) tHrefMultipleValues.get (yKEY)).add (yVALUE);
00148       }
00149     }
00150     else
00151     {
00152       List   tValues = new ArrayList();
00153 
00154       if ( tHrefMultipleValues == null )
00155       {
00156         tHrefMultipleValues = new HashMap();
00157       }
00158       tValues.add (tHrefValues.get (yKEY));
00159       tValues.add (yVALUE);
00160       tHrefMultipleValues.put (yKEY, tValues);
00161       tHrefValues.remove (yKEY);
00162     }
00163 
00164   }  // addHrefValue()
00165 
00166   public void setContent (String yTEXT)
00167   {
00168     yText = ( yTEXT != null ) ? yTEXT : "";
00169   }
00170 
00174   public void setHrefValue (String yKEY, String yVALUE)
00175   {
00176 
00177     yHrefValues = null;
00178     if ( ( tHrefMultipleValues != null )           &&
00179          ( tHrefMultipleValues.containsKey (yKEY) ) )
00180     {
00181       tHrefMultipleValues.remove (yKEY);
00182       if ( tHrefMultipleValues.size() == 0 )
00183       {
00184         tHrefMultipleValues = null;
00185       }
00186     }
00187     tHrefValues.put (yKEY, yVALUE);
00188       
00189   } // setHrefValue()
00190 
00191   public void setUrl (String yURL)
00192   {
00193 
00194     if ( yURL == null )
00195     {
00196       throw new NullPointerException();
00197     }
00198     else
00199     {
00200       if ( ( iType == _kiAbsolute )                                                &&
00201            ( !_kyHttpPrefix.equals (yURL.substring (0, _kyHttpPrefix.length())) ) )
00202       {
00203         // if there is no http prefix, we preppend it
00204         yUrl = _kyHttpPrefix + yURL;
00205       }
00206       else
00207       {
00208         yUrl = yURL;
00209       }
00210     }
00211 
00212   }  // setUrl()
00213 
00214   public void setValue (String yKEY, String yVALUE)
00215   {
00216     yValues = null;
00217     tValues.put (yKEY, yVALUE);
00218   }
00219 
00220 
00221   //
00222   //  S E L E C T O R S
00223   //
00224   
00225   protected String getHrefValues()
00226   {
00227 
00228     String    yKEY;
00229     boolean   gFirst = true;
00230     String    yRet   = "";
00231     String    ySeparator = "?";
00232     
00233     if ( yHrefValues == null )
00234     {
00235       Iterator   I = tHrefValues.keySet().iterator();
00236       yHrefValues = "";
00237       while ( I.hasNext() )
00238       {
00239         yKEY = (String) I.next();
00240         yHrefValues += ySeparator + yKEY + "=" + (String) tHrefValues.get (yKEY);
00241         if ( gFirst )
00242         {
00243           ySeparator = "&";
00244           gFirst = false;
00245         }
00246       }
00247       if ( tHrefMultipleValues != null )
00248       {
00249         Iterator    J;
00250         String      yValue;
00251         I = tHrefMultipleValues.keySet().iterator();
00252         while ( I.hasNext() )
00253         {
00254           yKEY = (String) I.next();
00255           J    = ((List) tHrefMultipleValues.get (yKEY)).iterator();
00256           while ( J.hasNext() )
00257           {
00258             yValue = (String) J.next();
00259             yHrefValues += ySeparator + yKEY + "=" + yValue;
00260             if ( gFirst )
00261             {
00262               ySeparator = "&";
00263               gFirst     = false;
00264             }
00265           }
00266         }
00267       }
00268     }
00269     yRet = yHrefValues;
00270     return yRet;
00271 
00272   }  // getSimpleHrefValues()
00273 
00274   protected String getValues()
00275   {
00276 
00277     String    yKey;
00278     boolean   gFirst     = true;
00279     String    yRet       = "";
00280     String    ySeparator = "";
00281     
00282     if ( yValues == null )
00283     {
00284       Iterator   I = tValues.keySet().iterator();
00285 
00286       yValues = "";
00287       while ( I.hasNext() )
00288       {
00289         yKey = (String) I.next();
00290         yValues += ySeparator + yKey + "=\"" + (String) tValues.get (yKey) + "\"";
00291         if ( gFirst )
00292         {
00293           ySeparator = " ";
00294           gFirst = false;
00295         }
00296       }
00297     }
00298     yRet = yValues;
00299     return yRet;
00300 
00301   }  // getValues()
00302 
00303   public static String getMailTo (String yMAIL_ADDRESS)
00304   {
00305     return "<a href=\"mailto:" + yMAIL_ADDRESS + "\">" + yMAIL_ADDRESS + "</a>";
00306   }
00307 
00308   public String getValue (String yKEY)
00309   {
00310     return (String) tValues.get (yKEY);
00311   }
00312 
00313   public String toHtml()
00314   {
00315     String   yRet = "<a ";
00316     
00317     if ( yUrl != null )
00318     {
00319       yRet += "href=\"" + yUrl + getHrefValues() + "\" ";
00320     }
00321     yRet += getValues() + ">" + yText + "</a>";
00322     return yRet;
00323   }
00324 
00325 }  // class THtmlAnchor

Generated on Mon Oct 13 02:35:24 2003 for MPCL by doxygen1.2.18