1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: FuncNamespace.java,v 1.10 2004/02/17 04:34:01 minchau Exp $
  18. */
  19. package com.sun.org.apache.xpath.internal.functions;
  20. import com.sun.org.apache.xml.internal.dtm.DTM;
  21. import com.sun.org.apache.xpath.internal.XPathContext;
  22. import com.sun.org.apache.xpath.internal.objects.XObject;
  23. import com.sun.org.apache.xpath.internal.objects.XString;
  24. /**
  25. * Execute the Namespace() function.
  26. * @xsl.usage advanced
  27. */
  28. public class FuncNamespace extends FunctionDef1Arg
  29. {
  30. /**
  31. * Execute the function. The function must return
  32. * a valid object.
  33. * @param xctxt The current execution context.
  34. * @return A valid XObject.
  35. *
  36. * @throws javax.xml.transform.TransformerException
  37. */
  38. public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
  39. {
  40. int context = getArg0AsNode(xctxt);
  41. String s;
  42. if(context != DTM.NULL)
  43. {
  44. DTM dtm = xctxt.getDTM(context);
  45. int t = dtm.getNodeType(context);
  46. if(t == DTM.ELEMENT_NODE)
  47. {
  48. s = dtm.getNamespaceURI(context);
  49. }
  50. else if(t == DTM.ATTRIBUTE_NODE)
  51. {
  52. // This function always returns an empty string for namespace nodes.
  53. // We check for those here. Fix inspired by Davanum Srinivas.
  54. s = dtm.getNodeName(context);
  55. if(s.startsWith("xmlns:") || s.equals("xmlns"))
  56. return XString.EMPTYSTRING;
  57. s = dtm.getNamespaceURI(context);
  58. }
  59. else
  60. return XString.EMPTYSTRING;
  61. }
  62. else
  63. return XString.EMPTYSTRING;
  64. return ((null == s) ? XString.EMPTYSTRING : new XString(s));
  65. }
  66. }