de.frame4j
Class FuR

java.lang.Object
  extended by de.frame4j.util.App
      extended by de.frame4j.FuR
All Implemented Interfaces:
AttrSettable, TextHelper.MessageComponents, UIInfo, ActionListener, EventListener, DynamicMBean

@MinDoc(copyright="Copyright 2000 - 2004, 2009  A. Weinert",
        author="Albrecht Weinert",
        version="V.64",
        lastModified="15.05.2012",
        lastModifiedBy="A. Weinert",
        usage="start as Java application (-? for help)",
        purpose="find and replace multiple texts in multiple files")
public class FuR
extends App

Find texts in files and replace them.

to do: to be translated — apologises for now.

This application visits text files in a start directory and optionally (option -r) also recursively in subdirectories. All file and directory criteria available for Frame4J applications may be used.

In every file visited all occurrences of old text patterns are searched for and replaced by a new or substitute pattern. The old pattern may be specified as single text (properties "old"), in the sense of:
  replace every occurrence of "<br>" (old) by "<br \>" (new).

On the other hand every text pattern to be replaced can be specified as bracketed by two sequences (old and oldEnd) with arbitrary content in between, in the sense of:
  replace everything between "<!--" (old) abd "-->" (oldEnd) by nothing "" (new).
The regular expression for the example would be ""<!--".*"-->"".

One single replacement task (for every specified file)
  oldText, [oldEnd,] newText
can be specified by start parameters (command line / script). The application can also handle up to 150 replacement orders specified in an extra .properties file. These are worked through per file content sequentially.
The properties
  old, [end,] new|newFile [ignoreCase] [ignoreWS] [keepBraces]
are the given in the form
  old0, [end0,] new0 to (max.) old150, [end150,] new150
governed by Prop's syntax for indexed properties (new150 and new[150] would both be recognised, e.g.).

The meaning hereby is

Remark 1: The newFile/keepBraces feature is heavily used (and sometimes misused) to have "include files" for languages that don't have, like HTML (use) or Java (misuse). See also the method TextHelper.fUr(CharSequence, ..) used, of course, in this application's FileVisitors.

Remark 2: Obviously the application doesn't use regular expressions for text matching. But the bracing and ignore white space features, well used, are a good, robust and performing substitute in most cases. They give enough freedom in white space ignoring languages like HTML, XML and Java.

The real power of this application shows when doing many dozens of replacements in many hundred files, selected by complex criteria, in one step. Voluminous changes and maintenance work on web spaces have been specified in one .properties file and done orders of magnitude faster and more robust than by script programmed nested loops and transformations (already with the predecessor tool).

Besides these big "100 replacements times hundreds of files" employments, this application also does the little every day things. Examples:
java FuR de.a_weinert de.a_weinert -i .\+.java -r
    replaces in all files *.java in the actual tree the text de.a_weinert in any case by the same text in only lower case.

java FuR "<br>" "<br \>" -i -r .\+.html
    replaces in all .html files frumpish breaks by the good ones loved by XML.

java FuR api\ api/ -i -v jdk1.3\docs\aweinertbib\+.html
    replaces in all .html files   api\  by   api/   so repairing a fault done by quite many javaDoc.exe versions while doing relative linking.


Hint 1: The file FuR.properties is an integral part of this application. It's placed in the same directory (usually within the deployment .jar) as FuR.class. The file FuR.properties may be regarded as part of the documentation.


© Copyright 2000 - 2004, 2009   Albrecht Weinert

See Also:
App, AppBase, Prop

Field Summary
 String directory
          Start directory.
protected  String[] fileNs
          Replacement text / substitute file name; (multiple).
protected  boolean[] ignCase
          Ignore case for search text matching (multiple).
 String ignFilesWith
          Exclude text.
 String ignFilesWith2
          Exclude text (number 2).
 boolean ignoreCase
          Ignore case for search text matching.
 boolean ignoreWS
          Ignore white spaces for search text matching.
protected  boolean[] ignWS
          Ignore white spaces search text matching (multiple).
 boolean incFileDate
          Add (only) 1/2 s the last file in case of replacements.
protected  boolean[] keepBrace
          Keep the braces, when replacing text (multiple).
 boolean keepBraces
          Keep the braces, when replacing text.
 boolean keepFileDate
          Keep the file date even after replacements.
protected  long[] newModif
          Modification date for the substitute (multiple).
protected  String[] newT
          Replacement text; substitute (multiple).
 String newText
          Replacement text; substitute.
protected  String[] oldE
          Search text, end (multiple).
 String oldEnd
          Search text (end).
protected  String[] oldT
          Search text (multiple).
 String oldText
          Search text (begin).
 boolean recursion
          Recursively visit subdirectories.
 String types
          File types to regard.
 boolean useNewModif
          Use the modification time of the replacement text or substitute.
 
Fields inherited from class de.frame4j.util.App
ano, appBase, appIO, appStartTime, args, bgColor, err, fullClassName, help, icon, INIT_ERROR, jmxRemoteNote, JOB_DONE_OK, log, LOG_OUT_ERROR, MAIN_THREAD_EXC, mainThread, myClass, myFrame, name, NO_PARS_ERROR, objectName, out, outMode, packName, prop, retCode, runFlag, shortClassName, title, verbose, verbosity
 
Fields inherited from interface de.frame4j.net.AttrSettable
ILLEGAL_TYPE, ILLEGAL_VALUE, NO_ATTRIBUTE, NO_KNOWN_ATTRIBUTE, NO_VALUE, OK
 
Method Summary
 int doIt()
          Working method of FuR.
static void main(String[] args)
          Start method of FuR .
 boolean parsePartial()
          FuR requests only partial start parameter parsing by Prop.
 
