Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Main {
    public static Element getSingleOptionalChildElement(Element parent, Enum<?> child) {
        final String elName = getXmlName(child);
        final NodeList list = parent.getElementsByTagName(elName);
        if (list.getLength() == 1) {
            return (Element) list.item(0);
        }
        if (list.getLength() == 0) {
            return null;
        }
        throw new IllegalArgumentException("expected a single " + elName + " child element of XML element "
                + parent.getNodeName() + ", but found " + list.getLength());
    }

    public static String getXmlName(Enum<?> node) {
        String name = node.name();

        //remove leading or trailing underscore, it is used if a name conflicts with a Java keyword
        if (name.length() > 0 && name.charAt(0) == '_') {
            name = name.substring(1);
        }
        if (name.length() > 0 && name.charAt(name.length() - 1) == '_') {
            name = name.substring(0, name.length() - 1);
        }
        return name.replace('_', '-');//use dashes instead of underscores in XML
    }
}