FFGConstants.java :  » Game » FFGenie-2010.3-src » ffg » util » Java Open Source

Java Open Source » Game » FFGenie 2010.3 src 
FFGenie 2010.3 src » ffg » util » FFGConstants.java
/*
 * FFGConstants.java
 *
 * Created on 23 May 2006, 17:04
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ffg.util;

import java.io.File;
import java.text.Collator;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerFactory;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Contains useful constants used throughout the application.
 * @author eugene
 */
public class FFGConstants {
    public static final String YEAR = "2010";
    public static final String RELEASE = "3";
    /**
     * Version number
     */
    public static final String VERSION = String.format("%s.%s", YEAR, RELEASE);
    /**
     * Format for prices
     */
    private static final ThreadLocal<DecimalFormat> local = new ThreadLocal<DecimalFormat>() {
        @Override
        protected DecimalFormat initialValue() {
            return new DecimalFormat("###,###");
        }
    };
    
    /**
     * Comparator for sorting strings.
     */
    public static final Collator COLLATOR = Collator.getInstance();
    static {
        COLLATOR.setStrength(Collator.PRIMARY);
    }
    
    /**
     * Date format
     */
    public static final DateFormat DATE_FORMAT = DateFormat.getDateTimeInstance();
    
    /**
     * Regular expression that returns the whole contents of a file in a Scanner
     */
    public static final Pattern GET_ALL_STRINGS_PATTERN = Pattern.compile("\\A");
    
    // 
//    public static final DecimalFormat PRICE_FORMAT = new DecimalFormat("###,###");
    /**
     * Used to parse the prices of players from the html
     * @return
     */
    public static final DecimalFormat getCommaDecimalFormat() {
//        return new DecimalFormat("###,###");
        return local.get();
    }
    
    /**
     * Space
     */
    public static final String SPACE = " ";
    /**
     * Tab
     */
    public static final String TAB = "\t";
    /**
     * Platform dependant line separator
     */
    public static final String NEW_LINE = System.getProperty("line.separator");
    /**
     * The user's directory
     */
    public static final String USER_DIR = System.getProperty("user.dir");
    /**
     * The user's home directory
     */
    public static final String USER_HOME = System.getProperty("user.home");
    /**
     * The application directory.
     */
    public static final File APP_DIR = new File(USER_HOME, ".FFGenie");

    public static final String ROUND_SCORES_FORMAT = "%s/?p=topplayers&page=%d";

    public static final String PLAYER_RESEARCH_FORMAT = "%s/?p=researchmini&pid=%d";

    public static final String COACH_RANKINGS_FORMAT = "%s/?p=topcoaches&overall=1&page=%d";

//    public static final String BASE_STATS_URL = "http://www.freewebs.com/ffgenie/stats/%s/%s/%s";
    public static final String BASE_STATS_URL = "http://ffgenie.com/stats/%s/%s/%s";

    public static final String ROUND_FILE_FORMAT = "round.txt";

    private FFGConstants() {}

    public static String getExtension(File file) {
        String fileName = file.getName();
        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex == -1) {
            return "";
        } else {
            return fileName.substring(dotIndex + 1);
        }
    }

    public static Element createElementUnder(Document doc, Element parent, String name) {
        Element elem = doc.createElement(name);
        parent.appendChild(elem);
        return elem;
    }

    public static void setAttribute(Document doc, Element parent, String attributeName, String attributeValue) {
        Attr attr = doc.createAttribute(attributeName);
        attr.setNodeValue(attributeValue);
        NamedNodeMap attrs = parent.getAttributes();
        attrs.setNamedItem(attr);
    }

    public static int DEFAULT_NUM_ROUNDS = 22;

    public static final DocumentBuilderFactory DOCUMENT_BUILDER_FACTORY = DocumentBuilderFactory.newInstance();
    public static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();

//    public static final XPathFactory X_PATH_FACTORY = XPathFactory.newInstance();
//    public static final XPath X_PATH = X_PATH_FACTORY.newXPath();

    public static Node getFirstChildNamed(Node parent, String name) {
        NodeList children = parent.getChildNodes();
        int nChildren = children.getLength();
        for (int i = 0; i < nChildren; i++) {
            Node childNode = children.item(i);
            if (name.equals(childNode.getNodeName())) {
                return childNode;
            }
        }
        return null;
    }

    public static List<Node> getChildrenNamed(Node parent, String name) {
        List<Node> result = new ArrayList<Node>();
        NodeList children = parent.getChildNodes();
        int nChildren = children.getLength();
        for (int i = 0; i < nChildren; i++) {
            Node childNode = children.item(i);
            if (name.equals(childNode.getNodeName())) {
                result.add(childNode);
            }
        }
        return result;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.