Package de.innovationgate.eclipse.wgadesigner.models

Source Code of de.innovationgate.eclipse.wgadesigner.models.DesignTemplate

/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.wgadesigner.models;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.VFS;

import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
import de.innovationgate.utils.TemporaryFile;


public class DesignTemplate {
 
  // design property for db default language - set when a new db is created for this design template 
  public static final String PROP_DEFAULT_LANGUAGE = "DefaultLanguage";

  public static final String PROPERTIES_FILENAME = "template.properties";
 
  private String _name;
  private String _location;

  private Properties _properties;
 
  public DesignTemplate(String name, String location){
    _location = location;
    _name = name;   
  }

  public String getName() {
    return _name;
  }
  public void setName(String name) {
    _name = name;
  }

  public String getLocation() {
    return _location;
  }

  public void setLocation(String location) {
    _location = location;
  }
 
  public InputStream getContent() throws IOException {
    return WGADesignerPlugin.getDefault().getResourceAsStream(_location);
  }
 
  public Properties getProperties() {
    if (_properties == null) {
      TemporaryFile tempFile = null;
      try {       
        tempFile = new TemporaryFile("template_" + _name, getContent(), WGADesignerPlugin.getDefault().getStateLocation().toFile());
        FileSystemManager fsManager = VFS.getManager();
        FileObject propFile = fsManager.resolveFile("zip:" + tempFile.getFile().toURI() + "!" + PROPERTIES_FILENAME);
        if (propFile.exists()) {
          _properties = new Properties();
          InputStream propIn = null;
          try {
            propIn = propFile.getContent().getInputStream();
            _properties.load(propIn);
          } finally {
            if (propIn != null) {
              try {
                propIn.close();
              } catch (IOException e) {
              }
            }
          }         
        } else {
          _properties = new Properties();
        }
       
      } catch (Exception e) {
        WGADesignerPlugin.getDefault().logError("Unable to load properties from template '" + _name + "'.", e);
      } finally {
        if (tempFile != null) {
          tempFile.delete();
        }
      }
    }
    return _properties;   
  }

  @Override
  public boolean equals(Object obj) {
    if(obj instanceof DesignTemplate){
      DesignTemplate cmpDes = (DesignTemplate) obj;
      if(cmpDes.getLocation().equals(_location) && cmpDes.getName().equals(_name)){
        return true;
      }
    }
   
    return false;
  }
}
TOP

Related Classes of de.innovationgate.eclipse.wgadesigner.models.DesignTemplate

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.