Eclipseplugins
JetTemplateFactory.java
1 package com.proalpha.pds.templates.jet;
2 
3 import org.eclipse.core.runtime.IStatus;
4 import org.eclipse.core.runtime.Platform;
5 import org.eclipse.core.runtime.Status;
6 import org.osgi.framework.Bundle;
7 
8 import com.proalpha.pds.templates.Activator;
9 
10 public final class JetTemplateFactory {
11 
12  private static final String COM_PROALPHA_PDS_TEMPLATES = "com.proalpha.pds.templates";
13  private static final String JET_FOLDER = ".jet.";
14  private static final String TRUNK = "trunk.";
15  private static final String VERSION = "version";
16 
17  private String proALPHAVersion;
18 
19  private static JetTemplateFactory factory;
20 
29  String proALPHAVersion) {
30  if (factory == null) {
31  factory = new JetTemplateFactory(proALPHAVersion);
32  } else {
33  factory.proALPHAVersion = proALPHAVersion;
34  }
35  return factory;
36  }
37 
44  private JetTemplateFactory(String proALPHAVersion) {
45  this.proALPHAVersion = proALPHAVersion;
46  }
47 
55  public IpaTemplateGenerator getTemplate(String instanceName)
56  throws Exception {
57  IpaTemplateGenerator template = null;
58 
59  try {
60 
61  try {
62  // First of all we check the template folder of the current
63  // selected
64  // proALPHA Version
65 
66  template = this
67  .getSelectedTemplate(COM_PROALPHA_PDS_TEMPLATES
68  + JET_FOLDER
69  + VERSION
70  + getProalphaVersionNumber(factory.proALPHAVersion)
71  + instanceName);
72 
73  } catch (ClassNotFoundException e) {
74 
75  // In this case we could not found a a template in the selected
76  // Version so we check if there is a common template in the
77  // trunk folder
78 
79  template = this
80  .getSelectedTemplate(COM_PROALPHA_PDS_TEMPLATES
81  + JET_FOLDER + TRUNK + instanceName);
82  }
83 
84  } catch (Exception e) {
85 
86  logErrorMessage(e);
87  }
88 
89  return template;
90 
91  }
92 
93  private void logErrorMessage(Throwable exception) {
94 
95  String message = (exception.getMessage().length() >= 0) ? exception
96  .getMessage()
97  : "The error message could not be extracted. Please check the error log.";
98 
99  IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, message);
100  Activator.getDefault().getLog().log(status);
101  }
102 
112  private IpaTemplateGenerator getSelectedTemplate(String instanceName)
113  throws ClassNotFoundException, InstantiationException,
114  IllegalAccessException {
115 
116  Bundle bundle = Platform
117  .getBundle(COM_PROALPHA_PDS_TEMPLATES);
118  Class<?> myClass = bundle.loadClass(instanceName);
119  IpaTemplateGenerator template = (IpaTemplateGenerator) myClass
120  .newInstance();
121  return template;
122  }
123 
130  private String extractPointFromString(String proVersion) {
131  String proALPHANumber;
132  proALPHANumber = proVersion.replace(".", "");
133  proALPHANumber = proALPHANumber.substring(0, 2);
134  proALPHANumber = proALPHANumber + ".";
135  return proALPHANumber;
136  }
137 
143  private String getProalphaVersionNumber(String proVersion) {
144 
145  //if contains a dot extract the Version Number
146  if (proVersion.contains("."))
147  proVersion = extractPointFromString(proVersion);
148  // return the Version
149  return proVersion;
150 
151  }
152 }
IpaTemplateGenerator getTemplate(String instanceName)
static JetTemplateFactory getFactoryInstance(String proALPHAVersion)