protected final String findOrCreateAttrPrefix ( String suggPrefix , String nsURI , SimpleOutputElement elem ) throws XMLStreamException { if ( nsURI == null || nsURI . length ( ) == 0 ) { /* Attributes never use the default namespace; missing * prefix always leads to the empty ns... so nothing * special is needed here. */ return null ; } // Maybe the suggested prefix is properly bound? if ( suggPrefix != null ) { int status = elem . isPrefixValid ( suggPrefix , nsURI , false ) ; if ( status == SimpleOutputElement . PREFIX_OK ) { return suggPrefix ; } /* Otherwise, if the prefix is unbound, let's just bind * it -- if caller specified a prefix, it probably prefers * binding that prefix even if another prefix already existed? * The remaining case (already bound to another URI) we don't * want to touch, at least not yet: it may or not be safe * to change binding, so let's just not try it. */ if ( status == SimpleOutputElement . PREFIX_UNBOUND ) { elem . addPrefix ( suggPrefix , nsURI ) ; doWriteNamespace ( suggPrefix , nsURI ) ; return suggPrefix ; } } // If not, perhaps there's another existing binding available? String prefix = elem . getExplicitPrefix ( nsURI ) ; if ( prefix != null ) { // already had a mapping for the URI... cool. return prefix ; } /* Nope, need to create one. First, let's see if there's a * preference... */ if ( suggPrefix != null ) { prefix = suggPrefix ; } else if ( mSuggestedPrefixes != null ) { prefix = mSuggestedPrefixes . get ( nsURI ) ; // note: def ns is never added to suggested prefix map } if ( prefix != null ) { /* Can not use default namespace for attributes. * Also, re-binding is tricky for attributes; can't * re-bind anything that's bound on this scope... or * used in this scope. So, to simplify life, let's not * re-bind anything for attributes. */ if ( prefix . length ( ) == 0 || ( elem . getNamespaceURI ( prefix ) != null ) ) { prefix = null ; } } if ( prefix == null ) { if ( mAutoNsSeq == null ) { mAutoNsSeq = new int [ 1 ] ; mAutoNsSeq [ 0 ] = 1 ; } prefix = mCurrElem . generateMapping ( mAutomaticNsPrefix , nsURI , mAutoNsSeq ) ; } // Ok; so far so good: let's now bind and output the namespace: elem . addPrefix ( prefix , nsURI ) ; doWriteNamespace ( prefix , nsURI ) ; return prefix ; }
Method called to somehow find a prefix for given namespace to be used for a new start element ; either use an existing one or generate a new one . If a new mapping needs to be generated it will also be automatically bound and necessary namespace declaration output .