|
From: Alexander G. <gaf...@us...> - 2006-01-16 09:23:37
|
Update of /cvsroot/cobricks/cobricks2/src/org/cobricks/portal/wiki In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11971/src/org/cobricks/portal/wiki Added Files: UtilityTest.java Utility.java Log Message: Wiki functionality added --- NEW FILE: UtilityTest.java --- /* * @author Alexander Gafriller * @version 1.0.0 * Erzeugt am: 14.12.2005 * */ package org.cobricks.portal.wiki; import junit.framework.TestCase; import org.cobricks.portal.wiki.Utility; /** * Erzeugt am: 14.12.2005 * */ public class UtilityTest extends TestCase { public void testParse() { String retValue; String parse; String result; //Single tests //italic parse = "Italic: ''italic''"; result = "Italic: <i>italic</i>"; retValue = Utility.parse(parse); // System.out.println(result); // System.out.println(retValue); assertTrue(result.equals(retValue)); parse = "Italic: ''italic\n"; result = "Italic: <i>italic</i>"; retValue = Utility.parse(parse); assertTrue(result.equals(retValue)); parse = "Italic: ''italic"; result = "Italic: <i>italic</i>"; retValue = Utility.parse(parse); assertTrue(result.equals(retValue)); //Bold parse = "Bold: '''bold'''"; result = "Bold: <b>bold</b>"; retValue = Utility.parse(parse); assertTrue(result.equals(retValue)); parse = "Bold: '''bold\n"; result = "Bold: <b>bold</b>"; retValue = Utility.parse(parse); assertTrue(result.equals(retValue)); parse = "Bold: '''bold"; result = "Bold: <b>bold</b>"; retValue = Utility.parse(parse); assertTrue(result.equals(retValue)); //italic and bold parse = "Italic and bold: '''''italic and bold'''''"; result = "Italic and bold: <i><b>italic and bold</b></i>"; retValue = Utility.parse(parse); assertTrue(result.equals(retValue)); //heading parse = "==heading=="; result = "<h1>heading</h1>"; retValue = Utility.parse(parse); assertTrue(result.equals(retValue)); parse = "==heading\n"; result = "<h1>heading</h1>"; retValue = Utility.parse(parse); assertTrue(result.equals(retValue)); parse = "==heading"; result = "<h1>heading</h1>"; retValue = Utility.parse(parse); assertTrue(result.equals(retValue)); //sub heading parse = "===sub heading==="; result = "<h2>sub heading</h2>"; retValue = Utility.parse(parse); assertTrue(result.equals(retValue)); parse = "===sub heading\n"; result = "<h2>sub heading</h2>"; retValue = Utility.parse(parse); assertTrue(result.equals(retValue)); parse = "===sub heading"; result = "<h2>sub heading</h2>"; retValue = Utility.parse(parse); assertTrue(result.equals(retValue)); //Non numbered list //Only this case does nor exist parse = "** two-one\n" + "** two-two\n" + "** two-three\n"; result = "</li><ul><li> two-one</li><li> two-two</li>" + "<li> two-three</li></ul>\n"; retValue = Utility.parse(parse); assertTrue(result.equals(retValue)); parse = "** two-one\n" + "** two-two\n" + "** two-three"; result = "</li><ul><li> two-one</li><li> two-two</li>" + "<li> two-three</li></ul>\n"; retValue = Utility.parse(parse); assertTrue(result.equals(retValue)); parse = "* one\n" + "* two\n" + "* three\n"; result = "<ul><li> one</li><li> two</li><li> three</li></ul>\n"; retValue = Utility.parse(parse); assertTrue(result.equals(retValue)); parse = "* one\n" + "* two\n" + "* three"; result = "<ul><li> one</li><li> two</li><li> three</li></ul>\n"; retValue = Utility.parse(parse); assertTrue(result.equals(retValue)); //Nested non numbered lists parse = "* one\n" + "* two\n" + "** two-one\n" + "** two-two\n" + "* three\n"; result = "<ul><li> one</li>" + "<li> two</li>" + "<ul><li> two-one</li>" + "<li> two-two</li></ul>" + "<li> three</li></ul>\n"; retValue = Utility.parse(parse); // System.out.println(result); // System.out.println(retValue); assertTrue(result.equals(retValue)); //Nested non numbered lists parse = "* one\n" + "* two\n" + "** two-one\n" + "** two-two\n" + "* three"; result = "<ul><li> one</li>" + "<li> two</li>" + "<ul><li> two-one</li>" + "<li> two-two</li></ul>" + "<li> three</li></ul>\n"; retValue = Utility.parse(parse); // System.out.println(result); // System.out.println(retValue); assertTrue(result.equals(retValue)); //Nested non numbered lists parse = "* one\n" + "* two\n" + "** two-one\n" + "** two-two\n" + "* three\n" + "* four\n" + "** four-one\n" + "** four-two\n" + "** four-three\n" + "* five\n"; result = "<ul><li> one</li>" + "<li> two</li>" + "<ul><li> two-one</li>" + "<li> two-two</li></ul>" + "<li> three</li>" + "<li> four</li>" + "<ul><li> four-one</li>" + "<li> four-two</li>" + "<li> four-three</li></ul>" + "<li> five</li></ul>\n"; retValue = Utility.parse(parse); // System.out.println(result); // System.out.println(retValue); assertTrue(result.equals(retValue)); //Nested non numbered lists parse = "* one\n" + "* two\n" + "** two-one\n" + "** two-two" + "* three\n" + "* four\n" + "** four-one" + "** four-two\n" + "** four-three\n" + "* five"; result = "<ul><li> one</li>" + "<li> two</li>" + "<ul><li> two-one</li>" + "<li> two-two" + "* three</li></ul>" + "<li> four</li>" + "<ul><li> four-one" + "** four-two</li>" + "<li> four-three</li></ul>" + "<li> five</li></ul>\n"; retValue = Utility.parse(parse); // System.out.println(result); // System.out.println(retValue); assertTrue(result.equals(retValue)); //Nested non numbered lists parse = "* one\n" + "* two\n" + "** two-one\n" + "** two-two\n" + "* three\n" + "* four\n" + "** four-one\n" + "** four-two\n" + "** four-three\n" + "** four-four\n"; result = "<ul><li> one</li>" + "<li> two</li>" + "<ul><li> two-one</li>" + "<li> two-two</li></ul>" + "<li> three</li>" + "<li> four</li>" + "<ul><li> four-one</li>" + "<li> four-two</li>" + "<li> four-three</li>" + "<li> four-four</li></ul></ul>\n"; retValue = Utility.parse(parse); // System.out.println(result); // System.out.println(retValue); assertTrue(result.equals(retValue)); //numbered lists //only this case does not exist parse = "## two-one\n" + "## two-two\n" + "## two-three\n"; result = "</li><ol><li> two-one</li><li> two-two</li>" + "<li> two-three</li></ol>\n"; retValue = Utility.parse(parse); // System.out.println(result); // System.out.println(retValue); assertTrue(result.equals(retValue)); parse = "# one\n" + "# two\n" + "# three\n"; result = "<ol><li> one</li><li> two</li><li> three</li></ol>\n"; retValue = Utility.parse(parse); assertTrue(result.equals(retValue)); parse = "# one\n" + "# two\n" + "# three\n"; result = "<ol><li> one</li><li> two</li><li> three</li></ol>\n"; retValue = Utility.parse(parse); assertTrue(result.equals(retValue)); //Nested numbered lists parse = "# one\n" + "# two\n" + "## two-one\n" + "## two-two\n" + "# three\n"; result = "<ol><li> one</li>" + "<li> two</li>" + "<ol><li> two-one</li>" + "<li> two-two</li></ol>" + "<li> three</li></ol>\n"; retValue = Utility.parse(parse); assertTrue(result.equals(retValue)); //Nested numbered lists parse = "# one\n" + "# two\n" + "## two-one\n" + "## two-two\n" + "# three\n" + "# four\n" + "## four-one\n" + "## four-two\n" + "# five\n"; result = "<ol><li> one</li>" + "<li> two</li>" + "<ol><li> two-one</li>" + "<li> two-two</li></ol>" + "<li> three</li>" + "<li> four</li>" + "<ol><li> four-one</li>" + "<li> four-two</li></ol>" + "<li> five</li></ol>\n"; retValue = Utility.parse(parse); //System.out.println(result); //System.out.println(retValue); assertTrue(result.equals(retValue)); //indent parse = "Normal text\n" + ": indented\n" + ":: double indented\n" + ":: double indented 2\n"; result = "Normal text" + "<dl><dd> indented</dd>" + "<dl><dd> double indented</dd>" + "<dd> double indented 2</dd></dl>" + "</dl>\n"; retValue = Utility.parse(parse); //System.out.println(result); //System.out.println(retValue); assertTrue(result.equals(retValue)); //No wiki syntax parse = "Normal '''text''' without sense ==heading== and " + "<nowiki>No '''wiki''' syntax * to # test</nowiki>"; result = "Normal <b>text</b> without sense <h1>heading</h1> and " + "No '''wiki''' syntax * to # test"; retValue = Utility.parse(parse); //System.out.println(result); //System.out.println(retValue); assertTrue(result.equals(retValue)); //Links to a WIKI page parse = "Test the links: " + "A simple link [[FAQ]]"; result = "Test the links: " + "A simple link " + "<a href=\"$portalRequest.getBaseUrl()FAQ.html\">FAQ</a>"; retValue = Utility.parse(parse); //System.out.println(result); //System.out.println(retValue); assertTrue(result.equals(retValue)); parse = "The same link with another name " + "[[FAQ|frequently asked questions]]"; result = "The same link with another name " + "<a href=\"$portalRequest.getBaseUrl()FAQ.html\">" + "frequently asked questions</a>"; retValue = Utility.parse(parse); //System.out.println(result); //System.out.println(retValue); assertTrue(result.equals(retValue)); //Weblinks parse = "Weblinks: " + "[[http://www.google.de/]] - description" + "[[http://www.google.de/|Google]]"; result = "Weblinks: " + "<a href=\"http://www.google.de/\">" + "http://www.google.de/</a> - description" + "<a href=\"http://www.google.de/\">Google</a>"; retValue = Utility.parse(parse); //System.out.println(result); //System.out.println(retValue); assertTrue(result.equals(retValue)); //Imagelink parse = "Imagelink: " + "[[wikibild.jpg|Image]]"; result = "Imagelink: " + "<a href=\"$portalRequest.getBaseUrl()/wikibild.jpg\">" + "Image</a>"; retValue = Utility.parse(parse); //System.out.println(result); //System.out.println(retValue); assertTrue(result.equals(retValue)); //Images parse = "Image: " + "[[:img:wikibild.jpg|Image]]"; result = "Image: " + "<img src=\"$portalRequest.getBaseUrl()/wikibild.jpg\" " + "alt=\"Image\">"; retValue = Utility.parse(parse); //System.out.println(result); //System.out.println(retValue); assertTrue(result.equals(retValue)); } public void testDeleteToken() { String parse, result, retValue; //Delete parse = "This text $portalPresenter.parse(\"" + "$contextPath/pageheader.html\", $portalRequest)should be" + "$portalPresenter.parse(\"" + "$contextPath/pagefooter.html\", $portalRequest) coherent." + "$portalPresenter.parse(\"" + "$contextPath/pageheader.html\", $portalRequest)"; result = "This text should be coherent."; retValue = Utility.deleteToken(parse); //System.out.println(result); //System.out.println(retValue); assertTrue(result.equals(retValue)); } public void testDeleteComments() { String parse, result, retValue; //Delete comments parse = "Delete comments: first <!-- first comment -->" + "'''second''' <!-- comment text -->" + "third text <!-- ''italic'' -->" + "delete open, but new line <!-- open comment\n" + "comment\n" + "and one with file end <!-- file end comment "; result = "Delete comments: first " + "'''second''' " + "third text " + "delete open, but new line \n" + "comment\n" + "and one with file end "; retValue = Utility.deleteComments(parse); //System.out.println(result); //System.out.println(retValue); assertTrue(result.equals(retValue)); } public void testInsertEditLink() { String parse, result, retValue; int itemid = 3; //Insert the edit link parse = "Edit this page: "; result = "Edit this page: " + "<a href=\"portal/edit.html?itemid=3\">Edit</a>"; retValue = Utility.insertEditLink(itemid, parse, parse.length()); //System.out.println(result); //System.out.println(retValue); assertTrue(result.equals(retValue)); } public void testInsertHistoryLink() { String parse, result, retValue, lang = "en"; int itemid = 3; //Insert the edit link parse = "History of the page: "; result = "History of the page: " + "<a href=\"portal/historywiki.html?itemid=3&lang=en\">" + "History</a>"; retValue = Utility.insertHistoryLink(itemid, lang, parse, parse .length()); //System.out.println(result); //System.out.println(retValue); assertTrue(result.equals(retValue)); } } --- NEW FILE: Utility.java --- /* * Copyright (c) 2003-2005 Cobricks Group. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the Cobricks Software * License, either version 1.0 of the License, or (at your option) any * later version (see www.cobricks.org). * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. */ package org.cobricks.portal.wiki; import java.util.Map; import java.util.HashMap; /** * Makes some functionality available to parse the wiki pages. It translate * the wiki-tokens into HTML. * @author gaf...@in... * @version $Date: 2006/01/16 09:23:28 $ * */ public class Utility { //Indicate the end of line in the String private final static String ENDOFLINE = "\n"; //Default ending of a file private final static String DEFAULTENDING = "html"; private final static String ITEMID = "%ITEMID%"; private final static String LANG = "%LANG%"; //Link to edit the page - the itemid must be replaced private final static String EDITLINK = "<a href=\"portal/edit.html?itemid="+ ITEMID+"\">Edit</a>"; //Link to history of the page private final static String HISTORYLINK = "<a href=\"" + "portal/historywiki.html?" + "itemid="+ITEMID+"&lang="+LANG+"\">History</a>"; //Indicate the begin of the text, that will not be changed private final static String BEGINNOWIKITEXT = "<nowiki>"; //Indicate the end of the text, that will not be changed private final static String ENDNOWIKITEXT = "</nowiki>"; //Contains the text, that will not be changed private static Map noWikiTextMap = new HashMap(); // Contains the token, which will searched in the content to exchange private static String[] searchToken = null; // Contains the token, which refer to the whole line private static String[] searchTokenWholeLine = null; // Contains the token, which will be replaced instead of the search token private static Map exchangeTokenMap = null; //Links private final static String BEGINLINK = "[["; private final static String ENDLINK = "]]"; private final static String SEPARATORLINK = "|"; //Image private final static String BEGINIMG = "[[:img:"; private final static String ENDIMG = "]]"; private final static String SEPARATORIMG = "|"; //Comments private final static String BEGINCOMMENT = "<!--"; private final static String ENDCOMMENT = "-->"; //delete this token private static String[] deleteToken = null; /** * Initialize the lists with the strings to search in the parse Sting. */ private static void initialize() { //Text formatting String[] searchTokenTmp = {"'''''", "'''", "''", "===", "=="}; //Token which refer to the whole line String[] searchTokenWholeLineTmp = {"**", "*", "##", "#", "::", ":"}; //Elements which will be exchanged exchangeTokenMap = new HashMap(); //Italic and bold exchangeTokenMap.put("'''''", "<i><b>"); exchangeTokenMap.put("/'''''","</b></i>"); //bold exchangeTokenMap.put("'''", "<b>"); exchangeTokenMap.put("/'''","</b>"); //italic exchangeTokenMap.put("''", "<i>"); exchangeTokenMap.put("/''","</i>"); //sub heading exchangeTokenMap.put("===", "<h2>"); exchangeTokenMap.put("/===","</h2>"); //heading exchangeTokenMap.put("==", "<h1>"); exchangeTokenMap.put("/==","</h1>"); //non numbered lists exchangeTokenMap.put("**list", "<ul>"); exchangeTokenMap.put("/**list","</ul>"); exchangeTokenMap.put("*list", "<ul>"); exchangeTokenMap.put("/*list","</ul>"); exchangeTokenMap.put("**", "<li>"); exchangeTokenMap.put("/**","</li>"); exchangeTokenMap.put("*", "<li>"); exchangeTokenMap.put("/*","</li>"); //numbered lists exchangeTokenMap.put("##list", "<ol>"); exchangeTokenMap.put("/##list","</ol>"); exchangeTokenMap.put("#list", "<ol>"); exchangeTokenMap.put("/#list","</ol>"); exchangeTokenMap.put("##", "<li>"); exchangeTokenMap.put("/##","</li>"); exchangeTokenMap.put("#", "<li>"); exchangeTokenMap.put("/#","</li>"); //indent exchangeTokenMap.put("::list", "<dl>"); exchangeTokenMap.put("/::list","</dl>"); exchangeTokenMap.put(":list", "<dl>"); exchangeTokenMap.put("/:list","</dl>"); exchangeTokenMap.put("::", "<dd>"); exchangeTokenMap.put("/::","</dd>"); exchangeTokenMap.put(":", "<dd>"); exchangeTokenMap.put("/:","</dd>"); searchToken = searchTokenTmp; searchTokenWholeLine = searchTokenWholeLineTmp; } /** * Parse the given String. All wiki elements will be exchanged with * HTML tags. * @param toParse * @return The parsed String */ public static String parse(String toParse) { if (toParse == null) return null; String tmpParse = toParse; /* ATENTION: The Methods must be called in the right sequence!! * 1. initialize() * 2. deleteNowikiElements(String) * 3. parseSearchToken(tmpParse) * 4. parseSearchTokenWholeLine(tmpParse) * 5. parseImages(tmpParse) * 6. parseLinks(tmpParse) * 7. insertNowikiElements(tmpParse) * * */ //initialize the search tokens initialize(); /* delete the elementes, which should not be parsed -> the elements must * be inserted with the method insertNowikiElements(String) */ tmpParse = deleteNowikiElements(tmpParse); //parse the common elements tmpParse = parseSearchToken(tmpParse); //parse the elementes which affect the whole line tmpParse = parseSearchTokenWholeLine(tmpParse); //parse the images tmpParse = parseImages(tmpParse); //parse the links tmpParse = parseLinks(tmpParse); /* * Insert the elements, which should not be parsed -> this elements must * be deleted with the method deleteNowikiElements(String) */ tmpParse = insertNowikiElements(tmpParse); return tmpParse; } /** * Delete all comments in the content. * @param toParse To search through * @return The parsed content without all comments. */ public static String deleteComments(String toParse) { StringBuffer tmpToParse = new StringBuffer(toParse); int position = tmpToParse.indexOf(BEGINCOMMENT); int lastExtractPosition; while(position > -1) { lastExtractPosition = tmpToParse.indexOf(ENDCOMMENT, position+1); if(lastExtractPosition > -1) { lastExtractPosition += ENDCOMMENT.length(); } else { //if the comment is open -> search a end of line if(lastExtractPosition < 0) { lastExtractPosition = tmpToParse.indexOf(ENDOFLINE, position+1); } //if nothing is in the content -> use the end of file if(lastExtractPosition < 0) { lastExtractPosition = tmpToParse.length(); } } tmpToParse = tmpToParse.delete(position, lastExtractPosition); position = tmpToParse.indexOf(BEGINCOMMENT, position+1); } return tmpToParse.toString(); } /** * Replace the elements which are stored in the array 'searchToken', * with the adequate HTML tags. * @param toParse String to parse * @return The parsed String or null. */ private static String parseSearchToken(String toParse) { if(toParse == null || searchToken == null || exchangeTokenMap == null) return null; StringBuffer tmpStringBuffer = new StringBuffer(toParse); String tmpToken = null, tmpEndToken = null; int position = -1; int endPosition = -1; for(int i = 0; i < searchToken.length; i++) { tmpToken = searchToken[i]; tmpEndToken = "/"+tmpToken; //find first position of the token position = tmpStringBuffer.indexOf(tmpToken); while(position > -1) { //replace the begin token tmpStringBuffer = tmpStringBuffer.replace( position, position+tmpToken.length(), (String)exchangeTokenMap.get(tmpToken)); //replace the end token endPosition = tmpStringBuffer.indexOf(tmpToken, position); if(endPosition > -1) { tmpStringBuffer = tmpStringBuffer.replace( endPosition, endPosition+tmpToken.length(), (String)exchangeTokenMap.get(tmpEndToken)); } else { //If there is no endtoken endPosition = tmpStringBuffer.indexOf(ENDOFLINE, position); if(endPosition > -1) { tmpStringBuffer = tmpStringBuffer.replace( endPosition, endPosition+ENDOFLINE.length(), (String)exchangeTokenMap.get(tmpEndToken)); } else { tmpStringBuffer = tmpStringBuffer.insert( tmpStringBuffer.length(), (String)exchangeTokenMap.get(tmpEndToken)); } } //Look up for a other occurence position = tmpStringBuffer.indexOf(tmpToken, position+1); } } return tmpStringBuffer.toString(); } /** * Replace the elements which are stored in the array * 'searchTokenWholeLine', with the adequate HTML tags. * @param toParse String to parse. * @return The parsed String or null. */ private static String parseSearchTokenWholeLine(String toParse) { if (toParse == null || searchTokenWholeLine == null || exchangeTokenMap == null) return null; //StringBuffer to make the changes StringBuffer tmpToParse = new StringBuffer(toParse); //nested elements boolean openedElement = false; String tmpToken = null, tmpEndToken = null; int position = -1; //The string which will be inserted String insertToken; for(int i = 0; i < searchTokenWholeLine.length; i++) { openedElement = false; tmpToken = searchTokenWholeLine[i]; tmpEndToken = "/"+tmpToken; //first position is a special case position = tmpToParse.indexOf(tmpToken); if (position == 0) { //open a nested element openedElement = true; if(tmpToken.length() > 1) { insertToken = ""+exchangeTokenMap.get( tmpEndToken.substring(0,2))+ exchangeTokenMap.get(tmpToken+"list")+ exchangeTokenMap.get(tmpToken); } else { insertToken = (String)exchangeTokenMap.get(tmpToken+"list")+ exchangeTokenMap.get(tmpToken); } tmpToParse = tmpToParse.replace(0, position+tmpToken.length(), insertToken); /* replace the end token at the end of line and check if * the nested element must be closed */ if(hasNextLineElement(tmpToParse.toString(), position+1, tmpToken, ENDOFLINE)) { insertToken = exchangeTokenMap.get(tmpEndToken)+ENDOFLINE; } else { insertToken = (String)exchangeTokenMap.get(tmpEndToken)+ exchangeTokenMap.get(tmpEndToken+"list") + ENDOFLINE; openedElement = false; } tmpToParse = tmpToParse.replace( tmpToParse.indexOf(ENDOFLINE), tmpToParse.indexOf(ENDOFLINE)+ENDOFLINE.length(), insertToken); } position = tmpToParse.indexOf(ENDOFLINE+tmpToken); while (position > -1) { if(!openedElement) { openedElement = true; if(tmpToken.length() > 1) { insertToken = (String)exchangeTokenMap.get( tmpEndToken.substring(0,2))+ exchangeTokenMap.get(tmpToken+"list")+ exchangeTokenMap.get(tmpToken); } else { insertToken = (String)exchangeTokenMap.get( tmpToken+"list")+ exchangeTokenMap.get(tmpToken); } } else { //replace the begin token insertToken = (String)exchangeTokenMap.get(tmpToken); } tmpToParse = tmpToParse.replace( tmpToParse.indexOf(ENDOFLINE+tmpToken,position), tmpToParse.indexOf(ENDOFLINE+tmpToken,position)+ ENDOFLINE.length()+tmpToken.length(), insertToken); /* replace the end token at the end of line and check if * the nested element must be closed */ if(hasNextLineElement(tmpToParse.toString(), position+1, tmpToken, ENDOFLINE)) { //Test if token is already closed int tmpEndTokenIndex = tmpToParse.indexOf( (String)exchangeTokenMap.get(tmpEndToken+"list"), position+insertToken.length()); if((tmpEndTokenIndex > -1) && (tmpEndTokenIndex < tmpToParse.indexOf( ENDOFLINE,position+insertToken.length()))) { insertToken = ENDOFLINE; } else { insertToken = (String)exchangeTokenMap.get(tmpEndToken)+ ENDOFLINE; } } else { int tmpEndTokenIndex = tmpToParse.indexOf( (String)exchangeTokenMap.get(tmpEndToken+"list"), position+insertToken.length()); if((tmpEndTokenIndex > -1) && (tmpEndTokenIndex < tmpToParse.indexOf( ENDOFLINE,position+insertToken.length()))) { insertToken = exchangeTokenMap.get(tmpEndToken+"list") + ENDOFLINE; } else { insertToken = (String)exchangeTokenMap.get(tmpEndToken)+ exchangeTokenMap.get(tmpEndToken+"list") + ENDOFLINE; } openedElement = false; } //Check if at the end of toParse is a ENDOFLINE token if (tmpToParse.indexOf(ENDOFLINE,position+1) > -1) { tmpToParse = tmpToParse.replace( tmpToParse.indexOf(ENDOFLINE,position+1), tmpToParse.indexOf(ENDOFLINE,position+1)+ ENDOFLINE.length(), insertToken); } else { tmpToParse.insert(tmpToParse.length(), insertToken); } position = tmpToParse.indexOf(ENDOFLINE+tmpToken,position+1); } } return tmpToParse.toString(); } /** * Check if there is an other token of the same group of a line element. * It can be used to check, if the group should be closed. * A line element is in this case a token, which affect the whole line. * @param toScan Will be searched through. * @param beginPosition Start position of the search. * @param searchToken Token to be searched. It will be searched after a * endOfLine. * @param endOfLine The end of line token. * @return true if there is an other searchToken after a endOfLine, * otherwise false */ private static boolean hasNextLineElement(String toScan, int beginPosition, String searchToken, String endOfLine) { int posFirstSearchToken = toScan.indexOf(endOfLine+searchToken, beginPosition); if(posFirstSearchToken < 0) return false; /* It's not allowed that a end of line is between the first element * and a other element of the same type */ int posFirstEndOfLine = toScan.indexOf(endOfLine, beginPosition+1); if((posFirstEndOfLine < 0) || (posFirstEndOfLine >= posFirstSearchToken)) return true; return false; } /** * Delete all elements that should not be edited. The extracted elements * will be stored in the 'noWikiTextMap'. To insert the elments it must * be used the method insertNowikiElements(String). * @param toParse Will be searched through * @return The parsed variable toParse. If there are non 'No wiki elements' * it would not be changed. */ private static String deleteNowikiElements(String toParse) { if (toParse == null) return toParse; StringBuffer tmpToParse = new StringBuffer(toParse); //Counter that ensure the singleness of the inserted text into the Map int counter = 0; int position = tmpToParse.indexOf(BEGINNOWIKITEXT); int firstExtractPosition; int lastExtractPosition; String notEditedText = null; while(position > -1) { //The first position that will be extracted firstExtractPosition = position + BEGINNOWIKITEXT.length(); //The last position that will be extracted lastExtractPosition = tmpToParse.indexOf(ENDNOWIKITEXT, firstExtractPosition); //Substring that will be extracted notEditedText = tmpToParse.substring(firstExtractPosition, lastExtractPosition); //Insert the extracted String into the Map noWikiTextMap.put(""+counter, notEditedText); //Replace the text tmpToParse= tmpToParse.replace(firstExtractPosition, lastExtractPosition, ""+counter); counter++; position = toParse.indexOf(BEGINNOWIKITEXT, position+1); } return tmpToParse.toString(); } /** * Insert all elemts that should not be edited from the map 'noWikiTextMap'. * The elements which don't should be edited must be deleted with the * method deleteNowikiElements(String). * @param toParse Will be searched through * @return The parsed variable toParse. If there are non 'No wiki elements' * in the map, it would not be changed. */ private static String insertNowikiElements(String toParse) { if (toParse == null) return toParse; StringBuffer tmpToParse = new StringBuffer(toParse); int position = tmpToParse.indexOf(BEGINNOWIKITEXT); int firstExtractPosition; int lastExtractPosition; String parsedCounter = null; String insertElement = null; while(position > -1) { //The first position that will be extracted firstExtractPosition = (position + BEGINNOWIKITEXT.length()); //The last position that will be extracted lastExtractPosition = tmpToParse.indexOf(ENDNOWIKITEXT, firstExtractPosition); //Substring that will be extracted parsedCounter = tmpToParse.substring(firstExtractPosition, lastExtractPosition); //Insert the extracted String into the Map insertElement = (String)noWikiTextMap.get(parsedCounter); //Replace the text tmpToParse = tmpToParse.replace( position, lastExtractPosition+ENDNOWIKITEXT.length(), insertElement); position = tmpToParse.indexOf(BEGINNOWIKITEXT, position+1); } return tmpToParse.toString(); } /** * Insert the right link to the images. * @param toParse Will be parsed * @return Return toParse with the inserted link to the images. * If there are no images it will be changed nothing. */ private static String parseImages(String toParse) { if(toParse == null) return toParse; StringBuffer tmpToParse = new StringBuffer(toParse); int position = tmpToParse.indexOf(BEGINIMG); int firstExtractPosition; int lastExtractPosition; int separatorPosition; String parsedLink = null; String url = null; String description = null; while(position > -1) { firstExtractPosition = position+BEGINIMG.length(); lastExtractPosition = tmpToParse.indexOf(ENDIMG, position); parsedLink = tmpToParse.substring(firstExtractPosition, lastExtractPosition); //Change it to replace the tokens lastExtractPosition += ENDIMG.length(); //Check if the link has a description separatorPosition = parsedLink.indexOf(SEPARATORIMG); if(separatorPosition > -1) { url = parsedLink.substring(0, separatorPosition); description = parsedLink.substring(separatorPosition+1); tmpToParse = tmpToParse.replace( position, lastExtractPosition, "<img src=\"$portalRequest.getBaseUrl()/"+url+ "\" alt=\""+description+"\">"); } else { tmpToParse = tmpToParse.replace( position, lastExtractPosition, "<img src=\"$portalRequest.getBaseUrl()/"+parsedLink+ "\" alt=\""+parsedLink+"\">"); } position = tmpToParse.indexOf(BEGINIMG, position+1); } return tmpToParse.toString(); } /** * Insert the right links. * @param toParse Will be parsed * @return Return toParse with the inserted links. If there are no links * it will be changed nothing. */ private static String parseLinks(String toParse) { if(toParse == null) return toParse; StringBuffer tmpToParse = new StringBuffer(toParse); int position = tmpToParse.indexOf(BEGINLINK); int firstExtractPosition; int lastExtractPosition; int separatorPosition; String parsedLink = null; String url = null; String description = null; while(position > -1) { firstExtractPosition = position+BEGINLINK.length(); lastExtractPosition = tmpToParse.indexOf(ENDLINK, position); parsedLink = tmpToParse.substring(firstExtractPosition, lastExtractPosition); //Change it for inserting lastExtractPosition = lastExtractPosition+ENDLINK.length(); //Check if the link has a description separatorPosition = parsedLink.indexOf(SEPARATORLINK); if(separatorPosition > -1) { url = parsedLink.substring(0, separatorPosition); description = parsedLink.substring(separatorPosition+1); //Check if the URL is a external URL if(url.indexOf("www.") > -1) { tmpToParse = tmpToParse.replace( position, lastExtractPosition, "<a href=\""+url+"\">"+description+"</a>"); } else { if (url.indexOf(".") < 0) { url = url + "."+DEFAULTENDING; } else { url = "/"+url; } tmpToParse = tmpToParse.replace( position, lastExtractPosition, "<a href=\"$portalRequest.getBaseUrl()"+ url+"\">"+description+"</a>"); } } else { url = parsedLink; if(url.indexOf("www.") > -1) { tmpToParse = tmpToParse.replace( position, lastExtractPosition, "<a href=\""+url+"\">"+url+"</a>"); } else { if (url.indexOf(".") < 0) { url = url + "."+DEFAULTENDING; } else { url = "/"+url; } tmpToParse = tmpToParse.replace( position, lastExtractPosition, "<a href=\"$portalRequest.getBaseUrl()"+ url+"\">"+parsedLink+"</a>"); } } position = tmpToParse.indexOf(BEGINLINK,position+1); } return tmpToParse.toString(); } /** * Delete all elements which are in 'deleteToken'. * @param toParse Will be parsed. * @return Return toParse with the deleted elements. If there are no * elements to delete it will be changed nothing. */ public static String deleteToken(String toParse) { if(toParse == null || deleteToken == null) return toParse; StringBuffer tmpToParse = new StringBuffer(toParse); int position = -1; for(int i = 0; i < deleteToken.length; i++) { position = tmpToParse.indexOf(deleteToken[i]); while(position > -1) { tmpToParse = tmpToParse.delete(position, position+deleteToken[i].length()); position = tmpToParse.indexOf(deleteToken[i], position); } } return tmpToParse.toString(); } /** * Insert the link to the edit side with the right itemid. * @param itemid Itemid of the page which should be edited. * @param content Content where the link will be inserted. * @param position Position where the link will be inserted. * @return The content with the edit-link. */ public static String insertEditLink(int itemid, String content, int position) { if(content == null) return null; StringBuffer tmp = new StringBuffer(content); tmp.insert(position, replaceItemid(itemid)); return tmp.toString(); } /** * Search the place holder of the itemid and replace it with the real * itemid of the content. * @param itemid The itemid that will be inserted. * @return The edit-link with the right itemid. */ private static String replaceItemid(int itemid) { if(EDITLINK == null) return EDITLINK; StringBuffer tmp = new StringBuffer(EDITLINK); int pos = tmp.indexOf(ITEMID); if(pos > -1) { tmp = tmp.replace(pos, pos+ITEMID.length(), ""+itemid); } return tmp.toString(); } /** * Insert the history link into the content at the specific position. * @param itemid Itemid of the side which history should be shown. * @param lang Language of the side * @param content Content where the history list should be inserted * @param position Position where the link should be inserted * @return Content with the inserted history link. */ public static String insertHistoryLink(int itemid, String lang, String content, int position) { if(content == null) return null; StringBuffer tmp = new StringBuffer(content); tmp.insert(position, replaceItemidAndLanguage(itemid, lang)); return tmp.toString(); } /** * Search the place holder of the itemid and language in the history * and replace it with the real values. * @param itemid Itemid of the page. * @param lang Language of the page. * @return The history link with the right itemid and language. */ private static String replaceItemidAndLanguage(int itemid, String lang) { if(HISTORYLINK == null) return null; StringBuffer tmp = new StringBuffer(HISTORYLINK); int pos = tmp.indexOf(ITEMID); if(pos > -1) { tmp = tmp.replace(pos, pos+ITEMID.length(), ""+itemid); } pos = tmp.indexOf(LANG); if(pos > -1) { tmp = tmp.replace(pos, pos+LANG.length(), lang); } return tmp.toString(); } } |