package textbender.g.xml.dom; // Copyright 2007, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import org.w3c.dom.*; /** Grab-bag of utilities for DOM. */ public final class DOM { private DOM() {} /** Builds a namespace prefix for an attribute, * to which the attribute name may be appended. * * @param context node, for prefix lookup * @param namespaceURI to which the prefix refers * @param b string builder to use, overwriting its existing content * * @return reference to b, containing the prefix, * complete with trailing ':' */ public static StringBuilder buildAttributePrefix( final Node context, final String namespaceURI, final StringBuilder b ) { return buildPrefix( context, namespaceURI, b, /*isForElement*/false ); } /** Builds a namespace prefix for an element, * to which the element name may be appended. * * @param context node, for prefix lookup * @param namespaceURI to which the prefix refers * @param b string builder to use, overwriting its existing content * * @return reference to b, containing the prefix, * complete with trailing ':' */ public static StringBuilder buildElementPrefix( final Node context, final String namespaceURI, final StringBuilder b ) { return buildPrefix( context, namespaceURI, b, /*isForElement*/true ); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// public static StringBuilder buildPrefix( final Node context, final String namespaceURI, final StringBuilder b, final boolean isForElement ) { b.delete( 0, Integer.MAX_VALUE ); assert context.getParentNode() != null: "context has parent, for accurate prefix lookup"; // Does not ensure ultimate ancestor is a document (as probably intended), but catches the most common error. Alternative of getOwnerDocument is no help, because unattached nodes still have owner document. if( isForElement && context.isDefaultNamespace( namespaceURI )) return b; // only elements can use default namespace, without a prefix final String prefix = context.lookupPrefix( namespaceURI ); if( prefix != null ) { b.append( prefix ); b.append( ':' ); } return b; } }