package textbender.a.u.transfer.clipboard; // 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.awt.*; import java.awt.datatransfer.*; import java.beans.*; import java.io.*; import java.util.*; import java.util.List; import java.util.concurrent.atomic.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; import org.w3c.dom.*; import org.w3c.dom.ls.*; import textbender.a.u.encoding.Encoder; import textbender.a.u.transfer.*; import textbender.d.gene.*; import textbender.d.revision.*; import textbender.g.lang.*; import textbender.g.util.logging.*; import textbender.g.xml.dom.*; import textbender.g.xml.dom.bootstrap.*; import static textbender._.Textbender.TEXTBENDER_NAMESPACE; /** Copies transferands to the clipboard. */ public @ThreadSafe final class Copier implements ClipboardOwner, PropertyChangeListener { /** The single instance of Copier. */ static Copier i() { return instanceA.get(); } private static final AtomicReference instanceA = new AtomicReference(); /** Creates the single instance of Copier, * and makes it available via {@linkplain #i() i}(). * * @param transferService source of transferands */ public Copier( final PRTransferS transferService ) throws IOException { if( !instanceA.compareAndSet( /*expect*/null, Copier.this )) throw new IllegalStateException(); // clipIndexEncoder.test(); IndexBlockMapW.create(); transferService.addPropertyChangeListener( Copier.this ); // no need to unregister, registry does not outlive this listener } // - C l i p b o a r d - O w n e r ---------------------------------------------------- public void lostOwnership( Clipboard clipboard, Transferable contents ) { // will later publish and visualize ownership/loss somehow, per task notes } // - P r o p e r t y - C h a n g e - L i s t e n e r ---------------------------------- public @ThreadSafe void propertyChange( PropertyChangeEvent e ) { final Transferand transferand = (Transferand)e.getNewValue(); if( transferand == null ) return; synchronized( Copier.this ) { try { // Read transfer source from file to DOM. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - final File file = transferand.documentFile(); final DOMImplementationLS domLS = DOMImplementationRegistryX.implementationLS(); final DOMImplementation dom = (DOMImplementation)domLS; final LSParser parser = domLS.createLSParser ( domLS.MODE_SYNCHRONOUS, "http://www.w3.org/TR/REC-xml" ); final DOMConfiguration parserConfig = parser.getDomConfig(); final InputStream in = new BufferedInputStream( new FileInputStream( file )); final LSInput lsInput = domLS.createLSInput(); parserConfig.setParameter ( "resource-resolver", new textbender.d.gene.xhtml.RecombinantXHTML.DOMResourceResolverMin( domLS ) ); final StringWriter errorStringWriter = new StringWriter(); final PrintWriter errorPrintWriter = new PrintWriter( errorStringWriter ); final DOMErrorHandlerPW errorHandler = new DOMErrorHandlerPW( errorPrintWriter ); parserConfig.setParameter( "error-handler", errorHandler ); Exception xParse = null; // thus far Document document = null; // till parsed from file errorHandler.reset(); try { lsInput.setByteStream( in ); try{ document = parser.parse( lsInput ); } catch( LSException x ) { xParse = x; } // fatal parse exception, will also be reported to the errorHandler, though what follows does not depend on it } finally{ in.close(); } if( errorHandler.count() > 0 ) { errorPrintWriter.flush(); xParse = new Exception ( "Unable to copy to clipboard. Reading source file:" + "\n " + file + "\n" + "Errors/warnings from parser: " + Integer.toString( errorHandler.count() ) + "\n" + errorStringWriter.toString() ); } if( xParse != null ) throw xParse; final Element metaData = DocumentRT.findMetaData( document ); final Element revisionLine = Revision.findRevisionLine( metaData ); final Element gg = Gene.findGG( metaData ); final List geneList; { final NodeList gList = gg.getElementsByTagNameNS ( TEXTBENDER_NAMESPACE, "g" ); geneList = Arrays.asList( new Element[gList.getLength()] ); initRecursively( document, geneList ); } // Copy from source (S) to transferand (T), as DOM fragment. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - final DocumentFragment fragmentT = document.createDocumentFragment(); Element highAncestor0S; // highest ancestor in source of gene0, that is not common with gene1; or gene0 itself if there is none { final Element gene0S = geneList.get( transferand.gIndex0() ); final Element gene1S = geneList.get( transferand.gIndex1() ); highAncestor0S = gene0S; // thus far bClear(); if( bAppendAsIndent(gene0S.getPreviousSibling()).length() > 0 ) { fragmentT.appendChild( document.createTextNode( b.toString() )); // indenting the first gene } Node insertionParentT = fragmentT; Node insertionPointT = null; for( Node nodeS = gene0S;; ) { if( NodeX.areAncestorAndDescendant( nodeS, gene1S )) // 2. Descend from common ancestor toward gene1. (Descents occur when gene1 is lower than the common ancestor. All descents occur after any rises (1 below).) { // ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` final Node ancestor = nodeS.cloneNode( /*deeply*/false ); insertionParentT.insertBefore( ancestor, insertionPointT ); insertionParentT = ancestor; bClear(); if( bAppendAsIndent(nodeS.getLastChild()).length() > 0 ) { b.insert( 0 , '\n' ); insertionPointT = document.createTextNode( b.toString() ); ancestor.appendChild( insertionPointT ); // indenting the ancestor end tag } else insertionPointT = null; // append nodeS = nodeS.getFirstChild(); continue; } insertionParentT.insertBefore ( nodeS.cloneNode( /*deeply*/true ), insertionPointT ); if( nodeS.isSameNode( gene1S )) break; for( ;; ) { Node nextNodeS = nodeS.getNextSibling(); if( nextNodeS != null ) { nodeS = nextNodeS; break; } // 1. Rise from gene0 toward common ancestor with gene1. (Rises occur when gene0 is lower than the common ancestor. All rises occur before any descents (2 above).) // ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` nodeS = nodeS.getParentNode(); highAncestor0S = (Element)nodeS; final Element parent = (Element)nodeS.cloneNode( /*deeply*/false ); parent.appendChild( document.createTextNode( "\n" )); // start tag on one line parent.appendChild( fragmentT ); assert !fragmentT.hasChildNodes() : "empty fragment"; fragmentT.appendChild( parent ); bClear(); if( bAppendAsIndent(nodeS.getPreviousSibling()).length() > 0 ) { fragmentT.insertBefore // indenting the parent ( document.createTextNode(b.toString()), parent ); } } } } // Prepend common ancestors, as namespace context. (a before b) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - fragmentT.insertBefore // start tags all on one line ( document.createTextNode( "\n" ), fragmentT.getFirstChild() ); fragmentT.appendChild( document.createTextNode( "\n" )); // end tags too for( Node node = highAncestor0S;; ) { node = node.getParentNode(); if( !(node instanceof Element )) break; final Node ancestor = node.cloneNode( /*deeply*/false ); ancestor.appendChild( fragmentT ); assert !fragmentT.hasChildNodes() : "empty fragment"; fragmentT.appendChild( ancestor ); } // Convert transferand genes to clip-genes. (b after a, for correct formation of namespace prefixes) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - final IndexBlock block = IndexBlockMapW.i().getOrCreateIndexBlock ( file, geneList.size(), new IndexBlockMapW.KeySD ( revisionLine.getAttributeNS(null,"id"), Gene.gROf(gg) ), clipIndexEncoder ); convertToClipGenes( fragmentT, block ); // Serialize transferand fragment to transferand string. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - DOMSource source = new DOMSource( fragmentT ); Transformer transformer = transformerFactory.newTransformer(); // identity, string serializer for embedded sequence copies // transformer.setOutputProperty( OutputKeys.METHOD, "xml" ); // else may default to HTML, depending on content transformer.setOutputProperty( OutputKeys.ENCODING, "US-ASCII" ); transformer.setOutputProperty( OutputKeys.OMIT_XML_DECLARATION, "yes" ); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult( writer ); transformer.transform( source, result ); bClear(); b.append( writer.toString() ); // Strip common ancestors from the string. (Aside from their namespace context, the user is unlikely to need them.) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - for( int c = 0; c < b.length(); ++c ) { if( b.charAt(c) == '\n' ) // delimiting first line, which contains only common ancestor start tags { b.delete( 0, c + 1 ); break; } } for( int c = b.length() - 1; c >= 0; --c ) { if( b.charAt(c) == '\n' ) // delimiting last line, which contains only common ancestor end tags { b.delete( c, b.length() ); break; } } } catch( Exception x ) // Exception parsing, IOException, TransformerException newTransformer transform { LoggerX.i(Copier.class).log( LoggerX.WARNING, /*message*/"", x ); b.append( x.toString() ); } b.append( '\n' ); // always end in a newline // // Add leading/trailing newlines, to aid user in resolving transferand after paste. (User can easily delete them.) // // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // b.insert( 0, '\n' ); // b.append( '\n' ); //////// but user's can do that with an editor macro if they want, or use other means to highlight the pasted area // Copy transferand string to clipboard. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - StringSelection copy = new StringSelection( b.toString() ); Toolkit.getDefaultToolkit().getSystemClipboard().setContents( copy, /*owner*/Copier.this ); } } //// P r i v a t e /////////////////////////////////////////////////////////////////////// /** Common string builder. */ private @ThreadRestricted("holds Copier.this") final StringBuilder b = new StringBuilder(); /** Empties and returns the common string builder. */ private @ThreadRestricted("holds Copier.this") StringBuilder bClear() { assert Thread.holdsLock( Copier.this ); b.delete( 0, Integer.MAX_VALUE ); return b; } /** @param node node to treat as leading indentation (if it is text) * @return string builder b, containing spaces * equivalent to indentation, if any */ private @ThreadRestricted("holds Copier.this") StringBuilder bAppendAsIndent( Node node ) { assert Thread.holdsLock( Copier.this ); if( !( node instanceof Text )) return b; final String textData = ((Text)node).getData(); final int cLast = textData.length() - 1; for( int c = cLast; c >= 0; --c ) { if( textData.charAt(c) == '\n' ) // first preceding newline, { for(; c < cLast; ++c ) b.append( ' ' ); break; } } return b; } private @ThreadRestricted("holds Copier.this") final ClipIndexEncoder clipIndexEncoder = new ClipIndexEncoder(); private @ThreadRestricted("holds Copier.this") void convertToClipGenes( final Node node, final IndexBlock block ) { assert Thread.holdsLock( Copier.this ); // Descendants. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - for( Node child = node.getFirstChild(); child != null; child = child.getNextSibling() ) { convertToClipGenes( child, block ); } // This node. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if( !( node instanceof Element )) return; final Element element = (Element)node; final Attr g = element.getAttributeNodeNS( TEXTBENDER_NAMESPACE, "g" ); if( g == null ) return; final String gString = g.getValue(); final int gIndex = Gene.gIndexOf( gString ); if( !Gene.gIndexIsGene( gIndex )) return; final String cString = clipIndexEncoder.cOf( block.clipIndexOf( gIndex )); element.removeAttributeNode( g ); element.setAttributeNS ( TEXTBENDER_NAMESPACE, DOM.buildAttributePrefix( element, TEXTBENDER_NAMESPACE, b ) .append( 'c' ).toString(), cString ); // adjust left padding, if gene is marginal // ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` final int delta = gString.length() - cString.length(); if( delta != 0 && Encoder.isMarginal(element) ) Gene.adjustLeftPadding( element, delta, b ); } private static void initRecursively( final Node node, final List geneList ) { if( node instanceof Element ) { final Element element = (Element)node; int gIndex = Gene.gIndexOf( element ); if( gIndex >= 0 ) geneList.set( gIndex, element ); } for( Node child = node.getFirstChild(); child != null; child = child.getNextSibling() ) { initRecursively( child, geneList ); } } private @ThreadRestricted("holds Copier.this") final TransformerFactory transformerFactory = TransformerFactory.newInstance(); }