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.io.*; import javax.xml.transform.stream.*; import org.w3c.dom.*; import org.w3c.dom.ls.*; import textbender.a.r.page.*; import textbender.a.u.encoding.*; import textbender.d.gene.xhtml.*; import textbender.g.lang.*; import textbender.g.xml.dom.*; import textbender.o.swing.*; /** An action to reload the working file as a new page. */ public @ThreadRestricted("AWT event dispatch") final class ReloadAction extends BlindAbstractAction { /** Constructs a ReloadAction. * * @param pV context */ @ThreadRestricted( "AWT event dispatch" ) public ReloadAction( PageVisit pV ) { assert java.awt.EventQueue.isDispatchThread(); pageVisit = pV; putValue( NAME, "Reload" ); putValue( SHORT_DESCRIPTION, "Reload working file as new page" ); putValue( SMALL_ICON, ImageIconX.tryCreate( getClass().getClassLoader().getResource ( "toolbarButtonGraphics/general/Refresh16.gif" ), 16 )); // from textbender/o/jlfgr.jar setEnabled( pageVisit.file() != null ); } // - B l i n d - A b s t r a c t - A c t i o n ---------------------------------------- /** Reads the latest content of the user's working file; * converts any editorial 'shorthand' to proper form, * by running it through the genetic encoder; * and presents the result as a new page (versioned file). */ public @Override void act() { assert java.awt.EventQueue.isDispatchThread(); 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 working file to DOM, and encode it. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Document document = null; // till parsed from file final File oldFile; { VersionedFile fV = pageVisit.versionedFile(); if( fV == null ) oldFile = pageVisit.file(); // loaded file is original else oldFile = fV.originalFile(); } final InputStream in = new BufferedInputStream( new FileInputStream( oldFile )); try { document = Encoder.encoded ( in, PageDaemonsEDT.i().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 load. Reading file:" + "\n " + oldFile + "\n" + "Errors/warnings from parser: " + Integer.toString( errorHandler.count() ) + "\n" + errorStringWriter.toString() ); } if( xParse != null ) throw xParse; // Write encoded DOM to new file, and show it. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - final VersionedFile newFile = VersionedFile.after( pageVisit.file() ); //{ // String content = document.getDocumentElement().getTextContent(); // for( int c = 0; c < content.length(); ++c ) // { // char ch = content.charAt( c ); // if( ch > 'z' ) // { // System.out.print( "E getTextContent: '" + content.charAt( c-1 ) + ch + content.charAt( c+1 ) ); // System.out.println( "' = " + (long)content.charAt( c-1 ) + " " + (long)ch + " " + ch + " " + (long)content.charAt( c+1 ) ); // } // } //} RecombinantXHTML.write ( document, 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 /////////////////////////////////////////////////////////////////////// private final PageVisit pageVisit; }