# XMLUri.hpp

Go to the documentation of this file.

    00001 /*
    00002  * Licensed to the Apache Software Foundation (ASF) under one or more
    00003  * contributor license agreements.  See the NOTICE file distributed with
    00004  * this work for additional information regarding copyright ownership.
    00005  * The ASF licenses this file to You under the Apache License, Version 2.0
    00006  * (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  *
    00009  *      http://www.apache.org/licenses/LICENSE-2.0
    00010  *
    00011  * Unless required by applicable law or agreed to in writing, software
    00012  * distributed under the License is distributed on an "AS IS" BASIS,
    00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    00014  * See the License for the specific language governing permissions and
    00015  * limitations under the License.
    00016  */
    00017
    00018 /*
    00019  * $Id: XMLUri.hpp 557254 2007-07-18 13:28:54Z amassari $
    00020  */
    00021
    00022 #if !defined(XERCESC_INCLUDE_GUARD_XMLURI_HPP)
    00023 #define XERCESC_INCLUDE_GUARD_XMLURI_HPP
    00024
    00025 #include <xercesc/util/XMemory.hpp>
    00026 #include <xercesc/util/XMLString.hpp>
    00027
    00028 #include <xercesc/internal/XSerializable.hpp>
    00029 #include <xercesc/framework/XMLBuffer.hpp>
    00030
    00031 XERCES_CPP_NAMESPACE_BEGIN
    00032
    00033 /*
    00034  * This class is a direct port of Java's URI class, to distinguish
    00035  * itself from the XMLURL, we use the name XMLUri instead of
    00036  * XMLURI.
    00037  *
    00038  * TODO: how to relate XMLUri and XMLURL since URL is part of URI.
    00039  *
    00040  */
    00041
    00042 class XMLUTIL_EXPORT XMLUri : public XSerializable, public XMemory
    00043 {
    00044 public:
    00045
    00046     // -----------------------------------------------------------------------
    00047     //  Constructors and Destructor
    00048     // -----------------------------------------------------------------------
    00049
    00078     XMLUri(const XMLCh* const    uriSpec,
    00079            MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
    00080
    00097     XMLUri(const XMLUri* const  baseURI
    00098          , const XMLCh* const   uriSpec
    00099          , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
    00100
    00104     XMLUri(const XMLUri& toCopy);
    00105     XMLUri& operator=(const XMLUri& toAssign);
    00106
    00107     virtual ~XMLUri();
    00108
    00109     // -----------------------------------------------------------------------
    00110     //  Getter methods
    00111     // -----------------------------------------------------------------------
    00117     const XMLCh* getUriText() const;
    00118
    00124      const XMLCh* getScheme() const;
    00125
    00131      const XMLCh* getUserInfo() const;
    00132
    00133
    00139      const XMLCh* getHost() const;
    00140
    00146      int getPort() const;
    00147
    00153      const XMLCh* getRegBasedAuthority() const;
    00154
    00161      const XMLCh* getPath() const;
    00162
    00170      const XMLCh* getQueryString() const;
    00171
    00179      const XMLCh* getFragment() const;
    00180
    00181     // -----------------------------------------------------------------------
    00182     //  Setter methods
    00183     // -----------------------------------------------------------------------
    00184
    00192      void setScheme(const XMLCh* const newScheme);
    00193
    00201      void setUserInfo(const XMLCh* const newUserInfo);
    00202
    00213      void setHost(const XMLCh* const newHost);
    00214
    00224      void setPort(int newPort);
    00225
    00234      void setRegBasedAuthority(const XMLCh* const newRegAuth);
    00235
    00256      void setPath(const XMLCh* const newPath);
    00257
    00266      void setQueryString(const XMLCh* const newQueryString);
    00267
    00276      void setFragment(const XMLCh* const newFragment);
    00277
    00278      // -----------------------------------------------------------------------
    00279     //  Miscellaneous methods
    00280     // -----------------------------------------------------------------------
    00281
    00289     static bool isURIString(const XMLCh* const uric);
    00290
    00294     static bool isValidURI( const XMLUri* const baseURI
    00295                           , const XMLCh* const uriStr
    00296                           , bool bAllowSpaces=false);
    00300     static bool isValidURI( bool haveBaseURI
    00301                           , const XMLCh* const uriStr
    00302                           , bool bAllowSpaces=false);
    00303
    00304
    00305     static void normalizeURI(const XMLCh*     const systemURI,
    00306                                    XMLBuffer&       normalizedURI);
    00307
    00308     /***
    00309      * Support for Serialization/De-serialization
    00310      ***/
    00311     DECL_XSERIALIZABLE(XMLUri)
    00312
    00313     XMLUri(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
    00314
    00315 private:
    00316
    00317     static const XMLCh MARK_OR_RESERVED_CHARACTERS[];
    00318     static const XMLCh RESERVED_CHARACTERS[];
    00319     static const XMLCh MARK_CHARACTERS[];
    00320     static const XMLCh SCHEME_CHARACTERS[];
    00321     static const XMLCh USERINFO_CHARACTERS[];
    00322     static const XMLCh REG_NAME_CHARACTERS[];
    00323     static const XMLCh PATH_CHARACTERS[];
    00324
    00325     //helper method for getUriText
    00326     void buildFullText();
    00327
    00328     // -----------------------------------------------------------------------
    00329     //  Private helper methods
    00330     // -----------------------------------------------------------------------
    00331
    00337     static bool isReservedCharacter(const XMLCh theChar);
    00338
    00344     static bool isPathCharacter(const XMLCh theChar);
    00345
    00351     static bool isUnreservedCharacter(const XMLCh theChar);
    00352
    00358     static bool isReservedOrUnreservedCharacter(const XMLCh theChar);
    00359
    00367     static bool isConformantSchemeName(const XMLCh* const scheme);
    00368
    00374     static void isConformantUserInfo(const XMLCh* const userInfo
    00375         , MemoryManager* const manager);
    00376
    00384     static bool isValidServerBasedAuthority(const XMLCh* const host
    00385                                            , const XMLSize_t hostLen
    00386                                            , const int port
    00387                                            , const XMLCh* const userinfo
    00388                                            , const XMLSize_t userLen);
    00389
    00397     static bool isValidServerBasedAuthority(const XMLCh* const host
    00398                                            , const int port
    00399                                            , const XMLCh* const userinfo
    00400                                            , MemoryManager* const manager);
    00401
    00409     static bool isValidRegistryBasedAuthority(const XMLCh* const authority
    00410                                              , const XMLSize_t authLen);
    00411
    00419     static bool isValidRegistryBasedAuthority(const XMLCh* const authority);
    00420
    00438      static bool isWellFormedAddress(const XMLCh* const addr
    00439          , MemoryManager* const manager);
    00440
    00450      static bool isWellFormedIPv4Address(const XMLCh* const addr, const XMLSize_t length);
    00451
    00465      static bool isWellFormedIPv6Reference(const XMLCh* const addr, const XMLSize_t length);
    00466
    00482      static int scanHexSequence (const XMLCh* const addr, XMLSize_t index, XMLSize_t end, int& counter);
    00483
    00491      bool isGenericURI();
    00492
    00493     // -----------------------------------------------------------------------
    00494     //  Miscellaneous methods
    00495     // -----------------------------------------------------------------------
    00496
    00502      void initialize(const XMLUri& toCopy);
    00503
    00518      void initialize(const XMLUri* const baseURI
    00519                    , const XMLCh*  const uriSpec);
    00520
    00527      void initializeScheme(const XMLCh* const uriSpec);
    00528
    00536      void initializeAuthority(const XMLCh* const uriSpec);
    00537
    00544      void initializePath(const XMLCh* const uriSpec);
    00545
    00550      void cleanUp();
    00551
    00552     static bool isConformantSchemeName(const XMLCh* const scheme,
    00553                                        const XMLSize_t schemeLen);
    00554     static bool processScheme(const XMLCh* const uriStr, XMLSize_t& index);
    00555     static bool processAuthority(const XMLCh* const uriStr, const XMLSize_t authLen);
    00556     static bool isWellFormedAddress(const XMLCh* const addr, const XMLSize_t addrLen);
    00557     static bool processPath(const XMLCh* const pathStr, const XMLSize_t pathStrLen,
    00558                             const bool isSchemePresent, const bool bAllowSpaces=false);
    00559
    00560     // -----------------------------------------------------------------------
    00561     //  Data members
    00562     //
    00563     //  for all the data member, we own it,
    00564     //  responsible for the creation and/or deletion for
    00565     //  the memory allocated.
    00566     //
    00567     // -----------------------------------------------------------------------
    00568     int             fPort;
    00569     XMLCh*          fScheme;
    00570     XMLCh*          fUserInfo;
    00571     XMLCh*          fHost;
    00572     XMLCh*          fRegAuth;
    00573     XMLCh*          fPath;
    00574     XMLCh*          fQueryString;
    00575     XMLCh*          fFragment;
    00576     XMLCh*          fURIText;
    00577     MemoryManager*  fMemoryManager;
    00578 };
    00579
    00580 // ---------------------------------------------------------------------------
    00581 //  XMLUri: Getter methods
    00582 // ---------------------------------------------------------------------------
    00583 inline const XMLCh* XMLUri::getScheme() const
    00584 {
    00585     return fScheme;
    00586 }
    00587
    00588 inline const XMLCh* XMLUri::getUserInfo() const
    00589 {
    00590     return fUserInfo;
    00591 }
    00592
    00593 inline const XMLCh* XMLUri::getHost() const
    00594 {
    00595     return fHost;
    00596 }
    00597
    00598 inline int XMLUri::getPort() const
    00599 {
    00600     return fPort;
    00601 }
    00602
    00603 inline const XMLCh* XMLUri::getRegBasedAuthority() const
    00604 {
    00605     return fRegAuth;
    00606 }
    00607
    00608 inline const XMLCh* XMLUri::getPath() const
    00609 {
    00610     return fPath;
    00611 }
    00612
    00613 inline const XMLCh* XMLUri::getQueryString() const
    00614 {
    00615     return fQueryString;
    00616 }
    00617
    00618 inline const XMLCh* XMLUri::getFragment() const
    00619 {
    00620     return fFragment;
    00621 }
    00622
    00623 inline const XMLCh* XMLUri::getUriText() const
    00624 {
    00625     //
    00626     //  Fault it in if not already. Since this is a const method and we
    00627     //  can't use mutable members due the compilers we have to support,
    00628     //  we have to cast off the constness.
    00629     //
    00630     if (!fURIText)
    00631         ((XMLUri*)this)->buildFullText();
    00632
    00633     return fURIText;
    00634 }
    00635
    00636 // ---------------------------------------------------------------------------
    00637 //  XMLUri: Helper methods
    00638 // ---------------------------------------------------------------------------
    00639 inline bool XMLUri::isReservedOrUnreservedCharacter(const XMLCh theChar)
    00640 {
    00641    return (XMLString::isAlphaNum(theChar) ||
    00642            XMLString::indexOf(MARK_OR_RESERVED_CHARACTERS, theChar) != -1);
    00643 }
    00644
    00645 inline bool XMLUri::isReservedCharacter(const XMLCh theChar)
    00646 {
    00647     return (XMLString::indexOf(RESERVED_CHARACTERS, theChar) != -1);
    00648 }
    00649
    00650 inline bool XMLUri::isPathCharacter(const XMLCh theChar)
    00651 {
    00652     return (XMLString::indexOf(PATH_CHARACTERS, theChar) != -1);
    00653 }
    00654
    00655 inline bool XMLUri::isUnreservedCharacter(const XMLCh theChar)
    00656 {
    00657     return (XMLString::isAlphaNum(theChar) ||
    00658             XMLString::indexOf(MARK_CHARACTERS, theChar) != -1);
    00659 }
    00660
    00661 XERCES_CPP_NAMESPACE_END
    00662
    00663 #endif