{Hlavny unit obsahuje vsetky klucove algoritmy na vytvaranie stromov, operatorovych stromov, kontrolu syntaxe, validaciu dokumentu}
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls, Buttons, ComCtrls, Menus,
  Unit3, Unit4, Unit5, Unit6, Unit7, Unit8, Unit9,
  Stack, Tree;

const WS: set of Char = [#9,#10,#13,#32];
//      NS: set of Char = ['A'..'z','0'..'9','.','-',':','_'];
      Letter = ['A'..'Z'] + ['a'..'z'];
      Digit = ['0'..'9'];
      NS = Letter + Digit + ['.','-',':','_'];

      SS = ['?','+','*'];
      TS = ['?','!','+','*','|'];

      INSIDE_FLAG = $1;
      CLOSURE_FLAG = $2;

      EM_EXLINEFROMCHAR	= WM_USER + 54;

      EM_SETUNDOLIMIT   = WM_USER + 82;
      EM_REDO		= WM_USER + 84;
      EM_CANREDO        = WM_USER + 85;
      EM_GETUNDONAME	= WM_USER + 86;
      EM_GETREDONAME	= WM_USER + 87;
      EM_STOPGROUPTYPING= WM_USER + 88;
      EM_SETTEXTMODE	= WM_USER + 89;
      EM_GETTEXTMODE	= WM_USER + 90;

      TM_PLAINTEXT		= 1;
      TM_RICHTEXT		= 2;	(* default behavior *)
      TM_SINGLELEVELUNDO	= 4;
      TM_MULTILEVELUNDO		= 8;	(* default behavior *)
      TM_SINGLECODEPAGE		= 16;
      TM_MULTICODEPAGE		= 32;   (* default behavior *)

type
  TXMLTreeNode = record
                   S,E: Longint;
                   CS,CE: Longint;
                   Empty,Closure: Boolean;
                 end;

implementation

function TContForm.Validate: Longint;   //Implicit: Tree,DTDTree
var N,D,X: TTagNode;
    Node: TTagNode;
begin
  XMLEditForm.RichEdit1.HideSelection := True;
  MForm.ClearDTD;
  Result := 0;
  D := DTDTree.Root;
  N := Tree.Root;
  if (N = nil) or (D = nil) then Exit;
//DEBUG  Caption := '';
  X := Tree.Root;
  repeat
    X.PCDATA := True;
    X := X.GetNextNode;
  until X = nil;

  ClearIDs;

  ValidateBlockOnlyOne(D.GetFirstChild,N,False,True);
  //SecondPassPCDATACheck - dorobit

{  repeat
    if D.Text[1] in TS then begin
      case D.Text[1] of
        '!': begin
               Node := N;
               repeat
                 ValidateBlockOnlyOne(D.GetFirstChild,Node);
               until Node = nil;
             end;
      end;
    end;
  until (Result < 0) or (D = nil);}
end;


//VERSION    #:   0.01.084.2001
//RELEASED   #:   08.05.2001 17:43
//WORKTIME   #:   393 hours
//KNOWN BUGS #:
//                X1#:graficky bug na hlavnom paneli, priesvitne listy( toolbars) - KILLED
//                2#:zakazat presuny text mysou !!!!!!!!!!!!!!!!!!!!! ->  NEDA SA - WINDOWS 98 BUG!!!!!!!!!
//                SOLUTION - NONE!!!
//                X3#:uff...., bug pri deletovani v insert mode  napr. stlacit delete ked je kurzor pred IkurzorI<DATE> - KILLED
//                X4#:debilny TreeView, po viac ako 1000 blbne( redeklaruje root a pod., cimz mi kazi rekurzie v DTD) - KILLED
//                SOLUTION - pouzit vlastny strom, neskor obojsmerny graf
//                5#:farebny bug - ak sa zafarbi riadok nacerveno, potom sa zlomi, a potom sa chyba odstrani, tak na dolny z tych dvoch zostane cerveny
//                SOLUTION - nejako interne si zapametat k Erroru aj startpos,endpos stxhighlightingu, potom to menit pri vkladani a vymazavani,
//                           pri cut&paste Redraw syntax highlighting
//                X6#:farebny bug pri validacii - niektore tagy zostanu cervene aj ked je dokument valid, ked necham bug v AddMessage, tak je to OK!!!!!!!!!!
//                SOLUTION - little move caret to the end and the immediatly back( what was this??)
//                7#:boldovy bug - niekedy nezhrubi riadov, po odstraneni chyby
//                SOLUTION - asi treba vylepsit rutinu na rozhodovanie, ci preboldovat, alebo nie v Unit5,
//                           alebo inak, spomalenie vsetko riesi
//                X8#:DTD attribute parsing bug - parser takes into account only the first attribute
//                X9#:popup-menu keyboard button doesn't function in conjuction with context based popup, it does not actualize the context
//
//KNOWN CONSTRAINTS #:
//                1#:XMLNameSpaces( i.e. XLS:BODY) not supported( only lefts : without an additional parsing)
//                2#:<,> as an attribute value not supported( in IE 5.5 < is supported, in XML 1.0 standart however < is not supported) - use &lt;,&gt;
//                SOLUTION - pri hladani < treba checkovat ' or " a potom ich par a preskocit za, a hladat dalej <,
//                           ak par neexistuje, tak Error
//                           podobne aj pri hladani >
//                           zrejme to dost spomali checkovanie, ale je to mozne
//                3#:AdvancedRebuildTree not yet fully optimized
//                4#:Syntax Highlighting is slow, therefore is not that, what it was meant to be
//                5#:Ignores IGNORE sections in DTD( that means: it includes them)
//                6#:Parametric & Character entities are slightly different from the standard
//                7#:Notation used only in Attribute ENTITY tokens
//                X8#:DTD Validator doesn't check #PCDATA sections( will require second pass( or nor ;-))
//                X9#:!!!!!!!!!!!!!!!!!!! ANY !!!!!!!!!!!!!!!!!!! NOT SUPPORTED in Validator
//               10#:External entities not yet supported
//               X11#:PI will be supported only in Header editor, not in content - PIs are now fully supported
//               12#:IDREF may referecnce only ID in upper areas of document( i think it's reasonable, but I'm not sure of the XML 1.0 standard)
//
//SPECIAL - IDEAS  #:
//                ??????? editovatelny treeview pre XML
//                ??????? Canonical XML
//                ??????? XMLError autocorrection
//                ??????? nefatalne errory, teda take, ze bude dalej checkovat dalsie errory
//                ??????? auto-build-dtd
//                ??????? check dtd for errors
//                ??????? speedy way of loading DTD, the use of structures for Elements & Attributes similar to structures for Entities & Notations
//                ??????? status bar v MForme na pocitanie errorov
//                ??????? editovanie a pridavanie character entities
//                ??????? zlepsit ponuku tagov v EditTagForme, iba na povolene
//                ??????? vylepsit enumerovane typy v ponuke( zmenit na ComboBoxy)
//                ??????? lepsi Save( save with/without external dtd)
//                ??????? viacej defaultnych Layout schem( popripade save layoutov)
//                ??????? DTD extracting - 90%
//                ??????? zrychlit GetXMLElement,CheckXMLTag - 50%
//                        HOW TO - prepisat SearchFor.... na cykly, prepisat Copy na tvar (S[I]='a' and S[I+1]='b' and S[I+2] = 'c')
//                               - GUARANTEED!
//
//OVERALL STATUS:
//Interface:      viacero kontajnerov sucasne - 100%
//                vyhadzovanie z pola pri Close( trochu ine riesenie) - 100%
//                resizovanie panelov pri closeovani formov - 100%
//                insert/delete mode - 100% -> bude velmi hard
//                otvaranie a dockovanie okien typu DTDTree a pod. po tom co boli zatvorene - 100% (vyriesene cez Reset Layout)
//
//XML Editor:
//                hlavicka - 99%  -> presunute do PHASE @3(@4)
//                CDATA - 99%
//                komentare - 99%
//                prazdne tagy - 100%
//DTD Validator:
//                #PCDATA - 99%
//                EMPTY - 99%
//                ANY - 99%
//
//                Warning: Treba zatvorkovat - napr. (H|L,A,B,C) - bude zle interpretovane,
//                         treba zmenit na ((H|L),A,B,C) - bude OK
//
//TIME SCHEDULE:
//
//<:::::::::::>
//YEAR 2000<::>
//<:::::::::::>
//
//PHASE @1:
//                Load/Save - 99%
//                Resizovanie okien podla fyzickeho rozlisenia a.k.a 640x480 v H6 - 100%
//                zlepsit detekciu OuterNode pre selekciu( nevracat Outer Worlds, ale najmensie pokrytie) - 100%
//                dorobit updatovanie stromu - 99%
//                vlastny strom aspon na XML - 99%
//                co s presuvanim,deletovanim,kopirovanim tagov????? - 99%
//                zakladne entity - 99%
//                text pred rootom a za rootom tj. asgsdg <root>....</root> asdfs - 100%
//PHASE @2:
//                dorobit error pri vyskyte ]]> vo vnutrajsku tagu - 100%
//                dorobit volne miesto za nazvom tagu example: <abc   >, ale nie <  abc> - 100%
//                dorobit v closure nemoze byt nic okrem Whitespace na konci - 100%
//                dorobit XMLError Messaging - 100%
//                dorobit prepocet z position do row,column - 100%
//                dorobit XMLError identifying( jump to row,column of error) - 100%
//                dorobit kontrolu na Attributy - 100%
//                ANOTHER WAY - najprv updatovat cely strom s dalsim stringov - HiddenText a potom parsovat atributy len z HTextu
//                              tymto by sa to dalo zjednodusit
//
//                dorobit XML syntax highlighting - 99%   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//                HOW TO - aby to islo rychlo, tak EM_GETFIRSTVISIBLELINE, alebo tak nejak
//                         potom VISIBLELINECOUNT zistit, a potom zobrat S,E a hodit do XMLTree a zobrazit farebne,len to co vidim
//                       - absolutne nepomaha, extremne pomale!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//                       - osetrenie dalsich farebnych bugov bude este radovo pomalsie!!!!!!!!!!!!!!!
//
//<:::::::::::>
//YEAR 2001<::>
//<:::::::::::>
//-JANUARY-
//                dorobit XML AttributeAnalyzer, ukladanie do datovej struktury, novy parameter v GetXMLElement - 100%
//                dorobit v XML strome, data pointre na datovu strukturu AttributeList(pole meno - hodnota) - 100%
//                dorobit DTD entities replacing in DTD (a.k.a % title = 'Flash') - 99%
//                dorobit DTD attributes parsing - 100%
//-FEBRUARY-
//PHASE @3:
//                nova datova struktura na DTD ENTITIES, DTD NOTATIONS - 100%
//                prerobit NMPrefix,PrefixNS NM->NS - 100%
//                dorobit right-click - 100%
//                dorobit XML entities(suvisi s rigth-clickom) - 100% (basic entities should be there, no they should not)
//                dorobit Entities Collapse & Expand - 100%
//                dorobit Synchronizaciu Containeru a Options menu - 100%
//-MARCH-
//                dorobit XML Document Validation - 99% ( don't forget that every Entity must be declared in DTD)
//                dorobit 2nd-pass #PCDATA Check - 100% ( phenomenal - first pass method)
//                dorobit loadovanie XML hlavicky, editor hlavicky XML, DTD linking - 99%
//                dorobit zakladne searchovacie rutinky, Find,Replace - 99%
//                dorobit UNDO - o nicom, lebo v RichEdit32.dll, ktory pouziva Delphi 5 je len jednoduchy UNDO - 100%
//                dorobit right-click ponuku tagov - 99%
//                dorobit form na editovanie stareho tagu - 99%
//                dorobit form na editovanie noveho tagu - 99%
//                dorobit form na vyber atributov - 99%
//                dorobit podporu pre ANY tag - 100%
//PHASE @4:
//                dorobit Flag na urcenie, ci sa bude v real time kontrolovat Well-formedness - 100%
//
//                dorobit Document(s) you're sure , you wanna quit( Save or not to Save) - 100%
//                zlepsit StatusBar - 100%
//
//                dorobit Cut/Copy/Paste/Delete/Select All v menu a v popup-menu - 100%
//                dorobit Reset Layout - 100%
//
//                dorobit Splash Screen - 100%
//                dorobit About... - 100%
//-APRIL-
//-MAY-
//                dorobit Interface( ikonky na toolbare a v menu a pod., ala Delphi IDE) - 99%
//                dorobit Help - 99%
//                dobobit Error Handling - 90% (not enough memory???, mouse not found???)
//                dorobit package EXE - 99%
//                dorobit package SRC - 99%
//>-----------------NOW-----------------<
//                dorobit package DOC - 90%
//


procedure TContForm.FormCreate(Sender: TObject);
begin
  Started := False;
  PostMessage(handle, WM_USER, 0, 0);
end;

procedure TContForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Stack.Destroy;
  Tree.Erase;
  DTDTree.Erase;

  MainForm.Tag := -1;
  MainForm.Formy[Tag].Free;
  MainForm.Formy[Tag] := nil;
  {tu by malo byt vyhadzovanie z pola kontajnerov}
end;

end.
