package com.wizzardo.tools.xml;

/**
 * Created by wizzardo on 21.07.15.
 */
public class GspParser<T extends GspParser.GspParserContext> extends HtmlParser<T> {

    public class GspParserContext extends HtmlParser.HtmlParserContext {
        protected boolean inGroovy = false;
        protected int brackets = 0;
        protected boolean inStringInGroovy = false;
        protected char quote = 0;

        @Override
        protected boolean onChar(char[] s, Node xml) {
            if (inGroovy) {
                switch (ch) {
                    case '}': {
                        sb.append('}');
                        if (!inStringInGroovy) {
                            brackets--;
                            inGroovy = brackets != 0;
                            if (!inGroovy && !inString && inTag) {
                                xml.attribute(sb.toString(), null);
                                sb.setLength(0);
                            }
                        }
                        break;
                    }
                    case '\"':
                    case '\'': {
                        if (inStringInGroovy) {
                            if (quote == ch && s[i - 1] != '\\')
                                inStringInGroovy = false;
                        } else {
                            quote = ch;
                            inStringInGroovy = true;
                        }
                        sb.append(ch);
                        break;
                    }
                    case '{': {
                        sb.append(ch);
                        if (!inStringInGroovy) {
                            brackets++;
                        }
                        break;
                    }
                    default:
                        sb.append(ch);
                }
                i++;
                return true;
            }
            return false;
        }

        protected void onCurlyBracketOpen(char[] s, Node xml) {
            if (i > 0 && s[i - 1] == '$') {
                inGroovy = true;
                brackets++;
            }
            super.onCurlyBracketOpen(s, xml);
        }

    }

    @Override
    protected T createContext() {
        return (T) new GspParserContext();
    }
}

@@ -1907,382 +1907,1645 @@
   %7D%0A
-            return false;%0A        %7D%0A%0A        protected void onCurlyBracketOpen(char%5B%5D s, Node xml) %7B%0A            if (i %3E 0 && s%5Bi - 1%5D == '$') %7B%0A                inGroovy = true;%0A                brackets++;%0A            %7D%0A            super.onCurlyBracketOpen(s, xml);%0A        %7D%0A%0A    %7D%0A%0A    @Override%0A    protected T createContext() %7B%0A        return (T) new GspParserContext();
+%0A            if (!comment && ch == '%25' && i %3C s.length - 3 && s%5Bi + 1%5D == '%7B' && s%5Bi + 2%5D == '-' && s%5Bi + 3%5D == '-') %7B%0A                comment = true;%0A                i += 4;%0A                return true;%0A            %7D%0A%0A            if (comment) %7B%0A                if (ch == '-' && i %3C s.length - 3 && s%5Bi + 1%5D == '-' && s%5Bi + 2%5D == '%7D' && s%5Bi + 3%5D == '%25') %7B%0A                    xml.add(new GspComment(sb.toString()));%0A                    sb.setLength(0);%0A                    comment = false;%0A                    i += 4;%0A                    return true;%0A                %7D else %7B%0A                    sb.append(ch);%0A                    i++;%0A                    return true;%0A                %7D%0A            %7D%0A%0A            return false;%0A        %7D%0A%0A        protected void onCurlyBracketOpen(char%5B%5D s, Node xml) %7B%0A            if (i %3E 0 && s%5Bi - 1%5D == '$') %7B%0A                inGroovy = true;%0A                brackets++;%0A            %7D%0A            super.onCurlyBracketOpen(s, xml);%0A        %7D%0A%0A    %7D%0A%0A    @Override%0A    protected T createContext() %7B%0A        return (T) new GspParserContext();%0A    %7D%0A%0A    private class GspComment extends TextNode %7B%0A        public GspComment(String text) %7B%0A            super(text);%0A        %7D%0A%0A        @Override%0A        public String toString() %7B%0A            return %22gsp comment: %22 + text;%0A        %7D%0A%0A        @Override%0A        protected String text(boolean recursive) %7B%0A            return %22%25%7B--%22 + text + %22--%7D%25%22;%0A        %7D%0A%0A        @Override%0A        protected String ownText() %7B%0A            return %22%25%7B--%22 + text + %22--%7D%25%22;%0A        %7D%0A%0A        @Override%0A        public boolean isComment() %7B%0A            return true;%0A        %7D
 %0A   

add gsp comments