进行html代码分析后,我们就要开始处理代码了,为了满足我规定的功能的第一项:必须能够自己定义开始和结束标识符,所以我设置了两个变量:
private String gytFrontSeparator = "{";
private String gytBackSeparator = "}";
初始值分别是{和},作为默认的开始和结束标识符,然后定义了两个方法去处理这个默认的变量:
public void setFrontSeparator(String gytemp) {
this.gytFrontSeparator = gytemp;
}
public void setBackSeparator(String gytemp) {
this.gytBackSeparator = gytemp;
}
然后在遇到变量的时候就使用这个变量就可以了。
然后我们开始处理RepInclude();
首先我先开始定义一个变量,作为所有只是简单替换的公用变量:
public HashMap<String, String> gytInVar;
@SuppressWarnings("rawtypes")
public HashMap setInVtrParameter(String ParameterName, String ParameterVale) {
this.gytInVar.put(ParameterName, ParameterVale);
return this.gytInVar;
}
然后使用这个公用变量进行处理包含,具体的方法是:
private void RepInclude() throws IOException {
String tempS = gydContent.getContent();
String[] gytParameterArray = null;
GySplit a = new GySplit();
a.tool(tempS, this.gytFrontSeparator + "include=");
String[] tempContent = a.Show();
a = null;
int temp_count = tempContent.length;
if (temp_count == 0) {
} else {
gytParameterArray = new String[temp_count - 1];
for (int i = 1; i < temp_count; i++) {
GySplit b = new GySplit();
b.tool(tempContent[i], this.gytBackSeparator);
String[] temp_end_array = b.Show();
b = null;
gytParameterArray[i - 1] = temp_end_array[0];
}
for (int i = 0; i < gytParameterArray.length; i++) {
String tempC = "";
try {
if (gytiCharset == null && gytiCharset.length() < 1) {
tempC = readFile(gytParameterArray[i]);
} else {
tempC = readFile(gytParameterArray[i], gytiCharset);
}
} catch (IOException e) {
e.printStackTrace();
}
// 使用自定义函数进行字符串替换
GyReplace r = new GyReplace();
tempS = r.replace(tempS, this.gytFrontSeparator + "include="
+ gytParameterArray[i] + this.gytBackSeparator, tempC);
r = null;
}
}
gydContent.setContent(tempS);
}
核心的思想很简单,就是先取出"开始变量+include=+内容+结束变量",根据内容得到要包含的html模板内容,然后把最后的html进行组装形成新的模板字符串。(应该说这是整个模板引擎的核心思想。)
处理完包含就开始处理if(判断)了,思想还是刚才说的思想,获取if中的内容,抛弃为假的进行重新封装。具体代码如下:
private void RepIf() {
String tempS=gydContent.getContent();
String[] gytParameterArray = null;
String[] gyitParameterArray = null;// 当前的if标签
Vector<String> v = new Vector<String>();
String[] tempContent = null;
int temp_count = 0;
GySplit h = new GySplit();
h.tool(tempS, this.gytFrontSeparator);
tempContent = h.Show();
h = null;
temp_count = tempContent.length;
if(temp_count==0)
{
}
else
{
gytParameterArray = new String[temp_count - 1];
if (temp_count > 0) {
for (int i = 1; i < temp_count; i++) {
GySplit b = new GySplit();
b.tool(tempContent[i], this.gytBackSeparator);
String[] temp_end_array = b.Show();
b = null;
gytParameterArray[i - 1] = temp_end_array[0];
}
}
for (int i = 0; i < gytParameterArray.length; i++) {
if (gytParameterArray[i].substring(0, 3).toString().equals("if:")) {
v.add(gytParameterArray[i]);
}
}
if (v == null || v.size() == 0) {
gydContent.setContent(tempS);
} else {
gyitParameterArray = new String[v.size()];
for (int i = 0; i < gyitParameterArray.length; i++) {
gyitParameterArray[i] = new String((String) v.elementAt(i));
}
v = null;
String tempSpStart = "";// 循环前的字符串
String tempSpEnd = "";// 循环后的字符串
String tempSpIf = "";// 循环的字符串
Iterator<Entry<String, String>> iSer = this.gytInIf.entrySet()
.iterator();
GySplit a = new GySplit();
a.tool(tempS, this.gytFrontSeparator + gyitParameterArray[0] + this.gytBackSeparator);
String[] tempSIfHead = a.Show();
a = null;
GySplit f = new GySplit();
f.tool(gyitParameterArray[0], " ");
String[] tempParameterArray = f.Show();
f = null;
GySplit b = new GySplit();
b.tool(tempSIfHead[1], this.gytFrontSeparator + "end" + tempParameterArray[0] + this.gytBackSeparator);
String[] tempSIfEnd = b.Show();
b = null;
tempSpStart = tempSIfHead[0];// 开始的 内容
tempSpIf = tempSIfEnd[0];// 要判断的内容
for (int af = 1; af < tempSIfEnd.length; af++) {
tempSpEnd = tempSpEnd + this.gytFrontSeparator + "end" + tempParameterArray[0] + this.gytBackSeparator + tempSIfEnd[af];
}
String tempsub = this.gytFrontSeparator + "end" + tempParameterArray[0] + this.gytBackSeparator;
tempSpEnd = tempSpEnd.substring(tempsub.length(),
tempSpEnd.length());
tempSIfHead = null;
tempSIfEnd = null;
GySplit c = new GySplit();
c.tool(tempParameterArray[0], ":");
String[] tempIfParameterArray = c.Show();
c = null;
while (iSer.hasNext()) {
@SuppressWarnings("rawtypes")
Map.Entry entry = (Map.Entry) iSer.next();
if (tempIfParameterArray[1].equals(entry.getKey().toString())) {
String gytempA = entry.getValue().toString();
String tempSS = "";
if (tempSpIf.indexOf(this.gytFrontSeparator + "else:"
+ tempIfParameterArray[1] + this.gytBackSeparator) > 0) {
GySplit e = new GySplit();
e.tool(tempSpIf, this.gytFrontSeparator + "else:"
+ tempIfParameterArray[1] + this.gytBackSeparator);
String[] tempSSIf = e.Show();
e = null;
if (tempParameterArray[1].toString().equals(gytempA)) {
tempSS = tempSSIf[0];
} else {
tempSS = tempSSIf[1];
}
} else {
tempSS = tempSpIf;
}
gydContent.setContent(tempSpStart + tempSS + tempSpEnd);
RepIf();
}
}
}
}
}
今天到此,明天继续说for,for有点麻烦,因为涉及到嵌套循环。