Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
 *
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the License). You may not use this file except in
 * compliance with the License.
 *
 * You can obtain a copy of the License at
 * https://opensso.dev.java.net/public/CDDLv1.0.html or
 * opensso/legal/CDDLv1.0.txt
 * See the License for the specific language governing
 * permission and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * Header Notice in each file and include the License file
 * at opensso/legal/CDDLv1.0.txt.
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * $Id: XMLUtils.java,v 1.5 2008-06-25 05:41:28 qcheng Exp $
 *
 */

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;

import java.util.Map;
import java.util.Set;

import org.w3c.dom.Element;

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

public class Main {
    private static final Map EMPTY_MAP = Collections.unmodifiableMap(new HashMap());

    /**
     * @param parentNode
     *            is the element tag that contains all the AttirbuteValuePair
     *            tags as children
     * @return Map Returns the AV pairs in a Map where each entry in the Map is
     *         an AV pair. The key is the attribute name and the value is a Set
     *         of String objects.
     */
    public static Map parseAttributeValuePairTags(Node parentNode) {

        NodeList avList = parentNode.getChildNodes();
        Map map = null;
        final int numAVPairs = avList.getLength();
        if (numAVPairs <= 0) {
            return EMPTY_MAP;
        }
        for (int l = 0; l < numAVPairs; l++) {
            Node avPair = avList.item(l);
            if ((avPair.getNodeType() != Node.ELEMENT_NODE) || !avPair.getNodeName().equals("AttributeValuePair")) {
                continue;
            }
            NodeList leafNodeList = avPair.getChildNodes();
            long numLeafNodes = leafNodeList.getLength();
            if (numLeafNodes < 2) {
                // TODO: More error handling required later for missing
                // 'Attribute' or
                // 'Value' tags.
                continue;
            }
            String key = null;
            Set values = null;
            // Since Attribute tag is always the first leaf node as per the
            // DTD,and
            // values can one or more, Attribute tag can be parsed first and
            // then
            // iterate over the values, if any.
            Node attributeNode = null;
            for (int i = 0; i < numLeafNodes; i++) {
                attributeNode = leafNodeList.item(i);
                if ((attributeNode.getNodeType() == Node.ELEMENT_NODE)
                        && (attributeNode.getNodeName().equals("Attribute"))) {
                    i = (int) numLeafNodes;
                } else {
                    continue;
                }
            }
            key = ((Element) attributeNode).getAttribute("name");
            // Now parse the Value tags. If there are not 'Value' tags, ignore
            // this key
            // TODO: More error handling required later for zero 'Value' tags.
            for (int m = 0; m < numLeafNodes; m++) {
                Node valueNode = leafNodeList.item(m);
                if ((valueNode.getNodeType() != Node.ELEMENT_NODE) || !valueNode.getNodeName().equals("Value")) {
                    // TODO: Error handling required here
                    continue;
                }
                if (values == null) {
                    values = new HashSet();
                }
                Node fchild = (Text) valueNode.getFirstChild();
                if (fchild != null) {
                    String value = fchild.getNodeValue();
                    if (value != null) {
                        values.add(value.trim());
                    }
                }
            }
            if (values == null) {
                // No 'Value' tags found. So ignore this key.
                // TODO: More error handling required later for zero
                // 'Value'tags.
                continue;
            }
            if (map == null) {
                map = new HashMap();
            }
            Set oldValues = (Set) map.get(key);
            if (oldValues != null)
                values.addAll(oldValues);
            map.put(key, values);

            // now reset values to prepare for the next AV pair.
            values = null;
        }
        if (map == null) {
            return EMPTY_MAP;
        } else {
            return map;
        }

    }

    public static Set getChildNodes(Node parentNode, String childName) {
        Set retVal = new HashSet();
        NodeList children = parentNode.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node node = children.item(i);
            if (node.getNodeName().equalsIgnoreCase(childName)) {
                retVal.add(node);
            }
        }
        return (retVal);
    }
}