Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 *  $Id$
 *
 *  This code is derived from public domain sources. Commercial use is allowed.
 *  However, all rights remain permanently assigned to the public domain.
 *
 *  XMLUtils.java : A class of static XML utility methods.
 *
 *  Copyright (C) 2009, 2010  Scott Herman
 *
 *  This is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This code is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this code.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

public class Main {
    /**
     * Returns the name of the first XML tag in the argument string.
     * @param inStr The argument String to search.
     * @return The name of the first XML tag in the argument string,
     * or null if the operation fails.
     */
    public static String firstTag(String inStr) {
        int startIndex = inStr.indexOf("<");
        if (startIndex < 0)
            return null;

        // skip comments
        while (inStr.substring(startIndex).equals("<!--")) {
            inStr = inStr.substring(startIndex + 4);
            startIndex = inStr.indexOf("-->");
            if (startIndex < 0)
                return null;

            startIndex = inStr.indexOf("<");
            if (startIndex < 0)
                return null;
        } // while

        int tagOffset = 1;
        while (Character.isWhitespace(inStr.charAt(startIndex + tagOffset))) {
            ++tagOffset;
        } // while

        int tokenLen = 0;
        if (isStartChar(inStr.charAt(startIndex + tagOffset))) {
            ++tokenLen;
            while (isNameChar(inStr.charAt(startIndex + tagOffset + tokenLen))) {
                ++tokenLen;
            } // while
        } // if

        char endChar = inStr.charAt(startIndex + tagOffset + tokenLen);
        if (!(Character.isWhitespace(endChar) || (endChar == '>'))) {
            return null;
        } // if

        return inStr.substring(startIndex + tagOffset, startIndex + tagOffset + tokenLen);
    }

    /**
     * NameStartChar ::=   ":" | [A-Z] | "_" | [a-z]
     * @param charV
     * @return
     */
    private static boolean isStartChar(char charV) {
        if (Character.isLetter(charV))
            return true;
        if (charV == ':')
            return true;
        if (charV == '_')
            return true;
        return false;
    }

    /**
     * NameChar ::= NameStartChar | "-" | "." | [0-9]
     * @param charV
     * @return
     */
    private static boolean isNameChar(char charV) {
        if (isStartChar(charV))
            return true;
        if (Character.isDigit(charV))
            return true;
        if (charV == '-')
            return true;
        if (charV == '.')
            return true;
        return false;
    }
}