001    package railo.runtime.converter;
002    
003    import java.util.Date;
004    import java.util.Iterator;
005    import java.util.Set;
006    
007    import railo.aprint;
008    import railo.commons.lang.ParserString;
009    import railo.commons.lang.StringUtil;
010    import railo.runtime.op.Caster;
011    import railo.runtime.type.Struct;
012    import railo.runtime.type.StructImpl;
013    import railo.runtime.type.dt.DateTime;
014    
015    public class ClientScopeConverter {
016    
017            public static Struct unserialize(String str) {
018                    Struct sct=new StructImpl();
019                    ParserString ps=new ParserString(str);
020                    
021                    StringBuffer sb=new StringBuffer();
022                    String key=null;
023                    while(!ps.isAfterLast()) {
024                            if(ps.isCurrent('#')) {
025                                    if(ps.isNext('=')){
026                                            ps.next();
027                                            sb.append('=');
028                                    }
029                                    else if(ps.isNext('#')){
030                                            ps.next();
031                                            sb.append('#');
032                                    }
033                                    else {
034                                            sct.setEL(key, sb.toString());
035                                            sb=new StringBuffer();
036                                    }
037                            }
038                            else if(ps.isCurrent('=')) {
039                                    key=sb.toString();
040                                    sb=new StringBuffer();
041                            }
042                            else sb.append(ps.getCurrent());
043                            ps.next();
044                    }
045                    
046                    
047                    if(!StringUtil.isEmpty(key) && !StringUtil.isEmpty(sb)) {
048                            sct.setEL(key, sb.toString());
049                    }
050                    return sct;
051                    
052                    /*
053                    int index=0,last=0;
054                    while((index=str.indexOf('#',last))!=-1) {
055                            outer:while(str.length()+1>index) {
056                                    c=str.charAt(index+1);
057                                    if(c=='#' || c=='=') {
058                                            last=index+1;
059                                            continue;
060                                    }
061                            }
062                            _unserialize(str.substring(last,index));
063                            last=index+1;
064                    }
065                    _unserialize(str.substring(last));
066                    */
067                    
068            }
069    
070    
071    
072            public static String serialize(Struct sct) throws ConverterException {
073                    // TODO Auto-generated method stub
074                    return serialize(sct,null);
075            }
076            
077            public static String serialize(Struct sct, Set ignoreSet) throws ConverterException {
078                    StringBuffer sb=new StringBuffer();
079                    Iterator it=sct.keyIterator();
080            boolean doIt=false;
081            Object oKey;
082            while(it.hasNext()) {
083                    oKey=it.next();
084                    if(ignoreSet!=null && ignoreSet.contains(oKey)) continue;
085                String key=Caster.toString(oKey,"");
086                if(doIt)sb.append('#');
087                doIt=true;
088                sb.append(escape(key));
089                sb.append('=');
090                sb.append(_serialize(sct.get(key,"")));
091            }
092            return sb.toString();
093            }
094    
095            private static String escape(String str) {
096                    int len=str.length();
097                    StringBuffer sb=new StringBuffer();
098                    char c;
099                    for(int i=0;i<len;i++) {
100                            c=str.charAt(i);
101                            if(c=='=')              sb.append("#=");
102                            else if(c=='#') sb.append("##");
103                            else                    sb.append(c);
104                    }
105                    return sb.toString();
106            }
107    
108            private static String _serialize(Object object) throws ConverterException {
109                    
110                    if(object==null) return "";
111                    
112                    // String
113                    else if(object instanceof String) return escape(object.toString());
114                    
115                    // Number
116                    else if(object instanceof Number) return Caster.toString(((Number)object).doubleValue());
117                    
118                    // Boolean
119                    else if(object instanceof Boolean) return Caster.toString(((Boolean)object).booleanValue());
120                    
121                    // DateTime
122                    else if(object instanceof DateTime) return Caster.toString(object,null);
123                    
124                    // Date
125                    else if(object instanceof Date) return Caster.toString(object,null);
126                    
127                    throw new ConverterException("can't convert complex value "+Caster.toTypeName(object)+" to a simple value");
128            }
129    
130            public static void main(String[] args) throws ConverterException {
131                    Struct sct=new StructImpl();
132                    sct.setEL("a", "b");
133                    sct.setEL("pe#=ter", "ab##c");
134                    sct.setEL("susi", Boolean.TRUE);
135                    sct.setEL("peter", "abc");
136                    sct.setEL("x", "");
137                    
138                    /*sct.setEL("abc=def", "abc");
139                    sct.setEL("abc#def", "ab#=c");
140                    */
141                    String str;
142                    aprint.out(sct);
143                    aprint.out(str=ClientScopeConverter.serialize(sct));
144                    aprint.out(ClientScopeConverter.unserialize(str));
145                    //aprint.out(new ScriptConverter().serialize(sct));
146                    
147                    
148                    
149            }
150    
151    }