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(STYLESHEETCONSTRUCTIONCONTEXTDEFAULT_HEADER_GUARD_1357924680)
19 #define STYLESHEETCONSTRUCTIONCONTEXTDEFAULT_HEADER_GUARD_1357924680
20 
21 
22 
23 // Base include file.  Must be first.
24 #include <xalanc/XSLT/XSLTDefinitions.hpp>
25 
26 
27 
28 #include <xalanc/Include/XalanVector.hpp>
29 
30 
31 
32 #include <xalanc/PlatformSupport/ArenaAllocator.hpp>
33 #include <xalanc/PlatformSupport/XalanArrayAllocator.hpp>
34 #include <xalanc/PlatformSupport/XalanDOMStringPool.hpp>
35 
36 
37 
38 #if defined(XALAN_AUTO_PTR_REQUIRES_DEFINITION) || defined(XALAN_BUILD_DLL)
39 #include <xalanc/XPath/XPathProcessor.hpp>
40 #endif
41 
42 
43 
44 #include <xalanc/Include/XalanMemMgrAutoPtr.hpp>
45 
46 
47 
48 #include <xalanc/PlatformSupport/XalanDOMStringCache.hpp>
49 
50 
51 
52 #include <xalanc/XPath/XalanQNameByReference.hpp>
53 #include <xalanc/XPath/XalanQNameByValue.hpp>
54 #include <xalanc/XPath/XalanQNameByValueAllocator.hpp>
55 
56 
57 
58 // Base class header file...
59 #include <xalanc/XSLT/StylesheetConstructionContext.hpp>
60 
61 
62 
63 #include <xalanc/XSLT/AVT.hpp>
64 #include <xalanc/XSLT/XalanAVTAllocator.hpp>
65 #include <xalanc/XSLT/AVTPartSimple.hpp>
66 #include <xalanc/XSLT/XalanAVTPartSimpleAllocator.hpp>
67 #include <xalanc/XSLT/AVTPartXPath.hpp>
68 #include <xalanc/XSLT/XalanAVTPartXPathAllocator.hpp>
69 #include <xalanc/XSLT/XalanElemApplyTemplatesAllocator.hpp>
70 #include <xalanc/XSLT/XalanElemAttributeAllocator.hpp>
71 #include <xalanc/XSLT/XalanElemAttributeSetAllocator.hpp>
72 #include <xalanc/XSLT/XalanElemCallTemplateAllocator.hpp>
73 #include <xalanc/XSLT/XalanElemElementAllocator.hpp>
74 #include <xalanc/XSLT/XalanElemLiteralResultAllocator.hpp>
75 #include <xalanc/XSLT/XalanElemTemplateAllocator.hpp>
76 #include <xalanc/XSLT/XalanElemTextLiteralAllocator.hpp>
77 #include <xalanc/XSLT/XalanElemValueOfAllocator.hpp>
78 #include <xalanc/XSLT/XalanElemVariableAllocator.hpp>
79 #include "xalanc/XSLT/XalanMatchPatternDataAllocator.hpp"
80 
81 
82 
83 namespace XALAN_CPP_NAMESPACE {
84 
85 
86 
87 class XPathEnvSupport;
88 class XPathFactory;
89 class XPathProcessor;
90 class XSLTEngineImpl;
91 
92 
93 
94 /**
95  *
96  * An default implementation of an abtract class which provides support for
97  * constructing the internal representation  of a stylesheet.
98  *
99  */
100 class XALAN_XSLT_EXPORT StylesheetConstructionContextDefault : public StylesheetConstructionContext
101 {
102 public:
103 
104     typedef XalanArrayAllocator<XalanDOMChar>           XalanDOMCharVectorAllocatorType;
105     typedef XalanArrayAllocator<const void*>            PointerVectorAllocatorType;
106     typedef XalanDOMCharVectorAllocatorType::size_type  VectorAllocatorSizeType;
107 
108     // Default size for vector allocation.
109     enum {
110             eDefaultXalanDOMCharVectorBlockSize = 1024,
111             eDefaultAVTBlockSize = 128,
112             eDefaultAVTPartSimpleBlockSize = 128,
113             eDefaultAVTPartXPathBlockSize = 128,
114             eDefaultXalanQNameByValueBlockSize = 32,
115             eDefaultPointerVectorBlockSize = 512,
116             eDefaultElemApplyTemplatesBlockSize = 10,
117             eDefaultElemAttributeBlockSize = 10,
118             eDefaultElemAttributeSetBlockSize = 10,
119             eDefaultElemCallTemplateBlockSize = 10,
120             eDefaultElemElementBlockSize = 10,
121             eDefaultElemLiteralResultBlockSize = 20,
122             eDefaultElemTemplateBlockSize = 10,
123             eDefaultElemTextLiteralBlockSize = 20,
124             eDefaultElemValueOfBlockSize = 10,
125             eDefaultElemVariableBlockSize = 10,
126             eDefaultMatchPatternDataBlockSize = 10 };
127 
128     /*
129      * Construct an instance.  If the stylesheet(s) constructed is/are meant to be reused (a.k.a. "compiled"),
130      * the XObjectFactory and XPathFactory instance must exist for the lifetime of the construction context
131      * and, therefore, for the lifetime of the stylesheet(s).  Otherwise, XObject and XPath instance will be
132      * destroyed when the corresponding factories are destryed, leaving pointers to destroyed objects in the.
133      * stylesheet(s).
134      *
135      * @param processor a reference to an XSLTEngineImpl instance.  Used for error reporting.
136      * @param xpathFactory a reference to an XPathFactory instance.  See comments above for important details.
137      * @param theXalanDOMCharVectorAllocatorBlockSize The block size to use for allocating vectors of XalanDOMChars
138      * @param theAVTAllocatorBlockSize The block size to use for allocating AVT instances.
139      * @param theAVTPartSimpleAllocatorBlockSize The block size to use for allocating AVTPartSimple instances.
140      * @param theAVTPartXPathAllocatorBlockSize The block size to use for allocating AVTPartXPath instances.
141      * @param theXalanQNameByValueAllocatorBlockSize The block size to use for allocating XalanQNameByValue instances.
142      * @param thePointerVectorAllocatorBlockSize The block size to use for allocating vectors of pointers.
143      */
144     StylesheetConstructionContextDefault(
145             MemoryManager&                      theManager,
146             XSLTEngineImpl&                         processor,
147             XPathFactory&                           xpathFactory,
148             VectorAllocatorSizeType                 theXalanDOMCharVectorAllocatorBlockSize = eDefaultXalanDOMCharVectorBlockSize,
149             XalanAVTAllocator::size_type            theAVTAllocatorBlockSize = eDefaultAVTBlockSize,
150             XalanAVTPartSimpleAllocator::size_type  theAVTPartSimpleAllocatorBlockSize = eDefaultAVTPartSimpleBlockSize,
151             XalanAVTPartXPathAllocator::size_type   theAVTPartXPathAllocatorBlockSize = eDefaultAVTPartXPathBlockSize,
152             XalanQNameByValueAllocator::size_type   theXalanQNameByValueAllocatorBlockSize = eDefaultXalanQNameByValueBlockSize,
153             VectorAllocatorSizeType                 thePointerVectorAllocatorBlockSize = eDefaultPointerVectorBlockSize);
154 
155     virtual
156     ~StylesheetConstructionContextDefault();
157 
158 
159     // These interfaces are inherited from StylesheetConstructionContext...
160 
161     virtual void
162     problem(
163             eSource                 source,
164             eClassification         classification,
165             const XalanDOMString&   msg,
166             const Locator*          locator,
167             const XalanNode*        sourceNode);
168 
169     virtual void
170     problem(
171             eSource                 source,
172             eClassification         classification,
173             const XalanDOMString&   msg,
174             const XalanNode*        sourceNode);
175 
176     virtual void
177     reset();
178 
179     virtual const XalanDOMString&
180     getPooledString(const XalanDOMString&   theString);
181 
182     virtual const XalanDOMString&
183     getPooledString(
184             const XalanDOMChar*         theString,
185             XalanDOMString::size_type   theLength = XalanDOMString::npos);
186 
187     virtual XalanDOMString&
188     getCachedString();
189 
190     virtual bool
191     releaseCachedString(XalanDOMString&     theString);
192 
193     virtual StylesheetRoot*
194     create(const XalanDOMString&    theBaseIdentifier);
195 
196     virtual StylesheetRoot*
197     create(const XSLTInputSource&   theInputSource);
198 
199     virtual Stylesheet*
200     create(
201             StylesheetRoot&         theStylesheetRoot,
202             const XalanDOMString&   theBaseIdentifier);
203 
204     virtual void
205     destroy(StylesheetRoot*     theStylesheetRoot);
206 
207     virtual URLAutoPtrType
208     getURLFromString(const XalanDOMString&  urlString);
209 
210     virtual XalanDOMString&
211     getURLStringFromString(
212             const XalanDOMString&   urlString,
213             XalanDOMString&         theResult);
214 
215     virtual URLAutoPtrType
216     getURLFromString(
217             const XalanDOMString&   urlString,
218             const XalanDOMString&   base);
219 
220     virtual XalanDOMString&
221     getURLStringFromString(
222             const XalanDOMString&   urlString,
223             const XalanDOMString&   base,
224             XalanDOMString& theResult);
225 
226     virtual const XalanDOMString&
227     getXSLTNamespaceURI() const;
228 
229     virtual XPath*
230     createMatchPattern(
231             const Locator*          locator,
232             const XalanDOMString&   str,
233             const PrefixResolver&   resolver,
234             bool                    allowVariableReferences = true,
235             bool                    allowKeyFunction = true);
236 
237     virtual XPath*
238     createMatchPattern(
239             const Locator*          locator,
240             const XalanDOMChar*     str,
241             const PrefixResolver&   resolver,
242             bool                    allowVariableReferences = true,
243             bool                    allowKeyFunction = true);
244 
245     virtual XPath*
246     createXPath(
247             const Locator*          locator,
248             const XalanDOMString&   str,
249             const PrefixResolver&   resolver,
250             bool                    allowVariableReferences = true,
251             bool                    allowKeyFunction = true);
252 
253     virtual XPath*
254     createXPath(
255             const Locator*          locator,
256             const XalanDOMChar*     str,
257             const PrefixResolver&   resolver,
258             bool                    allowVariableReferences = true,
259             bool                    allowKeyFunction = true);
260 
261     virtual XPath*
262     createXPath(
263             const Locator*              locator,
264             const XalanDOMChar*         str,
265             XalanDOMString::size_type   len,
266             const PrefixResolver&       resolver,
267             bool                        allowVariableReferences = true,
268             bool                        allowKeyFunction = true);
269 
270     virtual const Locator*
271     getLocatorFromStack() const;
272 
273     virtual void
274     pushLocatorOnStack(const Locator*   locator);
275 
276     virtual void
277     popLocatorStack();
278 
279     virtual const XalanDOMString&
280     getXalanXSLNameSpaceURL() const;
281 
282     virtual XalanDocument*
283     parseXML(
284             const XalanDOMString&   urlString,
285             DocumentHandler*        docHandler,
286             XalanDocument*          docToRegister,
287             ErrorHandler*           theErrorHandler = 0);
288 
289     virtual bool
290     isXMLSpaceAttribute(
291             const XalanDOMChar*     theAttributeName,
292             const Stylesheet&       theStylesheet,
293             const Locator*          theLocator = 0);
294 
295     virtual bool
296     isXSLUseAttributeSetsAttribute(
297             const XalanDOMChar*     theAttributeName,
298             const Stylesheet&       theStylesheet,
299             const Locator*          theLocator = 0);
300 
301     virtual bool
302     isValidQName(
303             const XalanDOMChar*     theName,
304             const Stylesheet&       theStylesheet,
305             const Locator*          theLocator = 0);
306 
307     virtual eElementToken
308     getElementToken(const XalanDOMString&   name) const;
309 
310     virtual double
311     getXSLTVersionSupported() const;
312 
313     virtual XalanDOMChar*
314     allocateXalanDOMCharVector(XalanDOMString::size_type    theLength);
315 
316     virtual XalanDOMChar*
317     allocateXalanDOMCharVector(
318             const XalanDOMChar*         theString,
319             XalanDOMString::size_type   theLength = XalanDOMString::npos,
320             bool                        fTerminate = true);
321 
322     virtual const AVT*
323     createAVT(
324             const Locator*          locator,
325             const XalanDOMChar*     name,
326             const XalanDOMChar*     stringedValue,
327             const PrefixResolver&   resolver);
328 
329     virtual const AVTPart*
330     createAVTPart(
331             const XalanDOMChar*         theString,
332             XalanDOMString::size_type   theLength = XalanDOMString::npos);
333 
334     virtual const AVTPart*
335     createAVTPart(
336             const Locator*              locator,
337             const XalanDOMChar*         str,
338             XalanDOMString::size_type   len,
339             const PrefixResolver&       resolver);
340 
341     virtual const AVT**
342     allocateAVTPointerVector(size_type  theLength);
343 
344     virtual const AVTPart**
345     allocateAVTPartPointerVector(size_type  theLength);
346 
347     virtual const XalanQName*
348     createXalanQName(
349             const XalanDOMString&       qname,
350             const NamespacesStackType&  namespaces,
351             const Locator*              locator = 0,
352             bool                        fUseDefault = false);
353 
354     virtual const XalanQName*
355     createXalanQName(
356             const XalanDOMChar*         qname,
357             const NamespacesStackType&  namespaces,
358             const Locator*              locator = 0,
359             bool                        fUseDefault = false);
360 
361     virtual const XalanQName**
362     tokenizeQNames(
363             size_type&                  count,
364             const XalanDOMChar*         qnameTokens,
365             const NamespacesStackType&  namespaces,
366             const Locator*              locator = 0,
367             bool                        fUseDefault = false);
368 
369     virtual ElemTemplateElement*
370     createElement(
371             int                         token,
372             Stylesheet&                 stylesheetTree,
373             const AttributeListType&    atts,
374             const Locator*              locator = 0);
375 
376     virtual ElemTemplateElement*
377     createElement(
378             int                         token,
379             Stylesheet&                 stylesheetTree,
380             const XalanDOMChar*         name,
381             const AttributeListType&    atts,
382             const Locator*              locator = 0);
383 
384     virtual ElemTemplateElement*
385     createElement(
386             Stylesheet&                 stylesheetTree,
387             const XalanDOMChar*         chars,
388             XalanDOMString::size_type   length,
389             bool                        preserveSpace,
390             bool                        disableOutputEscaping,
391             const Locator*              locator = 0);
392 
393     virtual ElemTemplateElement*
394     createElement(
395             Stylesheet&                 stylesheetTree,
396             const XalanDOMChar*         name,
397             const AttributeListType&    atts,
398             ExtensionNSHandler&         handler,
399             const Locator*              locator = 0);
400 
401     virtual const XalanMatchPatternData*
402     createXalanMatchPatternData(
403             const ElemTemplate&     theTemplate,
404             size_type               thePosition,
405             const XalanDOMString&   theTargetString,
406             const XPath&            theMatchPattern,
407             const XalanDOMString&   thePatternString,
408             XPath::eMatchScore      thePriority);
409 
410     static eElementToken
411     getElementNameToken(const XalanDOMString&   name);
412 
413     typedef XalanVector<StylesheetRoot*>            StylesheetVectorType;
414     typedef XalanVector<ElemTemplateElement*>   ElemTemplateElementVectorType;
415 
416 private:
417 
418     const AVT**
doAllocateAVTPointerVector(size_type theSize)419     doAllocateAVTPointerVector(size_type    theSize)
420     {
421         assert(sizeof(AVT**) == sizeof(PointerVectorAllocatorType::value_type));
422 
423         return reinterpret_cast<const AVT**>(m_pointerVectorAllocator.allocate(theSize));
424     }
425 
426     const AVTPart**
doAllocateAVTPartPointerVector(size_type theSize)427     doAllocateAVTPartPointerVector(size_type    theSize)
428     {
429         assert(sizeof(AVTPart**) == sizeof(PointerVectorAllocatorType::value_type));
430 
431         return reinterpret_cast<const AVTPart**>(m_pointerVectorAllocator.allocate(theSize));
432     }
433 
434     const XalanQName**
doAllocateXalanQNamePointerVector(size_type theSize)435     doAllocateXalanQNamePointerVector(size_type theSize)
436     {
437         assert(sizeof(XalanQName**) == sizeof(PointerVectorAllocatorType::value_type));
438 
439         return reinterpret_cast<const XalanQName**>(m_pointerVectorAllocator.allocate(theSize));
440     }
441 
442 
443     XSLTEngineImpl&                         m_processor;
444 
445     XPathFactory&                           m_xpathFactory;
446 
447     typedef XalanMemMgrAutoPtr<XPathProcessor>  XPathProcessAutoPtr;
448 
449     XPathProcessAutoPtr                     m_xpathProcessor;
450 
451     StylesheetVectorType                    m_stylesheets;
452 
453     XalanDOMStringPool                      m_stringPool;
454 
455     XalanDOMCharVectorAllocatorType         m_xalanDOMCharVectorAllocator;
456 
457     mutable XalanDOMString                  m_tempBuffer;
458 
459     XalanQNameByValue                       m_scratchQName;
460 
461     XalanDOMStringCache                     m_stringCache;
462 
463     XalanAVTAllocator                       m_avtAllocator;
464 
465     XalanAVTPartSimpleAllocator             m_avtPartSimpleAllocator;
466 
467     XalanAVTPartXPathAllocator              m_avtPartXPathAllocator;
468 
469     XalanQNameByValueAllocator              m_xalanQNameByValueAllocator;
470 
471     const XalanQNameByReference             m_useAttributeSetsQName;
472 
473     PointerVectorAllocatorType              m_pointerVectorAllocator;
474 
475     ElemTemplateElementVectorType           m_allocatedElements;
476 
477     XalanElemApplyTemplatesAllocator        m_elemApplyTemplatesAllocator;
478 
479     XalanElemAttributeAllocator             m_elemAttributeAllocator;
480 
481     XalanElemAttributeSetAllocator          m_elemAttributeSetAllocator;
482 
483     XalanElemCallTemplateAllocator          m_elemCallTemplateAllocator;
484 
485     XalanElemElementAllocator               m_elemElementAllocator;
486 
487     XalanElemLiteralResultAllocator         m_elemLiteralResultAllocator;
488 
489     XalanElemTemplateAllocator              m_elemTemplateAllocator;
490 
491     XalanElemTextLiteralAllocator           m_elemTextLiteralAllocator;
492 
493     XalanElemValueOfAllocator               m_elemValueOfAllocator;
494 
495     XalanElemVariableAllocator              m_elemVariableAllocator;
496 
497     XalanMatchPatternDataAllocator          m_matchPatternDataAllocator;
498 
499     const XalanQNameByReference             m_spaceAttrQName;
500 
501     // Static strings for stylesheet compilation...
502 
503     // The string "if"
504     static const XalanDOMChar   s_if[];
505 
506     // The string "key"
507     static const XalanDOMChar   s_key[];
508 
509     // The string "copy"
510     static const XalanDOMChar   s_copy[];
511 
512     // The string "sort"
513     static const XalanDOMChar   s_sort[];
514 
515     // The string "text"
516     static const XalanDOMChar   s_text[];
517 
518     // The string "when"
519     static const XalanDOMChar   s_when[];
520 
521     // The string "empty"
522     static const XalanDOMChar   s_empty[];
523 
524     // The string "param"
525     static const XalanDOMChar   s_param[];
526 
527     // The string "choose"
528     static const XalanDOMChar   s_choose[];
529 
530     // The string "import"
531     static const XalanDOMChar   s_import[];
532 
533     // The string "number"
534     static const XalanDOMChar   s_number[];
535 
536     // The string "output"
537     static const XalanDOMChar   s_output[];
538 
539     // The string "comment"
540     static const XalanDOMChar   s_comment[];
541 
542     // The string "copy-of"
543     static const XalanDOMChar   s_copyOf[];
544 
545     // The string "element"
546     static const XalanDOMChar   s_element[];
547 
548     // The string "include"
549     static const XalanDOMChar   s_include[];
550 
551     // The string "message"
552     static const XalanDOMChar   s_message[];
553 
554     // The string "fallback"
555     static const XalanDOMChar   s_fallback[];
556 
557     // The string "for-each"
558     static const XalanDOMChar   s_forEach[];
559 
560     // The string "template"
561     static const XalanDOMChar   s_template[];
562 
563     // The string "value-of"
564     static const XalanDOMChar   s_valueOf[];
565 
566     // The string "variable"
567     static const XalanDOMChar   s_variable[];
568 
569     // The string "attribute"
570     static const XalanDOMChar   s_attribute[];
571 
572     // The string "otherwise"
573     static const XalanDOMChar   s_otherwise[];
574 
575     // The string "transform"
576     static const XalanDOMChar   s_transform[];
577 
578     // The string "stylesheet"
579     static const XalanDOMChar   s_stylesheet[];
580 
581     // The string "with-param"
582     static const XalanDOMChar   s_withParam[];
583 
584     // The string "strip-space"
585     static const XalanDOMChar   s_stripSpace[];
586 
587     // The string "apply-imports"
588     static const XalanDOMChar   s_applyImports[];
589 
590     // The string "attribute-set"
591     static const XalanDOMChar   s_attributeSet[];
592 
593     // The string "call-template"
594     static const XalanDOMChar   s_callTemplate[];
595 
596     // The string "decimal-format"
597     static const XalanDOMChar   s_decimalFormat[];
598 
599     // The string "preserve-space"
600     static const XalanDOMChar   s_preserveSpace[];
601 
602     // The string "apply-templates"
603     static const XalanDOMChar   s_applyTemplates[];
604 
605     // The string "namespace-alias"
606     static const XalanDOMChar   s_namespaceAlias[];
607 
608     // The string "processing-instruction"
609     static const XalanDOMChar   s_processingInstruction[];
610 
611     // A struct for an array that maps stylesheet element names
612     // to int tokens.
613     struct ElementTokenTableEntry
614     {
615         const XalanDOMChar*     m_name;
616 
617         eElementToken           m_token;
618     };
619 
620     static const ElementTokenTableEntry     s_elementTokenTable[];
621 
622     static const XalanSize_t                s_elementTokenTableSize;
623 
624     static const ElementTokenTableEntry&    s_elementTokenTableLast;
625 
626     static const ElementTokenTableEntry&    s_elementTokenTableDummy;
627 };
628 
629 
630 
631 }
632 
633 
634 
635 #endif  // STYLESHEETCONSTRUCTIONCONTEXTDEFAULT_HEADER_GUARD_1357924680
636