Browse Source

* Factored ParseIgnoreSection into separate procedure
+ Overloaded THashTable.FindOrAdd(const WideString) for convenience

git-svn-id: trunk@18102 -

sergei 14 years ago
parent
commit
151a8b59fb
2 changed files with 38 additions and 27 deletions
  1. 30 27
      packages/fcl-xml/src/xmlread.pp
  2. 8 0
      packages/fcl-xml/src/xmlutils.pp

+ 30 - 27
packages/fcl-xml/src/xmlread.pp

@@ -383,6 +383,7 @@ type
     procedure ExpectEq;
     procedure ExpectEq;
     procedure ParseDoctypeDecl;                                         // [28]
     procedure ParseDoctypeDecl;                                         // [28]
     procedure ParseMarkupDecl;                                          // [29]
     procedure ParseMarkupDecl;                                          // [29]
+    procedure ParseIgnoreSection;
     procedure ParseStartTag;                                            // [39]
     procedure ParseStartTag;                                            // [39]
     procedure ParseEndTag;                                              // [42]
     procedure ParseEndTag;                                              // [42]
     function DoStartElement: TDOMElement;
     function DoStartElement: TDOMElement;
@@ -1306,8 +1307,8 @@ begin
     FStdPrefix_xml := FNSHelper.GetPrefix(@PrefixDefault, 3);
     FStdPrefix_xml := FNSHelper.GetPrefix(@PrefixDefault, 3);
     FStdPrefix_xmlns := FNSHelper.GetPrefix(@PrefixDefault, 5);
     FStdPrefix_xmlns := FNSHelper.GetPrefix(@PrefixDefault, 5);
 
 
-    FStdUri_xmlns := FNameTable.FindOrAdd(PWideChar(stduri_xmlns), Length(stduri_xmlns));
-    FStdUri_xml := FNameTable.FindOrAdd(PWideChar(stduri_xml), Length(stduri_xml));
+    FStdUri_xmlns := FNameTable.FindOrAdd(stduri_xmlns);
+    FStdUri_xml := FNameTable.FindOrAdd(stduri_xml);
   end;
   end;
 end;
 end;
 
 
@@ -2499,19 +2500,36 @@ begin
     Entity.Free;
     Entity.Free;
 end;
 end;
 
 
