001    package org.LiveGraph.dataFile.common;
002    
003    import com.softnetConsult.utils.string.StringTools;
004    
005    
006    /**
007     * This convenience class defines special tags for the LiveGraph data file format.
008     * 
009     * <p style="font-size:smaller;">This product includes software developed by the
010     *    <strong>LiveGraph</strong> project and its contributors.<br />
011     *    (<a href="http://www.live-graph.org" target="_blank">http://www.live-graph.org</a>)<br />
012     *    Copyright (c) 2007-2008 G. Paperin.<br />
013     *    All rights reserved.
014     * </p>
015     * <p style="font-size:smaller;">File: DataFormatTools.java</p> 
016     * <p style="font-size:smaller;">Redistribution and use in source and binary forms, with or
017     *    without modification, are permitted provided that the following terms and conditions are met:
018     * </p>
019     * <p style="font-size:smaller;">1. Redistributions of source code must retain the above
020     *    acknowledgement of the LiveGraph project and its web-site, the above copyright notice,
021     *    this list of conditions and the following disclaimer.<br />
022     *    2. Redistributions in binary form must reproduce the above acknowledgement of the
023     *    LiveGraph project and its web-site, the above copyright notice, this list of conditions
024     *    and the following disclaimer in the documentation and/or other materials provided with
025     *    the distribution.<br />
026     *    3. All advertising materials mentioning features or use of this software or any derived
027     *    software must display the following acknowledgement:<br />
028     *    <em>This product includes software developed by the LiveGraph project and its
029     *    contributors.<br />(http://www.live-graph.org)</em><br />
030     *    4. All advertising materials distributed in form of HTML pages or any other technology
031     *    permitting active hyper-links that mention features or use of this software or any
032     *    derived software must display the acknowledgment specified in condition 3 of this
033     *    agreement, and in addition, include a visible and working hyper-link to the LiveGraph
034     *    homepage (http://www.live-graph.org).
035     * </p>
036     * <p style="font-size:smaller;">THIS SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY
037     *    OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
038     *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT SHALL
039     *    THE AUTHORS, CONTRIBUTORS OR COPYRIGHT  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
040     *    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING  FROM, OUT OF OR
041     *    IN CONNECTION WITH THE SOFTWARE OR THE USE OR  OTHER DEALINGS IN THE SOFTWARE.
042     * </p>
043     * 
044     * @author Greg Paperin (<a href="http://www.paperin.org" target="_blank">http://www.paperin.org</a>)
045     * @version {@value org.LiveGraph.LiveGraph#version}
046     */
047    public class DataFormatTools {
048    
049    /**
050     * Every comment line must start with this tag.
051     */
052    public static final String TAGComment = "#";
053    
054    /**
055     * Every file description line must start with this tag.
056     */
057    public static final String TAGFileInfo = "@";
058    
059    /**
060     * An alternatice separator definition in the first line of the file must be
061     * ocated wetween two instances of this tag.
062     */
063    public static final String TAGSepDefinition = TAGComment + TAGComment;
064    
065    /**
066     * This is the default data separator tag.
067     */
068    public static final String DefaultSeparator = ",";
069    
070    
071    /**
072     * In order to read the data correctly a tag must not be confused with data value.
073     * This method checks whther a separator tag is valid. Note that while this method
074     * check for most common problems, it does not guarantee a correct separator.
075     *  
076     * @param separator A proposed data deparator string.
077     * @return {@code true} if the specified separetor tag can be used with {@code double}
078     * data values; {@code false} if the specified tag is not valid.  
079     */
080    public static String isValidSeparator(String separator) {
081            
082            if (null == separator)
083                    return "The saparator may not be a null string";
084            
085            try {
086                    double d = Double.parseDouble(separator);
087                    return "Separator may not represent a legal real value (" + d + ")";
088            } catch (NumberFormatException e) {}
089            
090            try {
091                    double d = StringTools.parseDouble(separator);
092                    return "Separator may not represent a legal real value (" + d + ")";
093            } catch (NumberFormatException e) {}
094            
095            String SEP = separator.toUpperCase();
096            if (SEP.equals("P") || SEP.equals("F") || SEP.equals("A") || SEP.equals("B") || SEP.equals("C")
097                             || SEP.equals("D") || SEP.equals("E") || SEP.equals("F") || SEP.equals("H") || SEP.equals("NAN")
098                             || SEP.equals("INFINITY") || SEP.equals("^") || SEP.equals("-") || SEP.equals("+")
099                             || SEP.equals(".") || SEP.equals("..") || SEP.equals("..."))
100                    return "Illegal separator (" + separator + ")";
101            
102            if (separator.contains("\n") || separator.contains("\r"))
103                    return "Separator may not contain any line feeds or carriage returns";
104            
105            return null;
106    }
107    
108    }