package de.FeatureModellingTool.DictEditor;
import de.FeatureModellingTool.FeatureModel.Feature;
import de.FeatureModellingTool.FeatureModel.FeatureModel;
import de.FeatureModellingTool.FeatureModel.FeatureRelation;
import java.io.*;
import java.util.*;
import java.sql.Timestamp;
/**
* Recoder: Raymond
* Date: 2003-10-10
* Time: 2:10:58
*/
public class HtmlGeneratorImpl
{
protected File htmlModel = null;
protected String htmlString = null;
protected String newHtml = null;
protected ArrayList keyList = null;
protected ArrayList instanceList = null;
protected FeatureModel featureModel = null;
public HtmlGeneratorImpl(File htmlModel)
{
this.htmlModel = htmlModel;
try
{
fread();
} catch
(IOException e)
{
System.err.print(e.getMessage());
}
}
public HtmlGeneratorImpl(String fileName)
{
java.net.URL url = getClass().getResource(fileName);
this.htmlModel = new File(url.getFile());
try
{
fread();
} catch
(IOException e)
{
System.err.print(e.getMessage());
}
}
public HtmlGeneratorImpl(File htmlModel, FeatureModel featureModel)
{
this.htmlModel = htmlModel;
this.featureModel = featureModel;
try
{
fread();
} catch
(IOException e)
{
System.err.print(e.getMessage());
}
}
public HtmlGeneratorImpl(String fileName, FeatureModel featureModel)
{
java.net.URL url = getClass().getResource(fileName);
this.htmlModel = new File(url.getFile());
this.featureModel = featureModel;
try
{
fread();
} catch
(IOException e)
{
System.err.print(e.getMessage());
}
}
public HtmlGeneratorImpl(FeatureModel featureModel)
{
this.featureModel = featureModel;
}
public void initFeature(Feature feature, boolean isForDict)
{
setKeylistForFeature();
setfInstancelist(feature, isForDict);
}
public void setFeatureModel(FeatureModel featureModel)
{
this.featureModel = featureModel;
}
public FeatureModel getFeatureModel()
{
return featureModel;
}
public void setHtmlModel(File htmlModel)
{
this.htmlModel = htmlModel;
try
{
fread();
} catch
(IOException e)
{
System.err.print(e.getMessage());
}
}
public void setHtmlModel(String fileName)
{
htmlModel = new File(fileName);
try
{
fread();
} catch
(IOException e)
{
System.err.print(e.getMessage());
}
}
public File getHtmlModel()
{
return htmlModel;
}
public void fread(String filename)
throws IOException
{
File from_file = null;
FileReader in = null;
StringBuffer htmlbf = new StringBuffer();
try
{
from_file = new File(filename);
in = new FileReader(from_file);
char[] buffer = new char[2048];
int len;
while ((len = in.read(buffer)) != -1)
{
String s = new String(buffer, 0, len);
htmlbf.append(s);
}
} finally
{
if (in != null)
try
{
in.close();
} catch (IOException e)
{
;
}
}
htmlString = htmlbf.toString();
}
public void fread()
throws IOException
{
if (htmlModel == null)
{
return;
}
File from_file = htmlModel;
FileReader in = null;
StringBuffer htmlbf = new StringBuffer();
try
{
in = new FileReader(from_file);
char[] buffer = new char[2048];
int len;
while ((len = in.read(buffer)) != -1)
{
String s = new String(buffer, 0, len);
htmlbf.append(s);
}
} finally
{
if (in != null)
try
{
in.close();
} catch (IOException e)
{
;
}
}
htmlString = htmlbf.toString();
}
public String produceHtml(String key, String instance)
{
if (htmlString == null || key == null || instance == null)
{
System.out.println("key or instance not still setted down.");
return "An appropriate representation of the requested resource / could not be found.";
}
newHtml = htmlString;
newHtml = newHtml.replaceAll(key, instance);
return newHtml;
}
public String produceHtml(ArrayList keyList, ArrayList instanceList)
{
if (keyList == null || instanceList == null || keyList.size() != instanceList.size())
{
System.out.println("key or instance not still setted down.");
return "An appropriate representation of the requested resource / could not be found.";
}
if (htmlString.equals(""))
{
System.out.println("htmlModel is not found.");
return "An appropriate representation of the requested resource / could not be found.";
}
newHtml = htmlString;
for (int i = 0; i < keyList.size(); i++)
{
newHtml = newHtml.replaceAll((String) keyList.get(i), (String) instanceList.get(i));
}
return newHtml;
}
public String produceHtml()
{
if (keyList == null || instanceList == null || keyList.size() != instanceList.size())
{
System.out.println("list is not still setted down.");
return "An appropriate representation of the requested resource / could not be found.";
}
if (htmlString.equals(""))
{
System.out.println("htmlModel is not found.");
return "An appropriate representation of the requested resource / could not be found.";
}
newHtml = htmlString;
for (int i = 0; i < keyList.size(); i++)
{
newHtml = newHtml.replaceAll((String) keyList.get(i), (String) instanceList.get(i));
}
return newHtml;
}
public void copy(String from, String to, boolean overwrite)
throws FileNotFoundException, IOException
{
File src = new File(from);
File dest = new File(to);
try
{
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest, !overwrite);
byte[] buf = new byte[512];
int size = fis.read(buf);
while (size != -1)
{
fos.write(buf, 0, size);
size = fis.read(buf);
}
fis.close();
fos.close();
} catch (FileNotFoundException fnfe)
{
if (src.isDirectory())
{
/* if src is derectory , copy this directory*/
File[] files = src.listFiles();
Stack stack = new Stack();
for (int i = 0; i < files.length; i++)
{
dest.mkdirs();
//System.out.println(files[i].getName());
copy(src + File.separator + files[i].getName(), to + File.separator + files[i].getName(), overwrite);
}
}
else
{
/* source file noe exist or other problems*/
throw fnfe;
}
} catch (IOException e)
{
throw e;
}
}
public void outFile(String filename)
{
try
{
FileWriter fw = new FileWriter(filename);
fw.write(newHtml);
fw.close();
} catch (Exception e)
{
System.out.println(e);
}
}
/**
* Initial KeyList
*
*/
protected void setKeylistForFeature()
{
keyList = new ArrayList();
keyList.add("@featureName"); // 1
keyList.add("@description"); // 2
keyList.add("@source"); // 3
keyList.add("@issue"); // 4
keyList.add("@note"); // 5
keyList.add("@category"); // 6
keyList.add("@variability"); // 7
keyList.add("@bindingTime"); // 8
keyList.add("@parent"); // 9
keyList.add("@include"); // 10
keyList.add("@dimension"); // 11
keyList.add("@requiring"); // 12
keyList.add("@required"); // 13
keyList.add("@mutex"); // 14
/*
keyList.add("@crosscutted"); // 15
keyList.add("@implemented"); // 16
*/
}
protected void setfInstancelist(Feature feature, boolean isForDict)
{
String stemp = "";
instanceList = new ArrayList();
//1 name
if (feature.getName() == null)
{
instanceList.add("");
}
else
instanceList.add(feature.getName());
//2 description
if (feature.getDescription() == null)
{
instanceList.add("");
}
else
instanceList.add(strAdjust(feature.getDescription())); //2004.6.2
//3 source
if (feature.getSource() == null)
{
instanceList.add("");
}
else
instanceList.add(strAdjust(feature.getSource()));
//4 issue
if (feature.getIssueAndDecision() == null)
{
instanceList.add("");
}
else
instanceList.add(strAdjust(feature.getIssueAndDecision()));
//5 note
if (feature.getNote() == null)
{
instanceList.add("");
}
else
instanceList.add(strAdjust(feature.getNote()));
//6 category
instanceList.add(feature.getCategory().getName());
//7 variability
instanceList.add(feature.getVariability().getName());
//8 bindingTime
instanceList.add(feature.getBindingTime().getName());
//9 parent
stemp = "";
Map parentMap = featureModel.getFeatureRelation(FeatureRelation.DECOMPOSE, feature, false);
if (parentMap != null)
{
Iterator iteraParent = parentMap.keySet().iterator();
if (iteraParent != null)
while (iteraParent.hasNext())
{
String id = (String) iteraParent.next();
FeatureRelation fr = (FeatureRelation) parentMap.get(id);
if (fr != null)
{
Feature iteraFeature = fr.getStartFeature();
stemp += "<a href='";
if (isForDict)
stemp += iteraFeature.getID();
else
{
stemp += iteraFeature.getName()+"@";
stemp += iteraFeature.getID();
stemp += ".html";
}
stemp += "'>";
stemp += iteraFeature.getName();
stemp += "</a> ";
}
}
}
instanceList.add(stemp);
//10 include
stemp = "";
Map incluMap = featureModel.getFeatureRelation(FeatureRelation.DECOMPOSE, feature, true);
if (incluMap != null)
{
Iterator iteraInclu = incluMap.keySet().iterator();
if (iteraInclu != null)
while (iteraInclu.hasNext())
{
String id = (String) iteraInclu.next();
FeatureRelation fr = (FeatureRelation) incluMap.get(id);
if (fr != null)
{
Feature incluFeature = fr.getEndFeature();
stemp += "<a href='";
if (isForDict)
stemp += incluFeature.getID();
else
{
stemp += incluFeature.getName()+"@";
stemp += incluFeature.getID();
stemp += ".html";
}
stemp += "'>";
stemp += incluFeature.getName();
stemp += "</a> ";
}
}
}
instanceList.add(stemp);
//11 dimensionValue
stemp = "";
Map dimenMap = featureModel.getFeatureRelation(FeatureRelation.ATTRIBUTE, feature, true);
if (dimenMap != null)
{
Iterator iteraDimen = dimenMap.keySet().iterator();
if (iteraDimen != null)
while (iteraDimen.hasNext())
{
String id = (String) iteraDimen.next();
FeatureRelation fr = (FeatureRelation) dimenMap.get(id);
if (fr != null)
{
Feature dimenFeature = fr.getEndFeature();
stemp += "<a href='";
if (isForDict)
stemp += dimenFeature.getID();
else
{
stemp += dimenFeature.getName()+"@";
stemp += dimenFeature.getID();
stemp += ".html";
}
stemp += "'>";
stemp += dimenFeature.getName();
stemp += "</a> ";
}
}
}
instanceList.add(stemp);
//12 requiring
stemp = "";
Map requiringMap = featureModel.getFeatureRelation(FeatureRelation.REQUIRE, feature, true);
if (requiringMap != null)
{
Iterator iteraRequire = requiringMap.keySet().iterator();
if (iteraRequire != null)
while (iteraRequire.hasNext())
{
String id = (String) iteraRequire.next();
FeatureRelation fr = (FeatureRelation) requiringMap.get(id);
if (fr != null)
{
Feature requiredFeature = fr.getEndFeature();
stemp += "<a href='";
if (isForDict)
stemp += requiredFeature.getID();
else
{
stemp += requiredFeature.getName()+"@";
stemp += requiredFeature.getID();
stemp += ".html";
}
stemp += "'>";
stemp += requiredFeature.getName();
stemp += "</a> ";
}
}
}
instanceList.add(stemp);
//12 requiring
stemp = "";
Map requiredMap = featureModel.getFeatureRelation(FeatureRelation.REQUIRE, feature, false);
if (requiredMap != null)
{
Iterator iteraRequired = requiredMap.keySet().iterator();
if (iteraRequired != null)
while (iteraRequired.hasNext())
{
String id = (String) iteraRequired.next();
FeatureRelation fr = (FeatureRelation) requiredMap.get(id);
if (fr != null)
{
Feature requiredFeature = fr.getStartFeature();
stemp += "<a href='";
if (isForDict)
stemp += requiredFeature.getID();
else
{
stemp += requiredFeature.getName()+"@"; //2004.6.2
stemp += requiredFeature.getID();
stemp += ".html";
}
stemp += "'>";
stemp += requiredFeature.getName();
stemp += "</a> ";
}
}
}
instanceList.add(stemp);
//13 mutex
stemp = "";
Map mutexMap = featureModel.getFeatureRelation(FeatureRelation.EXCLUDE, feature, true);
if (mutexMap != null)
{
Iterator iteraMutex = mutexMap.keySet().iterator();
if (iteraMutex != null)
while (iteraMutex.hasNext())
{
String id = (String) iteraMutex.next();
FeatureRelation fr = (FeatureRelation) mutexMap.get(id);
if (fr != null)
{
Feature mutexFeature = fr.getEndFeature();
stemp += "<a href='";
if (isForDict)
stemp += mutexFeature.getID();
else
{
stemp += mutexFeature.getName()+"@"; //5.7
stemp += mutexFeature.getID();
stemp += ".html";
}
stemp += "'>";
stemp += mutexFeature.getName();
stemp += "</a> ";
}
}
}
Map mutexdMap = featureModel.getFeatureRelation(FeatureRelation.EXCLUDE, feature, false);
if (mutexMap != null)
{
Iterator iteraMutexd = mutexdMap.keySet().iterator();
if (iteraMutexd != null)
while (iteraMutexd.hasNext())
{
String id = (String) iteraMutexd.next();
FeatureRelation fr = (FeatureRelation) mutexdMap.get(id);
if (fr != null)
{
Feature mutexFeature = fr.getStartFeature();
stemp += "<a href='";
if (isForDict)
stemp += mutexFeature.getID();
else
{
stemp += mutexFeature.getName()+"@"; //5.7
stemp += mutexFeature.getID();
stemp += ".html";
}
stemp += "'>";
stemp += mutexFeature.getName();
stemp += "</a> ";
}
}
}
instanceList.add(stemp);
/*/ temp
stemp = "";
instanceList.add(stemp);
instanceList.add(stemp);
//14 crosscutted
//15 implemented
*/
}
protected static String strAdjust(String in)
{
String out = in;
out = out.replaceAll("\u0020", " ");
out = out.replaceAll("\n", "<Br>");
out = out.replaceAll("\u0009", " ");
return out;
}
}