# XRefWriter

XRefWriter class inherits from `CXref` and provides

- public interface for making changes and storing them to the stream.
- maintains document revisions and current revision (with logic, what and how it can be do in which revision)
- keeps its mode
  - paranoid mode forces paranoiCheck method call in changeObject method. This checks whether given reference is known and given object is compared with current one by `CXref::typeSafe` method. If checking doesn't fail, object can be changed and delegates the rest of the work to the `CXRef::changeObject` implementation.
  - easy mode doesn't do any kind of checking. This is not very safe, but if class user knows what he is doing (e. g. to correct destroyed objects).

XRefWriter responsibility is to enable writing changes to the XRef's stream to make them visible after all required changes are finished. To separate from concrete implementation of storing, XRefWriter uses `IPdfWriter` abstract interface for object writing. Concrete implementation can be set in runtime and `OldStylePdfWriter` is used by default. XRefWriter is just responsible to provide with all changed objects which are retrieved from lower (CXref) layer (mapping is protected).

Underlaying stream with data which is stored in `XRef` supertype is typed as a `Stream` (see . `IPdfWriter` requires `StreamWriter` (see ). So XRefWriter uses `StreamWriter` implementator in constructor and `XRef` use this stream when fetching objects and `IPdfWriter` uses it when writing content.

Finally XRefWriter keeps information needed for revision handling. Each cross reference section and associated trailer is considered to form revision. This means that each document has at least one revision and all incremental update forms new revisions. The newest revision is one at the end of the file.

XRefWriter keeps an array of all available revisions. Index stands for revision number (0 is the oldest one) and value is stream offset of associated cross reference section start for revision. This is used to get information about revision - for example to get revision size (see `XRefWriter::getRevisionSize` method) - and to enable revision changing. `CXref` implements

    	void CXref::reopen(size_t xrefOff);

method which is responsible for clear XRef state change to forget all objects from current revision and to start parsing of cross reference section from given position. This enables to (rather simply) change current revision and so see documents in those state.

PDF format is prepared for such revisions and multi version documents very well, but doesn't support any kind of branching which means that changes can be done only for the newest revision. XRefWriter take in mind also this aspect and so all methods take care of current revision. This means that anytime current revision is not the newest one, changed objects are ignored and everything is delegated directly to the lowest layer. Also all methods producing changes throws `ReadOnlyDocumentException`. Even more all methods reimplemented by `CXRef` class, which depedns on changes, are omited and directly `XRef` implementation is used instead.