⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xpathutil.cpp

📁 Windows CE 6.0 Server 源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft shared
// source or premium shared source license agreement under which you licensed
// this source code. If you did not accept the terms of the license agreement,
// you are not authorized to use this source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the SOURCE.RTF on your install media or the root of your tools installation.
// THE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
//+----------------------------------------------------------------------------
//
//
// File:    xpathutil.cpp
//
// Contents:
//
//  implementation file
//
//        _WSDLUtil  collection of helpful utils
//
//-----------------------------------------------------------------------------
#include "headers.h"

#include "soapglo.h"

#ifdef UNDER_CE
#include "WinCEUtils.h"
#endif


XPathState::~XPathState()
{
    HRESULT hr=S_OK;

    if (! m_pDoc)
        return;

    CHK (m_pDoc->setProperty((BSTR)g_pwstrSelNamespaces, m_vNamespace));
    CHK (m_pDoc->setProperty((BSTR)g_pwstrSelLanguage, m_vLanguage));

Cleanup:
    ASSERT(SUCCEEDED(hr));
}


HRESULT XPathState::init(IXMLDOMNode * pNode)
{
    HRESULT hr=S_OK;
    CAutoRefc<IXMLDOMDocument>  pDOM1;

    CHK_BOOL(pNode, E_INVALIDARG);
    CHK_BOOL(m_pDoc == NULL, E_FAIL);

    CHK(pNode->get_ownerDocument(&pDOM1));

    if (pDOM1 == NULL)
    {
        // if pNode=NULL pnode 'are' the document
        pNode->AddRef();
        pDOM1 = (IXMLDOMDocument*)pNode;
    }

    CHK (initDOMDocument(pDOM1));

Cleanup:
    ASSERT (SUCCEEDED(hr));
    return hr;
}


HRESULT XPathState::initDOMDocument(IXMLDOMDocument * pDOMDoc)
{
    HRESULT hr=S_OK;
    CVariant varIn;

    CHK_BOOL(pDOMDoc, E_INVALIDARG);
    CHK_BOOL(m_pDoc == NULL, E_FAIL);

    CHK(pDOMDoc->QueryInterface(IID_IXMLDOMDocument2, (void**) &m_pDoc));

    CHK (m_pDoc->getProperty((BSTR)g_pwstrSelNamespaces, &m_vNamespace));
    CHK (m_pDoc->getProperty((BSTR)g_pwstrSelLanguage, &m_vLanguage));

    // by default we are going to use Xpath
    varIn.Assign(g_pwstrXpathLanguage);
    CHK( m_pDoc->setProperty((BSTR)g_pwstrSelLanguage, varIn) );

    // reset all the namespaces
    varIn.Assign(g_pwstrEmpty);
    CHK( m_pDoc->setProperty((BSTR)g_pwstrSelNamespaces, varIn) );

Cleanup:
    ASSERT (SUCCEEDED(hr));
    return hr;
}



HRESULT XPathState::setLanguage(const WCHAR * pXPath)
{
    HRESULT hr=S_OK;
    CVariant varIn;

    CHK_BOOL(m_pDoc, E_FAIL);

    // pre default we are going to set the xpath language
    if (pXPath)
    {
        varIn.Assign(pXPath);
        CHK( m_pDoc->setProperty((BSTR)g_pwstrSelLanguage, varIn) );
    }

Cleanup:
    ASSERT (SUCCEEDED(hr));
    return hr;
}


