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

📄 xpathutil.cpp

📁 Windows CE 6.0 Server 源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//      pSelectionNS: namespace URI
//  description:
//     just contacts the prefix and the URI and calls the real implemenation
//  returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT _XPATHUtilPrepareLanguage(
            IXMLDOMNode *pRootNode,
            WCHAR ** ppPrefix,
            WCHAR ** ppSelectionNS,
            const int      cb)
{
    WCHAR *pchBuffer=0;
    HRESULT hr;
    int     iLen=0;

#ifndef UNDER_CE
    for (int i =0;i<cb ;i++ )
    {
        iLen += wcslen(ppPrefix[i])+wcslen(ppSelectionNS[i])+15;
    }
#else
    for (int j =0;j<cb ;j++ )
    {
        iLen += wcslen(ppPrefix[j])+wcslen(ppSelectionNS[j])+15;
    }
#endif

#ifdef UNDER_CE
    int i = 0;
#endif 


    pchBuffer = new WCHAR[iLen];
    if (!pchBuffer)
    {
        hr = E_OUTOFMEMORY;
        goto Cleanup;
    }

#ifndef UNDER_CE 
    for (int i = 0;i<cb ;i++ )
#else
    for (i = 0; i<cb; i++)
#endif
    {
        if (i==0)
        {
            wcscpy(pchBuffer, L"xmlns:");
        }
        else
        {
            wcscat(pchBuffer, L" xmlns:");
        }

        wcscat(pchBuffer, ppPrefix[i]);
        wcscat(pchBuffer, L"=\"");
        wcscat(pchBuffer, ppSelectionNS[i]);
        wcscat(pchBuffer, L"\"");
    }


    hr = _XPATHUtilPrepareLanguage(pRootNode, pchBuffer);


Cleanup:
    ASSERT(hr==S_OK);
    delete [] pchBuffer;
    return (hr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////




/////////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT _XPATHUtilPrepareLanguage(IXMLDOMNode *pRootNode, TCHAR *pchPrefix, TCHAR *pchNameSpace)
//
//  parameters:
//      pPrefix: namespace prefix
//      pSelectionNS: namespace URI
//  description:
//     just contacts the prefix and the URI and calls the real implemenation
//  returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT _XPATHUtilPrepareLanguage(
            IXMLDOMNode *pRootNode,
            WCHAR * pPrefix,
            WCHAR * pSelectionNS)
{

    return(_XPATHUtilPrepareLanguage(pRootNode, &pPrefix, &pSelectionNS, 1));
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////








/////////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT _XPATHUtilResetLanguage(IXMLDOMNode *pRootNode)
//
//  parameters:
//
//  description:
//      resets the selection language to default
//  returns:
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT _XPATHUtilResetLanguage(IXMLDOMNode *pRootNode)
{
    HRESULT hr = E_FAIL;
    VARIANT     varIn;
    CAutoRefc<IXMLDOMDocument2> pDoc;

    VariantInit(&varIn);

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

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

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

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



/////////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT _WSDLUtilFindNodeListFromRoot
//
//  parameters:
//
//  description:
//        finds the root node, then searches for the first element with
//      a given pattern
//
//  returns:
//      E_FAIL if pattern not found
//      MSXML specific error result
//      S_OK if not problem
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT _XPATHUtilFindNodeListFromRoot(
            IXMLDOMNode     *pNode,
            const WCHAR     *pchElementsToFind,
            IXMLDOMNodeList **ppNodeList)
{
    HRESULT                     hr = E_FAIL;
    CAutoRefc<IXMLDOMDocument>  pDoc;
    CAutoRefc<IXMLDOMNode>      pXDN;

    ASSERT(pNode!=0);
    ASSERT(pchElementsToFind!=0);

    CHK_BOOL(ppNodeList!=0, E_INVALIDARG);
    *ppNodeList = NULL;


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

    if (pDoc == NULL)
    {
        // we are already at the root and pDoc is NULL (msxml behavior)
        pXDN = pNode;
        pNode->AddRef();
    }
    else
    {
        CHK ( pDoc->QueryInterface(IID_IXMLDOMNode, (void **)&pXDN));
    }

    CHK(_XPATHUtilFindNodeList(pXDN, pchElementsToFind, ppNodeList));

Cleanup:
    return (hr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT _WSDLUtilFindNodeFromRoot
//
//  parameters:
//
//  description:
//        finds the root node, then searches for the first element with
//      a given pattern
//
//  returns:
//      E_FAIL if pattern not found
//      MSXML specific error result
//      S_OK if not problem
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT _XPATHUtilFindNodeFromRoot(
            IXMLDOMNode *pNode,
            const WCHAR *pchElementsToFind,
            IXMLDOMNode **ppNode)
{
    HRESULT                     hr = E_FAIL;
    CAutoRefc<IXMLDOMDocument>  pDoc;
    CAutoRefc<IXMLDOMNode>      pXDN;

    ASSERT(pNode!=0);
    ASSERT(pchElementsToFind!=0);
    ASSERT(ppNode!=0);

    //make sure the return result is NULLed out
    *ppNode = NULL;

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

    if (pDoc == NULL)
    {
        // we are already at the root and pDoc is NULL (msxml behavior)
        pXDN = pNode;
        pNode->AddRef();
    }
    else
    {
        CHK ( pDoc->QueryInterface(IID_IXMLDOMNode, (void **)&pXDN));
    }


    CHK ( pXDN->selectSingleNode((BSTR) pchElementsToFind, ppNode) );
    CHK_BOOL(*ppNode != NULL, E_FAIL);

Cleanup:
    return (hr);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////




/////////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT _XPATHUtilDoNodesExist
//
//  parameters:
//
//  description:
//        searches for a subnode list from the given startnode based on pattern
//
//  returns:
//      E_FAIL if pattern not found -> checks if result list is 0 length to determine E_FAIL
//      MSXML specific error result
//      S_OK if not problem
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT _XPATHUtilFindNodeList(IXMLDOMNode *pStartNode,
                             const WCHAR *pchElementsToFind,
                             IXMLDOMNodeList **ppNodeList)
{
      CAutoRefc<IXMLDOMNodeList>  pNList;
    HRESULT                     hr = E_FAIL;
    LONG                        lLen;


    ASSERT(pStartNode!=0);
    ASSERT(pchElementsToFind!=0);

    CHK_BOOL(ppNodeList!=0, E_INVALIDARG);
    *ppNodeList = NULL;

    CHK ( pStartNode->selectNodes((BSTR) pchElementsToFind, &pNList) );
    CHK_BOOL(pNList != NULL, E_FAIL);

    CHK (pNList->get_length(&lLen));

    // if the len is 0, there are no nodes found
    CHK_BOOL(lLen > 0, E_FAIL);

     // the pattern was found, put it into the output variable
    *ppNodeList = pNList.PvReturn();

Cleanup:
    return (hr);

}
/////////////////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT FollowHref
//
//  parameters:
//
//  description:
//      function checks if an HREF definition is used.
//      if there it follows the href
//
//  returns:
//      the function will return S_OK in the successful case. In this case *ppNode will point to the node
//      to continue processing on. This can be the original node, or it can be the original node,
//      or the node retrieved from following the href.
//
//      in the error case **pNode will not be effected, but error can be occuring due to program errors
//      or due to wrong href's
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT FollowHref(IXMLDOMNode **ppNode)
{
    HRESULT hr = S_OK;
    CAutoBSTR bstrHref;
    IXMLDOMNode* pNode = NULL;

    ASSERT (ppNode);
    CHK_BOOL(*ppNode, E_INVALIDARG);

    showNode(*ppNode);

    if (SUCCEEDED(_WSDLUtilFindAttribute(*ppNode, g_pwstrHref, &bstrHref)))
    {
        XPathState xp;
        CAutoFormat autoFormat;
        WCHAR * pchhref = bstrHref;

        pNode = *ppNode;

        // add the envelope to the search namespace
        xp.init(pNode);
        // add the envelope to the search namespace
        CHK (xp.addNamespace(g_pwstrXpathEnv)); //give us env:envelopeNamespace

        if (*bstrHref == _T('#'))
            pchhref++;
        CHK_BOOL(*pchhref, E_INVALIDARG);

        CHK(autoFormat.sprintf(_T("env:Envelope//*[@id='%s']"), pchhref));

        // this returns E_FAIL if no node found
        hr = _XPATHUtilFindNodeFromRoot(pNode, &autoFormat, &pNode);
        ERROR_ONHR1( hr, WSML_IDS_COULDNOTFINDHREF, WSDL_IDS_MAPPER, bstrHref);
        }

Cleanup:
    if (SUCCEEDED(hr) && pNode)
    {
        // whatever was in the input node has to get released
        (*ppNode)->Release();

        // and take the new answer
        *ppNode = pNode;

        // no need to release pNode, if we make it here, we have a successful hr,
        // which means we have the node addrefed in FindNodeFromRoot,
        // and need to preserve the addref.
    }
    return (hr);
}


⌨️ 快捷键说明

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