/*
* Name: MetaUtil
* 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.Constants;
import org.jostraca.Tools;
import org.jostraca.Property;
import org.jostraca.Template;
import org.jostraca.TemplatePath;
import org.jostraca.util.Internal;
import org.jostraca.util.Standard;
import org.jostraca.util.FileUtil;
import org.jostraca.util.PropertySet;
import org.jostraca.util.ValueSet;
import org.jostraca.util.ValueCode;
import java.io.File;
/** Utilities for creating and saving template meta data properties.
*/
public class MetaUtil implements Constants {
// file extension for meta data files
public static final String FILE_EXT_meta = "-meta.properties";
public static void saveMetaData( Template pTemplate ) {
Template tm = (Template) Internal.null_arg( pTemplate );
PropertySet tmps = (PropertySet) tm.getMergedPropertySet();
if( tmps.isYes( Property.main_EnableMeta ) ) {
TemplatePath tp = tm.getTemplatePath();
File metaFile = makeMetaFilePath( tmps, tp );
try {
FileUtil.ensureParentFolder( metaFile );
tmps.set( Property.jostraca_properties_NameValueList, Standard.EMPTY );
tmps.set( Property.jostraca_template_properties,
Tools.normaliseTemplatePropertySet( pTemplate.getPropertySet( CONF_template ) ) );
//t.track( "saveMetaData.pPropertySet.size", pPropertySet.size() );
tmps.save( metaFile );
tm.setMetaFile( metaFile );
}
catch( Exception e ) {
throw ProcessException.CODE_save_meta( new ValueSet( ValueCode.FILE, metaFile ), e );
}
}
}
public static final File makeMetaFilePath( PropertySet pPropertySet, TemplatePath pTemplatePath ) {
String metaFileFolder = pPropertySet.get( Property.main_MetaFolder );
String metaFileName
= pTemplatePath.getTemplateFileName()
+ Standard.MINUS
+ pPropertySet.get( Property.main_CodeWriter )
+ pPropertySet.get( Property.main_MetaSuffix, FILE_EXT_meta )
;
if( "".equals( metaFileFolder ) ) {
metaFileFolder = pPropertySet.get( Property.main_WorkFolder );
}
File metaFile = new File( metaFileFolder, metaFileName );
return metaFile;
}
public static PropertySet loadMetaData( Template pTemplate ) {
Template tm = (Template) Internal.null_arg( pTemplate );
PropertySet tmps = (PropertySet) tm.getMergedPropertySet();
PropertySet ps = new PropertySet();
if( tmps.isYes( Property.main_EnableMeta ) ) {
TemplatePath tp = tm.getTemplatePath();
File metaFile = makeMetaFilePath( tmps, tp );
try {
ps.load( metaFile );
}
catch( Exception e ) {
throw ProcessException.CODE_load_meta( new ValueSet( ValueCode.FILE, metaFile ), e );
}
}
return ps;
}
}