1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // Document node functions
24 #ifndef XMLENGDOCUMENT_H
25 #define XMLENGDOCUMENT_H
29 #include <xml/dom/xmlengnode.h>
30 #include <xml/dom/xmlengserializationoptions.h>
32 class RXmlEngDOMImplementation;
35 This class represents an XML document in the DOM tree. It stores all nodes
36 and associated information about the XML document.
38 This class implements the interface. Another class, RXmlEngDOMImplementation,
39 provides the implementation. An instance of RXmlEngDOMImplementation must be
40 constructed and opened first and passed to RXmlEngDocument::OpenL().
42 class RXmlEngDocument : public TXmlEngNode
46 Default constructor. An instance of RXmlEngDocument must be "opened" with
47 one of OpenL() overloads before methods are invoked on the object.
49 IMPORT_C RXmlEngDocument();
53 @param aDOMImpl An opened DOM implementation object
54 @leave - One of the system-wide error codes
56 IMPORT_C void OpenL(RXmlEngDOMImplementation& aDOMImpl);
59 Opens the document, initializing it with the internal state pointer from
60 another RXmlEngDocument. This document becomes an alias for the document
61 whose state is represented by aInternal and a change in either document
62 will be reflected in the other. Close() need only be called once, however,
63 it is not an error to call Close() on each RXmlEngDocument.
65 @param aDOMImpl An opened DOM implementation object
66 @param aInternal The internal document state to initialize this object with
67 @leave - One of the system-wide error codes
69 IMPORT_C void OpenL(RXmlEngDOMImplementation& aDOMImpl, void* aInternal);
72 Opens the document and adds aRoot as the root of the DOM tree. If aRoot is
73 currently part of another document, it will be unlinked. Ownership is
74 transferred to this document.
76 @param aDOMImpl An opened DOM implementation object
77 @param aRoot The element that will be the root of the DOM tree
78 @leave - One of the system-wide error codes
80 IMPORT_C void OpenL(RXmlEngDOMImplementation& aDOMImpl, TXmlEngElement aRoot);
83 Closes document: All owned nodes, child nodes, and namespaces are freed. All
84 data containers on the data container list are freed.
86 IMPORT_C void Close();
89 Serializes document tree into a file. For nodes containing binary data in
90 the form of BinaryDataContainer, FileContainer or ChunkContainer, the
91 client can implement custom serialization by implementing the
92 MXmlEngDataSerializer interface and saving a pointer to the customer
93 serializer in the iDataSerializer member of the aSaveOptions parameter. If
94 no custom serialization is specified, the binary data container nodes are
95 serialized like text nodes.
97 If no aRoot is provided, the entire DOM tree is serialized. aRoot does not
98 need to be owned by this document.
100 @param aFileName A file name (with path)
101 @param aRoot Root node to be serialized
102 @param aSaveOptions Options that control how serialization is performed
103 @return Number of bytes written
104 @leave KXmlEngErrWrongEncoding Encoding not understood
105 @leave KXmlEngErrWrongUseOfAPI Document is NULL
106 @leave KXmlEngErrNegativeOutputSize The data to be serialized has a negative size
107 @leave - One of the system-wide error codes
109 IMPORT_C TInt SaveL( const TDesC& aFileName,
110 TXmlEngNode aRoot = TXmlEngNode(),
111 const TXmlEngSerializationOptions& aSaveOptions = TXmlEngSerializationOptions() ) const;
114 Serializes document tree into a file. For nodes containing binary data in
115 the form of BinaryDataContainer, FileContainer or ChunkContainer, the
116 client can implement custom serialization by implementing the
117 MXmlEngDataSerializer interface and saving a pointer to the customer
118 serializer in the iDataSerializer member of the aSaveOptions parameter. If
119 no custom serialization is specified, the binary data container nodes are
120 serialized like text nodes.
122 If no aRoot is provided, the entire DOM tree is serialized. aRoot does not
123 need to be owned by this document.
125 @param aRFs An open file Server session
126 @param aFileName A file name (with path)
127 @param aRoot Root node to be serialized
128 @param aSaveOptions Options that control how serialization is performed
129 @return Number of bytes written
130 @leave KXmlEngErrWrongEncoding Encoding not understood
131 @leave KXmlEngErrWrongUseOfAPI Document is NULL
132 @leave KXmlEngErrNegativeOutputSize The data to be serialized has a negative size
133 @leave - One of the system-wide error codes
135 IMPORT_C TInt SaveL( RFs& aRFs,
136 const TDesC& aFileName,
137 TXmlEngNode aRoot = TXmlEngNode(),
138 const TXmlEngSerializationOptions& aSaveOptions = TXmlEngSerializationOptions() ) const;
141 Serializes document tree into provided output stream, which supports
142 progressive writing of data. For nodes containing binary data in the form
143 of BinaryDataContainer, FileContainer or ChunkContainer, the client can
144 implement custom serialization by implementing the MXmlEngDataSerializer
145 interface and saving a pointer to the customer serializer in the
146 iDataSerializer member of the aSaveOptions parameter. If no custom
147 serialization is specified, the binary data container nodes are serialized
150 If no aRoot is provided, the entire DOM tree is serialized. aRoot does not
151 need to be owned by this document.
153 @param aStream An output stream to write the serialized DOM tree
154 @param aRoot Root node to be serialized
155 @param aSaveOptions Options that control how serialization is performed
156 @return Number of bytes written
157 @leave KXmlEngErrWrongEncoding Encoding not understood
158 @leave KXmlEngErrWrongUseOfAPI Document is NULL
159 @leave KXmlEngErrNegativeOutputSize The data to be serialized has a negative size
160 @leave - One of the system-wide error codes
162 IMPORT_C TInt SaveL( MXmlEngOutputStream& aStream,
163 TXmlEngNode aRoot = TXmlEngNode(),
164 const TXmlEngSerializationOptions& aSaveOptions = TXmlEngSerializationOptions() ) const;
167 Saves document tree into memory buffer.
169 Any existing contents in aBuffer will be deleted. The memory required for
170 aBuffer will be allocated by this method. The method caller must Close()
173 If no aRoot is provided, the entire DOM tree is serialized. aRoot does not
174 need to be owned by this document.
176 @param aBuffer Resulting buffer
177 @param aRoot The root of the subtree to serialize
178 @param aSaveOptions Options that control how serialization is performed
179 @return Size of buffer
180 @leave KXmlEngErrWrongEncoding Encoding not understood
181 @leave KXmlEngErrWrongUseOfAPI Document is NULL
182 @leave KXmlEngErrNegativeOutputSize The data to be serialized has a negative size
183 @leave - One of the system-wide error codes
185 IMPORT_C TInt SaveL(RBuf8& aBuffer,
186 TXmlEngNode aRoot = TXmlEngNode(),
187 const TXmlEngSerializationOptions& aSaveOptions = TXmlEngSerializationOptions()) const;
190 Creates a complete copy of the document and transfers ownership to the
191 caller. The caller is required to call Close() on the new document. The
192 new document is independant from this document and this document may be
193 changed or closed without affecting the new document.
195 @return Complete copy of the document
196 @leave - One of the system-wide error codes
198 IMPORT_C RXmlEngDocument CloneDocumentL() const;
201 Creates a new element from a specific namespace to be the root of the
202 document tree. Any existing document element of the document is destroyed.
204 @param aName Element name
205 @param aNamespaceUri Element namespace URI
206 @param aPrefix Element namemespace prefix
207 @return The new root element
208 @leave KXmlEngErrWrongUseOfAPI No name has been specified
209 @leave - One of the system-wide error codes
211 IMPORT_C TXmlEngElement CreateDocumentElementL(const TDesC8& aName,
212 const TDesC8& aNamespaceUri = KNullDesC8,
213 const TDesC8& aPrefix = KNullDesC8);
216 Replaces (and destroys) the document element.
218 Note: Use TXmlEngElement::ReconcileNamespacesL() on the new document
219 element if it or its descendants can contain references to namespace
220 declarations outside of the element.
222 @param aNewDocElement New document element
223 @see TXmlEngElement::ReconcileNamespacesL()
225 IMPORT_C void SetDocumentElement(TXmlEngElement aNewDocElement);
228 Get document encoding.
229 @return Encoding of the source XML data or TPtrC8("") if none.
231 IMPORT_C TPtrC8 XmlEncoding() const;
235 @return Version number reported by the XML declaration or TPtrC8("") if none.
237 IMPORT_C TPtrC8 XmlVersion() const;
240 Retrieves base URI (if defined) of the document
241 @return Document URI or TPtrC8("") if none.
243 IMPORT_C TPtrC8 DocumentUri() const;
246 Check if document is standalone
247 @return ETrue if standalone="true" was specified in the XML declaration in
250 IMPORT_C TBool IsStandalone() const;
253 Sets XML version number to be shown in XML declaration when document is serialized.
254 @param aVersion Version string
255 @leave - One of the system-wide error codes
257 IMPORT_C void SetXmlVersionL(const TDesC8& aVersion);
260 Sets the location of the document. The document's URI is used as the
261 top-level base URI definition.
262 @param aUri Document URI
263 @leave - One of the system-wide error codes
265 IMPORT_C void SetDocumentUriL(const TDesC8& aUri);
268 Sets "standalone" attribute of XML declaration for the document
269 @param aStandalone Is the document standalone
271 IMPORT_C void SetStandalone(TBool aStandalone);
274 Get the DOM implementation. Ownership is not transferred. Any operation
275 on the returned object will affect this document directly, in particular,
276 a call to RXmlEngDOMImplementation::Close() will cause further operations
277 on this document to fail.
279 @return Object that represents current DOM implementation
281 IMPORT_C RXmlEngDOMImplementation Implementation() const;
284 Get the document element
285 @return The document element -- the top-most element in the document tree
287 IMPORT_C TXmlEngElement DocumentElement() const;
290 Sets the "document" property on the node and all its descendants to be this
292 @param aSource Node that should be added.
294 @leave KXmlEngErrWrongUseOfAPI The node has a parent node, the node is
295 already owned by this document, or the node is a document.
296 @leave - One of the system-wide error codes
298 IMPORT_C TXmlEngNode AdoptNodeL(TXmlEngNode aSource);
301 Creates a new attribute.
303 aValue should represent the correct value of an attribute if it is put
304 as-is into an XML file (with all characters correctly escaped with entity
305 references when XML spec requires)
307 The TXmlEngElement class provides a rich set of attribute creation methods,
308 which not only create attributes but also link them into elements.
312 There is no way to create attributes with namespaces (despite the DOM spec);
313 you have to use one of the TXmlEngElement::AddNewAttributeL(..) methods instead
315 The returned attribute is the only reference to the allocated memory until
316 you have attached the attribute to some element node.
318 @param aName Name of the atribute; no prefix allowed
319 @param aValue Value of the attribute (optional)
320 @return The newly created attribute
321 @leave KXmlEngErrWrongUseOfAPI No name specified
322 @leave - One of the system-wide error codes
324 IMPORT_C TXmlEngAttr CreateAttributeL(const TDesC8& aName,
325 const TDesC8& aValue = KNullDesC8);
328 Creates a new text node and copies the content string into it.
329 @param aCharacters Text node content
330 @return The created node
331 @leave - One of the system-wide error codes
333 IMPORT_C TXmlEngTextNode CreateTextNodeL(const TDesC8& aCharacters = KNullDesC8);
336 Creates a new binary container and copies the specified cid and data into
337 it. A pointer to the container is stored in the document's data container
338 list that can be fetched using GetDataContainerList().
340 @see GetDataContainerList( RArray<TXmlEngDataContainer>& aList )
341 @param aCid Content identifier
342 @param aData Binary octets
343 @return The new binary container
344 @leave - One of the system-wide error codes
346 IMPORT_C TXmlEngBinaryContainer CreateBinaryContainerL( const TDesC8& aCid,
347 const TDesC8& aData );
350 Creates a new chunk container and copies the specified cid into it. A
351 reference to a memory chunk is stored in the container. The memory chunk
352 must stay in scope for the lifetime of the container. A pointer to the
353 container is stored in the document's data container list that can be
354 fetched using GetDataContainerList().
356 @see GetDataContainerList( RArray<TXmlEngDataContainer>& aList )
357 @param aCid Content identifier
358 @param aChunk RChunk reference
359 @param aChunkOffset Offset to the binary data in aChunk
360 @param aDataSize Size of binary data in aChunk
361 @return The new chunk container
362 @leave - One of the system-wide error codes
364 IMPORT_C TXmlEngChunkContainer CreateChunkContainerL( const TDesC8& aCid,
365 const RChunk& aChunk,
366 const TInt aChunkOffset,
367 const TInt aDataSize );
370 Creates a new file container and copies the specified cid into it. A
371 reference to a file is stored in the container. aFile must stay in scope
372 of the lifetime of the container. A pointer to the container is stored in
373 the document's data container list that can be fetched using
374 GetDataContainerList().
376 @see GetDataContainerList( RArray<TXmlEngDataContainer>& aList )
377 @param aCid Content identifier
378 @param aFile The file to reference
379 @return The new file container
380 @leave - One of the system-wide error codes
382 IMPORT_C TXmlEngFileContainer CreateFileContainerL( const TDesC8& aCid,
383 const RFile& aFile );
386 Creates a new element node that belongs to the specific namespace. A
387 namespace declaration node is created on the element.
389 If the provided namespace uri is NULL, the element will be created without
392 @param aNamespaceUri Namespace of new element
393 @param aPrefix Prefix to use for the namespace binding and the QName of the element
394 @param aLocalName Local name of the element
395 @return The created element
396 @leave KXmlEngErrWrongUseOfAPI No name specified
397 @leave - One of the system-wide error codes
399 IMPORT_C TXmlEngElement CreateElementL(const TDesC8& aLocalName,
400 const TDesC8& aNamespaceUri = KNullDesC8,
401 const TDesC8& aPrefix = KNullDesC8);
404 Creates a new comment node and copies the specified string into it.
405 @param aText New comment
406 @return The created node
407 @leave - One of the system-wide error codes
409 IMPORT_C TXmlEngComment CreateCommentL(const TDesC8& aText = KNullDesC8);
412 Creates a new CDATA section node and copies the specified string into it.
413 @param aContents CDATASection content
414 @return The created node
415 @leave - One of the system-wide error codes
417 IMPORT_C TXmlEngCDATASection CreateCDATASectionL(const TDesC8& aContents = KNullDesC8);
420 Creates a new entity reference node and copies the specified string into
423 Note: < , > , ' , " and other predefined entity references
424 should not be created with this method. These entity references are rather
425 "character references" and are encoded/decoded automatically.
427 @param aEntityRef is a string in one of these forms:
431 where name is the name of the entity
432 @return The new entity reference
433 @leave KXmlEngErrWrongUseOfAPI No entity specified
434 @leave - One of the system-wide error codes
436 IMPORT_C TXmlEngEntityReference CreateEntityReferenceL(const TDesC8& aEntityRef);
439 Creates a new empty Document Fragment node. The document fragment is owned by
441 @return The created document fragment
442 @leave - One of the system-wide error codes
444 IMPORT_C TXmlEngDocumentFragment CreateDocumentFragmentL();
447 Creates a new processing instruction node and copies "target" and "data"
450 @param aTarget Target
452 @return The created processing instruction
453 @leave KXmlEngErrWrongUseOfAPI No target specified
454 @leave - One of the system-wide error codes
456 IMPORT_C TXmlEngProcessingInstruction CreateProcessingInstructionL(const TDesC8& aTarget,
457 const TDesC8& aData = KNullDesC8);
460 Sets the specified attribute as a xml:id attribute, starting at
461 aStartElement and recursing through the subtree. To set the specified
462 attribute as a xml:id attribute for the entire DOM tree, see
463 RegisterXmlId(const TDesC8&,const TDesC8&).
465 @param aStartElement Root of the subtree to recurse
466 @param aLocalName Name of the attribute
467 @param aNamespaceUri Namespace of the new element (default empty)
468 @leave KXmlEngErrWrongUseOfAPI The starting element is NULL, the attribute
469 name is not specified, the starting element is the document, or the
470 starting element does not belong to a document.
471 @leave KErrAlreadyExists The attribute is already set to be xml:id
472 @leave - One of the system-wide error codes
474 IMPORT_C void RegisterXmlIdL(TXmlEngElement aStartElement,
475 const TDesC8& aLocalName,
476 const TDesC8& aNamespaceUri = KNullDesC8);
479 Sets the specified attribute as a xml:id attribute, recursing through the
480 entire DOM tree. In order to specify a subtree only, see
481 RegisterXmlId(TXmlEngElement,const TDesC8,const TDesC8).
483 @param aLocalName Name of attribute
484 @param aNamespaceUri Namespace of new element (default empty)
485 @leave KXmlEngErrWrongUseOfAPI The document is NULL
486 @leave KErrAlreadyExists The attribute is already set to be xml:id
487 @leave - One of the system-wide error codes
489 IMPORT_C void RegisterXmlIdL(const TDesC8& aLocalName,
490 const TDesC8& aNamespaceUri = KNullDesC8);
493 Looks for an element with the specified xml:id attribute
494 @param aValue Name of attribute
495 @return The found element or a NULL element if not found
496 @leave - One of the system-wide error codes
498 IMPORT_C TXmlEngElement FindElementByXmlIdL(const TDesC8& aValue ) const;
501 Retrieves an array of data containers owned by this document.
503 Note: The document ceases to be the owner of a data container when the data
504 container (or one of its predecessors) is removed from the document or
505 becomes part of another document. Unlinking a data container (or one of its
506 predecessors) doesn't remove ownership of the data container from this
507 document so the list might contain containers that are not linked to this
510 @param aList Array of data containers
511 @return KErrNone if successful or one of the system wide error codes otherwise
513 IMPORT_C TInt GetDataContainerList( RArray<TXmlEngDataContainer>& aList );
516 friend class RXmlEngDOMParser;
517 friend class TXmlEngNode;
518 friend class TXmlEngAttr;
519 friend class TXmlEngElement;
520 friend class RXmlEngDOMImplementation;
525 @param aInternal Document pointer
527 inline RXmlEngDocument(void* aInternal);
530 DISABLED for document; CloneDocumentL() must be used
532 inline TXmlEngNode CopyL() const;
535 DISABLED for document; Close() must be used
537 inline void Remove();
540 DISABLED for document; Close() must be used
542 inline void ReplaceWith(TXmlEngNode aNode);
543 inline void ReplaceWithL(TXmlEngNode aNode);
546 DISABLED for document; Close() must be used
548 inline TXmlEngNode SubstituteForL(TXmlEngNode aNode);
551 TInt SaveNodeL( TXmlEngNode aNode,
553 MXmlEngOutputStream* aOutputStream = NULL,
554 TXmlEngSerializationOptions aOpt = TXmlEngSerializationOptions()) const;
556 void InitOwnedNodeListL();
557 void TakeOwnership(TXmlEngNode aNode);
558 void RemoveOwnership(TXmlEngNode aNode);
561 /** Pointer to DOM implementation object */
562 RXmlEngDOMImplementation* iImpl;
564 };// class RXmlEngDocument
568 #include <xml/dom/xmlengdocument.inl>
570 #endif /* XMLENGDOCUMENT_H */