2 * Copyright (c) 2004-2006 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Symbian Foundation License v1.0" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.symbianfoundation.org/legal/licencesv10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
14 * Description: Document node functions
23 #ifndef XMLENGINE_DOCUMENT_H_INCLUDED
24 #define XMLENGINE_DOCUMENT_H_INCLUDED
28 #include "xmlengnode.h"
29 #include "xmlengserializationoptions.h"
31 // FORWARD DECLARATION
32 class RXmlEngDOMImplementation;
35 * Instance of RXmlEngDocument class represents an XML document in the DOM tree.
37 * Is a storage all nodes and information about XML data.
39 * @lib XmlEngineDOM.lib
42 class RXmlEngDocument : public TXmlEngNode
46 * Default constructor.
48 * Instance of RXmlEngDocument must be "opened" with one of OpenL() overloads.
52 IMPORT_C RXmlEngDocument();
58 * @param aDOMImpl DOM implementation object
59 * @return KErrNone if succeed.
61 IMPORT_C void OpenL(RXmlEngDOMImplementation& aDOMImpl);
67 * @param aDOMImpl DOM implementation object
68 * @param aInternal Document pointer
69 * @return KErrNone if succeed.
71 IMPORT_C void OpenL(RXmlEngDOMImplementation& aDOMImpl, void* aInternal);
77 * @param aDOMImpl DOM implementation object
78 * @param aRoot element taht will be root of the DOM tree
79 * @return KErrNone if succeed.
81 IMPORT_C void OpenL(RXmlEngDOMImplementation& aDOMImpl, TXmlEngElement aRoot);
88 IMPORT_C void Close();
91 * Serializes document tree into a file. For nodes containing binary data in the form of BinaryDataContainer,
92 * FileContainer or ChunkContainer, client can implement custom serialization by implementing the interface
93 * MXmlEngDataSerializer and specify the pointer in iDataSerializer member of aSaveOptions parameter. If no
94 * custom serialization is specified, the binary data container nodes are serialized like text nodes.
97 * @param aFileName A file name (with path)
98 * @param aRoot Root node to be serialized
99 * @param aSaveOptions Options that control how serialization is performed
100 * @return Number of byte written
101 * @leave KErrNoMemory, KErrGeneral, KXmlEngErrWrongEncoding, KErrDiskFull.
103 IMPORT_C TInt SaveL( const TDesC& aFileName,
104 TXmlEngNode aRoot = TXmlEngNode(),
105 const TXmlEngSerializationOptions& aSaveOptions = TXmlEngSerializationOptions() ) const;
108 * Serializes document tree into a file. For nodes containing binary data in the form of BinaryDataContainer,
109 * FileContainer or ChunkContainer, client can implement custom serialization by implementing the interface
110 * MXmlEngDataSerializer and specify the pointer in iDataSerializer member of aSaveOptions parameter. If no
111 * custom serialization is specified, the binary data container nodes are serialized like text nodes.
114 * @param aRFs File Server session
115 * @param aFileName A file name (with path)
116 * @param aRoot Root node to be serialized
117 * @param aSaveOptions Options that control how serialization is performed
118 * @return Number of byte written
119 * @leave KErrNoMemory, KErrGeneral, KXmlEngErrWrongEncoding, KErrDiskFull.
121 IMPORT_C TInt SaveL( RFs& aRFs,
122 const TDesC& aFileName,
123 TXmlEngNode aRoot = TXmlEngNode(),
124 const TXmlEngSerializationOptions& aSaveOptions = TXmlEngSerializationOptions() ) const;
127 * Serializes a document tree into provided output stream, which supports progressive writing of data.
128 * For nodes containing binary data in the form of BinaryDataContainer, FileContainer or ChunkContainer,
129 * client can implement custom serialization by implementing the interface MXmlEngDataSerializer and specify
130 * the pointer in iDataSerializer member of aSaveOptions parameter. If no custom serialization is specified,
131 * the binary data container nodes are serialized like text nodes.
134 * @param aStream An output stream to write serialized DOM tree
135 * @param aRoot Root node to be serialized
136 * @param aSaveOptions Options that control how serialization is performed
137 * @return Number of byte written
138 * @leave KXmlEngErrWrongUseOfAPI or one of general codes (e.g.KErrNoMemory)
139 * @see MXmlEngOutputStream
141 IMPORT_C TInt SaveL( MXmlEngOutputStream& aStream,
142 TXmlEngNode aRoot = TXmlEngNode(),
143 const TXmlEngSerializationOptions& aSaveOptions = TXmlEngSerializationOptions() ) const;
146 * Saves document tree into memory buffer
149 * @param aBuffer Resulting buffer
150 * @param aRoot A "root" of the subtree to serialize
151 * @param aSaveOptions Various options to be effective during serialization
152 * @return Number of bytes in updated buffer
153 * @leave KErrNoMemory, KErrGeneral, KXmlEngErrWrongEncoding.
155 * @note Result returned via aBuffer argument owns the memory buffer; it is up to
156 * method caller to free it like in this sample:
158 * @see TXmlEngSerializationOptions
160 IMPORT_C TInt SaveL(RBuf8& aBuffer,
161 TXmlEngNode aRoot = TXmlEngNode(),
162 const TXmlEngSerializationOptions& aSaveOptions = TXmlEngSerializationOptions()) const;
165 * Creates complete copy of the document
168 * @return Complete copy of the document tree
170 IMPORT_C RXmlEngDocument CloneDocumentL() const;
173 * Creates new element from specific namespace to be a root of the document tree.
174 * Any existing document element of the document is destroyed
177 * @param aName Element name
178 * @param aNamespaceUri Element namespace URI
179 * @param aPrefix Element namemespace prefix
180 * @return A new root element
182 IMPORT_C TXmlEngElement CreateDocumentElementL(const TDesC8& aName,
183 const TDesC8& aNamespaceUri = KNullDesC8,
184 const TDesC8& aPrefix = KNullDesC8);
187 * Replaces (and destroys) document element with another one
188 * New document element is added as the last child to the document node
191 * @param aNewDocElement New document tree
193 * @note Use TXmlEngElement::ReconcileNamespacesL() on the new document element
194 * if it or its descendants can contain references to namespace declarations
196 * @see TXmlEngElement::ReconcileNamespacesL()
198 IMPORT_C void SetDocumentElement(TXmlEngElement aNewDocElement);
201 * Get document encoding
204 * @return Encoding of the source XML data.
206 IMPORT_C TPtrC8 XmlEncoding() const;
212 * @return Version number of XML taken from XML declaration
214 IMPORT_C TPtrC8 XmlVersion() const;
217 * Retrieves base URI (if defined) of the document or NULL
220 * @return Document URI
222 IMPORT_C TPtrC8 DocumentUri() const;
225 * Check if document is standalone
228 * @return Whether standalone="true" was specified in XML declaration in the source XML file.
230 IMPORT_C TBool IsStandalone() const;
233 * Sets XML version number to be shown in XML declaration when document is serialized.
236 * @param aVersion New version
238 IMPORT_C void SetXmlVersionL(const TDesC8& aVersion);
241 * Sets location of the document.
242 * Document's URI is used as top-level base URI definition.
245 * @param aUri New document URI
247 IMPORT_C void SetDocumentUriL(const TDesC8& aUri);
250 * Sets 'standalone' attribute of XML declaration for a document
253 * @param aStandalone Is document standalone
255 IMPORT_C void SetStandalone(TBool aStandalone);
258 * Get dom implementation.
261 * @return Object that represents current DOM implementation
263 * @note There is no practical use of implementation object in this version
264 * of API other than for creating new RXmlEngDocument instances, but
265 * it will change in the future, when an implementation object
266 * is used for changing configuration settings at run-time.
268 IMPORT_C RXmlEngDOMImplementation Implementation() const;
271 * Get document element
274 * @return A document element - the top-most element in the document tree
276 IMPORT_C TXmlEngElement DocumentElement() const;
279 * Sets "document" property on the node and all its descendants to be this RXmlEngDocument node
282 * @param aSource Node that should be added.
283 * @return Adopted node
285 IMPORT_C TXmlEngNode AdoptNodeL(TXmlEngNode aSource);
288 * Creates new attribute,
291 * @param aName Name of the atribute; no prefix allowed
292 * @param aValue Value of the attribute (optional)
293 * @return Handler to the newly created attribute
296 * aValue should represent a correct value of an attribute if it is put as is into XML file
297 * (with all characters correctly escaped with entity references when XML spec requires)
299 * TXmlEngElement class provides a rich set of attribute creation methods, which not
300 * just create attribute but also link it into element.
302 * There is no way to create attributes with namespace (despite the DOM spec);
303 * you have to use one of the TXmlEngElement::AddNewAttributeL(..) methods instead
305 * Returned handler is the only reference to the allocated memory
306 * until you have attached the attribute to some element node
308 IMPORT_C TXmlEngAttr CreateAttributeL(const TDesC8& aName,
309 const TDesC8& aValue = KNullDesC8);
312 * Creates new text node and copies the content string into it.
315 * @param aCharacters Text node content
316 * @return Created node
318 IMPORT_C TXmlEngTextNode CreateTextNodeL(const TDesC8& aCharacters = KNullDesC8);
321 * Creates new binary container and copies the content string into it.
322 * Pointer to the container is stored in the document's
323 * data container list that can be fetched using GetDataContainerList().
324 * @see GetDataContainerList( RArray<TXmlEngDataContainer>& aList )
327 * @param aCid Content identifier
328 * @param aData Binary octets
329 * @return Created node
331 IMPORT_C TXmlEngBinaryContainer CreateBinaryContainerL( const TDesC8& aCid,
332 const TDesC8& aData );
335 * Creates new chunk container that stores reference to
337 * Pointer to the container is stored in the document's
338 * data container list that can be fetched using GetDataContainerList().
339 * @see GetDataContainerList( RArray<TXmlEngDataContainer>& aList )
342 * @param aCid Content identifier
343 * @param aChunk RChunk reference
344 * @param aChunkOffset Offset to the binary data in aChunk
345 * @param aDataSize Size of binary data in aChunk
346 * @return Created node
348 IMPORT_C TXmlEngChunkContainer CreateChunkContainerL( const TDesC8& aCid,
349 const RChunk& aChunk,
350 const TInt aChunkOffset,
351 const TInt aDataSize );
354 * Creates new file container that stores reference to
355 * file in file system.
356 * Pointer to the container is stored in the document's
357 * data container list that can be fetched using GetDataContainerList().
358 * @see GetDataContainerList( RArray<TXmlEngDataContainer>& aList )
361 * @param aCid Content identifier
362 * @param aFile RFile reference
363 * @return Created node
365 IMPORT_C TXmlEngFileContainer CreateFileContainerL( const TDesC8& aCid,
366 const RFile& aFile );
369 * Creates new element node that belongs to specific namespace.
370 * A namespace declaration node is created on the element.
373 * @param aNamespaceUri Namespace of new element
374 * @param aPrefix Prefix to use for namespace binding and QName of the element
375 * @param aLocalName Local name of the element
376 * @return Created node
377 * @note If null namespace uri is provided element will be created without namespace.
379 IMPORT_C TXmlEngElement CreateElementL(const TDesC8& aLocalName,
380 const TDesC8& aNamespaceUri = KNullDesC8,
381 const TDesC8& aPrefix = KNullDesC8);
384 * Creates new comment node and copies the content string into it.
387 * @param aText New comment
388 * @return Created node
390 IMPORT_C TXmlEngComment CreateCommentL(const TDesC8& aText = KNullDesC8);
393 * Creates new CDATA section node and copies the content into it.
396 * @param aContents CDATASection content
397 * @return Created node
399 IMPORT_C TXmlEngCDATASection CreateCDATASectionL(const TDesC8& aContents = KNullDesC8);
402 * Creates new entity reference node for aEntityName entity
405 * @param aEntityRef is a string in one of the forms:
407 * - <b>&</b><i>name</i>
408 * - <b>&</b><i>name</i><b>;</b>
409 * where <i>name</i> is the name of the entity
410 * @return Created node
412 * @note < , > , ' , " and other <b>predefined</b> entity references
413 * should not be created with this method. These entity refs are rather
414 * "character references" and encoded/decoded automatically.
416 IMPORT_C TXmlEngEntityReference CreateEntityReferenceL(const TDesC8& aEntityRef);
419 * Creates new processing instruction node and set its "target" and "data" values
422 * @param aTarget Target
424 * @return Created node
426 IMPORT_C TXmlEngProcessingInstruction CreateProcessingInstructionL(const TDesC8& aTarget,
427 const TDesC8& aData = KNullDesC8);
430 * Registers specified attribute as xml:id.
431 * First parametr allows user, to specify sub-tree, not to search whole document.
432 * To search whole tree see @see RegisterXmlId(const TDesC8,const TDesC8)
435 * @param aStartElement Root of tree to search (should be part of the document)
436 * @param aLocalName Name of attribute
437 * @param aNamespaceUri Namespace of new element (default empty)
439 IMPORT_C void RegisterXmlIdL(TXmlEngElement aStartElement,
440 const TDesC8& aLocalName,
441 const TDesC8& aNamespaceUri = KNullDesC8);
444 * Registers specified attribute as xml:id.
445 * Not to search whole tree see @see RegisterXmlId(TXmlEngElement,const TDesC8,const TDesC8)
448 * @param aLocalName Name of attribute
449 * @param aNamespaceUri Namespace of new element (default empty)
451 IMPORT_C void RegisterXmlIdL(const TDesC8& aLocalName,
452 const TDesC8& aNamespaceUri = KNullDesC8);
455 * Looks for element with specified value of xml:id
458 * @param aValue Name of attribute
459 * @return found element or null-element.
461 IMPORT_C TXmlEngElement FindElementByXmlIdL(const TDesC8& aValue ) const;
464 * Retrieves an array of data containers owned by this document.
466 * @note The document ceases to be the owner of data container when data container
467 * (or one of its predecessors) is removed from the document or data container
468 * (or one of its predecessors) becomes a part of another document.
469 * Unlinking data container (or one of its predecessors) doesn't remove
470 * ownership of data container from the this document so the list might
471 * contain containers that are not linked to this document anymore.
473 * @param aList Array of data containers
475 IMPORT_C void GetDataContainerList( RArray<TXmlEngDataContainer>& aList );
478 friend class RXmlEngDOMParser;
479 friend class TXmlEngNode;
480 friend class TXmlEngAttr;
481 friend class TXmlEngElement;
482 friend class RXmlEngDOMImplementation;
489 * @param aInternal Document pointer
491 inline RXmlEngDocument(void* aInternal);
494 * DISABLED for document; CloneDocumentL() must be used
498 inline TXmlEngNode CopyL() const;
501 * DISABLED for document; Destroy() must be used
505 inline void Remove();
508 * DISABLED for document; Destroy() must be used
512 inline void ReplaceWith(TXmlEngNode aNode);
515 * Main implementation of SaveL() functions that puts together all common code
516 * and serializes to buffer or output stream.
519 * @param aNode Root node to be serialized
520 * @param aBuffer buffer with serialized data.
521 * @param aOutputStream stream that should be used during serialization
522 * @param aSaveOptions Options that control how serialization is performed
523 * @return Number of bytes written
524 * @leave KErrNoMemory, KErrGeneral, KXmlEngErrWrongEncoding, KErrDiskFull.
526 TInt SaveNodeL( TXmlEngNode aNode,
528 MXmlEngOutputStream* aOutputStream = NULL,
529 TXmlEngSerializationOptions aOpt = TXmlEngSerializationOptions()) const;
532 * "Secondary" constructor that should be called on every newly created document node.
533 * Initializes container for nodes owned by the document.
535 * The need for such secondary constructor is in the fact that underlying libxml2
536 * library knows nothing about ownership of unlinked nodes -- this feature is
537 * implemented in C++ DOM wrapper.
541 void InitOwnedNodeListL();
544 * Adds aNode to the list of owned nodes - the nodes that are not linked yet into a
545 * document tree, but still destroyed with the document that owns them.
548 * @param aNode Node that should be added to document
550 * In case of OOM (during growing node list container) the argument node is freed with
553 void TakeOwnership(TXmlEngNode aNode);
556 * Remove aNode from the list of owned nodes.
559 * @param aNode Node that should be removed from document
561 void RemoveOwnership(TXmlEngNode aNode);
564 /** Pointer to DOM implementation object */
565 RXmlEngDOMImplementation* iImpl;
567 };// class RXmlEngDocument
571 #include "xmlengdocument.inl"
573 #endif /* XMLENGINE_DOCUMENT_H_INCLUDED */