Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 Copyright (C) 2010 Haowen Ning
    
 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 program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    
 */

import java.util.ArrayList;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static List<String> findFileInCardText(String cardText, String[] fileExtensions) {

        List<String> filesFound = new ArrayList<String>();
        if (fileExtensions == null || fileExtensions.length == 0) {
            assert false : "fileExtensions should never be empty or null";
            return filesFound;
        }

        StringBuilder extensionPatternBuilder = new StringBuilder();

        // File name pattern
        extensionPatternBuilder.append("[A-Za-z0-9_-]+");

        // extension pattern
        extensionPatternBuilder.append("\\.(");
        for (int i = 0; i < fileExtensions.length; i++) {
            // The format is ext1|ext2|ext3 so the first occurance
            // does not have a |.
            if (i == 0) {
                extensionPatternBuilder.append(fileExtensions[i]);
            } else {
                extensionPatternBuilder.append("|" + fileExtensions[i]);
            }
        }
        extensionPatternBuilder.append(")");

        // The regex here should match the file types in SUPPORTED_AUDIO_FILE_TYPE
        Pattern p = Pattern.compile(extensionPatternBuilder.toString());
        Matcher m = p.matcher(cardText);
        while (m.find()) {
            filesFound.add(m.group());
        }
        return filesFound;
    }
}