qmake/generators/xmloutput.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the qmake application of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef XMLOUTPUT_H
       
    43 #define XMLOUTPUT_H
       
    44 
       
    45 #include <qtextstream.h>
       
    46 #include <qstack.h>
       
    47 
       
    48 QT_BEGIN_NAMESPACE
       
    49 
       
    50 class XmlOutput
       
    51 {
       
    52 public:
       
    53     enum ConverstionType {
       
    54         NoConversion,       // No change
       
    55         EscapeConversion,   // Use '\"'
       
    56         XMLConversion       // Use &quot;
       
    57     };
       
    58     enum XMLFormat {
       
    59         NoNewLine,          // No new lines, unless added manually
       
    60         NewLine             // All properties & tags indented on new lines
       
    61     };
       
    62     enum XMLState {
       
    63         Bare,               // Not in tag or attribute
       
    64         Tag,                // <tagname attribute1="value"
       
    65         Attribute           //  attribute2="value">
       
    66     };
       
    67     enum XMLType {
       
    68         tNothing,           // No XML output, and not state change
       
    69         tRaw,               // Raw text (no formating)
       
    70         tDeclaration,       // <?xml version="x.x" encoding="xxx"?>
       
    71         tTag,               // <tagname attribute1="value"
       
    72         tCloseTag,          // Closes an open tag
       
    73         tAttribute,         //  attribute2="value">
       
    74         tData,              // Tag data (formating done)
       
    75         tComment,           // <!-- Comment -->
       
    76         tCDATA              // <![CDATA[ ... ]]>
       
    77     };
       
    78 
       
    79     XmlOutput(QTextStream &file, ConverstionType type = XMLConversion);
       
    80     ~XmlOutput();
       
    81 
       
    82     // Settings
       
    83     void setIndentString(const QString &indentString);
       
    84     QString indentString();
       
    85     void setIndentLevel(int level);
       
    86     int indentLevel();
       
    87     void setState(XMLState state);
       
    88     XMLState state();
       
    89 
       
    90 
       
    91     struct xml_output {
       
    92         XMLType xo_type;    // Type of struct instance
       
    93         QString xo_text;    // Tag/Attribute name/xml version
       
    94         QString xo_value;   // Value of attributes/xml encoding
       
    95 
       
    96         xml_output(XMLType type, const QString &text, const QString &value)
       
    97             : xo_type(type), xo_text(text), xo_value(value) {}
       
    98         xml_output(const xml_output &xo)
       
    99             : xo_type(xo.xo_type), xo_text(xo.xo_text), xo_value(xo.xo_value) {}
       
   100     };
       
   101 
       
   102     // Streams
       
   103     XmlOutput& operator<<(const QString& o);
       
   104     XmlOutput& operator<<(const xml_output& o);
       
   105 
       
   106 private:
       
   107     void increaseIndent();
       
   108     void decreaseIndent();
       
   109     void updateIndent();
       
   110 
       
   111     QString doConversion(const QString &text);
       
   112 
       
   113     // Output functions
       
   114     void newTag(const QString &tag);
       
   115     void newTagOpen(const QString &tag);
       
   116     void closeOpen();
       
   117     void closeTag();
       
   118     void closeTo(const QString &tag);
       
   119     void closeAll();
       
   120 
       
   121     void addDeclaration(const QString &version, const QString &encoding);
       
   122     void addRaw(const QString &rawText);
       
   123     void addAttribute(const QString &attribute, const QString &value);
       
   124     void addData(const QString &data);
       
   125 
       
   126     // Data
       
   127     QTextStream &xmlFile;
       
   128     QString indent;
       
   129 
       
   130     QString currentIndent;
       
   131     int currentLevel;
       
   132     XMLState currentState;
       
   133 
       
   134     XMLFormat format;
       
   135     ConverstionType conversion;
       
   136     QStack<QString> tagStack;
       
   137 };
       
   138 
       
   139 inline XmlOutput::xml_output noxml()
       
   140 {
       
   141     return XmlOutput::xml_output(XmlOutput::tNothing, QString(), QString());
       
   142 }
       
   143 
       
   144 inline XmlOutput::xml_output raw(const QString &rawText)
       
   145 {
       
   146     return XmlOutput::xml_output(XmlOutput::tRaw, rawText, QString());
       
   147 }
       
   148 
       
   149 inline XmlOutput::xml_output declaration(const QString &version = QString("1.0"),
       
   150                                          const QString &encoding = QString())
       
   151 {
       
   152     return XmlOutput::xml_output(XmlOutput::tDeclaration, version, encoding);
       
   153 }
       
   154 
       
   155 inline XmlOutput::xml_output decl(const QString &version = QString("1.0"),
       
   156                                   const QString &encoding = QString())
       
   157 {
       
   158     return declaration(version, encoding);
       
   159 }
       
   160 
       
   161 inline XmlOutput::xml_output tag(const QString &name)
       
   162 {
       
   163     return XmlOutput::xml_output(XmlOutput::tTag, name, QString());
       
   164 }
       
   165 
       
   166 inline XmlOutput::xml_output closetag()
       
   167 {
       
   168     return XmlOutput::xml_output(XmlOutput::tCloseTag, QString(), QString());
       
   169 }
       
   170 
       
   171 inline XmlOutput::xml_output closetag(const QString &toTag)
       
   172 {
       
   173     return XmlOutput::xml_output(XmlOutput::tCloseTag, toTag, QString());
       
   174 }
       
   175 
       
   176 inline XmlOutput::xml_output closeall()
       
   177 {
       
   178     return XmlOutput::xml_output(XmlOutput::tCloseTag, QString(), QString("all"));
       
   179 }
       
   180 
       
   181 inline XmlOutput::xml_output attribute(const QString &name,
       
   182                                        const QString &value)
       
   183 {
       
   184     return XmlOutput::xml_output(XmlOutput::tAttribute, name, value);
       
   185 }
       
   186 
       
   187 inline XmlOutput::xml_output attr(const QString &name,
       
   188                                   const QString &value)
       
   189 {
       
   190     return attribute(name, value);
       
   191 }
       
   192 
       
   193 inline XmlOutput::xml_output data(const QString &text = QString())
       
   194 {
       
   195     return XmlOutput::xml_output(XmlOutput::tData, text, QString());
       
   196 }
       
   197 
       
   198 inline XmlOutput::xml_output comment(const QString &text)
       
   199 {
       
   200     return XmlOutput::xml_output(XmlOutput::tComment, text, QString());
       
   201 }
       
   202 
       
   203 inline XmlOutput::xml_output cdata(const QString &text)
       
   204 {
       
   205     return XmlOutput::xml_output(XmlOutput::tCDATA, text, QString());
       
   206 }
       
   207 
       
   208 QT_END_NAMESPACE
       
   209 
       
   210 #endif // XMLOUTPUT_H