1313import java .io .Writer ;
1414import java .lang .reflect .Method ;
1515import java .math .BigDecimal ;
16+ import java .util .Date ;
1617import java .nio .charset .Charset ;
1718import java .sql .ResultSet ;
19+ import java .sql .Clob ;
20+ import java .util .Map ;
1821import java .util .ArrayList ;
1922import java .util .Calendar ;
2023import java .util .Iterator ;
4952import lucee .runtime .type .Struct ;
5053import lucee .runtime .type .dt .DateTime ;
5154import lucee .runtime .type .scope .Argument ;
55+ import lucee .runtime .ext .function .Function ;
5256import lucee .runtime .util .Cast ;
5357import lucee .runtime .util .Creation ;
5458import lucee .runtime .util .DBUtil ;
5559import lucee .runtime .util .Decision ;
5660import lucee .runtime .util .ORMUtil ;
5761import lucee .runtime .util .Operation ;
62+ import lucee .runtime .op .Castable ;
63+ import lucee .runtime .type .ObjectWrap ;
5864
5965public class CommonUtil {
6066
@@ -228,7 +234,9 @@ public static String toString(long l) {
228234 *
229235 * @param file
230236 * @param charset
237+ *
231238 * @return readed string
239+ *
232240 * @throws IOException
233241 */
234242 public static String toString (Resource file , Charset charset ) throws IOException {
@@ -511,6 +519,68 @@ public static boolean isStruct(Object obj) {
511519 return decision ().isStruct (obj );
512520 }
513521
522+ /**
523+ * See if a given value is coercable to a string.
524+ * <p>
525+ * Blatantly copied from Lucee core because it's not in the Lucee loader, so we don't have access to run it without
526+ * reflection.
527+ *
528+ * @link https://github.com/lucee/Lucee/blob/6.0/core/src/main/java/lucee/runtime/op/Decision.java#L964
529+ *
530+ * @param o
531+ * Value to compare
532+ *
533+ * @return Boolean, true if value is a String or castable to a String.
534+ */
535+ public static boolean isString (Object o ) {
536+ if (o instanceof String )
537+ return true ;
538+ else if (o instanceof Boolean )
539+ return true ;
540+ else if (o instanceof Number )
541+ return true ;
542+ else if (o instanceof Date )
543+ return true ;
544+ else if (o instanceof Castable ) {
545+ return ((Castable ) o ).castToString ("this is a unique string" ) != "this is a unique string" ;
546+
547+ } else if (o instanceof Clob )
548+ return true ;
549+ else if (o instanceof Node )
550+ return true ;
551+ else if (o instanceof Map || o instanceof List || o instanceof Function )
552+ return false ;
553+ else if (o == null )
554+ return true ;
555+ else if (o instanceof ObjectWrap )
556+ return isString (((ObjectWrap ) o ).getEmbededObject ("" ));
557+ return true ;
558+ }
559+
560+ public static boolean isSimpleValue (Object obj ) {
561+ return decision ().isSimpleValue (obj );
562+ }
563+
564+ public static boolean isCastableToBoolean (Object obj ) {
565+ return decision ().isCastableToBoolean (obj );
566+ }
567+
568+ public static boolean isCastableToArray (Object o ) {
569+ return decision ().isCastableToArray (o );
570+ }
571+
572+ public static boolean isCastableToStruct (Object o ) {
573+ return decision ().isCastableToStruct (o );
574+ }
575+
576+ public static boolean isBinary (Object obj ) {
577+ return decision ().isBinary (obj );
578+ }
579+
580+ public static boolean isBoolean (Object obj ) {
581+ return decision ().isBoolean (obj );
582+ }
583+
514584 public static boolean isAnyType (String type ) {
515585 return decision ().isAnyType (type );
516586 }
@@ -612,7 +682,8 @@ static class SQLImpl implements SQL {
612682 /**
613683 * Constructor only with SQL String
614684 *
615- * @param strSQL SQL String
685+ * @param strSQL
686+ * SQL String
616687 */
617688 public SQLImpl (String strSQL ) {
618689 this .strSQL = strSQL ;
@@ -704,24 +775,23 @@ public String toString() {
704775 }
705776 }
706777
707- public static DataSource getDataSource (PageContext pc , String dsn , DataSource defaultValue ) {
708- if (Util .isEmpty (dsn , true ) || dsn .equals ("__default__" )) return orm ().getDefaultDataSource (pc , defaultValue );
709- return pc .getDataSource (dsn .trim (), defaultValue );
710- }
711-
778+ /**
779+ * Get the datasource defined for the provided name, or the default if name is null.
780+ *
781+ * @param pc
782+ * Lucee's PageContext object.
783+ * @param name
784+ * Datasource name, or <code>null</code> to retrieve the default
785+ *
786+ * @return A Datasource object
787+ *
788+ * @throws PageException
789+ */
712790 public static DataSource getDataSource (PageContext pc , String name ) throws PageException {
713791 if (Util .isEmpty (name , true )) return orm ().getDefaultDataSource (pc );
714792 return pc .getDataSource (name );
715793 }
716794
717- /*
718- * private static Object getDatasourceManager(PageContext pc) throws PageException { try { if
719- * (mGetDataSourceManager == null || pc.getClass() != mGetDataSourceManager.getDeclaringClass())
720- * mGetDataSourceManager = pc.getClass().getMethod("getDataSourceManager", ZEROC); return
721- * mGetDataSourceManager.invoke(pc, ZEROO); } catch (Exception e) { throw
722- * CFMLEngineFactory.getInstance().getCastUtil().toPageException(e); } }
723- */
724-
725795 public static DatasourceConnection getDatasourceConnection (PageContext pc , DataSource ds , String user , String pass , boolean transactionSensitive ) throws PageException {
726796 if (transactionSensitive ) {
727797 return pc .getDataSourceManager ().getConnection (pc , ds , user , pass );
@@ -793,14 +863,6 @@ public static String[] trimItems(String[] arr) {
793863 return arr ;
794864 }
795865
796- public static Document getDocument (Node node ) {
797- return XMLUtil .getDocument (node );
798- }
799-
800- public static Document newDocument () throws PageException {
801- return XMLUtil .newDocument ();
802- }
803-
804866 public static void setFirst (Node parent , Node node ) {
805867 XMLUtil .setFirst (parent , node );
806868 }
@@ -935,8 +997,8 @@ public static Object getPropertyValue(Component cfc, String name, Object default
935997 return orm ().getPropertyValue (cfc , name , defaultValue );
936998 }
937999
938- public static String toString ( Node node , boolean omitXMLDecl , boolean indent , String publicId , String systemId , String encoding ) throws PageException {
939- return XMLUtil . toString ( node , omitXMLDecl , indent , publicId , systemId , encoding );
1000+ public static String toBase64 ( Object o ) throws PageException {
1001+ return caster (). toBase64 ( o );
9401002 }
9411003
9421004 public static Locale toLocale (String strLocale ) throws PageException {
@@ -958,4 +1020,40 @@ public static DateTime toDate(Object value, TimeZone timeZone) throws PageExcept
9581020 public static Calendar toCalendar (DateTime date , TimeZone timeZone , Locale locale ) {
9591021 return caster ().toCalendar (date .getTime (), timeZone , locale );
9601022 }
1023+
1024+ /**
1025+ * Tests if this string starts with the specified prefix.
1026+ * <p>
1027+ * Blatantly copied from the Lucee core, since we don't have access to this method without reflection.
1028+ *
1029+ * @link https://github.com/lucee/Lucee/blob/6.0/core/src/main/java/lucee/commons/lang/StringUtil.java#L870
1030+ *
1031+ * @param str
1032+ * string to check first char
1033+ * @param prefix
1034+ * the prefix.
1035+ *
1036+ * @return is first of given type
1037+ */
1038+ public static boolean startsWith (String str , char prefix ) {
1039+ return str != null && str .length () > 0 && str .charAt (0 ) == prefix ;
1040+ }
1041+
1042+ /**
1043+ * Tests if this string ends with the specified suffix.
1044+ * <p>
1045+ * Blatantly copied from the Lucee core, since we don't have access to this method without reflection.
1046+ *
1047+ * @link https://github.com/lucee/Lucee/blob/6.0/core/src/main/java/lucee/commons/lang/StringUtil.java#L870
1048+ *
1049+ * @param str
1050+ * string to check first char
1051+ * @param suffix
1052+ * the suffix.
1053+ *
1054+ * @return is last of given type
1055+ */
1056+ public static boolean endsWith (String str , char suffix ) {
1057+ return str != null && str .length () > 0 && str .charAt (str .length () - 1 ) == suffix ;
1058+ }
9611059}
0 commit comments