Back to index

lightning-sunbird  0.9+nobinonly
XMLUtils.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is TransforMiiX XSLT processor code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * The MITRE Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Keith Visco <kvisco@ziplink.net> (Original Author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00043 #ifndef MITRE_XMLUTILS_H
00044 #define MITRE_XMLUTILS_H
00045 
00046 #include "txCore.h"
00047 #include "nsCOMPtr.h"
00048 #include "nsDependentSubstring.h"
00049 #include "nsIAtom.h"
00050 #include "txXPathNode.h"
00051 
00052 #ifndef TX_EXE
00053 #include "nsIParserService.h"
00054 #endif
00055 
00056 class nsIAtom;
00057 class txNamespaceMap;
00058 
00059 class txExpandedName {
00060 public:
00061     txExpandedName() : mNamespaceID(kNameSpaceID_None)
00062     {
00063     }
00064 
00065     txExpandedName(PRInt32 aNsID,
00066                    nsIAtom* aLocalName) : mNamespaceID(aNsID),
00067                                           mLocalName(aLocalName)
00068     {
00069     }
00070 
00071     txExpandedName(const txExpandedName& aOther) :
00072         mNamespaceID(aOther.mNamespaceID),
00073         mLocalName(aOther.mLocalName)
00074     {
00075     }
00076 
00077     ~txExpandedName()
00078     {
00079     }
00080     
00081     nsresult init(const nsAString& aQName, txNamespaceMap* aResolver,
00082                   MBool aUseDefault);
00083 
00084     void reset()
00085     {
00086         mNamespaceID = kNameSpaceID_None;
00087         mLocalName = nsnull;
00088     }
00089 
00090     PRBool isNull()
00091     {
00092         return mNamespaceID == kNameSpaceID_None && !mLocalName;
00093     }
00094 
00095     txExpandedName& operator = (const txExpandedName& rhs)
00096     {
00097         mNamespaceID = rhs.mNamespaceID;
00098         mLocalName = rhs.mLocalName;
00099         return *this;
00100     }
00101 
00102     MBool operator == (const txExpandedName& rhs) const
00103     {
00104         return ((mLocalName == rhs.mLocalName) &&
00105                 (mNamespaceID == rhs.mNamespaceID));
00106     }
00107 
00108     MBool operator != (const txExpandedName& rhs) const
00109     {
00110         return ((mLocalName != rhs.mLocalName) ||
00111                 (mNamespaceID != rhs.mNamespaceID));
00112     }
00113 
00114     PRInt32 mNamespaceID;
00115     nsCOMPtr<nsIAtom> mLocalName;
00116 };
00117 
00118 #ifdef TX_EXE
00119 extern "C" int MOZ_XMLCheckQName(const char* ptr, const char* end,
00120                                  int ns_aware, const char** colon);
00121 extern "C" int MOZ_XMLIsLetter(const char* ptr);
00122 extern "C" int MOZ_XMLIsNCNameChar(const char* ptr);
00123 #else
00124 extern nsIParserService *gTxParserService;
00125 #endif
00126 
00127 class XMLUtils {
00128 
00129 public:
00130     static nsresult splitExpatName(const PRUnichar *aExpatName,
00131                                    nsIAtom **aPrefix, nsIAtom **aLocalName,
00132                                    PRInt32* aNameSpaceID);
00133     static nsresult splitQName(const nsAString& aName, nsIAtom** aPrefix,
00134                                nsIAtom** aLocalName);
00135     static const nsDependentSubstring getLocalPart(const nsAString& src);
00136 
00137     /*
00138      * Returns true if the given character is whitespace.
00139      */
00140     static MBool isWhitespace(const PRUnichar& aChar)
00141     {
00142         return (aChar <= ' ' &&
00143                 (aChar == ' ' || aChar == '\r' ||
00144                  aChar == '\n'|| aChar == '\t'));
00145     }
00146 
00150     static PRBool isWhitespace(const nsAFlatString& aText);
00151 
00155     static void normalizePIValue(nsAString& attValue);
00156 
00160     static PRBool isValidQName(const nsAFlatString& aQName,
00161                                const PRUnichar** aColon)
00162     {
00163 #ifdef TX_EXE
00164         const PRUnichar* end;
00165         aQName.EndReading(end);
00166 
00167         const char *colonPtr;
00168         int result = MOZ_XMLCheckQName(NS_REINTERPRET_CAST(const char*,
00169                                                            aQName.get()),
00170                                        NS_REINTERPRET_CAST(const char*,
00171                                                            end),
00172                                        PR_TRUE, &colonPtr);
00173         *aColon = NS_REINTERPRET_CAST(const PRUnichar*, colonPtr);
00174         return result == 0;
00175 #else
00176         return NS_SUCCEEDED(gTxParserService->CheckQName(aQName, PR_TRUE, aColon));
00177 #endif
00178     }
00179 
00183     static PRBool isLetter(PRUnichar aChar)
00184     {
00185 #ifdef TX_EXE
00186         return MOZ_XMLIsLetter(NS_REINTERPRET_CAST(const char*, &aChar));
00187 #else
00188         return gTxParserService->IsXMLLetter(aChar);
00189 #endif
00190     }
00191 
00195     static PRBool isNCNameChar(PRUnichar aChar)
00196     {
00197 #ifdef TX_EXE
00198         return MOZ_XMLIsNCNameChar(NS_REINTERPRET_CAST(const char*, &aChar));
00199 #else
00200         return gTxParserService->IsXMLNCNameChar(aChar);
00201 #endif
00202     }
00203 
00204     /*
00205      * Walks up the document tree and returns true if the closest xml:space
00206      * attribute is "preserve"
00207      */
00208     static MBool getXMLSpacePreserve(const txXPathNode& aNode);
00209 };
00210 
00211 #endif