XML Mill  1.0.0
A GUI based XML editor with a memory.
xmlsyntaxhighlighter.cpp
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the examples of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:BSD$
00010 ** You may use this file under the terms of the BSD license as follows:
00011 **
00012 ** "Redistribution and use in source and binary forms, with or without
00013 ** modification, are permitted provided that the following conditions are
00014 ** met:
00015 **   * Redistributions of source code must retain the above copyright
00016 **     notice, this list of conditions and the following disclaimer.
00017 **   * Redistributions in binary form must reproduce the above copyright
00018 **     notice, this list of conditions and the following disclaimer in
00019 **     the documentation and/or other materials provided with the
00020 **     distribution.
00021 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
00022 **     the names of its contributors may be used to endorse or promote
00023 **     products derived from this software without specific prior written
00024 **     permission.
00025 **
00026 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00030 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
00037 ** $QT_END_LICENSE$
00038 **
00039 ****************************************************************************/
00040 
00041 #include "xmlsyntaxhighlighter.h"
00042 
00043 XmlSyntaxHighlighter::XmlSyntaxHighlighter( QTextDocument* parent )
00044 : QSyntaxHighlighter( parent )
00045 {
00046   HighlightingRule rule;
00047 
00048   // tag format
00049   tagFormat.setForeground( QColor( 70, 70, 110 ) );
00050   //tagFormat.setFontWeight(QFont::Bold);
00051   rule.pattern = QRegExp( "(<[a-zA-Z0-9_:]+\\b|<\\?[a-zA-Z0-9_:]+\\b|\\?>|>|/>|</[a-zA-Z0-9_:]+>)" );
00052   rule.format = tagFormat;
00053   highlightingRules.append( rule );
00054 
00055   // attribute format
00056   attributeFormat.setForeground( QColor( 160, 10, 10 ) );
00057   rule.pattern = QRegExp( "[a-zA-Z0-9_:]+=" );
00058   rule.format = attributeFormat;
00059   highlightingRules.append( rule );
00060 
00061   // attribute content format
00062   attributeContentFormat.setForeground( QColor( 160, 10, 130 ) );
00063   rule.pattern = QRegExp( "(\"[^\"]*\"|'[^']*')" );
00064   rule.format = attributeContentFormat;
00065   highlightingRules.append( rule );
00066 
00067   commentFormat.setForeground( QColor( 30, 130, 0 ) );
00068   commentFormat.setFontItalic( true );
00069 
00070   commentStartExpression = QRegExp( "<!--" );
00071   commentEndExpression = QRegExp( "-->" );
00072 }
00073 
00074 void XmlSyntaxHighlighter::highlightBlock( const QString& text )
00075 {
00076   foreach (const HighlightingRule &rule, highlightingRules)
00077   {
00078    QRegExp expression( rule.pattern );
00079    int index = text.indexOf( expression );
00080    while( index >= 0 )
00081    {
00082      int length = expression.matchedLength();
00083      setFormat( index, length, rule.format );
00084      index = text.indexOf( expression, index + length );
00085    }
00086   }
00087   setCurrentBlockState( 0 );
00088 
00089   int startIndex = 0;
00090   if( previousBlockState() != 1 )
00091     startIndex = text.indexOf( commentStartExpression );
00092 
00093   while( startIndex >= 0 )
00094   {
00095     int endIndex = text.indexOf( commentEndExpression, startIndex );
00096     int commentLength;
00097     if( endIndex == -1 )
00098     {
00099       setCurrentBlockState( 1 );
00100       commentLength = text.length() - startIndex;
00101     }
00102     else
00103     {
00104       commentLength = endIndex - startIndex
00105         + commentEndExpression.matchedLength();
00106     }
00107     setFormat( startIndex, commentLength, commentFormat );
00108     startIndex = text.indexOf( commentStartExpression,
00109                                startIndex + commentLength );
00110   }
00111 }