Package com.nexirius.tools

Source Code of com.nexirius.tools.ReplaceHeader

//{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;

import java.io.File;
import java.util.Date;

public class ReplaceHeader {



    public static final String CLASSNAME = "$CLASSNAME";
    public static final String DATE = "$DATE";
    public static final String NOW = "$NOW";
    public static final String VERSION = "$VERSION";
    public static final String BEGIN_HEADER = "//{HEADER";
    public static final String END_HEADER = "//}HEADER";
    public static final String VERSION_MEMBER = "\tpublic static final String _NEXIRIUS_VERSION = ";
    XFile m_root;
    StringVector m_files;

    public int go(String dirname, String header, String version, String prepend)
            throws Exception {
        String now = new Date().toString();
        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 (fname.endsWith(".java")) {
                int replacements = 0;

                System.out.println("JAVA FILE: " + dirname + java.io.File.separator + fname);
                ++fcount;
                XFile file = new XFile(dirname, fname);
                long mod = file.lastModified() + 1000L;
                StringVector sv = file.getTextLines();
                StringVector svout = new StringVector();
                XString xheader = new XString(header);
                XString className = new XString(prepend + fname.substring(0, fname.length() - 5));

                className.replace(File.separator, ".");

                String modificationDateString = new Date(mod).toString();
                xheader.replace(DATE, modificationDateString);
                xheader.replace(NOW, now);
                xheader.replace(VERSION, version);
                xheader.replace(CLASSNAME, className.toString());
                boolean removing = false;

                svout.append(BEGIN_HEADER);
                svout.append(xheader.toString());
                svout.append(END_HEADER);
                boolean needVersion = false;
                boolean needVersionNow = false;
                boolean versionInserted = false;

                for (String s = sv.firstItem(); s != null; s = sv.nextItem()) {
                    if (removing) {
                        if (s.startsWith(END_HEADER)) {
                            removing = false;
                        }
                    } else {
                        if (s.startsWith(BEGIN_HEADER)) {
                            ++replacements;
                            removing = true;
                        } else {
                            if (needVersionNow) {
                                String vstring = "";
                                if (s.indexOf(VERSION_MEMBER) >= 0) {
                                    s = vstring;
                                } else {
                                    svout.append(vstring);
                                }

                                needVersion = false;
                                needVersionNow = false;
                                versionInserted = true;
                            }

                            if (!versionInserted && s.indexOf("public class ") >= 0) {
                                needVersion = true;
                            }

                            if (needVersion) {
                                if (s.indexOf("{") >= 0) {
                                    needVersionNow = true;
                                }
                            }

                            svout.append(s);
                        }
                    }
                }

                if (replacements > 0) {
                    ++ret;
                }

                System.out.println("WRITING (" + replacements + ") : " + fname + " [" + modificationDateString + "]");
                file.writeTextLines(svout);

                file.setLastModified(mod);
            } else {
                //System.out.println(fname);
            }
        }

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

        return ret;
    }

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

        if (argv.length < 3) {
            System.err.println("USAGE: java ReplaceHeader Directory HeaderFileName version prepend");

            return;
        }

        String headerFileName = argv[1];
        XFile headerFile = new XFile(headerFileName);

        if (!headerFile.canRead()) {
            System.err.println("Cannot read file: " + headerFileName);

            return;
        }

        String prepend = "";

        if (argv.length >= 4) {
            prepend = argv[3];
        }

        try {
            r.go(argv[0], new String(headerFile.getBytes()), argv[2], prepend);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
TOP

Related Classes of com.nexirius.tools.ReplaceHeader

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.