NCBI C++ ToolKit
extension_element.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: extension_element.cpp 92096 2020-12-21 18:27:50Z satskyse $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Sergey Satskiy, NCBI
27  * Credits: Denis Vakatov, NCBI (API design)
28  *
29  */
30 
31 
32 /** @file
33  * This file contains the implementation of the xslt::extension_element class.
34 **/
35 
36 
37 #include <string>
38 
39 #include <libxslt/xsltutils.h>
40 
41 // xmlwrapp includes
44 
46 
47 
48 static xmlXPathObjectPtr
49 evaluate_xpath_expression (xsltTransformContextPtr ctxt,
50  const char * xpath_expression,
51  xmlNodePtr node)
52 {
53  int old_context_size = ctxt->xpathCtxt->contextSize;
54  int old_proximity_position = ctxt->xpathCtxt->proximityPosition;
55  int old_ns_nr = ctxt->xpathCtxt->nsNr;
56  xmlNsPtr * old_namespaces = ctxt->xpathCtxt->namespaces;
57  xmlNodePtr old_node = ctxt->xpathCtxt->node;
58 
59  ctxt->xpathCtxt->node = node == NULL ? ctxt->node : node;
60  if (node != NULL)
61  ctxt->xpathCtxt->contextSize = ctxt->xpathCtxt->proximityPosition;
62 
63  xmlXPathObjectPtr result_obj = xmlXPathEvalExpression(
64  reinterpret_cast<const xmlChar*>(xpath_expression),
65  ctxt->xpathCtxt);
66 
67  ctxt->xpathCtxt->node = old_node;
68  ctxt->xpathCtxt->contextSize = old_context_size;
69  ctxt->xpathCtxt->proximityPosition = old_proximity_position;
70  ctxt->xpathCtxt->nsNr = old_ns_nr;
71  ctxt->xpathCtxt->namespaces = old_namespaces;
72 
73  if (result_obj == NULL)
74  throw xslt::exception("XPath expression evaluation failed. "
75  "Expression: " +
76  std::string(xpath_expression));
77 
78  return result_obj;
79 }
80 
81 
82 namespace xslt {
83 
84  namespace impl {
86  xslt_ctxt(NULL), instruction_node(NULL)
87  {}
88  }
89 
90 
91  //
92  // Extension element implementation
93  //
94 
96  pimpl_(new impl::extension_element_impl)
97  {}
98 
100  {
101  if (pimpl_ != NULL)
102  delete pimpl_;
103  }
104 
106  pimpl_(new impl::extension_element_impl)
107  {
108  pimpl_->xslt_ctxt = other.pimpl_->xslt_ctxt;
109  return;
110  }
111 
114  {
115  pimpl_->xslt_ctxt = other.pimpl_->xslt_ctxt;
116  return *this;
117  }
118 
120  pimpl_(other.pimpl_)
121  {
122  other.pimpl_ = NULL;
123  }
124 
127  {
128  if (this != &other) {
129  if (pimpl_ != NULL)
130  delete pimpl_;
131  pimpl_ = other.pimpl_;
132  other.pimpl_ = NULL;
133  }
134  return *this;
135  }
136 
138  {
139  if (pimpl_->xslt_ctxt == NULL)
140  throw xslt::exception("Reporting XSLT extension element error "
141  "out of XSLT context.");
142  if (pimpl_->instruction_node == NULL)
143  throw xslt::exception("Reporting XSLT extension element error "
144  "when there is no XSLT instruction node.");
145 
146  xsltTransformError(pimpl_->xslt_ctxt, pimpl_->xslt_ctxt->style,
147  pimpl_->instruction_node, "%s", error);
148  }
149 
150  xpath_object extension_element::evaluate (const char * xpath_expression,
151  const xml::node & node)
152  {
153  if (pimpl_->xslt_ctxt == NULL)
154  throw xslt::exception("Evaluating XPath expression "
155  "out of XSLT context.");
157  pimpl_->xslt_ctxt,
158  xpath_expression,
159  static_cast<xmlNodePtr>(node.get_node_data())));
160  }
161 
162  xpath_object extension_element::evaluate (const char * xpath_expression)
163  {
164  if (pimpl_->xslt_ctxt == NULL)
165  throw xslt::exception("Evaluating XPath expression "
166  "out of XSLT context.");
168  xpath_expression, NULL));
169  }
170 
171 } // xslt namespace
172 
The xml::node class is used to hold information about one XML node.
Definition: node.hpp:106
void * get_node_data(void) const
Definition: node.cpp:753
This exception class is thrown by xmlwrapp for all runtime XSLT-related errors.
The XSLT extension element object is used to be a base class for the user provided XSLT extension ele...
virtual ~extension_element()
Destroy extension element object and clean the memory up.
xpath_object evaluate(const char *xpath_expression, const xml::node &node)
Evaluate XPath expression.
extension_element()
Create a new extension element.
impl::extension_element_impl * pimpl_
extension_element & operator=(const extension_element &other)
Create a copy of the extension element object.
void report_error(const char *error)
Report an error to the XSLT processor.
The xslt::xpath_object class is used to store extension function arguments and return values.
static xmlXPathObjectPtr evaluate_xpath_expression(xsltTransformContextPtr ctxt, const char *xpath_expression, xmlNodePtr node)
XSLT extension element object.
This file contains definition of the xslt::impl::extension_element_impl class.
string
Definition: cgiapp.hpp:687
#define NULL
Definition: ncbistd.hpp:225
XML library namespace.
This file contains the definition of the xslt::exception class.
Modified on Wed Sep 04 14:59:05 2024 by modify_doxy.py rev. 669887