package textbender.a.r.page.navdo; // 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.event.*; import java.io.*; import java.util.*; 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.r.page.*; import textbender.d.gene.*; import textbender.d.gene.xhtml.*; import textbender.g.xml.dom.*; import textbender.g.xml.dom.bootstrap.*; import textbender.g.hold.*; import textbender.g.lang.*; import textbender.o.swing.*; /** An action to undo-ably modify the current page, * for purposes of testing and reference. */ public @ThreadRestricted("AWT event dispatch") final class TestAction extends BlindAbstractAction { /** Constructs a TestAction. * * @param pV context */ public TestAction( PageVisit pV ) { assert java.awt.EventQueue.isDispatchThread(); pageVisit = pV; putValue( NAME, "Test Modify" ); putValue( SMALL_ICON, ImageIconX.tryCreate( getClass().getClassLoader().getResource( "toolbarButtonGraphics/general/Edit16.gif" ), 16 )); // from textbender/o/jlfgr.jar setEnabled( pageVisit.file() != null ); // per VersionedFile.getOrCreateDocumentAsModifiableFile } // - B l i n d - A b s t r a c t - A c t i o n ---------------------------------------- /** Creates a new {@linkplain VersionedFile versioned file} * with a minor modification in it, and shows it in the browser. * The modification is some extra text * appended to a {@linkplain User#getLeafGene() user-specified gene}. */ public @Override void act() { assert java.awt.EventQueue.isDispatchThread(); try { final File oldFile = VersionedFile.getOrCreateDocumentAsModifiableFile( pageVisit ); final VersionedFile newFile = VersionedFile.after( oldFile ); { // read old file to clean DOM // ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` final DOMImplementationLS dom = DOMImplementationRegistryX.implementationLS(); final LSParser parser = dom.createLSParser ( dom.MODE_SYNCHRONOUS, "http://www.w3.org/TR/REC-xml" ); final DOMConfiguration domConfig = parser.getDomConfig(); Exception x = null; // thus far final StringWriter errorStringWriter = new StringWriter(); final PrintWriter errorPrintWriter = new PrintWriter( errorStringWriter ); DOMErrorHandlerPW errorHandler = new DOMErrorHandlerPW( errorPrintWriter ); domConfig.setParameter( "error-handler", errorHandler ); domConfig.setParameter ( "resource-resolver", new textbender.d.gene.xhtml.RecombinantXHTML.DOMResourceResolverMin( dom ) ); Document documentF = null; // till parsed from file final InputStream in = new BufferedInputStream( new FileInputStream( oldFile )); try { final LSInput lsInput = dom.createLSInput(); lsInput.setByteStream( in ); try{ documentF = parser.parse( lsInput ); } catch( LSException xLS ) { x = xLS; } // 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(); x = new Exception ( "Unable to modify, errors/warnings from parser: " + Integer.toString( errorHandler.count() ) + "\n" + errorStringWriter.toString() ); } if( x != null ) throw x; // modify DOM // ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` final int gIndexToModify; { final Element userSelectedLeafGene = pageVisit.user().getLeafGene(); if( userSelectedLeafGene == null ) gIndexToModify = -1; // first leaf else gIndexToModify = Gene.gIndexOf( userSelectedLeafGene ); } modifyWithin( documentF, gIndexToModify ); // write to new file // ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` // final DOMSource source = new DOMSource( documentF ); // final StreamResult result = new StreamResult( newFile ); // final Transformer t = PageDaemonsEDT.i().transformerFactory().newTransformer(); // identity // // t.setOutputProperty( OutputKeys.METHOD, "xml" ); // else may default to HTML, depending on content // final DocumentType doctype = documentF.getDoctype(); // doctype: if( doctype != null ) // FIX, replace with call to TransformerX.setOutputDoctypeFrom // { // String id = doctype.getSystemId(); // if( id == null || id.length() == 0 ) break doctype; // // t.setOutputProperty( OutputKeys.DOCTYPE_SYSTEM, id ); // else no DOCTYPE declaration is output // id = doctype.getPublicId(); // if( id == null || id.length() == 0 ) break doctype; // // t.setOutputProperty( OutputKeys.DOCTYPE_PUBLIC, id ); // } // t.transform( source, result ); // though it crams XML and DOCTYPE declarations onto first line RecombinantXHTML.write ( documentF, PageDaemonsEDT.i().transformerFactory(), new StreamResult(newFile) ); } newFile.loadAsNewVersion( pageVisit ); } catch( Exception x ) { pageVisit.user().logAndShow( x ); } // Exception parsing, IOException, TransformerException newTransformer transform } //// P r i v a t e /////////////////////////////////////////////////////////////////////// /** @param gIndexToModify gIndex of leaf gene to modify; * or negative to modify the first leaf gene in document order */ private static boolean modifyWithin( final Node node, final int gIndexToModify ) { // Depth first (for speed). // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - for( Node child = node.getFirstChild(); child != null; child = child.getNextSibling() ) { if( modifyWithin( child, gIndexToModify )) return true; } // This node. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if( !( node instanceof Element )) return false; final Element element = (Element)node; if( gIndexToModify > 0 ) { if( Gene.gIndexOf(element) != gIndexToModify ) return false; } else if( !Gene.isGene(element) || Gene.hasChildGene(element) ) return false; element.appendChild( element.getOwnerDocument().createTextNode( " TESTING" )); return true; } private final PageVisit pageVisit; }