package textbender.d.revision; // 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 java.util.*; import org.w3c.dom.*; import textbender.g.lang.*; import textbender.g.xml.dom.*; import static textbender._.Textbender.TEXTBENDER_NAMESPACE; /** Utilities for revision lines and working documents. * * @see * revision.mod */ public final @ThreadSafe class Revision { private Revision() {} /** Constructs an unattached revisionLine element. * * @param metaData the document's * {@linkplain textbender.d.gene.DocumentRT#findMetaData(Node) meta-data element}, * to which caller will attach new revisionLine element * @param b string builder to use, overwriting its existing content * * @see #ensureRevisionLine(Element,StringBuilder) */ public static Element createRevisionLine( final Element metaData, final StringBuilder b ) { Element revisionLine = metaData.getOwnerDocument().createElementNS ( TEXTBENDER_NAMESPACE, DOM.buildElementPrefix( metaData, TEXTBENDER_NAMESPACE, b ) .append( "revision-line" ).toString() ); return revisionLine; } /** Returns a document's 'revision-line' element; if necessary creating it * and attaching it into the document. * * @param metaData the document's * {@linkplain textbender.d.gene.DocumentRT#findMetaData(Node) meta-data element} * @param b string builder to use, overwriting its existing content * * @return 'revision-line' element, or null if none found * * @see #createRevisionLine(Element,StringBuilder) * @see #findRevisionLine(Element) */ public static Element ensureRevisionLine( final Element metaData, StringBuilder b ) { Element revisionLine = findRevisionLine( metaData ); if( revisionLine == null ) { final Document document = metaData.getOwnerDocument(); // final Node firstChild = metaData.getFirstChild; revisionLine = createRevisionLine( metaData, b ); // metaData.insertBefore( document.createTextNode( "\n " ), firstChild ); // metaData.insertBefore( revisionLine, firstChild ); metaData.appendChild( revisionLine ); metaData.appendChild( document.createTextNode( "\n " )); } return revisionLine; } /** Finds a document's 'revision-line' element. * * @param metaData the document's * {@linkplain textbender.d.gene.DocumentRT#findMetaData(Node) meta-data element} * @return 'revision-line' element, or null if none found * * @see #ensureRevisionLine(Element,StringBuilder) */ public static Element findRevisionLine( final Element metaData ) { for( Node child = metaData.getFirstChild(); child != null; child = child.getNextSibling() ) { if( !( child instanceof Element )) continue; if( "revision-line".equals( child.getLocalName() ) && TEXTBENDER_NAMESPACE.equals( child.getNamespaceURI() )) return (Element)child; } return null; } }