HRESULT XPathState::addNamespace(const WCHAR *pNamespace)
{
    HRESULT hr=S_OK;
#ifdef CE_NO_EXCEPTIONS
    WCHAR *pBuffer = NULL;
#endif


    CHK_BOOL(m_pDoc, E_FAIL);

    if (pNamespace)
    {
        CVariant varIn;
        long len = wcslen(pNamespace)+2;
#ifndef CE_NO_EXCEPTIONS     
        WCHAR * pBuffer;
#endif
        BSTR bstr;

        CHK( m_pDoc->getProperty((BSTR)g_pwstrSelNamespaces, &varIn) );

        bstr = V_BSTR(&varIn);
        if (bstr)
            len = len+::SysStringLen(bstr);
#ifndef UNDER_CE
        pBuffer = (WCHAR *) _alloca(sizeof(WCHAR) * len);
#else
#ifdef CE_NO_EXCEPTIONS
        pBuffer = new WCHAR[len];
        if(!pBuffer)
        {
            hr = E_OUTOFMEMORY;
            goto Cleanup;
        }
#else
        try{
        pBuffer = (WCHAR *) _alloca(sizeof(WCHAR) * len);
        }

        catch(...){
            hr = E_OUTOFMEMORY;
            goto Cleanup;
        }
#endif
#endif

        wcscpy(pBuffer, bstr);
        wcscat(pBuffer, L" ");
        wcscat(pBuffer, pNamespace);

        varIn.Assign(pBuffer);

        CHK( m_pDoc->setProperty((BSTR)g_pwstrSelNamespaces, varIn) );

    }

Cleanup:
#ifdef CE_NO_EXCEPTIONS
    if(pBuffer)
        delete [] pBuffer;
#endif

    ASSERT (SUCCEEDED(hr));
    return hr;
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method:      _XPATHGetNamespaceURIForPrefix
//
// Synopsis:    Given a prefix and a DOM node, find a corresponding namespace
//              in scope.
//
// Arguments:   pNode -[in]  xml dom node
//              pPrefix     - [in]  namespace prefix
//              pbNamespaceURI - [out] namespace-uri
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT _XPATHGetNamespaceURIForPrefix
    (
    IXMLDOMNode * pNode,
    const WCHAR * pPrefix,
    BSTR * pbNamespaceURI
    )
{
    HRESULT hr;
    CAutoRefc<IXMLDOMNode>  pResult;
    XPathState xp;

    WCHAR acBuffer[250];
    *pbNamespaceURI = NULL;

    ASSERT(pNode && pPrefix && pbNamespaceURI);

    xp.init(pNode);
    swprintf(acBuffer, L"ancestor-or-self::*[namespace::%.100s]/namespace::%.100s", pPrefix, pPrefix);

    showNode (pNode);

    CHK (pNode->selectSingleNode( acBuffer, &pResult));

    if (pResult)
    {
        showNode(pResult);
        CHK(pResult->get_text(pbNamespaceURI));
    }


Cleanup:
    ASSERT(SUCCEEDED(hr));
    return hr;
}





/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method:      _XPATHFindAttribute
//
// Synopsis:    Given a attribute name a DOM node, find a corresponding attribute value
//
//
// Arguments:   pNode -[in]  xml dom node
//              pName - [in]  attribute name including prefix
//              pAttribute - [out] attribute
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT _XPATHFindAttribute(
    IXMLDOMNode * pNode,
    const WCHAR * pName,
    BSTR * pAttribute)
{
    HRESULT hr(S_OK);
    CAutoRefc<IXMLDOMNode>  pResult(NULL);
    
    CHK_BOOL(pAttribute, E_INVALIDARG);
    *pAttribute = NULL;
    
    CHK (_XPATHFindAttributeNode(pNode, pName, &pResult));

    if (pResult)
    {
        //showNode(pResult);
        CHK(pResult->get_text(pAttribute));
    }


Cleanup:
    ASSERT(SUCCEEDED(hr));
    return hr;
}



/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method:      _XPATHFindAttributeNode
//
// Synopsis:    Given a attribute name and a DOM node, find a corresponding attribute value and return the node
//        
//
// Arguments:   pNode -[in]  xml dom node
//              pName - [in]  attribute name including prefix
//              pResultNode - [out] attribute node
//
// Result: node is NULL if nothing found (and S_FALSE)
//  node and S_OK
// E_INVALIDARG
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT _XPATHFindAttributeNode(
    IXMLDOMNode * pNode,
    const WCHAR * pName,
    IXMLDOMNode ** pNodeResult)
{
    HRESULT hr(S_OK);

    WCHAR acBuffer[250];

    CHK_BOOL(pNodeResult, E_INVALIDARG);
    *pNodeResult = NULL;
    
    CHK_BOOL(pNode, E_INVALIDARG);
    CHK_BOOL(pName, E_INVALIDARG);

    swprintf(acBuffer, L"./@%.200s", pName);

    //showNode (pNode);
    CHK (pNode->selectSingleNode(acBuffer, pNodeResult));

Cleanup:
    ASSERT(SUCCEEDED(hr));
    return hr;
}



/////////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT _XPATHUtilPrepareLanguage(IXMLDOMNode *pRootNode, TCHAR *pchPrefix, TCHAR *pchNameSpace)
//
//  parameters:
//
//  description:
//
//  returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT _XPATHUtilPrepareLanguage(
            IXMLDOMNode *pRootNode,
            const WCHAR * pSelectionNS)
{
    HRESULT hr = E_FAIL;

    CAutoRefc<IXMLDOMDocument2> pDoc;
    VARIANT     varIn;

    VariantInit(&varIn);

    CHK (pRootNode->QueryInterface(IID_IXMLDOMDocument2, (void**)&pDoc));

    V_VT(&varIn)    = VT_BSTR;
    V_BSTR(&varIn)  = L"XPath";

    CHK( pDoc->setProperty(L"SelectionLanguage", varIn) );

    V_BSTR(&varIn) = ::SysAllocString(pSelectionNS);

    hr = pDoc->setProperty(L"SelectionNamespaces", varIn);
    ::SysFreeString(V_BSTR(&varIn));

Cleanup:
    ASSERT(hr==S_OK);
    return (hr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT _XPATHUtilPrepareLanguage(IXMLDOMNode *pRootNode, TCHAR *pchPrefix, TCHAR *pchNameSpace)
//
//  parameters:
//      pPrefix: namespace prefix

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -