package textbender.a.u.branch; // 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.io.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; import org.w3c.dom.*; import org.w3c.dom.ls.*; import textbender.a.r.page.*; import textbender.a.r.page.navdo.*; import textbender.d.gene.xhtml.*; import textbender.g.lang.*; import textbender.g.xml.dom.*; import textbender.o.swing.*; /** Brancher of a Web document, via an in-page user-applet. */ public @ThreadRestricted("AWT event dispatch") final class InPageBrancher extends BlindAbstractAction { /** Constructs an InPageBrancher. * * @param pV context */ @ThreadRestricted( "AWT event dispatch" ) public InPageBrancher( PageVisit pV ) { assert java.awt.EventQueue.isDispatchThread(); pageVisit = pV; putValue( NAME, "Branch Revision Line" ); putValue( SMALL_ICON, ImageIconX.tryCreate( getClass().getClassLoader().getResource ( "toolbarButtonGraphics/general/New16.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} * of the same document, but * {@linkplain Brancher#branch assiged to a new revision line}; * and shows it in the browser. */ public @Override void act() { assert java.awt.EventQueue.isDispatchThread(); final TransformerFactory transformerFactory = PageDaemonsEDT.i().transformerFactory(); final StringWriter errorStringWriter = new StringWriter(); final PrintWriter errorPrintWriter = new PrintWriter( errorStringWriter ); final DOMErrorHandlerPW errorHandler = new DOMErrorHandlerPW( errorPrintWriter ); Exception xParse = null; // thus far try { // Read from file to branched DOM. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Document document = null; // till parsed from file final File oldFile = VersionedFile.getOrCreateDocumentAsModifiableFile( pageVisit ); final InputStream in = new BufferedInputStream( new FileInputStream( oldFile )); try{ document = Brancher.branched( in, transformerFactory, errorHandler ); } 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 branch. Reading file:" + "\n " + oldFile + "\n" + "Errors/warnings from parser: " + Integer.toString( errorHandler.count() ) + "\n" + errorStringWriter.toString() ); } if( xParse != null ) throw xParse; // Write branched DOM to new file, and show it. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - final VersionedFile newFile = VersionedFile.after( oldFile ); RecombinantXHTML.write ( document, 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 /////////////////////////////////////////////////////////////////////// private final PageVisit pageVisit; }