Package com.nexirius.tools

Source Code of com.nexirius.tools.Replacer

//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.tools;

import com.nexirius.util.StringVector;
import com.nexirius.util.XFile;
import com.nexirius.util.XString;

public class Replacer {



    XFile m_root;
    StringVector m_files;
    String m_pattern[];
    String m_replace[];

    public int goCopy(String dirname, String targetDir, String pat[], String rep[], String extension)
            throws Exception {
        if (!dirname.endsWith(XFile.separator)) {
            dirname = dirname + XFile.separator;
        }

        if (!targetDir.endsWith(XFile.separator)) {
            targetDir = targetDir + XFile.separator;
        }

        XFile target = new XFile(targetDir);
        target.mkdirs();

        m_root = new XFile(dirname);
        m_files = m_root.getFiles(true);

        for (String fname = m_files.firstItem(); fname != null; fname = m_files.nextItem()) {
            XFile newfile = new XFile(targetDir, fname);

            newfile.mkdirs();
            newfile.delete();
            newfile.createFrom(new XFile(dirname, fname), false);
        }

        return go(targetDir, pat, rep, extension);
    }

    public int go(String dirname, String pat[], String rep[], String extension)
            throws Exception {
        m_pattern = pat;
        m_replace = rep;
        int ret = 0;
        int fcount = 0;
        m_root = new XFile(dirname);
        m_files = m_root.getFiles(true);

        for (String fname = m_files.firstItem(); fname != null; fname = m_files.nextItem()) {
            if (extension != null && fname.endsWith(extension)) {
                int replacements = 0;

                //System.out.println("JAVA FILE: " + fname);
                ++fcount;
                XFile file = new XFile(dirname, fname);
                StringVector sv = file.getTextLines();
                StringVector svout = new StringVector();

                for (String s = sv.firstItem(); s != null; s = sv.nextItem()) {
                    XString xs = new XString(s);
                    int reps = 0;

                    for (int i = 0; i < m_pattern.length; ++i) {
                        reps += xs.replace(m_pattern[i], m_replace[i]);
                    }

                    if (reps > 0) {
                        replacements += reps;
                        //System.out.println("BEFORE:" + s);
                        //System.out.println("AFTER :" + xs.toString());
                    }

                    svout.append(xs.toString());
                }

                if (replacements > 0) {
                    ++ret;
                    System.out.println("WRITING (" + replacements + ") : " + fname);
                    file.writeTextLines(svout);
                }
            } else {
                //System.out.println(fname);
            }
        }

        System.out.println(ret + " of " + fcount + " files changed.");

        return ret;
    }

    public static void main(String argv[]) {
        Replacer r = new Replacer();

        if (argv.length < 3) {
            System.err.println("OR: java Replacer .extension Directory pat1 rep1 pat2 rep2 ...");
            return;
        }

        String extension = extension = argv[0];
        String dir = argv[1];

        int pn = (argv.length - 2) / 2;
        String pat[] = new String[pn];
        String rep[] = new String[pn];

        for (int i = 0; i < pn; ++i) {
            pat[i] = argv[2 + 2 * i];
            rep[i] = argv[3 + 2 * i];
        }

        try {
            r.go(dir, pat, rep, extension);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
TOP

Related Classes of com.nexirius.tools.Replacer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.