001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.functions.other;
020
021import lucee.runtime.PageContext;
022import lucee.runtime.ext.function.Function;
023
024/**
025 * Implements the CFML Function xmlformat
026 */
027public final class XMLFormat implements Function {
028        public static String call(PageContext pc , String xmlString) {
029                int len=xmlString.length();
030                int plus=0;
031                
032                for(int pos=0;pos<len;pos++) {
033            char chr = xmlString.charAt(pos);
034            switch(chr){
035                case '<':    plus+=3;        break;
036                case '>':    plus+=3;        break;
037                case '&':   plus+=4;        break;
038                case '"':       plus+=5;        break;
039                case '\'':      plus+=5;        break;
040            }
041                }
042        if(plus==0) return xmlString;
043        
044        char[] chars=new char[len+plus];
045        int count=0;
046        
047                for(int pos=0;pos<len;pos++) {
048            char chr = xmlString.charAt(pos);
049            switch(chr){
050                case '<': 
051                    chars[count++]='&';
052                    chars[count++]='l';
053                    chars[count++]='t';
054                    chars[count++]=';';
055                break;
056                case '>': 
057                    chars[count++]='&';
058                    chars[count++]='g';
059                    chars[count++]='t';
060                    chars[count++]=';';
061                break;
062                case '&': 
063                    chars[count++]='&';
064                    chars[count++]='a';
065                    chars[count++]='m';
066                    chars[count++]='p';
067                    chars[count++]=';';
068                break;
069                case '"': 
070                    chars[count++]='&';
071                    chars[count++]='q';
072                    chars[count++]='u';
073                    chars[count++]='o';
074                    chars[count++]='t';
075                    chars[count++]=';';
076                break;
077                case '\'': 
078                    chars[count++]='&';
079                    chars[count++]='a';
080                    chars[count++]='p';
081                    chars[count++]='o';
082                    chars[count++]='s';
083                    chars[count++]=';';
084                break;
085                default: 
086                    chars[count++]=chr;
087                break;
088            }
089                }
090        
091        
092                //if(start<len)sb.append(xmlString.substring(start,len));
093                return new String(chars);
094        }
095
096
097
098
099}