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 #include "FunctionSystemProperty.hpp"
19 
20 
21 
22 #include <xalanc/XalanDOM/XalanNode.hpp>
23 #include <xalanc/XalanDOM/XalanDocument.hpp>
24 
25 
26 
27 #include <xalanc/PlatformSupport/DOMStringHelper.hpp>
28 #include <xalanc/PlatformSupport/XalanMessageLoader.hpp>
29 
30 
31 
32 #include <xalanc/XPath/MutableNodeRefList.hpp>
33 #include <xalanc/XPath/NodeRefListBase.hpp>
34 #include <xalanc/XPath/XalanQName.hpp>
35 #include <xalanc/XPath/XObjectFactory.hpp>
36 
37 
38 
39 namespace XALAN_CPP_NAMESPACE {
40 
41 
42 
43 static const XalanDOMString     s_emptyString(XalanMemMgrs::getDummyMemMgr());
44 
45 
46 
FunctionSystemProperty(MemoryManager & theManager)47 FunctionSystemProperty::FunctionSystemProperty(MemoryManager&   theManager) :
48     Function(),
49     m_xsltNamespaceURI("http://www.w3.org/1999/XSL/Transform", theManager),
50     m_versionPropertyString("version", theManager),
51     m_vendorPropertyString("vendor", theManager),
52     m_vendorURLPropertyString("vendor-url", theManager),
53     m_vendorString("Apache Software Foundation", theManager),
54     m_vendorURLString("http://xml.apache.org/xalan-c", theManager)
55 {
56 }
57 
58 
59 
FunctionSystemProperty(const FunctionSystemProperty & other,MemoryManager & theManager)60 FunctionSystemProperty::FunctionSystemProperty(
61             const FunctionSystemProperty&   other,
62             MemoryManager&              theManager) :
63     Function(other),
64     m_xsltNamespaceURI(other.m_xsltNamespaceURI, theManager),
65     m_versionPropertyString(other.m_versionPropertyString, theManager),
66     m_vendorPropertyString(other.m_vendorPropertyString, theManager),
67     m_vendorURLPropertyString(other.m_vendorURLPropertyString, theManager),
68     m_vendorString(other.m_vendorString, theManager),
69     m_vendorURLString(other.m_vendorURLString, theManager)
70 {
71 }
72 
73 
74 
~FunctionSystemProperty()75 FunctionSystemProperty::~FunctionSystemProperty()
76 {
77 }
78 
79 
80 
81 
82 inline void
validateNCName(XPathExecutionContext & executionContext,XalanNode * context,const Locator * locator,const XalanDOMString & ncname)83 validateNCName(
84             XPathExecutionContext&  executionContext,
85             XalanNode*              context,
86             const Locator*          locator,
87             const XalanDOMString&   ncname)
88 {
89     if (XalanQName::isValidNCName(ncname) == false)
90     {
91         const Function::GetCachedString     theGuard(executionContext);
92 
93         executionContext.problem(
94             XPathExecutionContext::eXPath,
95             XPathExecutionContext::eError,
96             XalanMessageLoader::getMessage(
97                 theGuard.get(),
98                 XalanMessages::PropertyIsNotValidQName_1Param,
99                 "system-property()"),
100             locator,
101             context);
102     }
103 }
104 
105 
106 
107 XObjectPtr
execute(XPathExecutionContext & executionContext,XalanNode * context,const XObjectPtr arg1,const Locator * locator) const108 FunctionSystemProperty::execute(
109             XPathExecutionContext&  executionContext,
110             XalanNode*              context,
111             const XObjectPtr        arg1,
112             const Locator*          locator) const
113 {
114     assert(arg1.null() == false);
115 
116     const XalanDOMString&               fullName = arg1->str(executionContext);
117     const XalanDOMString::size_type     fullNameLength = fullName.length();
118     const XalanDOMString::size_type     indexOfNSSep = indexOf(fullName, XalanUnicode::charColon);
119 
120     if(indexOfNSSep < fullNameLength)
121     {
122         const GetCachedString   guard(executionContext);
123 
124         XalanDOMString&     theBuffer = guard.get();
125 
126         substring(fullName, theBuffer, 0, indexOfNSSep);
127 
128         validateNCName(executionContext, context, locator, theBuffer);
129 
130         const XalanDOMString* const     nspace = executionContext.getNamespaceForPrefix(theBuffer);
131 
132         if (nspace == 0)
133         {
134             const GetCachedString   theGuard(executionContext);
135 
136             executionContext.problem(
137                 XPathExecutionContext::eXPath,
138                 XPathExecutionContext::eError,
139                 XalanMessageLoader::getMessage(
140                     theGuard.get(),
141                     XalanMessages::PrefixIsNotDeclared_1Param,
142                     theBuffer),
143                 locator,
144                 context);
145         }
146         else
147         {
148             if(*nspace == m_xsltNamespaceURI)
149             {
150                 substring(fullName, theBuffer, indexOfNSSep + 1);
151 
152                 validateNCName(executionContext, context, locator, theBuffer);
153 
154                 if(equals(theBuffer, m_versionPropertyString))
155                 {
156                     return executionContext.getXObjectFactory().createNumber(1.0);
157                 }
158                 else if(equals(theBuffer, m_vendorPropertyString))
159                 {
160                     return executionContext.getXObjectFactory().createStringReference(m_vendorString);
161                 }
162                 else if(equals(theBuffer, m_vendorURLPropertyString))
163                 {
164                     return executionContext.getXObjectFactory().createStringReference(m_vendorURLString);
165                 }
166                 else
167                 {
168                     return executionContext.getXObjectFactory().createStringReference(s_emptyString);
169                 }
170             }
171         }
172     }
173     else
174     {
175         validateNCName(executionContext, context, locator, fullName);
176 
177         XalanDOMString::CharVectorType theResultVect(executionContext.getMemoryManager());
178 
179         TranscodeToLocalCodePage(fullName, theResultVect, true);
180 
181         const char* const   theEnvString =
182             std::getenv(c_str(theResultVect));
183 
184         if (theEnvString != 0)
185         {
186             GetCachedString     theResult(executionContext);
187 
188             XalanDOMString&     theString = theResult.get();
189 
190             TranscodeFromLocalCodePage(theEnvString, theString);
191 
192             return executionContext.getXObjectFactory().createString(theResult);
193         }
194     }
195 
196     return executionContext.getXObjectFactory().createStringReference(s_emptyString);
197 }
198 
199 
200 
201 FunctionSystemProperty*
clone(MemoryManager & theManager) const202 FunctionSystemProperty::clone(MemoryManager&    theManager) const
203 {
204     return XalanCopyConstruct(theManager, *this, theManager);
205 }
206 
207 
208 
209 const XalanDOMString&
getError(XalanDOMString & theResult) const210 FunctionSystemProperty::getError(XalanDOMString&    theResult) const
211 {
212     return XalanMessageLoader::getMessage(
213                 theResult,
214                 XalanMessages::FunctionAcceptsOneArgument_1Param,
215                 "system-property()");
216 }
217 
218 
219 
220 }
221