+procedure TXMLTextReader.ParseIgnoreSection;
+var
+  IgnoreLoc: TLocation;
+  IgnoreLevel: Integer;
+  wc: WideChar;
+begin
+  StoreLocation(IgnoreLoc);
+  IgnoreLevel := 1;
+  repeat
+    FValue.Length := 0;
+    wc := FSource.SkipUntil(FValue, [#0, '<', ']']);
+    if FSource.Matches('<![') then
+      Inc(IgnoreLevel)
+    else if FSource.Matches(']]>') then
+      Dec(IgnoreLevel)
+    else if wc <> #0 then
+      FSource.NextChar
+    else // PE's aren't recognized in ignore section, cannot ContextPop()
+      DoErrorPos(esFatal, 'IGNORE section is not closed', IgnoreLoc);
+  until IgnoreLevel=0;
+end;
 
 
 procedure TXMLTextReader.ParseMarkupDecl;        // [29]
 procedure TXMLTextReader.ParseMarkupDecl;        // [29]
 var
 var
   IncludeLevel: Integer;
   IncludeLevel: Integer;
-  IgnoreLevel: Integer;
   CurrentEntity: TObject;
   CurrentEntity: TObject;
   IncludeLoc: TLocation;
   IncludeLoc: TLocation;
-  IgnoreLoc: TLocation;
-  wc: WideChar;
   CondType: (ctUnknown, ctInclude, ctIgnore);
   CondType: (ctUnknown, ctInclude, ctIgnore);
 begin
 begin
   IncludeLevel := 0;
   IncludeLevel := 0;
-  IgnoreLevel := 0;
   repeat
   repeat
     SkipWhitespace;
     SkipWhitespace;
 
 
@@ -2563,22 +2581,7 @@ begin
           Inc(IncludeLevel);
           Inc(IncludeLevel);
         end
         end
         else if CondType = ctIgnore then
         else if CondType = ctIgnore then
-        begin
-          StoreLocation(IgnoreLoc);
-          IgnoreLevel := 1;
-          repeat
-            FValue.Length := 0;
-            wc := FSource.SkipUntil(FValue, [#0, '<', ']']);
-            if FSource.Matches('<![') then
-              Inc(IgnoreLevel)
-            else if FSource.Matches(']]>') then
-              Dec(IgnoreLevel)
-            else if wc <> #0 then
-              FSource.NextChar
-            else // PE's aren't recognized in ignore section, cannot ContextPop()
-              DoErrorPos(esFatal, 'IGNORE section is not closed', IgnoreLoc);
-          until IgnoreLevel=0;
-        end;
+          ParseIgnoreSection;
       end
       end
       else
       else
       begin
       begin
@@ -3275,13 +3278,13 @@ begin
       b := TBinding(FCurrNode^.FPrefix^.Data);
       b := TBinding(FCurrNode^.FPrefix^.Data);
       if not (Assigned(b) and (b.uri <> '')) then
       if not (Assigned(b) and (b.uri <> '')) then
         DoErrorPos(esFatal, 'Unbound element name prefix "%s"', [FCurrNode^.FPrefix^.Key],FCurrNode^.FLoc);
         DoErrorPos(esFatal, 'Unbound element name prefix "%s"', [FCurrNode^.FPrefix^.Key],FCurrNode^.FLoc);
-      FCurrNode^.FNsUri := FNameTable.FindOrAdd(PWideChar(b.uri), Length(b.uri));
+      FCurrNode^.FNsUri := FNameTable.FindOrAdd(b.uri);
     end
     end
     else
     else
     begin
     begin
       b := FNSHelper.DefaultNSBinding;
       b := FNSHelper.DefaultNSBinding;
       if Assigned(b) then
       if Assigned(b) then
-        FCurrNode^.FNsUri := FNameTable.FindOrAdd(PWideChar(b.uri), Length(b.uri));
+        FCurrNode^.FNsUri := FNameTable.FindOrAdd(b.uri);
     end;
     end;
   end;
   end;
 
 
@@ -3464,7 +3467,7 @@ function TXMLTextReader.AddBinding(attrData: PNodeData): Boolean;
 var
 var
   nsUri, Pfx: PHashItem;
   nsUri, Pfx: PHashItem;
 begin
 begin
-  nsUri := FNameTable.FindOrAdd(PWideChar(attrData^.FValueStr), Length(attrData^.FValueStr));
+  nsUri := FNameTable.FindOrAdd(attrData^.FValueStr);
   if attrData^.FColonPos > 0 then
   if attrData^.FColonPos > 0 then
     Pfx := FNSHelper.GetPrefix(@attrData^.FQName^.key[7], Length(attrData^.FQName^.key)-6)
     Pfx := FNSHelper.GetPrefix(@attrData^.FQName^.key[7], Length(attrData^.FQName^.key)-6)
   else
   else
@@ -3514,7 +3517,7 @@ begin
     if FNsAttHash.Locate(@b.uri, @AttrName^.Key[J], Length(AttrName^.Key) - J+1) then
     if FNsAttHash.Locate(@b.uri, @AttrName^.Key[J], Length(AttrName^.Key) - J+1) then
       DoErrorPos(esFatal, 'Duplicate prefixed attribute', attrData^.FLoc);
       DoErrorPos(esFatal, 'Duplicate prefixed attribute', attrData^.FLoc);
 
 
-    attrData^.FNsUri := FNameTable.FindOrAdd(PWideChar(b.uri), Length(b.uri));
+    attrData^.FNsUri := FNameTable.FindOrAdd(b.uri);
   end;
   end;
 end;
 end;
 
 
@@ -3634,7 +3637,7 @@ var
   Notation: TNotationDecl;
   Notation: TNotationDecl;
   Entry: PHashItem;
   Entry: PHashItem;
 begin
 begin
-  Entry := FDocType.Notations.FindOrAdd(PWideChar(aName), Length(aName));
+  Entry := FDocType.Notations.FindOrAdd(aName);
   if Entry^.Data = nil then
   if Entry^.Data = nil then
   begin
   begin
     Notation := TNotationDecl.Create;
     Notation := TNotationDecl.Create;

+ 8 - 0
packages/fcl-xml/src/xmlutils.pp

@@ -103,6 +103,7 @@ type
     function Find(Key: PWideChar; KeyLen: Integer): PHashItem;
     function Find(Key: PWideChar; KeyLen: Integer): PHashItem;
     function FindOrAdd(Key: PWideChar; KeyLen: Integer; var Found: Boolean): PHashItem; overload;
     function FindOrAdd(Key: PWideChar; KeyLen: Integer; var Found: Boolean): PHashItem; overload;
     function FindOrAdd(Key: PWideChar; KeyLen: Integer): PHashItem; overload;
     function FindOrAdd(Key: PWideChar; KeyLen: Integer): PHashItem; overload;
+    function FindOrAdd(const Key: WideString): PHashItem; overload;
     function Get(Key: PWideChar; KeyLen: Integer): TObject;
     function Get(Key: PWideChar; KeyLen: Integer): TObject;
     function Remove(Entry: PHashItem): Boolean;
     function Remove(Entry: PHashItem): Boolean;
     function RemoveData(aData: TObject): Boolean;
     function RemoveData(aData: TObject): Boolean;
@@ -554,6 +555,13 @@ begin
   Result := Lookup(Key, KeyLen, Dummy, True);
   Result := Lookup(Key, KeyLen, Dummy, True);
 end;
 end;
 
 
+function THashTable.FindOrAdd(const Key: WideString): PHashItem;
+var
+  Dummy: Boolean;
+begin
+  Result := Lookup(PWideChar(Key), Length(Key), Dummy, True);
+end;
+
 function THashTable.Get(Key: PWideChar; KeyLen: Integer): TObject;
 function THashTable.Get(Key: PWideChar; KeyLen: Integer): TObject;
 var
 var
   e: PHashItem;
   e: PHashItem;