@Override
	public void setPrefix(final String prefix, final String uri)
			throws XMLStreamException {
		if (prefix == null) {
			throw new IllegalArgumentException("prefix may not be null");
		}
		if (prefix.equals(JDOMConstants.NS_PREFIX_XMLNS)) {
			return;
		}
		if (document == null || done) {
			throw new IllegalStateException(
					"Attempt to set prefix at an illegal stream state.");
		}
		
		/*
		 * Setting a prefix (or a default namespace) is not the same as writing the namespace.
		 * It is only a 'binding' of the URI to a prefix. This binding is used for two purposes:
		 * - any prefixes bound in here are used if a prefix is not specified in the startelement
		 * - if repairnamespace is false, then all used namespaces must first be pre-bound
		 * This method is specified to allow a null URI. This makes no sense unless the prefix is ""
		 * and will cause a Namespace exception if the prefix is not "".
		 */

		final Namespace ns = Namespace.getNamespace(prefix, uri);
		if (!boundstack.isInScope(ns)) {
			final ArrayList<Namespace> al = new ArrayList<Namespace>();
			for (Namespace n : boundstack) {
				if (n.getPrefix().equals(prefix)) {
					// do not rebind the same prefix with the old URI.
					continue;
				}
				al.add(n);
			}
			// bind the new URI to the prefix.
			al.add(ns);
			// kill the previous bindings.
			boundstack.pop();
			// reset the binding.
			boundstack.push(al);
		}
	}