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.tag.util;
020
021import lucee.commons.lang.StringUtil;
022import lucee.runtime.exp.ApplicationException;
023
024
025public class FileUtil {
026
027        public static final int NAMECONFLICT_UNDEFINED  =  1;   // can't start at 0 because we need to be able to do a bitmask test
028        public static final int NAMECONFLICT_ERROR      =  2;
029        public static final int NAMECONFLICT_SKIP       =  4;   // same as IGNORE
030        public static final int NAMECONFLICT_OVERWRITE  =  8;   // same as MERGE
031        public static final int NAMECONFLICT_MAKEUNIQUE = 16;
032//      public static final int NAMECONFLICT_CLOSURE    = 32;   // FUTURE
033
034
035        public static int toNameConflict( String nameConflict ) throws ApplicationException {
036
037                if(StringUtil.isEmpty(nameConflict,true)) return NAMECONFLICT_UNDEFINED;
038                nameConflict = nameConflict.trim().toLowerCase();
039
040                if("error".equals( nameConflict) )
041                        return NAMECONFLICT_ERROR;
042
043                if("skip".equals(nameConflict) || "ignore".equals(nameConflict))
044                        return NAMECONFLICT_SKIP;
045
046                if("merge".equals(nameConflict) || "overwrite".equals(nameConflict))
047                        return NAMECONFLICT_OVERWRITE;
048
049                if("makeunique".equals(nameConflict) || "unique".equals(nameConflict))
050                        return NAMECONFLICT_MAKEUNIQUE;
051
052                throw new ApplicationException("Invalid value for attribute nameConflict ["+nameConflict+"]",
053                                "valid values are [" + fromNameConflictBitMask( Integer.MAX_VALUE ) + "]");
054        }
055
056
057        /**
058         *
059         * @param nameConflict
060         * @param allowedValuesMask
061         * @return
062         * @throws ApplicationException
063         */
064        public static int toNameConflict( String nameConflict, int allowedValuesMask ) throws ApplicationException {
065
066                int result = toNameConflict( nameConflict );
067
068                if ( ( allowedValuesMask & result ) == 0 ) {
069
070                        throw new ApplicationException("Invalid value for attribute nameConflict ["+nameConflict+"]",
071                                "valid values are [" + fromNameConflictBitMask( allowedValuesMask ) + "]");
072                }
073
074                return result;
075        }
076
077
078        /**
079         *
080         * @param nameConflict
081         * @param allowedValuesMask
082         * @param defaultValue
083         * @return
084         * @throws ApplicationException
085         */
086        public static int toNameConflict( String nameConflict, int allowedValuesMask, int defaultValue ) throws ApplicationException {
087
088                int result = toNameConflict( nameConflict, allowedValuesMask );
089
090                if ( result == NAMECONFLICT_UNDEFINED )
091                        return defaultValue;
092
093                return result;
094        }
095
096
097        public static String fromNameConflictBitMask( int bitmask ) {
098
099                StringBuilder sb = new StringBuilder();
100
101                if ( (bitmask & NAMECONFLICT_ERROR) > 0 )      sb.append( "error" ).append(',');
102                if ( (bitmask & NAMECONFLICT_MAKEUNIQUE) > 0 ) sb.append( "makeunique (unique)" ).append(',');
103                if ( (bitmask & NAMECONFLICT_OVERWRITE) > 0 )  sb.append( "overwrite (merge)" ).append(',');
104                if ( (bitmask & NAMECONFLICT_SKIP) > 0 )       sb.append( "skip (ignore)" ).append(',');
105
106                if ( sb.length() > 0 )
107                        sb.setLength( sb.length() - 1 );    // remove last ,
108
109                return sb.toString();
110        }
111
112}