All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.coherentlogic.fred.client.misc.utils.Utils Maven / Gradle / Ivy

There is a newer version: 2.0.3-RELEASE
Show newest version
package com.coherentlogic.fred.client.misc.utils;

import java.io.IOException;
import java.io.StringReader;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * This class provides various utility methods which are shared amongst several
 * modules.
 *
 * @author Support
 */
public class Utils {

    public static Date using (int year, int month, int day) {

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, day);

        return calendar.getTime();
    }

    /**
     * Method iterates over all children under the targetElement and, for
     * each one with a tag name equal to tagName, executes the action
     * , passing in that child element.
     */
    public static void forEachChild (
        Element targetElement,
        String tagName,
        Action action
    ) {

        List children = null;

        if (targetElement != null && targetElement.hasChildNodes())
            children =
                DomUtils.getChildElementsByTagName(targetElement, tagName);

        if (children != null)
            for (Element next : children)
                action.execute(next);
    }

    /**
     * Method reads in the xml and returns the top level document.
     */
    public static Document toDocument (String xml)
        throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource source = new InputSource (new StringReader(xml));

        return builder.parse ( source );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy