Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {
    private static final String PROFILES_NODE_NAME = "profiles";
    private static final String PROFILE_NODE_NAME = "profile";
    private static final String PROFILE_ID_NAME = "id";
    private static final String ACTIVATION_NAME = "activation";
    private static final String ACTIVEBYDEFAULT_NAME = "activeByDefault";
    private static final String ACTIVEBYDEFAULT_VAL = "false";
    private static final String BUILD_NAME = "build";
    private static final String PLUGINS_NAME = "plugins";

    /**
     * If there is no <profiles> section, create one with relevant profile with given profileId
     * If <profiles> already contains a <profile> with given profileId, delete all of its plugins
     * @param document
     * @param pomRoot
     * @param profileIdVal
     * @return the <plugins> node (under profiles/profile/build), or null for any error
     */
    public static Element initiateProfile(Document document, Element pomRoot, String profileIdVal) {
        //If <profiles> section does not exist, then create and get out
        NodeList childrenRoot = pomRoot.getChildNodes();
        int numProfileNodes = childrenRoot.getLength();
        Node profiles = null;
        for (int index = 0; index < numProfileNodes; index++) {
            Node currNode = childrenRoot.item(index);
            if (currNode.getNodeName().equals(PROFILES_NODE_NAME)) {
                profiles = currNode;
                break;
            }
        }
        if (profiles == null) {
            //no <profiles> section was found.
            //create <profiles>
            Element profilesNew = addProfilesSection(document, pomRoot);

            //Create <profile>, add to <profiles>, and get out
            return addNewProfileSection(document, profilesNew, profileIdVal);
        }

        //the <profiles> section already exists, go over all profiles to determine if the specific profile (with given profileIdVal) exists
        NodeList profileNodes = profiles.getChildNodes();
        numProfileNodes = profileNodes.getLength();
        boolean isProblem = false;
        Element profile = null;
        Element plugins = null;
        for (int profileIndex = 0; profileIndex < numProfileNodes; profileIndex++) {
            //Convert to Element if possible, otherwise it is not relevant
            Node profileNode = profileNodes.item(profileIndex);
            if (!(profileNode instanceof Element)) {
                continue;
            }
            Element currProfile = (Element) profileNode;

            NodeList profileChildren = currProfile.getChildNodes();
            int numProfileChildren = profileChildren.getLength();
            Element profileId = null;
            for (int indexProfileChildren = 0; indexProfileChildren < numProfileChildren; indexProfileChildren++) {
                if (!(profileChildren.item(indexProfileChildren) instanceof Element)) {
                    continue;
                }

                if (profileChildren.item(indexProfileChildren).getNodeName().equals(PROFILE_ID_NAME)) {
                    profileId = (Element) profileChildren.item(indexProfileChildren);
                    break;
                }
            }
            if (profileId == null) {
                //we have a profile without an id, ignore it and move to the next one
                continue;
            }

            //Check if its id is the one we are interested in
            String currProfileIdVal = profileId.getTextContent();
            if (currProfileIdVal.equals(profileIdVal)) {
                profile = currProfile;
                //A profile was found with id = profileId
                //get/create the <build> element
                Element build = getChildElement(document, currProfile, BUILD_NAME);
                if (build == null) {
                    isProblem = true;
                    break;
                }

                //get/create the <plugins> element
                plugins = getChildElement(document, build, PLUGINS_NAME);
                if (plugins == null) {
                    isProblem = true;
                    break;
                }

                //remove all children from the <plugins> element (whether or not they are <plugin>'s)
                NodeList pluginNodes = plugins.getChildNodes();
                int numPluginsChildren = pluginNodes.getLength();
                for (int pluginIndex = numPluginsChildren - 1; pluginIndex >= 0; pluginIndex--) {
                    plugins.removeChild(pluginNodes.item(pluginIndex));
                }

                //whatever happened above, we are done now, don't check any other profiles
                break;
            }
            //else continue to the next profile
        }

        if (isProblem)
            return null;

        if (profile == null) {
            //the required profile was never found.  Create it now, add to profiles, and get out
            return addNewProfileSection(document, (Element) profiles, profileIdVal);
        } else {
            //we did find the profile, so return the plugins element
            return plugins;
        }
    }

    /**
     * add profiles section under pomRoot
     * @param pomRoot
     * @return the profiles node
     */
    private static Element addProfilesSection(Document document, Element root) {
        Node profiles = document.createElement(PROFILES_NODE_NAME);
        root.appendChild(profiles);
        return (Element) profiles;
    }

    private static Element addNewProfileSection(Document document, Element profiles, String profileIdVal) {
        //start with profile node
        Element profile = document.createElement(PROFILE_NODE_NAME);
        profiles.appendChild(profile);

        //create the id tag
        Node id = document.createElement(PROFILE_ID_NAME);
        //id.setNodeValue(profileIdVal);
        id.setTextContent(profileIdVal);
        //add it to the profile
        profile.appendChild(id);

        //create activation tag
        Node activeByDefault = document.createElement(ACTIVEBYDEFAULT_NAME);
        //activeByDefault.setNodeValue(ACTIVEBYDEFAULT_VAL);
        activeByDefault.setTextContent(ACTIVEBYDEFAULT_VAL);
        Node activation = document.createElement(ACTIVATION_NAME);
        activation.appendChild(activeByDefault);
        //add it to profile
        profile.appendChild(activation);

        //create build tag
        Node build = document.createElement(BUILD_NAME);
        //add it to profile
        profile.appendChild(build);

        //create plugins tag, add to build
        Node plugins = document.createElement(PLUGINS_NAME);
        build.appendChild(plugins);

        return (Element) plugins;
    }

    private static Element getChildElement(Document document, Element origElement, String subElementName) {
        NodeList buildNodes = origElement.getChildNodes();
        int numChildrenOfOrig = buildNodes.getLength();
        for (int indexChildOfOrig = 0; indexChildOfOrig < numChildrenOfOrig; indexChildOfOrig++) {
            Node child = buildNodes.item(indexChildOfOrig);
            if (child.getNodeName().equals(subElementName)) {
                return (Element) child;
            }
        }

        //if we are here, the child node was never found, so create it now
        Element newElem = document.createElement(subElementName);
        origElement.appendChild(newElem);
        return newElem;

    }
}