001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 package org.apache.xalan.extensions; 020 021 import java.util.Iterator; 022 import javax.xml.XMLConstants; 023 import javax.xml.namespace.NamespaceContext; 024 import org.apache.xalan.res.XSLMessages; 025 import org.apache.xalan.res.XSLTErrorResources; 026 027 /** 028 * A sample implementation of NamespaceContext, with support for 029 * EXSLT extension functions and Java extension functions. 030 */ 031 public class ExtensionNamespaceContext implements NamespaceContext 032 { 033 public static final String EXSLT_PREFIX = "exslt"; 034 public static final String EXSLT_URI = "http://exslt.org/common"; 035 public static final String EXSLT_MATH_PREFIX = "math"; 036 public static final String EXSLT_MATH_URI = "http://exslt.org/math"; 037 public static final String EXSLT_SET_PREFIX = "set"; 038 public static final String EXSLT_SET_URI = "http://exslt.org/sets"; 039 public static final String EXSLT_STRING_PREFIX = "str"; 040 public static final String EXSLT_STRING_URI = "http://exslt.org/strings"; 041 public static final String EXSLT_DATETIME_PREFIX = "datetime"; 042 public static final String EXSLT_DATETIME_URI = "http://exslt.org/dates-and-times"; 043 public static final String EXSLT_DYNAMIC_PREFIX = "dyn"; 044 public static final String EXSLT_DYNAMIC_URI = "http://exslt.org/dynamic"; 045 public static final String JAVA_EXT_PREFIX = "java"; 046 public static final String JAVA_EXT_URI = "http://xml.apache.org/xalan/java"; 047 048 /** 049 * Return the namespace uri for a given prefix 050 */ 051 public String getNamespaceURI(String prefix) 052 { 053 if (prefix == null) 054 throw new IllegalArgumentException( 055 XSLMessages.createMessage( 056 XSLTErrorResources.ER_NAMESPACE_CONTEXT_NULL_PREFIX, null)); 057 058 if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) 059 return XMLConstants.NULL_NS_URI; 060 else if (prefix.equals(XMLConstants.XML_NS_PREFIX)) 061 return XMLConstants.XML_NS_URI; 062 else if (prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) 063 return XMLConstants.XMLNS_ATTRIBUTE_NS_URI; 064 else if (prefix.equals(EXSLT_PREFIX)) 065 return EXSLT_URI; 066 else if (prefix.equals(EXSLT_MATH_PREFIX)) 067 return EXSLT_MATH_URI; 068 else if (prefix.equals(EXSLT_SET_PREFIX)) 069 return EXSLT_SET_URI; 070 else if (prefix.equals(EXSLT_STRING_PREFIX)) 071 return EXSLT_STRING_URI; 072 else if (prefix.equals(EXSLT_DATETIME_PREFIX)) 073 return EXSLT_DATETIME_URI; 074 else if (prefix.equals(EXSLT_DYNAMIC_PREFIX)) 075 return EXSLT_DYNAMIC_URI; 076 else if (prefix.equals(JAVA_EXT_PREFIX)) 077 return JAVA_EXT_URI; 078 else 079 return XMLConstants.NULL_NS_URI; 080 } 081 082 /** 083 * Return the prefix for a given namespace uri. 084 */ 085 public String getPrefix(String namespace) 086 { 087 if (namespace == null) 088 throw new IllegalArgumentException( 089 XSLMessages.createMessage( 090 XSLTErrorResources.ER_NAMESPACE_CONTEXT_NULL_NAMESPACE, null)); 091 092 if (namespace.equals(XMLConstants.XML_NS_URI)) 093 return XMLConstants.XML_NS_PREFIX; 094 else if (namespace.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) 095 return XMLConstants.XMLNS_ATTRIBUTE; 096 else if (namespace.equals(EXSLT_URI)) 097 return EXSLT_PREFIX; 098 else if (namespace.equals(EXSLT_MATH_URI)) 099 return EXSLT_MATH_PREFIX; 100 else if (namespace.equals(EXSLT_SET_URI)) 101 return EXSLT_SET_PREFIX; 102 else if (namespace.equals(EXSLT_STRING_URI)) 103 return EXSLT_STRING_PREFIX; 104 else if (namespace.equals(EXSLT_DATETIME_URI)) 105 return EXSLT_DATETIME_PREFIX; 106 else if (namespace.equals(EXSLT_DYNAMIC_URI)) 107 return EXSLT_DYNAMIC_PREFIX; 108 else if (namespace.equals(JAVA_EXT_URI)) 109 return JAVA_EXT_PREFIX; 110 else 111 return null; 112 } 113 114 public Iterator getPrefixes(String namespace) 115 { 116 final String result = getPrefix(namespace); 117 118 return new Iterator () { 119 120 private boolean isFirstIteration = (result != null); 121 122 public boolean hasNext() { 123 return isFirstIteration; 124 } 125 126 public Object next() { 127 if (isFirstIteration) { 128 isFirstIteration = false; 129 return result; 130 } 131 else 132 return null; 133 } 134 135 public void remove() { 136 throw new UnsupportedOperationException(); 137 } 138 }; 139 } 140 }