Package org.jostraca.process

Source Code of org.jostraca.process.GenericSaver

/*
* Name:    GenericSaver
* Authors: Richard Rodger
*
* Copyright (c) 2004 Richard Rodger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/


// package
package org.jostraca.process;


// import
import org.jostraca.Template;
import org.jostraca.Property;

import org.jostraca.util.PropertySet;
import org.jostraca.util.Standard;
import org.jostraca.util.FileUtil;
import org.jostraca.util.BasicWayPoint;
import org.jostraca.util.WayPointRecorder;

import java.util.List;

import java.io.File;
import java.io.IOException;


/** Processing class for saving the <code>CodeWriter</code> to a file.
*/
public class GenericSaver extends TemplateHandlerSupport {

  public GenericSaver() {
    // do nothing
  }


  protected void processImpl( Template pTemplate ) {
    Template    template   = pTemplate;
    PropertySet tmps       = pTemplate.getMergedPropertySet();

    if( tmps.isYes( Property.main_SaveCodeWriter ) ) {

      File   codeWriterPath   = template.getCodeWriterPath();
      String codeWriterSource = template.getCodeWriterSource();

      boolean changed                  = true;
      String  previousCodeWriterSource = Standard.EMPTY;

      if( codeWriterPath.exists() ) {
        try {
          previousCodeWriterSource = FileUtil.readFile( codeWriterPath );       
          changed = !( codeWriterSource.equals( previousCodeWriterSource ) );
        }
        catch( IOException ioe ) {
          // assume that this is a non-fatal error
          // keep going to try to at least save the CodeWriter
          changed = true;
        }
      }

      try {
        // always save code writer so that we can tell that template was parsed
        FileUtil.ensureParentFolder( codeWriterPath );
        FileUtil.writeFile( codeWriterPath, codeWriterSource );
        WayPointRecorder.add( BasicWayPoint.SavingCodeWriter.make( codeWriterPath.getAbsolutePath() ) );
      }
      catch( Exception e ) {
        throw ProcessException.CODE_cw_not_saved( new String[] {"path", codeWriterPath.getAbsolutePath()}, e );
      }
     
      template.setCodeWriterChanged( changed );
    }

    MetaUtil.saveMetaData( template );
  }


  protected void completeImpl( List pTemplateList ) {
    // do nothing
  }




}




TOP

Related Classes of org.jostraca.process.GenericSaver

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.