001    package railo.runtime.functions.other;
002    
003    import railo.runtime.PageContext;
004    import railo.runtime.ext.function.Function;
005    
006    /**
007     * Implements the Cold Fusion Function xmlformat
008     */
009    public final class XMLFormat implements Function {
010            public static String call(PageContext pc , String xmlString) {
011                    int len=xmlString.length();
012                    StringBuffer sb=new StringBuffer(len);
013                    int plus=0;
014                    
015                    for(int pos=0;pos<len;pos++) {
016                char chr = xmlString.charAt(pos);
017                switch(chr){
018                    case '<':    plus+=3;        break;
019                    case '>':    plus+=3;        break;
020                    case '&':   plus+=4;        break;
021                    case '"':       plus+=5;        break;
022                    case '\'':      plus+=5;        break;
023                }
024                    }
025            if(plus==0) return xmlString;
026            
027            char[] chars=new char[len+plus];
028            int count=0;
029            
030                    for(int pos=0;pos<len;pos++) {
031                char chr = xmlString.charAt(pos);
032                switch(chr){
033                    case '<': 
034                        chars[count++]='&';
035                        chars[count++]='l';
036                        chars[count++]='t';
037                        chars[count++]=';';
038                    break;
039                    case '>': 
040                        chars[count++]='&';
041                        chars[count++]='g';
042                        chars[count++]='t';
043                        chars[count++]=';';
044                    break;
045                    case '&': 
046                        chars[count++]='&';
047                        chars[count++]='a';
048                        chars[count++]='m';
049                        chars[count++]='p';
050                        chars[count++]=';';
051                    break;
052                    case '"': 
053                        chars[count++]='&';
054                        chars[count++]='q';
055                        chars[count++]='u';
056                        chars[count++]='o';
057                        chars[count++]='t';
058                        chars[count++]=';';
059                    break;
060                    case '\'': 
061                        chars[count++]='&';
062                        chars[count++]='a';
063                        chars[count++]='p';
064                        chars[count++]='o';
065                        chars[count++]='s';
066                        chars[count++]=';';
067                    break;
068                    default: 
069                        chars[count++]=chr;
070                    break;
071                }
072                    }
073            
074            
075                    //if(start<len)sb.append(xmlString.substring(start,len));
076                    return new String(chars);
077            }
078    
079    
080    
081    
082    }