Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

    /**
     * Takes an object of a CPC supported data type and returns its string representation.<br/>
     * In most cases this method will simply call {@link Object#toString()}.<br/>
     * Special handling is implemented for dates.<br/>
     * <br/>
     * All return values are properly escaped. 
     * 
     * @param value the value to convert into an XML string value, may be NULL.
     * @return the escaped string value of the given object, empty string if object is NULL.
     */
    public static String mapToXMLValue(Comparable<? extends Object> value) {
        if (value == null)
            return "";

        if (value instanceof Date)
            return simpleDateFormat.format((Date) value);

        return escapeData(value.toString());
    }

    /**
     * Takes an arbitrary string and encodes it in a way which allows its use as content of an XML element.<br/>
     * The string is left unchanged, if it does not violate any XML requirements. Otherwise it is encapsulated
     * in a CDATA element. 
     * 
     * @param data the string to escape, never null. 
     * @return escaped version of the given string which is save for use as content of an XML element, never null.
     */
    public static String escapeData(String data) {
        data = data.replace("&", "&amp;");
        data = data.replace("<", "&lt;");
        data = data.replace(">", "&gt;");

        return data;
    }
}