Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;
import java.awt.Font;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Main {
    public static final String PROPERTY_NODE = "property";
    public static final String NAME_ATTR = "name";
    public static final String STRING_ATTR = "stringvalue";
    public static final String INTEGER_ATTR = "intvalue";
    public static final String DOUBLE_ATTR = "doublevalue";
    public static final String FLOAT_ATTR = "doublevalue";
    public static final String BOOLEAN_ATTR = "booleanvalue";
    public static final String COLOR_ATTR = "colorvalue";
    public static final String FONT_ATTR = "fontvalue";

    public static void addProperty(Document document, Element parent, String name, Object value) {
        if (value instanceof String) {
            addStringProperty(document, parent, name, (String) value);
        } else if (value instanceof Integer) {
            addIntegerProperty(document, parent, name, (Integer) value);
        } else if (value instanceof Double) {
            addDoubleProperty(document, parent, name, (Double) value);
        } else if (value instanceof Float) {
            addFloatProperty(document, parent, name, (Float) value);
        } else if (value instanceof Boolean) {
            addBooleanProperty(document, parent, name, (Boolean) value);
        } else if (value instanceof Color) {
            addColorProperty(document, parent, name, (Color) value);
        } else if (value instanceof Font) {
            addFontProperty(document, parent, name, (Font) value);
        } else {
            return;
        }
    }

    public static void addStringProperty(Document document, Element parent, String name, String value) {
        addPropertyNode(document, parent, name).setAttribute(STRING_ATTR, value);
    }

    public static void addIntegerProperty(Document document, Element parent, String name, int value) {
        addPropertyNode(document, parent, name).setAttribute(INTEGER_ATTR, Integer.toString(value));
    }

    public static void addDoubleProperty(Document document, Element parent, String name, double value) {
        addPropertyNode(document, parent, name).setAttribute(DOUBLE_ATTR, Double.toString(value));
    }

    public static void addFloatProperty(Document document, Element parent, String name, double value) {
        addPropertyNode(document, parent, name).setAttribute(FLOAT_ATTR, Double.toString(value));
    }

    public static void addBooleanProperty(Document document, Element parent, String name, boolean value) {
        addPropertyNode(document, parent, name).setAttribute(BOOLEAN_ATTR, Boolean.toString(value));
    }

    public static void addColorProperty(Document document, Element parent, String name, Color value) {
        addPropertyNode(document, parent, name).setAttribute(COLOR_ATTR,
                value.getRed() + "," + value.getGreen() + "," + value.getBlue());
    }

    public static void addFontProperty(Document document, Element parent, String name, Font value) {
        addPropertyNode(document, parent, name).setAttribute(FONT_ATTR,
                value.getName() + "," + value.getStyle() + "," + value.getSize());
    }

    private static Element addPropertyNode(Document document, Element parent, String name) {
        Element element = document.createElement(PROPERTY_NODE);
        parent.appendChild(element);
        element.setAttribute(NAME_ATTR, name);
        return element;
    }
}