Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Created on 16/07/2004
 * YAWLEditor v1.01 
 *
 * @author Lindsay Bradford
 * 
 * 
 *
 * This library 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 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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
 */

import java.io.File;

public class Main {
    /**
     * Takes a full file path string and returns a valid XML Name equivalent
     * via the {@link #toValidXMLName(String)} method.
     * @param fullFilePath as a String
     * @return A valid XML name conversion of that file name.
     */
    public static String fileNameToURI(String fullFilePath) {
        if (fullFilePath == null || fullFilePath.trim().equals("")) {
            fullFilePath = "";
        }
        File theFile = new File(fullFilePath);
        return toValidXMLName(theFile.getName());
    }

    /**
     * Returns a variant of the supplied string with all invalid XML name characters
     * removed. A special case is the the space character, which is converted to the '_'
     * character instead.
     * @param name A string that could be used as an XML name.
     * @return A valid XML name equivalent of name.
     */

    public static String toValidXMLName(String name) {

        char[] nameAsCharArray = name.toCharArray();
        char[] newNameAsCharArray = new char[nameAsCharArray.length];

        boolean currentCharacterValid = false;
        int j = 0;

        for (int i = 0; i < nameAsCharArray.length; i++) {
            currentCharacterValid = false;

            if (nameAsCharArray[i] == ' ') {
                nameAsCharArray[i] = '_';
            }

            if (Character.isLetterOrDigit(nameAsCharArray[i])) {
                currentCharacterValid = true;
            }

            if (nameAsCharArray[i] == '_' || nameAsCharArray[i] == '-' || nameAsCharArray[i] == '.') {
                currentCharacterValid = true;
            }

            if (currentCharacterValid) {
                newNameAsCharArray[j] = nameAsCharArray[i];
                j++;
            }
        }

        String validElementName = new String(newNameAsCharArray);
        validElementName = validElementName.trim();

        return validElementName;
    }
}