@ -61,7 +61,8 @@ public class WikiCode extends AbstractWikiParser implements WikiParser {
EMPHASIZE_2 ( "\'\'\'" , "<b>" , "</b>" ) ,
EMPHASIZE_2 ( "\'\'\'" , "<b>" , "</b>" ) ,
EMPHASIZE_3 ( "\'\'\'\'\'" , "<b><i>" , "</i></b>" ) ,
EMPHASIZE_3 ( "\'\'\'\'\'" , "<b><i>" , "</i></b>" ) ,
STRIKE ( "<s>" , "</s>" , "<span class=\"strike\">" , "</span>" ) ;
STRIKE ( "<s>" , "</s>" , "<span class=\"strike\">" , "</span>" ) ,
UNDERLINE ( "<u>" , "</u>" , "<span class=\"underline\">" , "</span>" ) ;
final String openHTML ;
final String openHTML ;
final String closeHTML ;
final String closeHTML ;
@ -129,11 +130,13 @@ public class WikiCode extends AbstractWikiParser implements WikiParser {
private static final int LEN_WIKI_CLOSE_PRE_ESCAPED = WIKI_CLOSE_PRE_ESCAPED . length ( ) ;
private static final int LEN_WIKI_CLOSE_PRE_ESCAPED = WIKI_CLOSE_PRE_ESCAPED . length ( ) ;
private static final int LEN_WIKI_OPEN_PRE_ESCAPED = WIKI_OPEN_PRE_ESCAPED . length ( ) ;
private static final int LEN_WIKI_OPEN_PRE_ESCAPED = WIKI_OPEN_PRE_ESCAPED . length ( ) ;
private static final int LEN_WIKI_OPEN_LINK = WIKI_OPEN_LINK . length ( ) ;
private static final int LEN_WIKI_OPEN_LINK = WIKI_OPEN_LINK . length ( ) ;
private static final int LEN_WIKI_CLOSE_LINK = WIKI_CLOSE_LINK . length ( ) ;
private static final int LEN_WIKI_IMAGE = WIKI_IMAGE . length ( ) ;
private static final int LEN_WIKI_IMAGE = WIKI_IMAGE . length ( ) ;
private static final int LEN_WIKI_OPEN_EXTERNAL_LINK = WIKI_OPEN_EXTERNAL_LINK . length ( ) ;
private static final int LEN_WIKI_OPEN_EXTERNAL_LINK = WIKI_OPEN_EXTERNAL_LINK . length ( ) ;
private static final int LEN_WIKI_CLOSE_EXTERNAL_LINK = WIKI_CLOSE_EXTERNAL_LINK . length ( ) ;
private static final int LEN_WIKI_CLOSE_EXTERNAL_LINK = WIKI_CLOSE_EXTERNAL_LINK . length ( ) ;
private static final int LEN_WIKI_HR_LINE = WIKI_HR_LINE . length ( ) ;
private static final int LEN_WIKI_HR_LINE = WIKI_HR_LINE . length ( ) ;
private static final int LEN_PIPE_ESCAPED = PIPE_ESCAPED . length ( ) ;
private static final int LEN_PIPE_ESCAPED = PIPE_ESCAPED . length ( ) ;
private static final int LEN_WIKI_OPEN_METADATA = WIKI_OPEN_METADATA . length ( ) ;
/** List of properties which can be used in tables. */
/** List of properties which can be used in tables. */
private final static String [ ] TABLE_PROPERTIES = { "rowspan" , "colspan" , "vspace" , "hspace" , "cellspacing" , "cellpadding" , "border" } ;
private final static String [ ] TABLE_PROPERTIES = { "rowspan" , "colspan" , "vspace" , "hspace" , "cellspacing" , "cellpadding" , "border" } ;
@ -180,51 +183,10 @@ public class WikiCode extends AbstractWikiParser implements WikiParser {
private final int lenTableEnd = tableEnd . length ( ) ;
private final int lenTableEnd = tableEnd . length ( ) ;
private final int lenAttribDivider = attribDivider . length ( ) ;
private final int lenAttribDivider = attribDivider . length ( ) ;
private void processHeadline ( StringBuilder input , int firstPosition , final Tags tags , int secondPosition , String direlem ) {
//add anchor and create headline
if ( ( direlem = input . substring ( firstPosition + tags . openWikiLength , secondPosition ) ) ! = null ) {
//counting double headlines
int doubles = 0 ;
final Iterator < String > iterator = tableOfContent . iterator ( ) ;
String element ;
while ( iterator . hasNext ( ) ) {
element = iterator . next ( ) ;
// no element with null value should ever be in directory
assert ( element ! = null ) ;
if ( element . substring ( 1 ) . equals ( direlem ) ) {
doubles + + ;
}
}
String anchor = REGEX_NOT_CHAR_NUM_OR_UNDERSCORE_PATTERN . matcher ( SPACE_PATTERN . matcher ( direlem ) . replaceAll ( "_" ) ) . replaceAll ( EMPTY ) ; //replace blanks with underscores and delete everything thats not a regular character, a number or _
//if there are doubles, add underscore and number of doubles plus one
if ( doubles > 0 ) {
anchor = anchor + "_" + ( doubles + 1 ) ;
}
final StringBuilder link = new StringBuilder ( ) ;
link . append ( "<a name=\"" ) ;
link . append ( anchor ) ;
link . append ( "\"></a>" ) ;
link . append ( tags . openHTML ) ;
link . append ( direlem ) ;
link . append ( tags . closeHTML ) ;
input . replace ( firstPosition , secondPosition + tags . closeWikiLength , link . toString ( ) ) ;
/ *
input = input . substring ( 0 , firstPosition ) + "<a name=\"" + anchor + "\"></a>" + tags . openHTML
+ direlem + tags . closeHTML + input . substring ( secondPosition + tags . closeWikiLength ) ;
* /
//add headlines to list of headlines (so TOC can be created)
if ( Arrays . binarySearch ( HEADLINE_TAGS , tags . openWiki ) > = 0 ) {
tableOfContent . add ( ( tags . openWikiLength - 1 ) + direlem ) ;
}
}
}
private enum ListType {
private enum ListType {
ORDERED , UNORDERED ;
ORDERED , UNORDERED ;
}
}
private String orderedListLevel = EMPTY ;
private String orderedListLevel = EMPTY ;
private String unorderedListLevel = EMPTY ;
private String unorderedListLevel = EMPTY ;
private String defListLevel = EMPTY ;
private String defListLevel = EMPTY ;
@ -240,18 +202,11 @@ public class WikiCode extends AbstractWikiParser implements WikiParser {
private boolean processingTable = false ; //needed for tables, because they reach over several lines
private boolean processingTable = false ; //needed for tables, because they reach over several lines
private int preindented = 0 ; //needed for indented <pre>s
private int preindented = 0 ; //needed for indented <pre>s
private final TableOfContent tableOfContent = new TableOfContent ( ) ;
private final TableOfContent tableOfContents = new TableOfContent ( ) ;
/ * *
* Constructor
* @param address
* /
public WikiCode ( ) {
super ( ) ;
}
/ * *
/ * *
* Transforms a text which contains wiki code to HTML fragment .
* Transforms a text which contains wiki code to HTML fragment .
* @param hostport
* @param reader contains the text to be transformed .
* @param reader contains the text to be transformed .
* @param length expected length of text , used to create buffer with right size .
* @param length expected length of text , used to create buffer with right size .
* @return HTML fragment .
* @return HTML fragment .
@ -265,10 +220,53 @@ public class WikiCode extends AbstractWikiParser implements WikiParser {
out . append ( processLineOfWikiCode ( hostport , line ) ) . append ( serverCore . CRLF_STRING ) ;
out . append ( processLineOfWikiCode ( hostport , line ) ) . append ( serverCore . CRLF_STRING ) ;
}
}
out . insert ( 0 , createTableOfContents ( ) ) ;
out . insert ( 0 , createTableOfContents ( ) ) ;
tableOfContent . clear ( ) ;
tableOfContent s . clear ( ) ;
return out . toString ( ) ;
return out . toString ( ) ;
}
}
private void processHeadline (
final StringBuilder input ,
final int firstPosition ,
final Tags tags ,
final int secondPosition ,
String direlem )
{
//add anchor and create headline
if ( ( direlem = input . substring ( firstPosition + tags . openWikiLength , secondPosition ) ) ! = null ) {
//counting double headlines
int doubles = 0 ;
final Iterator < String > iterator = tableOfContents . iterator ( ) ;
String element ;
while ( iterator . hasNext ( ) ) {
element = iterator . next ( ) ;
// no element with null value should ever be in directory
assert ( element ! = null ) ;
if ( element . substring ( 1 ) . equals ( direlem ) ) {
doubles + + ;
}
}
String anchor = REGEX_NOT_CHAR_NUM_OR_UNDERSCORE_PATTERN . matcher ( SPACE_PATTERN . matcher ( direlem ) . replaceAll ( "_" ) ) . replaceAll ( EMPTY ) ; //replace blanks with underscores and delete everything thats not a regular character, a number or _
//if there are doubles, add underscore and number of doubles plus one
if ( doubles > 0 ) {
anchor = anchor + "_" + ( doubles + 1 ) ;
}
final StringBuilder link = new StringBuilder ( ) ;
link . append ( "<a name=\"" ) ;
link . append ( anchor ) ;
link . append ( "\"></a>" ) ;
link . append ( tags . openHTML ) ;
link . append ( direlem ) ;
link . append ( tags . closeHTML ) ;
input . replace ( firstPosition , secondPosition + tags . closeWikiLength , link . toString ( ) ) ;
//add headlines to list of headlines (so TOC can be created)
if ( Arrays . binarySearch ( HEADLINE_TAGS , tags . openWiki ) > = 0 ) {
tableOfContents . add ( ( tags . openWikiLength - 1 ) + direlem ) ;
}
}
}
// contributed by [FB], changes by [MN]
// contributed by [FB], changes by [MN]
/ * *
/ * *
* Processes tags which are connected to tables .
* Processes tags which are connected to tables .
@ -576,10 +574,11 @@ public class WikiCode extends AbstractWikiParser implements WikiParser {
/ * *
/ * *
* Processes tags which are connected to links and images .
* Processes tags which are connected to links and images .
* @author [ AS ] , [ MN ]
* @author [ AS ] , [ MN ]
* @param hostport
* @param line line of text to be transformed from wiki code to HTML
* @param line line of text to be transformed from wiki code to HTML
* @return HTML fragment
* @return HTML fragment
* /
* /
private String processLinksAndImages ( String hostport , String line ) {
private String processLinksAndImages ( final String hostport , String line ) {
// create links
// create links
String kl , kv , alt , align ;
String kl , kv , alt , align ;
@ -636,7 +635,7 @@ public class WikiCode extends AbstractWikiParser implements WikiParser {
kl = "http://" + hostport + "/" + kl ;
kl = "http://" + hostport + "/" + kl ;
}
}
line = line . substring ( 0 , positionOfOpeningTag ) + "<img src=\"" + kl + "\"" + align + alt + ">" + line . substring ( positionOfClosingTag + 2 ) ;
line = line . substring ( 0 , positionOfOpeningTag ) + "<img src=\"" + kl + "\"" + align + alt + ">" + line . substring ( positionOfClosingTag + LEN_WIKI_CLOSE_LINK ) ;
}
}
// if it's no image, it might be an internal link
// if it's no image, it might be an internal link
else {
else {
@ -646,7 +645,7 @@ public class WikiCode extends AbstractWikiParser implements WikiParser {
} else {
} else {
kv = kl ;
kv = kl ;
}
}
line = line . substring ( 0 , positionOfOpeningTag ) + "<a class=\"known\" href=\"Wiki.html?page=" + kl + "\">" + kv + "</a>" + line . substring ( positionOfClosingTag + 2 ) ; // oob exception in append() !
line = line . substring ( 0 , positionOfOpeningTag ) + "<a class=\"known\" href=\"Wiki.html?page=" + kl + "\">" + kv + "</a>" + line . substring ( positionOfClosingTag + LEN_WIKI_CLOSE_LINK ) ; // oob exception in append() !
}
}
}
}
@ -679,6 +678,7 @@ public class WikiCode extends AbstractWikiParser implements WikiParser {
/ * *
/ * *
* Processes tags which are connected preformatted text ( & lt ; pre & gt ; & lt ; / pre & gt ; ) .
* Processes tags which are connected preformatted text ( & lt ; pre & gt ; & lt ; / pre & gt ; ) .
* @param hostport
* @param line line of text to be transformed from wiki code to HTML
* @param line line of text to be transformed from wiki code to HTML
* @return HTML fragment
* @return HTML fragment
* /
* /
@ -767,23 +767,23 @@ public class WikiCode extends AbstractWikiParser implements WikiParser {
int level6 = 0 ;
int level6 = 0 ;
int doubles = 0 ;
int doubles = 0 ;
String anchorext = EMPTY ;
String anchorext = EMPTY ;
if ( ( s = tableOfContent . size ( ) ) > 2 ) {
if ( ( s = tableOfContent s . size ( ) ) > 2 ) {
directory . append ( "<table><tr><td><div class=\"WikiTOCBox\">\n" ) ;
directory . append ( "<table><tr><td><div class=\"WikiTOCBox\">\n" ) ;
for ( int i = 0 ; i < s ; i + + ) {
for ( int i = 0 ; i < s ; i + + ) {
if ( i > = tableOfContent . size ( ) ) {
if ( i > = tableOfContent s . size ( ) ) {
break ;
break ;
}
}
element = tableOfContent . get ( i ) ;
element = tableOfContent s . get ( i ) ;
if ( element = = null ) {
if ( element = = null ) {
continue ;
continue ;
}
}
//counting double headlines
//counting double headlines
doubles = 0 ;
doubles = 0 ;
for ( int j = 0 ; j < i ; j + + ) {
for ( int j = 0 ; j < i ; j + + ) {
if ( j > = tableOfContent . size ( ) ) {
if ( j > = tableOfContent s . size ( ) ) {
break ;
break ;
}
}
final String d = tableOfContent . get ( j ) ;
final String d = tableOfContent s . get ( j ) ;
if ( d = = null | | d . isEmpty ( ) ) {
if ( d = = null | | d . isEmpty ( ) ) {
continue ;
continue ;
}
}
@ -925,10 +925,9 @@ public class WikiCode extends AbstractWikiParser implements WikiParser {
if ( Arrays . binarySearch ( HEADLINE_TAGS , tags . openWiki ) > = 0 ) {
if ( Arrays . binarySearch ( HEADLINE_TAGS , tags . openWiki ) > = 0 ) {
processHeadline ( stringBuilder , firstPosition , tags , secondPosition , direlem ) ;
processHeadline ( stringBuilder , firstPosition , tags , secondPosition , direlem ) ;
} else {
} else {
int delta = stringBuilder . length ( ) ;
int oldLength = stringBuilder . length ( ) ;
stringBuilder . replace ( firstPosition , firstPosition + tags . openWikiLength , tags . openHTML ) ;
stringBuilder . replace ( firstPosition , firstPosition + tags . openWikiLength , tags . openHTML ) ;
delta = ( delta - stringBuilder . length ( ) ) * - 1 ;
secondPosition + = stringBuilder . length ( ) - oldLength ;
secondPosition + = delta ;
stringBuilder . replace ( secondPosition , secondPosition + tags . closeWikiLength , tags . closeHTML ) ;
stringBuilder . replace ( secondPosition , secondPosition + tags . closeWikiLength , tags . closeHTML ) ;
}
}
}
}
@ -937,10 +936,11 @@ public class WikiCode extends AbstractWikiParser implements WikiParser {
}
}
/ * * Replaces wiki tags with HTML tags in one line of text .
/ * * Replaces wiki tags with HTML tags in one line of text .
* @param hostport
* @param line line of text to be transformed from wiki code to HTML
* @param line line of text to be transformed from wiki code to HTML
* @return HTML fragment
* @return HTML fragment
* /
* /
p ublic String processLineOfWikiCode ( String hostport , String line ) {
p rivate String processLineOfWikiCode ( final String hostport , String line ) {
//If HTML has not been replaced yet (can happen if method gets called in recursion), replace now!
//If HTML has not been replaced yet (can happen if method gets called in recursion), replace now!
line = processMetadata ( line ) ;
line = processMetadata ( line ) ;
if ( ( ! replacedHtmlAlready | | preformattedSpanning ) & & line . indexOf ( WIKI_CLOSE_PRE_ESCAPED ) < 0 ) {
if ( ( ! replacedHtmlAlready | | preformattedSpanning ) & & line . indexOf ( WIKI_CLOSE_PRE_ESCAPED ) < 0 ) {
@ -990,7 +990,7 @@ public class WikiCode extends AbstractWikiParser implements WikiParser {
line = tagReplace ( line , Tags . EMPHASIZE_1 ) ;
line = tagReplace ( line , Tags . EMPHASIZE_1 ) ;
line = tagReplace ( line , Tags . STRIKE ) ;
line = tagReplace ( line , Tags . STRIKE ) ;
line = tagReplace ( line , Tags . UNDERLINE ) ;
line = processUnorderedList ( line ) ;
line = processUnorderedList ( line ) ;
line = processOrderedList ( line ) ;
line = processOrderedList ( line ) ;
@ -1010,11 +1010,11 @@ public class WikiCode extends AbstractWikiParser implements WikiParser {
}
}
p ublic String processMetadata ( String line ) {
p rivate String processMetadata ( String line ) {
int p , q , s = 0 ;
int p , q , s = 0 ;
while ( ( p = line . indexOf ( WIKI_OPEN_METADATA , s ) ) > = 0 & & ( q = line . indexOf ( WIKI_CLOSE_METADATA , p + 1 ) ) > = 0 ) {
while ( ( p = line . indexOf ( WIKI_OPEN_METADATA , s ) ) > = 0 & & ( q = line . indexOf ( WIKI_CLOSE_METADATA , p + 1 ) ) > = 0 ) {
s = q ; // continue with next position
s = q ; // continue with next position
String a = line . substring ( p + 2 , q ) ;
String a = line . substring ( p + LEN_WIKI_OPEN_METADATA , q ) ;
if ( a . toLowerCase ( ) . startsWith ( "coordinate" ) ) {
if ( a . toLowerCase ( ) . startsWith ( "coordinate" ) ) {
// parse Geographical Coordinates as described in
// parse Geographical Coordinates as described in
// http://en.wikipedia.org/wiki/Wikipedia:Manual_of_Style_%28dates_and_numbers%29#Geographical_coordinates
// http://en.wikipedia.org/wiki/Wikipedia:Manual_of_Style_%28dates_and_numbers%29#Geographical_coordinates