Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 *  Copyright  2012-2015 eBay Software Foundation
 *  This program is dual licensed under the MIT and Apache 2.0 licenses.
 *  Please see LICENSE for more information.
 *******************************************************************************/

import java.io.StringWriter;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

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

public class Main {
    private static String loadBeanDef(Element rootElement, String beanId) {
        String beanDefinition = null;
        Element beanElement = findChildById(rootElement, "bean", beanId);

        if (beanElement != null) {
            beanDefinition = getStringFromDocument(beanElement);
        }

        return beanDefinition;
    }

    private static Element findChildById(Element parent, String tag, String attributeIdValue) {
        // First check to see if any parameters are null
        if (parent == null || tag == null)
            return null;
        // Check to see if this is the element we are interested in. This is
        // redundant apart from first call, but keep in to keep functionality
        //      if (nodeNameEqualTo(parent, tag))
        //         return parent;
        // Get all the children
        NodeList list = parent.getChildNodes();
        int listCount = list.getLength();
        for (int k = 0; k < listCount; k++) {
            Node child = list.item(k);
            // If the node is not an element, ignore it
            if (child.getNodeType() != Node.ELEMENT_NODE)
                continue;
            // Check to see if this node is the node we want
            if (nodeNameEqualTo(child, tag)) {
                if (((Element) child).getAttribute("id").equals(attributeIdValue)) {
                    return (Element) child;
                }
            }
        }
        // Now that we have checked all children, we can recurse
        for (int k = 0; k < listCount; k++) {
            Node child = list.item(k);
            // If the node is not an element, ignore it
            if (child.getNodeType() != Node.ELEMENT_NODE)
                continue;
            Element result = findChildById((Element) child, tag, attributeIdValue);
            if (result != null)
                return result;
        }
        return null;
    }

    private static String getStringFromDocument(Node doc) {
        try {
            DOMSource domSource = new DOMSource(doc);
            StringWriter writer = new StringWriter();
            StreamResult result = new StreamResult(writer);
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.transform(domSource, result);
            return writer.toString();
        } catch (TransformerException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * Check whether a name of given node is equal to a given name. Strips
     * namespace (if any). Case-sensitive.
     */
    private static boolean nodeNameEqualTo(Node node, String target) {
        if (node == null || target == null)
            return false;
        String name = node.getNodeName();
        // If target contains namespace, require exact match
        if (target.indexOf(':') < 0) {
            int index = name.indexOf(':');
            if (index >= 0)
                name = name.substring(index + 1); // Strip namespace
        }
        return name.equals(target);
    }
}