InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
CXMLOutStream.h
1 //========================================================================================
2 //
3 // $File$
4 //
5 // Owner: Ryan Gano
6 //
7 // $Author$
8 //
9 // $DateTime$
10 //
11 // $Revision$
12 //
13 // $Change$
14 //
15 // Copyright 1997-2010 Adobe Systems Incorporated. All rights reserved.
16 //
17 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance
18 // with the terms of the Adobe license agreement accompanying it. If you have received
19 // this file from a source other than Adobe, then your use, modification, or
20 // distribution of it requires the prior written permission of Adobe.
21 //
22 //========================================================================================
23 
24 #ifndef __CXMLOutStream__
25 #define __CXMLOutStream__
26 
27 #include "IXMLOutStream.h"
28 #include "CPMUnknown.h"
29 
30 #include "K2Stack.h"
31 #include "K2Vector.h"
32 #include "KeyValuePair.h"
33 #include "UTF16TextCharStreamBuf.h"
34 
35 
36 
37 //----------------------------------------------------------------------------------------
38 // CLASS XMLStream - base implementation of IXMLOutStream
39 //----------------------------------------------------------------------------------------
40 
41 class CXMLOutStream : public CPMUnknown<IXMLOutStream>
42 {
43 public:
44 
45 
46  CXMLOutStream(IPMUnknown* boss);
47  virtual ~CXMLOutStream();
48 
50 
51  //----- Stream
52  // must be overriden by derived classes. Call Initialize on Open() and Terminante on Close()
53 
54  virtual bool16 Open(eEncodingType encoding = kUTF16, bool16 stripInvalid = kTrue) = 0;
55  virtual void Close() = 0;
56 
57  virtual ErrorCode Flush();
58  // force all pending writes to happen
59 
60  //----- Standard elements and data
61 
62  virtual void DocHeader(const WideString& version, bool16 standalone);
63  // Write the XML header for this document
64  virtual void TextDeclaration(const WideString& version);
65  // Write the XML text declaration
66  virtual void DocType(const WideString& root, const WideString& URI);
67  // Write a SYSTEM DocType with root element
68  // and DTD pointed to by URI (auto adds quotes)
69  virtual void DocType(const WideString& root, const WideString& name, const WideString& URI);
70  // Write a PUBLIC DocType with root element (auto adds brackets)
71  // and DTD pointed to by URI (auto adds quotes) and a name of public DTD (auto adds quotes)
72  virtual void BeginDocType(const WideString& root, bool16 newLine);
73  // Begin definition of an Internal DTD with root element.
74  virtual void BeginDocType(const WideString& root, const WideString& systemIDLiteral, bool16 newLine);
75  // Begin definition of an external SYSTEM DTD with root element.
76  virtual void BeginDocType(const WideString& root, const WideString& publicIDLiteral, const WideString& systemIDLiteral, bool16 newLine);
77  // Begin definition of an external PUBLIC DTD with root element.
78  virtual void EndDocType(bool16 newLine);
79  // Ends definition of an Internal DTD.
80  virtual void WriteAttributeList(const WideString &elementName, const AttributeDfnList &dfns, bool16 newLine);
81  // Write a declaration for an ATTLIST.
82 
83  virtual void BeginElementTypeDecl(const WideString& elementName, bool16 newLine);
84  // Begin an element type declaration. i.e. "<!ELEMENT elementName "
85  virtual void EndElementTypeDecl(bool16 newLine);
86  // End an element type declaration. i.e. ">"
87  virtual void BeginContentModelDecl(eContentParticleSeparator separator, bool16 newLine);
88  // Begin a content model declaration. i.e. ", ("
89  virtual void EndContentModelDecl(eContentParticleOccurrence occurrence, bool16 newLine);
90  // End a content model declaration. i.e. ")+"
91  virtual void BeginMixedContentModelDecl(eContentParticleSeparator separator, bool16 newLine);
92  // Begin a mixed content model declaration. i.e. ", (#PCDATA"
93  virtual void EndMixedContentModelDecl(bool16 writeOccurrence, bool16 newLine);
94  // End a mixed content model declaration. i.e. ")*"
95  virtual void WriteSeparator(eContentParticleSeparator separator, bool16 newLine);
96  // Write separator
97  virtual void WriteContentParticle(const WideString& contentParticleName, eContentParticleOccurrence occurrence, bool16 newLine);
98  // Write content particle
99  virtual void WriteKeyword(eKeyword keyword, bool16 newLine);
100  // Write a keyword
101  virtual void BeginAttListDecl(const WideString& elementName, bool16 newLine);
102  // Begin an ATTLIST declaration
103  virtual void EndAttListDecl(bool16 newLine);
104  // End an ATTLIST declaration
105  virtual void WriteName(const WideString& name, bool16 newLine);
106  // Write a name
107  virtual void WriteAttributeType(eAttributeType attType, bool16 newLine);
108  // Write an attribute type
109  virtual void WriteAttributeValue(const WideString& attValue, bool16 newLine);
110  // Write an attribute value
111  virtual void BeginEntityDecl(const WideString& entityName, bool16 generalEntity, bool16 newLine);
112  // Begin an ENTITY declaration. i.e. "<!ENTITY entityName " or "<!ENTITY % entityName "
113  virtual void EndEntityDecl(bool16 newLine);
114  // End an ENTITY declaration. i.e. ">"
115  virtual void WriteLiteral(const WideString& literal, bool16 wrapInQuotes, bool16 newLine);
116  // Write literal. i.e. ""literal""
117  virtual void BeginNotationDecl(const WideString& notationName, bool16 newLine);
118  // Begin a NOTATION declaration. i.e. "<!NOTATION notationName "
119  virtual void EndNotationDecl(bool16 newLine);
120  // End a NOTATION declaration. i.e. ">"
121 
122  virtual void Push(const WideString& tagName, bool16 newLine);
123  // Use push to write a new tag and optional data after it, standard
124  // entities are escaped.
125  virtual void PushWithAttributes(const WideString& tagName, const AttributeList& attrs, bool16 newLine);
126  // Use push to write a new tag with attributes and optional data after it,
127  //standard entities are escaped.
128  virtual ErrorCode Pop(bool16 newLine);
129  // Use pop to write the end tag for the top tag on the stack and to remove
130  // it from the stack
131  virtual void Write(const WideString& data, bool16 newLine, bool16 escape );
132  // Write will output data to stream at current position, and escape
133  // the standard 5 entities, unless a CData or PI section has been started
134  // with escaping off
135  virtual void WritePlatformText(IPMStream *data, int32 start, int32 count, bool16 newLine = kFalse, bool16 escape = kTrue);
136  // WritePlatformText will output data to stream at current position, and escape
137  // the standard 5 entities, unless a CData or PI section has been started
138  // with escaping off
139  virtual void WriteEmpty(const WideString& tagName, bool16 newLine);
140  // Write an empty tag such as '<foo/>'
141  virtual void WriteEmptyWithAttributes(const WideString& tag, const AttributeList& attrs, bool16 newLine);
142  // Write an empty tag with attributes such as '<foo bar = true/>'
143  virtual void Comment(const WideString& comment, bool16 newLine);
144  // Comment will output a comment at the current stream position
145  // with no escaping of entities
146 
147  virtual void BeginCData(const WideString& data, bool16 escape);
148  // Begin a CData section data and subsequent calls to Write() will escape entities or not
149  // depending on bool - escape.
150  virtual void EndCData(bool16 newLine);
151  // end a CDataSection, with a newLine after if neccesary.
152 
153  virtual void BeginPI(const WideString& piTarget, const WideString& piData, bool16 newLine, bool16 escape);
154  // Begin a Processing Instruction section.
155  // until EndCdata() is called. NewLine after the tag if necessary
156  virtual void EndPI(bool16 newLine);
157  // end a Processing Instruction, with a newLine after if neccesary
158 
159 
160  //----- Formatting
161 
162  virtual void IndentLevel(bool increase);
163  // Indent level will determine how much to indent each write
164  // to the output stream, only effective if Indent is on
165  virtual void Indent(bool on);
166  // If on, each output operation is indented based on the current indent level
167  virtual bool16 DoIndent(void) const;
168  // whether indent is on
169  virtual void WriteIndent(void);
170  // output the current indent
171 
172  virtual void ResetEntityMap(bool16 addStandardXMLEntity = kTrue);
173  // Clears Entity map & resets the "default" set of mappings.
174 
175  virtual void AppendEntityMap( EntityMap& map);
176  // Add Character Entity References
177 
178  //----- Utility
179  virtual bool CheckStatus();
180 
181 
182 
183 protected:
184 
185  //-----
186  // Set-up
187  // Derived classes must call these methods before use, and before destruction of the stream
188  // to ensure that all data output is properly flushed. This call permanately sets the encoding
189  // type of the stream.
190 
191  virtual ErrorCode Initialize(std::streambuf* destBuf, eEncodingType encoding = kUTF16, bool16 stripInvalid = kTrue, bool16 showCannotEncodeAlert = kFalse);
192  // Use to initialize the stream with the indicated conversion, outputing result to destBuf
193  virtual ErrorCode Terminate();
194  // Call before the destruction of destBuf passed in to Initialize().
195  // After call to terminate, stream is no longer availabe for any output
196 
197  virtual ErrorCode sputnEsc(const WideString& source, bool16 bJustWriteIt = kFalse);
198  // Writes to the stream, escaping entities, if bJustWriteIt is set don't try and translate or enclose characters in PIs
199 
200 private:
201 
202  bool16 IsValidXMLChar(UTF16TextChar c);
203  void EncodeChar(UTF16TextChar charToEncode);
204  bool16 SubstituteValidChar(UTF16TextChar& c);
205  void WriteOccurrence(eContentParticleOccurrence occurrence);
206  void EscAttrValueChars(const WideString& source, WideString& dest);
207  void InsertToEscBuf(int32& index, UTF16TextChar c);
208 
209 private:
210 
211  K2::UTF16TextCharStreamBuf* fConvBuf;
212  std::streambuf* fFinalBuf;
213 
214  TagStack fTags;
215 
216  EntityMap fEntities;
217  EntityMap fAttrEntities;
218  UTF16TextChar* fEscBuf;
219  int32 fEscLen;
220 
221  int32 fIndentLevel;
222  bool fDoIndent;
223  eEncodingType fEncoding;
224  bool16 fCData;
225  bool16 fPI;
226  bool16 fDoEscape;
227  bool16 fStrip;
228  bool16 fShowCannotEncodeAlert; // display an alert if characters cannot be encoded
229 
230  bool16 fInitialized; // this flag check internal integrity
231 };
232 
233 
234 #endif //__CXMLOutStream