Xalan-C++ API Reference  1.12.0
XPathFunctionTable.hpp
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #if !defined(XPATHFUNCTIONTABLE_HEADER_GUARD_1357924680)
19 #define XPATHFUNCTIONTABLE_HEADER_GUARD_1357924680
20 
21 
22 
23 // Base include file. Must be first.
25 
26 
27 
28 #include <algorithm>
29 
30 
31 
33 
34 
35 
37 
38 
39 
42 
43 
44 
45 namespace XALAN_CPP_NAMESPACE {
46 
47 
48 
49 /**
50  * Exception class thrown when an unknown function is encountered
51  */
53 {
54 public:
55 
57 
59  const XalanDOMString& theFunctionName,
60  XalanDOMString& theResult,
61  const Locator* theLocator);
62 
64 
66 };
67 
68 
69 
70 /**
71  * Exception class thrown when an installFunction() is called with a
72  * function name that is not supported.
73  */
75 {
76 public:
77 
79  const XalanDOMChar* theFunctionName,
80  XalanDOMString& theResult,
81  const Locator* theLocator);
82 
84 
86 };
87 
88 
89 
90 /**
91  * Class defines a table of functions that can be called in XPath expresions.
92  */
94 {
95 public:
96 
97  enum { InvalidFunctionNumberID = -1, TableSize = 36 };
98 
99  typedef size_t SizeType;
102 
103  /**
104  * Constructor.
105  *
106  * @param fCreateTable If true, the internal table will be created. Otherwise, CreateTable() must be called.
107  */
108  XPathFunctionTable(bool fCreateTable = true);
109 
111 
112  void
113  setMemoryManager(MemoryManager& theManager)
114  {
115  m_memoryManager = &theManager;
116  }
117  /**
118  * Set up the internal table.
119  */
120  void
121  CreateTable();
122 
123  /**
124  * Destroy the internal table.
125  */
126  void
127  DestroyTable();
128 
129  /**
130  * Retrieve the function object for a specified function name. If
131  * the named Function is not found, an exception is thrown.
132  *
133  * @param theFunctionName The name of function
134  * @param theLocator The Locator instance to use when reporting an error.
135  * @return function named
136  */
137  const Function&
139  const XalanDOMString& theFunctionName,
140  const Locator* theLocator) const
141  {
142  const int theFunctionID =
143  getFunctionIndex(theFunctionName);
144 
145  if (theFunctionID != InvalidFunctionNumberID)
146  {
147  return *m_functionTable[theFunctionID];
148  }
149  else
150  {
151  MemoryManager* const theManager = m_memoryManager;
152 
153  XalanDOMString theResult(*theManager);
154 
156  theFunctionName,
157  theResult,
158  theLocator);
159  }
160  }
161 
162 private:
163 
164  /**
165  * Retrieve the function object for a specified function name.
166  *
167  * @deprecated This operator is deprecated.
168  * @param theFunctionName name of function
169  * @return function named
170  */
171  const Function&
172  operator[](const XalanDOMString& theFunctionName) const
173  {
174  const int theFunctionID =
175  getFunctionIndex(theFunctionName);
176 
177  if (theFunctionID != InvalidFunctionNumberID)
178  {
179  return *m_functionTable[theFunctionID];
180  }
181  else
182  {
183  MemoryManager* const theManager = m_memoryManager;
184 
185  XalanDOMString theResult(*theManager);
186 
187  throw XPathExceptionFunctionNotAvailable(
188  theFunctionName,
189  theResult,
190  0);
191  }
192  }
193 
194 public:
195 
196  /**
197  * Retrieve the function object for a specified function ID number.
198  *
199  * @param theFunctionID ID number of the function
200  * @return function named
201  */
202  const Function&
203  operator[](int theFunctionID) const
204  {
205  assert(theFunctionID >= 0 && theFunctionID < TableSize);
206  assert(m_functionTable[theFunctionID] != 0);
207 
208  return *m_functionTable[theFunctionID];
209  }
210 
211  /**
212  * Map a function ID to the corresponding name.
213  *
214  * @param theFunctionID The ID number of the function
215  * @return The name of the function, or an empty string if the function doesn't exist.
216  */
217  const XalanDOMString&
218  idToName(int theFunctionID,
219  XalanDOMString& theResult) const
220  {
221 
222  if (theFunctionID >= 0 && theFunctionID < TableSize)
223  {
224  theResult.assign(
225  s_functionNames[theFunctionID].m_name,
226  s_functionNames[theFunctionID].m_size);
227  }
228 
229  return theResult;
230  }
231 
232  /**
233  * Map a function name to the corresponding ID number.
234  *
235  * @param theName name of function
236  * @return The ID number of function, or InvalidFunctionNumberID if the function doesn't exist.
237  */
238  int
239  nameToID(const XalanDOMString& theName) const
240  {
241  return getFunctionIndex(theName);
242  }
243 
244  /**
245  * Insert a named function into the function table.
246  *
247  * @param theFunctionName name of function
248  * @param theFunction function object corresponding to name
249  */
250  void
252  const XalanDOMString& theFunctionName,
253  const Function& theFunction)
254  {
255  InstallFunction(theFunctionName.c_str(), theFunction);
256  }
257 
258  /**
259  * Remove a named function from the function table.
260  *
261  * @param theFunctionName name of function
262  * @return true if the function was found and removed.
263  */
264  bool
265  UninstallFunction(const XalanDOMString& theFunctionName)
266  {
267  return UninstallFunction(theFunctionName.c_str());
268  }
269 
270  /**
271  * Insert a named function into the function table.
272  *
273  * @param theFunctionName name of function
274  * @param theFunction function object corresponding to name
275  */
276  void
277  InstallFunction(
278  const XalanDOMChar* theFunctionName,
279  const Function& theFunction);
280 
281  /**
282  * Remove a named function from the function table.
283  *
284  * @param theFunctionName name of function
285  * @return true if the function was found and removed.
286  */
287  bool
288  UninstallFunction(const XalanDOMChar* theFunctionName);
289 
290  /**
291  * Whether a named function is in the function table.
292  *
293  * @param theFunctionName name of function
294  * @return true if function is in table
295  */
296  bool
297  isInstalledFunction(const XalanDOMString& theFunctionName) const
298  {
299  return getFunctionIndex(theFunctionName) != InvalidFunctionNumberID ? true : false;
300  }
301 
302  /**
303  * Add a list of the names of installed functions to a vector of names.
304  *
305  * @param theIterator function table iterator to append names to
306  */
307  template<class OutputIteratorType>
308  void
309  getInstalledFunctionNames(OutputIteratorType theIterator) const
310  {
311  XalanDOMString theString(XalanMemMgrs::getDefaultXercesMemMgr());
312 
313  for (int i = 0; i < TableSize; ++i)
314  {
315  if (m_functionTable[i] != 0)
316  {
317  theString.assign(
318  s_functionNames[i].m_name,
319  s_functionNames[i].m_size);
320 
321  *theIterator = theString;
322 
323  ++theIterator;
324  }
325  }
326  }
327 
329  {
330  const XalanDOMChar* m_name;
331 
333  };
334 
335  // These are static strings for the functions supported.
336  // Note that the XSLT functions are also here, since it's
337  // just easier to do it this way.
338 
339  // The string "id"
340  static const XalanDOMChar s_id[];
341 
342  // The string "key"
343  static const XalanDOMChar s_key[];
344 
345  // The string "not"
346  static const XalanDOMChar s_not[];
347 
348  // The string "sum"
349  static const XalanDOMChar s_sum[];
350 
351  // The string "lang"
352  static const XalanDOMChar s_lang[];
353 
354  // The string "last"
355  static const XalanDOMChar s_last[];
356 
357  // The string "name"
358  static const XalanDOMChar s_name[];
359 
360  // The string "true"
361  static const XalanDOMChar s_true[];
362 
363  // The string "count"
364  static const XalanDOMChar s_count[];
365 
366  // The string "false"
367  static const XalanDOMChar s_false[];
368 
369  // The string "floor"
370  static const XalanDOMChar s_floor[];
371 
372  // The string "round"
373  static const XalanDOMChar s_round[];
374 
375  // The string "concat"
376  static const XalanDOMChar s_concat[];
377 
378  // The string "number"
379  static const XalanDOMChar s_number[];
380 
381  // The string "string"
382  static const XalanDOMChar s_string[];
383 
384  // The string "boolean"
385  static const XalanDOMChar s_boolean[];
386 
387  // The string "ceiling"
388  static const XalanDOMChar s_ceiling[];
389 
390  // The string "current"
391  static const XalanDOMChar s_current[];
392 
393  // The string "contains"
394  static const XalanDOMChar s_contains[];
395 
396  // The string "document"
397  static const XalanDOMChar s_document[];
398 
399  // The string "position"
400  static const XalanDOMChar s_position[];
401 
402  // The string "substring"
403  static const XalanDOMChar s_substring[];
404 
405  // The string "translate"
406  static const XalanDOMChar s_translate[];
407 
408  // The string "local-name"
409  static const XalanDOMChar s_localName[];
410 
411  // The string "generate-id"
412  static const XalanDOMChar s_generateId[];
413 
414  // The string "starts-with"
415  static const XalanDOMChar s_startsWith[];
416 
417  // The string "format-number"
418  static const XalanDOMChar s_formatNumber[];
419 
420  // The string "namespace-uri"
421  static const XalanDOMChar s_namespaceUri[];
422 
423  // The string "string-length"
424  static const XalanDOMChar s_stringLength[];
425 
426  // The string "normalize-space"
427  static const XalanDOMChar s_normalizeSpace[];
428 
429  // The string "substring-after"
430  static const XalanDOMChar s_substringAfter[];
431 
432  // The string "system-property"
433  static const XalanDOMChar s_systemProperty[];
434 
435  // The string "substring-before"
436  static const XalanDOMChar s_substringBefore[];
437 
438  // The string "element-available"
439  static const XalanDOMChar s_elementAvailable[];
440 
441  // The string "function-available"
442  static const XalanDOMChar s_functionAvailable[];
443 
444  // The string "unparsed-entity-uri"
445  static const XalanDOMChar s_unparsedEntityUri[];
446 
447  // A table of function names.
448  static const FunctionNameTableEntry s_functionNames[];
449 
450  // The size of the table.
452 
453 private:
454 
455  static int
456  getFunctionIndex(const XalanDOMString& theName)
457  {
458  return getFunctionIndex(
459  theName.c_str(),
460  theName.length());
461  }
462 
463  static int
464  getFunctionIndex(const XalanDOMChar* theName)
465  {
466  return getFunctionIndex(
467  theName,
468  XalanDOMString::length(theName));
469  }
470 
471  static int
472  getFunctionIndex(
473  const XalanDOMChar* theName,
474  StringSizeType theNameLength);
475 
476  MemoryManager* m_memoryManager;
477 
478  const Function* m_functionTable[TableSize];
479 
480  const Function** const m_functionTableEnd;
481 
482  // The last one in the table of function names.
483  static const FunctionNameTableEntry* const s_lastFunctionName;
484 };
485 
486 
487 
488 }
489 
490 
491 
492 #endif // XPATHFUNCTIONTABLE_HEADER_GUARD_1357924680
xalanc::XPathFunctionTable::idToName
const XalanDOMString & idToName(int theFunctionID, XalanDOMString &theResult) const
Map a function ID to the corresponding name.
Definition: XPathFunctionTable.hpp:218
xalanc::XalanDOMString::assign
XalanDOMString & assign(const XalanDOMChar *theSource)
Definition: XalanDOMString.hpp:390
XALAN_CPP_NAMESPACE
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
Definition: XalanVersion.hpp:76
xalanc::XPathFunctionTable::setMemoryManager
void setMemoryManager(MemoryManager &theManager)
Definition: XPathFunctionTable.hpp:113
xalanc::XPathFunctionTable::s_functionNamesSize
static const SizeType s_functionNamesSize
Definition: XPathFunctionTable.hpp:451
xalanc::XPathFunctionTable::isInstalledFunction
bool isInstalledFunction(const XalanDOMString &theFunctionName) const
Whether a named function is in the function table.
Definition: XPathFunctionTable.hpp:297
XalanDOMString.hpp
xalanc::XPathFunctionTable::get
const Function & get(const XalanDOMString &theFunctionName, const Locator *theLocator) const
Retrieve the function object for a specified function name.
Definition: XPathFunctionTable.hpp:138
xalanc::Function::LocatorType
xercesc::Locator LocatorType
Definition: Function.hpp:61
xalanc::XPathFunctionTable::InstallFunction
void InstallFunction(const XalanDOMString &theFunctionName, const Function &theFunction)
Insert a named function into the function table.
Definition: XPathFunctionTable.hpp:251
xalanc::XPathFunctionTable::FunctionNameTableEntry::m_name
const XalanDOMChar * m_name
Definition: XPathFunctionTable.hpp:330
xalanc::XPathFunctionTable::DeleteFunctorType
DeleteFunctor< Function > DeleteFunctorType
Definition: XPathFunctionTable.hpp:101
xalanc::XPathFunctionTable::SizeType
size_t SizeType
Definition: XPathFunctionTable.hpp:99
xalanc::XPathFunctionTable::FunctionNameTableEntry::m_size
StringSizeType m_size
Definition: XPathFunctionTable.hpp:332
XALAN_XPATH_EXPORT
#define XALAN_XPATH_EXPORT
Definition: XPathDefinitions.hpp:35
STLHelper.hpp
xalanc::XalanXPathException
Definition: XalanXPathException.hpp:46
xalanc::XalanDOMString::length
size_type length() const
Definition: XalanDOMString.hpp:209
xalanc::XPathFunctionTable
Class defines a table of functions that can be called in XPath expresions.
Definition: XPathFunctionTable.hpp:93
xalanc::Function
Definition: Function.hpp:57
xalanc::length
XalanDOMString::size_type length(const XalanDOMString &theString)
Get the length of a XalanDOMString.
Definition: DOMStringHelper.hpp:235
xalanc::DeleteFunctor
Functor to delete objects, used in STL iteration algorithms.
Definition: STLHelper.hpp:100
xalanc::XPathFunctionTable::getInstalledFunctionNames
void getInstalledFunctionNames(OutputIteratorType theIterator) const
Add a list of the names of installed functions to a vector of names.
Definition: XPathFunctionTable.hpp:309
xalanc::XPathExceptionFunctionNotSupported
Exception class thrown when an installFunction() is called with a function name that is not supported...
Definition: XPathFunctionTable.hpp:74
xalanc::XPathExceptionFunctionNotAvailable::LocatorType
Function::LocatorType LocatorType
Definition: XPathFunctionTable.hpp:56
xalanc::XalanDOMString::c_str
const XalanDOMChar * c_str() const
Definition: XalanDOMString.hpp:344
xalanc::XPathFunctionTable::StringSizeType
XalanDOMString::size_type StringSizeType
Definition: XPathFunctionTable.hpp:100
xalanc::XPathFunctionTable::UninstallFunction
bool UninstallFunction(const XalanDOMString &theFunctionName)
Remove a named function from the function table.
Definition: XPathFunctionTable.hpp:265
xalanc::XalanDOMString::size_type
XalanSize_t size_type
Definition: XalanDOMString.hpp:57
xalanc::XalanDOMString
Definition: XalanDOMString.hpp:45
xalanc::XPathFunctionTable::nameToID
int nameToID(const XalanDOMString &theName) const
Map a function name to the corresponding ID number.
Definition: XPathFunctionTable.hpp:239
xalanc::XPathFunctionTable::operator[]
const Function & operator[](int theFunctionID) const
Retrieve the function object for a specified function ID number.
Definition: XPathFunctionTable.hpp:203
XalanXPathException.hpp
Function.hpp
XPathDefinitions.hpp
xalanc::XPathExceptionFunctionNotAvailable
Exception class thrown when an unknown function is encountered.
Definition: XPathFunctionTable.hpp:52
xalanc::XPathFunctionTable::FunctionNameTableEntry
Definition: XPathFunctionTable.hpp:328