Fixed Template class to be much nicer and pass all tests

This commit is contained in:
Ziver Koc 2015-03-24 13:56:53 +00:00
parent 500f45b77a
commit 88477e97c9
4 changed files with 64 additions and 29 deletions

View file

@ -0,0 +1,33 @@
/*
* Copyright (c) 2014 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package zutil.net.ssdp;
import zutil.net.http.HttpPrintStream;
/**
* Created by Ziver on 2014-11-07.
*/
public interface SSDPCustomInfo extends SSDPServiceInfo{
public void setHeaders(HttpPrintStream http);
}

View file

@ -76,12 +76,12 @@ public class MathParser {
}
else{
if(c == '('){
MathNode parantes = new MathNode();
MathOperation previusParantes = previus;
parse(functionString, temp, previus, parantes);
previusParantes.math2 = parantes;
System.out.println(parantes);
container = parantes;
MathNode parenteses = new MathNode();
MathOperation previousParanteses = previus;
parse(functionString, temp, previus, parenteses);
previousParanteses.math2 = parenteses;
System.out.println(parenteses);
container = parenteses;
// get the next operation
c = functionString.charAt(0);

View file

@ -75,37 +75,39 @@ public class Templator {
}
private void parseTemplate(TemplateNode root, String tmpl, MutableInt m){
StringBuilder data = new StringBuilder();
StringBuilder tags = new StringBuilder();
boolean tagOpen = false;
boolean secondOpenBracet = false;
boolean secondCloseBracet = false;
for(; m.i<tmpl.length(); ++m.i){
char c = tmpl.charAt(m.i);
switch(c){
case '{':
c = tmpl.charAt(++m.i);
switch(c){
case '{':
String d = ""+ c + (m.i+1<tmpl.length() ? tmpl.charAt(m.i+1) : ' ');
switch( d ){
case "{{":
root.addEntity(new TmplStaticString(data.toString()));
data = new StringBuilder();
data.delete(0, data.length());
tags.delete(0, data.length());
tags.append("{{");
tagOpen = true;
++m.i;
break;
default:
data.append('{').append(c);
}
break;
case '}':
c = tmpl.charAt(++m.i);
switch(c){
case '}':
break;
default:
data.append('}').append(c);
case "}}":
if(!tagOpen){ // Tag not opened, incorrect enclosure
data.append(c);
continue;
}
tagOpen = false;
++m.i;
root.addEntity(new TmplDataAttribute(data.toString()));
data.delete(0, data.length());
break;
default:
data.append(c);
break;
}
}
if(tagOpen)
data.insert(0, tags);
if(data.length() > 0)
root.addEntity(new TmplStaticString(data.toString()));
}

View file

@ -52,14 +52,14 @@ public class TemplatorTest {
assertEquals("<HTML>}}</HTML>",
new Templator("<HTML>}}</HTML>").compile());
assertEquals("<HTML></HTML>}}",
new Templator("<HTML>}}</HTML>}}").compile());
new Templator("<HTML></HTML>}}").compile());
assertEquals("<HTML></HTML>{{",
new Templator("<HTML></HTML>{{").compile());
assertEquals("<HTML>{</HTML>",
new Templator("<HTML>{</HTML>").compile());
assertEquals("<HTML>}</HTML>{{",
assertEquals("<HTML>}</HTML>",
new Templator("<HTML>}</HTML>").compile());
assertEquals("<HTML>{}</HTML>{{",
assertEquals("<HTML>{}</HTML>",
new Templator("<HTML>{}</HTML>").compile());
assertEquals("<HTML>{test}</HTML>",
new Templator("<HTML>{test}</HTML>").compile());