Example usage for org.apache.commons.lang ObjectUtils toString

List of usage examples for org.apache.commons.lang ObjectUtils toString

Introduction

In this page you can find the example usage for org.apache.commons.lang ObjectUtils toString.

Prototype

public static String toString(Object obj) 

Source Link

Document

Gets the toString of an Object returning an empty string ("") if null input.

 ObjectUtils.toString(null)         = "" ObjectUtils.toString("")           = "" ObjectUtils.toString("bat")        = "bat" ObjectUtils.toString(Boolean.TRUE) = "true" 

Usage

From source file:org.medici.bia.validator.peoplebase.ShowUploadPortraitPersonValidator.java

/**
 * //from   ww  w.j av  a2s  . c o  m
 * @param link
 * @param errors
 */
private void validateRemoteUrl(String link, Errors errors) {
    if (errors.hasErrors()) {
        if (!ObjectUtils.toString(link).equals("") && !ObjectUtils.toString(link).equals("http://")) {
            try {
                URL url = new URL(link);
                HttpURLConnection huc = (HttpURLConnection) url.openConnection();
                int responseCode = huc.getResponseCode();

                if (responseCode != HttpURLConnection.HTTP_OK) {
                    errors.reject("link", "error.link.notFound");
                }
            } catch (UnknownHostException uhe) {
                errors.reject("link", "error.link.notFound");
            } catch (IOException ioException) {
                errors.reject("link", "error.link.notFound");
            }
        }
    }
}

From source file:org.medici.bia.validator.user.ShowUploadPortraitUserValidator.java

/**
 * //from   ww w.  ja  v a 2 s .  c om
 * @param link
 * @param errors
 */
private void validateRemoteUrl(String link, Errors errors) {
    if (!ObjectUtils.toString(link).equals("") && !ObjectUtils.toString(link).equals("http://")) {
        try {
            URL url = new URL(link);
            HttpURLConnection huc = (HttpURLConnection) url.openConnection();
            int responseCode = huc.getResponseCode();

            if (responseCode != HttpURLConnection.HTTP_OK) {
                errors.reject("link", "error.link.notFound");
            }
        } catch (UnknownHostException uhe) {
            errors.reject("link", "error.link.notFound");
        } catch (IOException ioException) {
            errors.reject("link", "error.link.notFound");
        }
    }
}

From source file:org.mifos.reports.branchreport.BranchReportClientSummaryBO.java

public BranchReportClientSummaryBO(String fieldName, Integer total, Integer veryPoorTotal) {
    this.fieldName = fieldName;
    this.total = ObjectUtils.toString(total);
    this.veryPoorTotal = ObjectUtils.toString(veryPoorTotal);
}

From source file:org.mobicents.maven.plugin.EclipseMojo.java

/**
 * Retrieves any additional source directories which are defined within the andromda-multi-source-plugin.
 *
 * @param project the maven project from which to retrieve the extra source directories.
 * @return the list of extra source directories.
 *//*from  ww  w.j a  va2  s.  c o m*/
