Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Manchester Centre for Integrative Systems Biology
 * University of Manchester
 * Manchester M1 7ND
 * United Kingdom
 * 
 * Copyright (C) 2007 University of Manchester
 * 
 * This program is released under the Academic Free License ("AFL") v3.0.
 * (http://www.opensource.org/licenses/academic.php)
 *******************************************************************************/

import java.util.*;

import org.w3c.dom.*;
import org.w3c.dom.Element;

public class Main {
    /**
     * 
     */
    public static final int ALL_VALUES = 2;

    /**
     * 
     * @param element
     * @param identifyingProperties
     * @return Map
     */
    public static Map<String, String> getResult(final Element element,
            final Collection<String> identifyingProperties) {
        // for each identifying property get that property value for this
        // document
        final String SEPARATOR = "; "; //$NON-NLS-1$
        final Map<String, String> result = new TreeMap<>();

        for (Iterator<String> identifyingPropertiesIterator = identifyingProperties
                .iterator(); identifyingPropertiesIterator.hasNext();) {
            final String identifyingProperty = identifyingPropertiesIterator.next();
            final List<String> newValues = getDOMElementTextByTagName(element, identifyingProperty, ALL_VALUES);
            final String existingValue = result.get(identifyingProperty);

            if (existingValue != null) {
                newValues.add(existingValue);
            }

            final StringBuffer buffer = new StringBuffer();

            boolean needsSeperator = false;

            for (Iterator<String> newValuesIterator = newValues.iterator(); newValuesIterator.hasNext();) {
                if (needsSeperator) {
                    buffer.append(SEPARATOR);
                }

                final String value = newValuesIterator.next();

                if (value != null) {
                    buffer.append(value);
                }

                needsSeperator = true;
            }

            result.put(identifyingProperty, buffer.toString());
        }

        return result;
    }

    /**
     * 
     * @param element
     * @param tagname
     * @param valueRange
     * @return List
     */
    public static List<String> getDOMElementTextByTagName(final Element element, String tagname,
            final int valueRange) {
        final List<String> list = new ArrayList<>();

        if (element != null) {
            // Just to ensure we pick off by the name of the child node...
            final NodeList nodeList = element.getElementsByTagName(tagname.substring(tagname.lastIndexOf("/") + 1)); //$NON-NLS-1$

            if (nodeList.getLength() == 0) {
                list.add(null);
            }

            for (int i = 0; i < ((valueRange == ALL_VALUES) ? nodeList.getLength() : 1); i++) {
                final Node node = nodeList.item(i);

                if (node != null) {
                    final Node text = node.getFirstChild();
                    list.add(text.getNodeValue());
                }
            }
        } else {
            list.add(null);
        }

        return list;
    }
}