Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * File:  xml_utils.java.java
 *
 * Copyright (C) 2002,  Ruth Mikkelson
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 * Contact : Ruth Mikkelson <mikkelsonr@uwstout.edu>
 *           Department of Mathematics, Statistics and Computer Science
 *           University of Wisconsin-Stout
 *           Menomonie, WI 54751, USA
 *
 * This work was supported by the Intense Pulsed Neutron Source Division
 * of Argonne National Laboratory, Argonne, IL 60439-4845, USA.
 *
 * For further information, see <http://www.pns.anl.gov/ISAW/>
 *
 * Modified:
 *
 *  $Log$
 *  Revision 1.10  2004/03/11 23:07:13  dennis
 *  Removed dependency on DataSet Attributes
 *
 *  Revision 1.9  2004/01/22 02:19:05  bouzekc
 *  Removed unused imports and local variables.
 *
 *  Revision 1.8  2003/10/20 16:31:06  rmikk
 *  Fixed javadoc errors
 *
 *  Revision 1.7  2003/06/18 20:35:35  pfpeterson
 *  Changed calls for NxNodeUtils.Showw(Object) to
 *  DataSetTools.util.StringUtil.toString(Object)
 *
 *  Revision 1.6  2003/03/03 16:49:16  pfpeterson
 *  Changed SharedData.status_pane.add(String) to SharedData.addmsg(String)
 *
 *  Revision 1.5  2002/11/27 23:14:07  pfpeterson
 *  standardized header
 *
 *  Revision 1.4  2002/11/12 03:36:24  dennis
 *  Since Attributes are no longer mutable, this can't call setValue()
 *  for Double, Float and Int Attributes (line 421).  This must still be
 *  modified to adapt to immutable Attributes.
 *
 *  Revision 1.3  2002/07/10 19:35:23  rmikk
 *  Change in documentation
 *
 *  Revision 1.2  2002/06/17 22:46:19  rmikk
 *  Prettied  up the file. Made minor changes
 *
 *  Revision 1.1  2002/06/14 21:25:44  rmikk
 *  Common utilities for Implementers of the IXmlIO interface
 *
 */

import java.io.*;
import java.util.*;

public class Main {
    public static String delimiters = " =\n\t\r\f\"";
    public static String errormessage;
    public static char lastchar = 0;
    public static StringBuffer sb = new StringBuffer(2000);

    /**
     * Reads the next xml attribute in a tag.
     * @return  A vector with two elements.  The first element is
     *          the xml attributes key and the second its value as a String
     */
    public static Vector getNextAttribute(InputStream is) {
        errormessage = null;
        Vector Res = new Vector();
        try {
            char c;
            sb.setLength(0);
            if (lastchar == '>')
                return Res;
            else
                c = findChar(is, delimiters, false);

            if (c == '>') {
                lastchar = '>';
                return Res;
            }
            sb.setLength(0);
            sb.append(c);

            getNextWord(is);
            String U = sb.toString();
            sb.setLength(0);

            Res.addElement(U);
            if (lastchar == '=')
                c = lastchar;
            else {
                c = findChar(is, " \n\r\f\t", false);
            }
            if (c != '=') {
                errormessage = "Improperly formed attribute. Need =" + U + "::" + c;
                return null;
            }
            c = findChar(is, " \t\r\n\f", false);

            if (c != '\"') {
                errormessage = "Attribute values must be quoted " + c + U;
                return null;
            }
            sb.setLength(0);
            for (c = (char) is.read(); c != '\"'; c = (char) is.read())
                sb.append(c);
            lastchar = 0;

            Res.addElement(sb.toString());
            //lastchar = c;
            return Res;

        } catch (Exception s) {
            errormessage = s.getMessage();
            return null;
        }

    }

    private static char findChar(InputStream is, String delim, boolean stop) throws IOException {
        char c;
        if (lastchar == '>')
            c = lastchar;
        else
            c = (char) is.read();
        lastchar = 0;
        boolean done = delim.indexOf(c) >= 0;
        if (!stop)
            done = !done;
        while (!done) {
            c = (char) is.read();
            done = delim.indexOf(c) >= 0;
            if (!stop)
                done = !done;
        }
        return c;
    }

    private static String getNextWord(InputStream is) throws IOException {
        char c;
        if (lastchar == '>')
            return null;
        else
            c = (char) is.read();
        lastchar = 0;

        if (delimiters.indexOf(c) >= 0) {
            return sb.toString();
        }
        sb.append(c);
        for (c = (char) is.read(); (delimiters + "=></\"").indexOf(c) < 0; c = (char) is.read())
            sb.append(c);
        lastchar = c;
        if (sb.length() <= 0) {
            errormessage = "No Tag";
            return null;
        }

        return sb.toString();
    }
}