private List getExtraSourceDirectories(final MavenProject project) {
    final List sourceDirectories = new ArrayList();
    final Build build = project.getBuild();
    if (build != null) {
        final PluginManagement pluginManagement = build.getPluginManagement();
        if (pluginManagement != null && !pluginManagement.getPlugins().isEmpty()) {
            Plugin multiSourcePlugin = null;
            for (final Iterator iterator = pluginManagement.getPlugins().iterator(); iterator.hasNext();) {
                final Plugin plugin = (Plugin) iterator.next();
                if (MULTI_SOURCE_PLUGIN_ARTIFACT_ID.equals(plugin.getArtifactId())) {
                    multiSourcePlugin = plugin;
                    break;
                }
            }
            final Xpp3Dom configuration = this.getConfiguration(multiSourcePlugin);
            if (configuration != null && configuration.getChildCount() > 0) {
                final Xpp3Dom directories = configuration.getChild(0);
                if (directories != null) {
                    final int childCount = directories.getChildCount();
                    if (childCount > 0) {
                        final String baseDirectory = PathNormalizer
                                .normalizePath(ObjectUtils.toString(project.getBasedir()) + '/');
                        final Xpp3Dom[] children = directories.getChildren();
                        for (int ctr = 0; ctr < childCount; ctr++) {
                            final Xpp3Dom child = children[ctr];
                            if (child != null) {
                                String directoryValue = PathNormalizer.normalizePath(child.getValue());
                                if (directoryValue != null) {
                                    if (!directoryValue.startsWith(baseDirectory)) {
                                        directoryValue = PathNormalizer
                                                .normalizePath(baseDirectory + directoryValue.trim());
                                    }
                                    sourceDirectories.add(directoryValue);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return sourceDirectories;
}

From source file:org.mule.management.support.SimplePasswordJmxAuthenticator.java

public Subject authenticate(Object authToken) {
    if (authToken == null) {
        throw new SecurityException("No authentication token available");
    }/*from  w w w.j a  v  a2s.  c  o  m*/
    if (!(authToken instanceof String[]) || ((String[]) authToken).length != 2) {
        throw new SecurityException("Unsupported credentials format");
    }

    String[] authentication = (String[]) authToken;

    String username = StringUtils.defaultString(authentication[0]);
    String password = StringUtils.defaultString(authentication[1]);

    if (!credentials.containsKey(username)) {
        throw new SecurityException("Unauthenticated user: " + username);
    }

    if (!password.equals(ObjectUtils.toString(credentials.get(username)))) {
        throw new SecurityException("Invalid password");
    }

    Set principals = new HashSet();
    principals.add(new JMXPrincipal(username));
    return new Subject(true, principals, Collections.EMPTY_SET, Collections.EMPTY_SET);
}

From source file:org.objectstyle.cayenne.dataview.DataTypeSpec.java

public Object toDataType(DataTypeEnum dataType, Object untypedValue) {
    Class dataTypeClass = getJavaClass(dataType);
    if (dataTypeClass == null || untypedValue == null
            || ClassUtils.isAssignable(untypedValue.getClass(), dataTypeClass)) {
        if (DataTypeEnum.DATE_TYPE.equals(dataType) && Date.class.equals(dataTypeClass)) {
            return DateUtils.truncate(untypedValue, Calendar.DATE);
        }//from   w  w  w . ja v  a  2s . co  m
        return untypedValue;
    }

    Object v = null;
    String strUntypedValue = null;
    boolean isStringUntypedValue;
    Number numUntypedValue = null;
    boolean isNumberUntypedValue;
    if (isStringUntypedValue = untypedValue instanceof String)
        strUntypedValue = (String) untypedValue;
    if (isNumberUntypedValue = untypedValue instanceof Number)
        numUntypedValue = (Number) untypedValue;

    switch (dataType.getValue()) {
    case DataTypeEnum.BOOLEAN_TYPE_VALUE:
        if (isNumberUntypedValue)
            v = BooleanUtils.toBooleanObject(numUntypedValue.intValue());
        else if (isStringUntypedValue)
            v = BooleanUtils.toBooleanObject(strUntypedValue);
        break;
    case DataTypeEnum.INTEGER_TYPE_VALUE:
        if (isNumberUntypedValue)
            v = new Integer(numUntypedValue.intValue());
        else if (isStringUntypedValue)
            v = NumberUtils.createInteger(strUntypedValue);
        break;
    case DataTypeEnum.DOUBLE_TYPE_VALUE:
        if (isNumberUntypedValue)
            v = new Double(numUntypedValue.doubleValue());
        else if (isStringUntypedValue)
            v = NumberUtils.createDouble(strUntypedValue);
        break;
    case DataTypeEnum.STRING_TYPE_VALUE:
        v = ObjectUtils.toString(untypedValue);
        break;
    case DataTypeEnum.DATE_TYPE_VALUE:
        if (isNumberUntypedValue)
            v = DateUtils.truncate(new Date(numUntypedValue.longValue()), Calendar.DATE);
        break;
    case DataTypeEnum.DATETIME_TYPE_VALUE:
        if (isNumberUntypedValue)
            v = new Date(numUntypedValue.longValue());
        break;
    case DataTypeEnum.MONEY_TYPE_VALUE:
        if (isNumberUntypedValue)
            v = new Double(numUntypedValue.doubleValue());
        else if (isStringUntypedValue)
            v = NumberUtils.createDouble(strUntypedValue);
        break;
    case DataTypeEnum.PERCENT_TYPE_VALUE:
        if (isNumberUntypedValue)
            v = new Double(numUntypedValue.doubleValue());
        else if (isStringUntypedValue)
            v = NumberUtils.createDouble(strUntypedValue);
        break;
    }
    return v;
}

From source file:org.objectstyle.cayenne.dataview.DataTypeSpec.java

public Object fromDataType(Class untypedValueClass, DataTypeEnum dataType, Object typedValue) {
    if (typedValue == null)
        return null;
    Class dataTypeClass = getJavaClass(dataType);
    //    Validate.isTrue(typedValue.getClass().equals(dataTypeClass));

    if (untypedValueClass == null)
        return typedValue;

    if (ClassUtils.isAssignable(dataTypeClass, untypedValueClass))
        return typedValue;

    String strTypedValue = null;//from   w  w  w .  j  a  v a 2 s.  c  om
    boolean isStringTypedValue;
    Number numTypedValue = null;
    boolean isNumberTypedValue;
    Boolean boolTypedValue = null;
    boolean isBooleanTypedValue;
    Date dateTypedValue = null;
    boolean isDateTypedValue;

    if (isStringTypedValue = typedValue instanceof String)
        strTypedValue = (String) typedValue;
    if (isNumberTypedValue = typedValue instanceof Number)
        numTypedValue = (Number) typedValue;
    if (isBooleanTypedValue = typedValue instanceof Boolean)
        boolTypedValue = (Boolean) typedValue;
    if (isDateTypedValue = typedValue instanceof Date)
        dateTypedValue = (Date) typedValue;

    Object v = null;
    if (String.class.equals(untypedValueClass)) {
        v = ObjectUtils.toString(typedValue);
    } else if (BigDecimal.class.equals(untypedValueClass)) {
        if (isStringTypedValue)
            v = NumberUtils.createBigDecimal(strTypedValue);
        else if (isNumberTypedValue)
            v = new BigDecimal(numTypedValue.doubleValue());
        else if (isBooleanTypedValue)
            v = new BigDecimal(BooleanUtils.toInteger(boolTypedValue.booleanValue()));
        else if (isDateTypedValue)
            v = new BigDecimal(dateTypedValue.getTime());
    } else if (Boolean.class.equals(untypedValueClass)) {
        if (isStringTypedValue)
            v = BooleanUtils.toBooleanObject(strTypedValue);
        else if (isNumberTypedValue)
            v = BooleanUtils.toBooleanObject(numTypedValue.intValue());
        else if (isDateTypedValue)
            v = BooleanUtils.toBooleanObject((int) dateTypedValue.getTime());
    } else if (Byte.class.equals(untypedValueClass)) {
        if (isStringTypedValue)
            v = Byte.valueOf(strTypedValue);
        else if (isNumberTypedValue)
            v = new Byte(numTypedValue.byteValue());
        else if (isBooleanTypedValue)
            v = new Byte((byte) BooleanUtils.toInteger(boolTypedValue.booleanValue()));
        else if (isDateTypedValue)
            v = new Byte((byte) dateTypedValue.getTime());
    } else if (byte[].class.equals(untypedValueClass)) {
        if (isStringTypedValue)
            v = strTypedValue.getBytes();
    } else if (Double.class.equals(untypedValueClass)) {
        if (isStringTypedValue)
            v = NumberUtils.createDouble(strTypedValue);
        else if (isNumberTypedValue)
            v = new Double(numTypedValue.doubleValue());
        else if (isBooleanTypedValue)
            v = new Double(BooleanUtils.toInteger(boolTypedValue.booleanValue()));
        else if (isDateTypedValue)
            v = new Double(dateTypedValue.getTime());
    } else if (Float.class.equals(untypedValueClass)) {
        if (isStringTypedValue)
            v = NumberUtils.createFloat(strTypedValue);
        else if (isNumberTypedValue)
            v = new Float(numTypedValue.floatValue());
        else if (isBooleanTypedValue)
            v = new Float(BooleanUtils.toInteger(boolTypedValue.booleanValue()));
        else if (isDateTypedValue)
            v = new Float(dateTypedValue.getTime());
    } else if (Integer.class.equals(untypedValueClass)) {
        if (isStringTypedValue)
            v = NumberUtils.createInteger(strTypedValue);
        else if (isNumberTypedValue)
            v = new Integer(numTypedValue.intValue());
        else if (isBooleanTypedValue)
            v = BooleanUtils.toIntegerObject(boolTypedValue.booleanValue());
        else if (isDateTypedValue)
            v = new Integer((int) dateTypedValue.getTime());
    } else if (Long.class.equals(untypedValueClass)) {
        if (isStringTypedValue)
            v = NumberUtils.createLong(strTypedValue);
        else if (isNumberTypedValue)
            v = new Long(numTypedValue.longValue());
        else if (isBooleanTypedValue)
            v = new Long(BooleanUtils.toInteger(boolTypedValue.booleanValue()));
        else if (isDateTypedValue)
            v = new Long(dateTypedValue.getTime());
    } else if (java.sql.Date.class.equals(untypedValueClass)) {
        if (isNumberTypedValue)
            v = new java.sql.Date(numTypedValue.longValue());
        else if (isDateTypedValue)
            v = new java.sql.Date(dateTypedValue.getTime());
    } else if (java.sql.Time.class.equals(untypedValueClass)) {
        if (isNumberTypedValue)
            v = new java.sql.Time(numTypedValue.longValue());
        else if (isDateTypedValue)
            v = new java.sql.Time(dateTypedValue.getTime());
    } else if (java.sql.Timestamp.class.equals(untypedValueClass)) {
        if (isNumberTypedValue)
            v = new java.sql.Timestamp(numTypedValue.longValue());
        else if (isDateTypedValue)
            v = new java.sql.Timestamp(dateTypedValue.getTime());
    } else if (Date.class.equals(untypedValueClass)) {
        if (isNumberTypedValue)
            v = new Date(numTypedValue.longValue());
    }
    return v;
}

From source file:org.objectstyle.cayenne.dataview.ObjEntityViewField.java

public String getFormattedValue(DataObject obj) {
    Object value = getRawValue(obj);
    String formattedValue = null;
    if (!isLookup()) {
        Format f = (displayFormat != null ? displayFormat : editFormat);
        if (f == null)
            formattedValue = ObjectUtils.toString(value);
        else {/*w w w.j a  va2s .  com*/
            try {
                formattedValue = f.format(value);
            } catch (Exception ex) {
                formattedValue = "";
            }
        }
    } else {
        formattedValue = lookupField.getFormattedValue((DataObject) value);
    }
    return formattedValue;
}

From source file:org.onehippo.forge.content.exim.core.impl.AbstractContentMigrationTask.java

@Override
public String getSummary() {
    StringWriter sw = new StringWriter(1024);
    PrintWriter out = new PrintWriter(sw);
    CSVPrinter csvPrinter = null;/* w w  w  .  ja  v a  2  s .  c  o  m*/

    try {
        sw = new StringWriter(1024);
        out = new PrintWriter(sw);

        int totalCount = 0;
        int processedCount = 0;
        int successCount = 0;

        for (ContentMigrationRecord record : getContentMigrationRecords()) {
            ++totalCount;

            if (record.isProcessed()) {
                ++processedCount;

                if (record.isSucceeded()) {
                    ++successCount;
                }
            }
        }

        out.println(
                "===============================================================================================================");
        out.println("Execution Summary:");
        out.println(
                "---------------------------------------------------------------------------------------------------------------");
        out.printf("Total: %d, Processed: %d, Suceeded: %d, Failed: %d, Duration: %dms", totalCount,
                processedCount, successCount, processedCount - successCount,
                getStoppedTimeMillis() - getStartedTimeMillis());
        out.println();
        out.println(
                "---------------------------------------------------------------------------------------------------------------");
        out.println("Details (in CSV format):");
        out.println(
                "---------------------------------------------------------------------------------------------------------------");

        try {
            csvPrinter = CSVFormat.DEFAULT
                    .withHeader("SEQ", "PROCESSED", "SUCCEEDED", "ID", "PATH", "TYPE", "ATTRIBUTES", "ERROR")
                    .print(out);

            int seq = 0;

            for (ContentMigrationRecord record : getContentMigrationRecords()) {
                csvPrinter.printRecord(++seq, record.isProcessed(), record.isSucceeded(),
                        StringUtils.defaultString(record.getContentId()),
                        StringUtils.defaultString(record.getContentPath()),
                        StringUtils.defaultString(record.getContentType()),
                        ObjectUtils.toString(record.getAttributeMap()),
                        StringUtils.defaultString(record.getErrorMessage()));
            }
        } catch (IOException e) {
            e.printStackTrace(out);
        }

        out.println(
                "===============================================================================================================");
        out.flush();

        return sw.toString();
    } finally {
        IOUtils.closeQuietly(csvPrinter);
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(sw);
    }
}

From source file:org.openanzo.client.cli.CallCommand.java

private int call(CommandContext context, AnzoClient client, boolean time, URI serviceUri,
        RdfInputArgument input, Writer output, RDFFormat outputFormat, URI base) throws AnzoException {
    int result = 1;
    boolean owns = false;
    long start = (time) ? System.currentTimeMillis() : 0;
    try {/*www  . j a  v  a 2s  .c o m*/
        owns = !client.isConnected();
        if (owns) {
            client.connect();
            printOnConnectionSuccess(context);
        }
        IDataset storeIn = new Dataset();

        Reader in = input.getReader();
        RDFFormat inputFormat = input.getFormat();

        if (base == null) {
            base = input.getDefaultGraphURI();
        }

        ReadWriteUtils.loadQuadStore(storeIn, in, inputFormat, ObjectUtils.toString(base));

        Collection<Statement> stmts = client.executeService(serviceUri, storeIn.getStatements());

        Dataset storeOut = new Dataset();
        context.populateDataset(storeOut, stmts);
        context.outputRdf(storeOut, outputFormat, output);
        if (time) {
            context.writeOutput("execution time (ms): " + (System.currentTimeMillis() - start));
        }
        result = 0;
    } finally {
        try {
            if (owns) {
                client.close();
            }
        } catch (AnzoRuntimeException e) {
            log.error("Error closing connection", e);
        }
    }
    return result;
}