Parent directory
acceder au document
/*
* LICENCE GPL 2.
*
*/

package loremIpsum;

/**
* Simple lorem ipsum text generator.
*
* <p>
* Suitable for creating sample data for test cases and performance tests.
* </p>
*
* @author peperillon, after a work from Sven Jacobs
* @version 1.1
*
*/

public class LoremIpsum
{
public static final String LATIN_CONNECTOR = "cum et sed et si et sed et non qui quod sitque nunc , et quoque";
public static final String LATIN_WORD = "cum omnis res imperatore ac delegata me ab intentiorem exigat curam seu naturalis sollicitudo seu fides sedula non ad diligentiam in modo verum amorem commissae rei instigent res lorem mihi ab Nerva Augusto nescio lorem diligentiore an amantiore rei publicae imperatore aquarum Caesar iniunctum officium ad usum tum ad salubritatem atque etiam securitatem urbis pertinens administratum per principes semper civitatis nostrae viros ipsum primum potissimum Matrem existimo sicut ceteris negotiis institueram nosse suscepi";
public static final String LOREM_IPSUM = "Lorem ipsum ";
public static final String PUNCTUATION_FINAL = "! . . ? . . ...";
public static final String PUNCTUATION_INLINE = ", ; , , : , ,";
private static int typicalParaLen = 8;
private static int typicalPropLen = 8;
private static int typicalWordLen = 8; //used to infere memory use.
private String[] finalPunctuations;
protected int grace;
private String[] inlinePunctuations;
private String[] latinConnectors;
private String[] latinWords;
protected int wordsAskQuantity;
protected int wordsCount;
protected StringBuilder laius;
protected boolean mustUpperCase;

/**
*
*
*/
public LoremIpsum( )
{
this(1000, 0);
}


public LoremIpsum(int qtt)
{
this( (int)( qtt), (int)( qtt*.025 ));
}

public LoremIpsum(int qtt, int grace)
{
this.wordsAskQuantity = qtt;
this.grace = grace;
this.latinWords = LoremIpsum.LATIN_WORD.split( "\\s" );
this.latinConnectors = LoremIpsum.LATIN_CONNECTOR.split("\\s");
this.inlinePunctuations = LoremIpsum.PUNCTUATION_INLINE.split("\\s");
this.finalPunctuations = LoremIpsum.PUNCTUATION_FINAL.split("\\s");
this.laius = new StringBuilder(LoremIpsum.LOREM_IPSUM );
this.laius.ensureCapacity(wordsAskQuantity * (LoremIpsum.typicalWordLen)/2);

this.wordsCount = 2;
this.mustUpperCase = false;
}


/**
*
* @param word
* @return
*/
public static StringBuilder changeFirstLetterUpperCase( StringBuilder word )
{
// System.err.println("> "+word.toString());
word.setCharAt(0, word.substring(0, 1).toUpperCase().charAt(0) ) ;
return word;
}

/**
*
* @param array
* @return
*/
protected int chooseIndex( String[] array )
{
return (int) ( array.length * Math.random());
}

/**
*
* @return int number of needed sentencees
*/
public int chooseParagraphLen()
{
return LoremIpsum.typicalParaLen + (int)( 9 * Math.random() ) - 5 ;
}

/**
*
* @return
*/
protected int choosePropLen()
{
return LoremIpsum.typicalPropLen + ( (int) Math.random() * 5 -3 ) ;
}

/**
*
* @return number of proposition in sentences.
*/
protected int chooseSentenceLen()
{

int max = ( this.wordsAskQuantity - this.wordsCount) / LoremIpsum.typicalPropLen ;

if ( max < 5 )
{
return max+1;
}
else
{
double alea = Math.random();

if ( alea < .2 )
return 1;
else if ( alea < .4 )
return 2;
else if ( alea < .6 )
return 3;
else if ( alea < .9 )
return 4;
else
return 5;
}

}

/**
*
* @return
*/
protected int chooseWordIndex()
{
return chooseIndex( latinWords );
}


/**
*
* @param lastIndex
* @return
*/
protected int chooseWordIndex(int lastIndex)
{
if ( Math.random() > 0.9 )
{
return chooseWordIndex();
}
else
{
return lastIndex++ % latinWords.length;
}
}

/**
* allow to create a paragraph
* @param amount
* @return
*/
public String makeParagraph(int amount)
{
for ( int it = 0 ; it < amount ; it++ )
{
this.makeSentence();
}
return laius.toString();

}

/**
*
* @param isFist
* @return
*/
public String makeSentence()
{
int oldLen = laius.length();
makeWordSequences(chooseSentenceLen());
int newLen = laius.length();
if ( newLen > oldLen )
{
laius.append(this.finalPunctuations[ chooseIndex(this.finalPunctuations) ]);
this.mustUpperCase = true ;
laius.append(' ');
}
return laius.toString();

}

/**
* allow to
* @param grace
* @return
*/
public String makeText()
{
while( this.isntComplete() )
{
this.makeParagraph(this.chooseParagraphLen());
laius.append("\n\n");
}

return laius.toString();
}

/**
* Returns words from the lorem ipsum text.
*
* @param amount Amount of words
* @param startIndex Start index of word to begin with (must be >= 0 and < 50)
* @return int last index used.
* @throws IndexOutOfBoundsException If startIndex is < 0 or > 49
*/
public String makeWords( int amount, int startIndex )
{
int word = startIndex;
String tmp;

for( int i = 0; i < amount; i++, word++ ) {

word = chooseWordIndex(word);
tmp = latinWords[ word ];

if ( this.mustUpperCase )
{
tmp = LoremIpsum.changeFirstLetterUpperCase( new StringBuilder(tmp)).toString();
this.mustUpperCase = false;
}

laius.append( tmp );
this.wordsCount++;

if ( i < amount - 1 ) {
laius.append( ' ' );
}
}

return laius.toString();
}


/**
*
* @param amount of proposition in a sentence
* @return
*/
protected String makeWordSequences(int amount )
{

for ( int it = 0 ; it < amount ; it++ )
{
makeWords( this.choosePropLen(), chooseWordIndex() );

if ( it < amount - 1 )
{
// compute separator for the next
if ( Math.random() < 0.5 )
{
laius.append(' ');
laius.append(this.latinConnectors[ chooseIndex(this.latinConnectors) ]);
this.wordsCount++;
}
else
{
laius.append(this.inlinePunctuations[ chooseIndex(this.inlinePunctuations) ]);
}

laius.append( ' ' );
}

}
return laius.toString();
}



/**
* @return string
*/
public String toString()
{
return this.laius.toString();
}

/**
* print a random txt
*
*/
public void print()
{
System.out.println(this.makeText());
// System.err.println("text length:"+ this.wordsCount+" words");
// System.err.println("len "+laius.length()+ " capacity "+laius.capacity() );
}


public boolean isntComplete()
{
return (( this.wordsAskQuantity - this.wordsCount ) > this.grace );
}

}
acceder au document