Methods inherited from class de.frame4j.util.App
actionPerformed, allowNoPropertiesFile, clientOrder, condHelpLog, connect, errMeld, errMeld, errorExit, errorExit, errorExit, errorText, formMessage, formMessage, getAbout, getAboutText, getActTime, getAppLogger, getAppStartTime, getArgs, getAttribute, getAttributes, getAuthor, getCopyright, getDbLogger, getExecTimeMs, getExecTimeString, getHelp, getHelpText, getIcon, getLanguage, getLogHandler, getMBeanInfo, getMessageComponent, getMessageComponentsLength, getMyFrame, getName, getNameWithVersDate, getOutMode, getProp, getPurpose, getStartTime, getStateString, getTitle, getUsage, getVerbosity, getVerbosityString, getVersDate, getXMLinput, go, go, go, haveIcon, invoke, isDebug, isHelp, isHelpLog, isNormal, isRunFlag, isSilent, isTest, isVerbose, logVerbose, logVerbose, makeDbLogger, makeMenuBar, makeMenuBar, makeStatusTextEnd, makeStatusTextStart, messageFormat, normalExit, performeAction, postDeregister, postRegister, preRegister, queueAction, registerAsMBean, setAboutText, setAttribute, setAttribute, setAttribute, setAttributes, setAuthor, setBgColor, setCodePages, setCopyright, setHelp, setJmxRemoteNote, setLogOut2, setName, setOutMode, setOutMode, setPurpose, setTitle, setUsage, setVerbose, setVerbose, setVerbosity, setVerbosity, setVerbosityString, setVersDate, stop, threeLineEndMsg, toString, twoLineEndMsg, twoLineStartMsg, valueLang, valueLang, wakeMainThread
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

recursion

public boolean recursion
Recursively visit subdirectories.

If true also files in subdirectories are considered and if applicable worked on.

Default: false.


oldText

public String oldText
Search text (begin).

This is the complete search (or old) text or the opening brace for a spot to be replaced.

See Also:
oldEnd

oldEnd

public String oldEnd
Search text (end).

Is oldEnd not null or empty, then oldText and oldEnd form braces to find a spot for replacement. An exemplary use would be two distinct HTML comments to mark a spot for inserting or replacing a new text (block).

Note that the new text(s) may be given as (text) value, also multiline, or as a path to a file. The difference is specified by the name of the (indexed) property: new[32] versus newFile[32]


newText

public String newText
Replacement text; substitute.


useNewModif

public boolean useNewModif
Use the modification time of the replacement text or substitute.

If useNewModif is true and if the replacement text has a modification date (because read from a file) and if the modification date is to be kept (keepFileDate is true)
then the maximum of the file modification dates is used as the (new) modification date.

Note: this single property useNewModif is used as default for all indexed properties useNewModif[32].
Note 2: These properties have no effect if keepFileDate is false.
Default: true


keepFileDate

public boolean keepFileDate
Keep the file date even after replacements.

default: true

See Also:
incFileDate

incFileDate

public boolean incFileDate
Add (only) 1/2 s the last file in case of replacements.

Like @link keepFileDate, but with a tiny increase of the last file modification date.
The purpose is a sticking to the last otherwise done file modification date, but nevertheless having a little time difference to an unmodified copy.
Some tools like Update might otherwise refrain from from necessary updates.

default: true


ignoreCase

public boolean ignoreCase
Ignore case for search text matching.

If true the matching of the oldText oldEnd pattern is not case sensitive.

Note: this single property ignoreCase is used as default for all indexed properties ignoreCase[32].
default: false.


ignoreWS

public boolean ignoreWS
Ignore white spaces for search text matching.

If true the matching of the oldText oldEnd pattern ignores embedded white spaces in the pattern and the original text.

Note: this single property ignoreWS is used as default for all indexed properties ignoreWS[32].
default: false.


oldT

protected String[] oldT
Search text (multiple).

See Also:
oldText

oldE

protected String[] oldE
Search text, end (multiple).

See Also:
oldEnd

ignCase

protected boolean[] ignCase
Ignore case for search text matching (multiple).

See Also:
ignoreCase

ignWS

protected boolean[] ignWS
Ignore white spaces search text matching (multiple).

See Also:
ignoreWS

newT

protected String[] newT
Replacement text; substitute (multiple).


fileNs

protected String[] fileNs
Replacement text / substitute file name; (multiple).


newModif

protected long[] newModif
Modification date for the substitute (multiple).


keepBraces

public boolean keepBraces
Keep the braces, when replacing text.

default: true


keepBrace

protected boolean[] keepBrace
Keep the braces, when replacing text (multiple).


ignFilesWith

public String ignFilesWith
Exclude text.

Is ignFilesWith not null or empty, files containing this text as is will not be touched (even if matching the visitor's file and directory criteria).

If this stopper text is inserted by one run of FuR that's the procedure to inhibit multiple works on the same file.

default: null


ignFilesWith2

public String ignFilesWith2
Exclude text (number 2).

Same effect as ignFilesWith. (Both excluding conditions are ORed.)


directory

public String directory
Start directory.


types

public String types
File types to regard.

Method Detail

parsePartial

public boolean parsePartial()
FuR requests only partial start parameter parsing by Prop.

Overrides:
parsePartial in class App

main

public static void main(String[] args)
Start method of FuR .

Return code:
  Exit = 0, if run successfully,
  Exit >0 error(s) while running,
  Exit >90 parameter or start errors; nothing done.

Parameters:
args - Command line start parameters

doIt

public int doIt()
Working method of FuR.

Specified by:
doIt in class App
See Also:
Prop, App.go(String[], boolean), App.go(String[], String, boolean, CharSequence), App.mainThread