gHtmlHAttr.cpp

Go to the documentation of this file.
00001 #include "gHtmlHAttr.h"
00002 #include "gHtmlHATypes.h"
00003 #include "gHtmlCtrl.h"
00004 ////////////////////////////////////////////////////////////
00005 gHAttrValue::gHAttrValue (char* strL, gHAType* pValue)
00006     : gString( strL ),
00007       valuePtr( pValue )
00008 {
00009 }
00010 
00011 gHAttrValue::~gHAttrValue ()
00012 {
00013  delete valuePtr;
00014 }
00015 
00016 gHAType& gHAttrValue::GetValue ()
00017 {
00018  ASSERTION(valuePtr!=nil,"valuePtr!=nil");
00019  return *valuePtr;
00020 }
00021 
00022 gHAType* gHAttrValue::GetValuePtr ()
00023 {
00024  return valuePtr;
00025 }
00026 ////////////////////////////////////////////////////////////
00027 gHAttrs::gHAttrs ()
00028 {
00029 }
00030 
00031 gHAttrs::~gHAttrs ()
00032 {
00033  Reset();
00034 }
00035 
00036 bool gHAttrs::IsOk ()
00037 {
00038  // Returns true if all attributes contained are o.k.
00039  unsigned i, n;
00040  for (i=1, n=N(); i<=n; i++) {
00041      gStorage* pObjx = GetObjectPtr( i );
00042      ASSERTION(pObjx!=nil,"pObjx!=nil");
00043      if ( pObjx->IsOk()==false ) return false;
00044  }
00045  // Note for the code above:
00046  // Since usually BuildFromLRLists is done with addOnError=false, no bad attributes exist.
00047  return gList::IsOk();
00048 }
00049 
00050 gHAttrValue& gHAttrs::GetAttrValue (unsigned idx)
00051 {
00052  gHAttrValue* aLR = GetAttrValuePtr( idx );
00053  ASSERTION(aLR!=nil,"aLR!=nil");
00054  return *aLR;
00055 }
00056 
00057 gHAttrValue* gHAttrs::GetAttrValuePtr (unsigned idx)
00058 {
00059  gHAttrValue* aLR = (gHAttrValue*)GetObjectPtr( idx );
00060  return aLR;
00061 }
00062 
00063 gHAType& gHAttrs::GetAttr (unsigned idx)
00064 {
00065  gHAType* pValue = GetAttrPtr( idx );
00066  ASSERTION(pValue!=nil,"pValue!=nil");
00067  return *pValue;
00068 }
00069 
00070 gHAType* gHAttrs::GetAttrPtr (unsigned idx)
00071 {
00072  if ( idx<1 || idx>N() ) return nil;
00073  // Note: indexes are also validated by gList::GetObjectPtr.
00074  gStorage* pObx = GetObjectPtr( idx );
00075  if ( pObx==nil ) return nil;
00076  gHAType* pValue = ((gHAttrValue*)pObx)->GetValuePtr();
00077  ASSERTION(pValue!=nil,"pValue!=nil");
00078  return pValue;
00079 }
00080 
00081 sHAttrType* gHAttrs::TypeName2HAttrType (char* strType)
00082 {
00083  bool isBasicType;  // Those at lHAttrTypes with typeFamily=1
00084  t_int16 idxType, typeFamily, nTypes;
00085  t_uchar lastChr;
00086  gHtmlParser* pParser = gHtmlCtrl::Self().ParserPtr();
00087  sHAttrType* pAttrType;
00088 
00089  if ( strType==nil || strType[0]==0 ) return nil;
00090 
00091  isBasicType = strType[0]!='%' && strType[0]!='(';
00092  strType += (isBasicType==false);  // Skip '%'or'('
00093 
00094  gString s( strType );
00095  if ( (lastChr = s[ s.Length() ])==';' || lastChr==')' ) s[ s.Length() ] = '\0';
00096 
00097  for (idxType=1, nTypes=pParser->GetNAttrType(); idxType<=nTypes++; idxType++) {
00098      pAttrType = pParser->GetAttrType( idxType );
00099      DBGPRINT_MIN("TypeName2HAttrType(%s,%s): %d/%d\n",strType,s.Str(),idxType,nTypes);
00100      ASSERTION(pAttrType!=nil,"pAttrType!=nil");
00101      typeFamily = pAttrType->typeFamily;
00102      if ( isBasicType ) {
00103          if ( typeFamily!=1 ) return nil;
00104          if ( s.Find( pAttrType->strType )==1 ) return pAttrType;
00105      }
00106      else {
00107          if ( s.Match( pAttrType->strType ) ) return pAttrType;
00108      }
00109  }
00110 
00111  return nil;
00112 }
00113 
00114 int gHAttrs::TypeEnumToSmartL (sHAttrType* pAttrType, gSmartList& lEnumResult)
00115 {
00116  ASSERTION(pAttrType!=nil,"pAttrType!=nil");
00117  lEnumResult.Delete();
00118  lEnumResult.SetSmartChr( '|' );
00119  lEnumResult.SetCaseSense( false );  // Non-case-sensitive!
00120  // E.g. "left|center|right" copyied into the list (3 elements)
00121  return lEnumResult.AddFromStr( pAttrType->strType );
00122 }
00123 
00124 void gHAttrs::Reset ()
00125 {
00126  gList::Reset();
00127  Delete();
00128 }
00129 
00130 int gHAttrs::BuildFromLRLists (char* strTag, gList& lLVal, gList& lRVal, bool addOnError, unsigned iLine)
00131 {
00132  unsigned i, n;
00133 
00134  gHtmlParser* pParser = gHtmlCtrl::Self().ParserPtr();
00135  sAttrRefer* lstAttrRef = pParser->GetAttrRef();
00136  sAttrDef* dAttr;
00137  sHAttrType* pAttrType;
00138  t_int16 idx, uniqIdx;
00139  char* strL;
00140  char* strR;
00141  FILE* fErr = gHtmlCtrl::Self().hLog.fRepErr;
00142 
00143  ASSERTION(lLVal.N()==lRVal.N(),"lLVal.N()==lRVal.N()");
00144 
00145  for (i=1, n=lLVal.N(); i<=n; i++) {
00146      strL = lLVal.Str( i );
00147      strR = lRVal.Str( i );
00148      if ( strL==nil || strL[0]==0 ) continue;
00149      idx = lstAttrRef->FindAttr( strL, strTag, uniqIdx );
00150      ASSERTION(idx>=0,"idx>=0");
00151      ASSERTION(uniqIdx>=0,"uniqIdx>=0");
00152      dAttr = pParser->GetAttrDef( idx );
00153      pAttrType = TypeName2HAttrType( dAttr->strType );
00154      if ( pAttrType==nil ) {
00155          HTML_LOG( stderr, LOG_ERROR, "Internal-error: Unable to cross-check attribute: '%s' for tag: %s\n",dAttr->strType,strTag);
00156          continue;
00157      }
00158 
00159      DBGPRINT_WEBA1("DBG: iLine:%u strL:%s=%s (TypeId:%s=%d)!\n",iLine,strL,strR,pAttrType->strType,pAttrType->hAttrType);
00160 
00161      gHAType* newValue = NewType( strR, pAttrType );
00162 
00163      // DEBUG only: TODO: cut
00164      if ( newValue==nil ) {
00165          DBGPRINT_WEBA1("DBG: <<TODO:attr:%s>>\n",strL);
00166          continue;
00167      }
00168      ASSERTION(newValue!=nil,"newValue!=nil");
00169 
00170      //if ( pAttrType->hAttrType==e_HATp_EnumChoice ) printf("DBG: enum(%s):%d:OK?%c\n",newValue->GetStr(),((gHATypeSimple*)newValue)->v,ISyORn(newValue->IsOk()));
00171 
00172      if ( newValue->IsOk()==false ) {
00173          HTML_LOG( fErr, LOG_NOTICE, "Line %u: Invalid value (%s) for attribute: %s, of type %s\n", iLine, strR, strL, dAttr->strType );
00174          if ( addOnError==false ) {
00175              delete newValue;
00176              newValue = nil;
00177          }
00178      }
00179 
00180      if ( newValue!=nil ) {
00181          gHAttrValue* newLR = new gHAttrValue( strL, newValue );
00182          ASSERTION(newLR!=nil,"newLR!=nil");
00183          AppendObject( newLR );
00184      }
00185  }
00186 
00187  return 0;
00188 }
00189 
00190 gHAType* gHAttrs::NewType (char* strValue, sHAttrType* pAttrType)
00191 {
00192  eHAttrType hAttrType;
00193  gSmartList lEnumResult;
00194  short hAttrSingle;
00195 
00196  ASSERTION(strValue!=nil,"strValue!=nil");
00197  if ( pAttrType==nil ) return nil;
00198 
00199  hAttrType = pAttrType->hAttrType;
00200  hAttrSingle = pAttrType->hAttrSingle;
00201 
00202  switch ( pAttrType->typeFamily ) {
00203  case 1:  // Basic attribute-types
00204      switch ( hAttrType ) {
00205      case e_HAT_CDATA:
00206          return new gHATypeCDATA( strValue );
00207      case e_HAT_ID:
00208          return new gHATypeID( strValue );
00209      case e_HAT_IDREF:
00210          return new gHATypeIDREF( strValue );
00211      case e_HAT_IDREFS:
00212          return new gHATypeIDREFS( strValue );
00213      case e_HAT_NAME:
00214          return new gHATypeNAME( strValue );
00215      case e_HAT_NUMBER:
00216          return new gHATypeNUMBER( strValue );
00217          break;
00218      default:
00219          ASSERTION_FALSE("NewType(Family1)");
00220      }
00221 
00222  case 0:  // Compound attribute-types
00223      switch ( hAttrType ) {
00224      case e_HATp_CAlign:
00225          break;
00226      case e_HATp_Character:
00227          break;
00228      case e_HATp_Charset:
00229      case e_HATp_Charsets:
00230          return new gHATypeCharsets( strValue, hAttrSingle );
00231      case e_HATp_Color:
00232          return new gHATypeColor( strValue );
00233      case e_HATp_ContentType:
00234      case e_HATp_ContentTypes:
00235          return new gHATypeContentTypes( strValue, hAttrSingle );
00236      case e_HATp_Coords:
00237      case e_HATp_DateTime:
00238          break;
00239      case e_HATp_FrameTarget:
00240          return new gHATypeFrameTarget( strValue );
00241      case e_HATp_HtmlVersion:
00242          break;
00243      case e_HATp_IAlign:
00244          break;
00245      case e_HATp_InputType:
00246          break;
00247      case e_HATp_LAlign:
00248          break;
00249      case e_HATp_LanguageCode:
00250          break;
00251      case e_HATp_Length:
00252          return new gHATypeLength( strValue );
00253      case e_HATp_LinkTypes:
00254          break;
00255      case e_HATp_LIStyle:
00256          break;
00257      case e_HATp_MediaDesc:
00258          break;
00259      case e_HATp_MultiLength:
00260      case e_HATp_MultiLengths:
00261          return new gHATypeMultiLengths( strValue, hAttrSingle );
00262      case e_HATp_OLStyle:
00263          break;
00264      case e_HATp_Pixels:
00265          return new gHATypePixels( strValue );
00266      case e_HATp_Scope:
00267          break;
00268      case e_HATp_Script:
00269          break;
00270      case e_HATp_Shape:
00271          break;
00272      case e_HATp_StyleSheet:
00273          return new gHATypeStyleSheet( strValue );
00274      case e_HATp_TAlign:
00275          break;
00276      case e_HATp_Text:
00277          return new gHATypeText( strValue );
00278      case e_HATp_TFrame:
00279          break;
00280      case e_HATp_TRules:
00281          break;
00282      case e_HATp_ULStyle:
00283          break;
00284      case e_HATp_URI:
00285          return new gHATypeURI( strValue );
00286      default:
00287          ASSERTION_FALSE("NewType(Family0)");
00288      }//end inner-CASE
00289      break;
00290 
00291  case 10:  // Enum attribute-types
00292      TypeEnumToSmartL( pAttrType, lEnumResult );
00293      return new gHATypeEnum( hAttrType, strValue, lEnumResult );
00294  default:
00295      break;
00296  };//end outer-CASE
00297 
00298  return nil;
00299 }
00300 
00301 void gHAttrs::Show (bool doShowAll)
00302 {
00303  unsigned i, n=N();
00304  char* str;
00305  const char* strNL=doShowAll?"\n":"\0";
00306  gHAType* pValue;
00307 
00308  DBGPRINT_WEBA1("DBG: gHAttrs::Show%s (N=%u)\n",doShowAll?":ALL":"\0",n);
00309 
00310  for (i=1; i<=n; i++) {
00311      pValue = GetAttrPtr( i );
00312      str = pValue->GetStr();
00313      DBGPRINT_MIN("typeId%d",pValue->GetTypeId());
00314      if ( pValue->IsValue() )
00315          printf("[%s|%ld]",str,(long)pValue->GetInt32());
00316      else
00317          printf("[%s]",str);
00318      printf("%s",i<n?" ":strNL);
00319  }
00320 }
00321 ////////////////////////////////////////////////////////////
00322 

Generated on Sat Aug 18 02:40:54 2007 for xpfweb_v2x lib by  doxygen 1.4.2