|
From: Alexander G. <gaf...@us...> - 2006-01-31 19:59:29
|
Update of /cvsroot/cobricks/cobricks2/src/org/cobricks/portal/wiki In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20639/src/org/cobricks/portal/wiki Modified Files: UtilityTest.java Utility.java Log Message: Link abbreviations added Index: UtilityTest.java =================================================================== RCS file: /cvsroot/cobricks/cobricks2/src/org/cobricks/portal/wiki/UtilityTest.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- UtilityTest.java 16 Jan 2006 09:23:28 -0000 1.1 +++ UtilityTest.java 31 Jan 2006 19:59:20 -0000 1.2 @@ -25,8 +25,8 @@ parse = "Italic: ''italic''"; result = "Italic: <i>italic</i>"; retValue = Utility.parse(parse); - // System.out.println(result); - // System.out.println(retValue); + System.out.println(result); + System.out.println(retValue); assertTrue(result.equals(retValue)); parse = "Italic: ''italic\n"; Index: Utility.java =================================================================== RCS file: /cvsroot/cobricks/cobricks2/src/org/cobricks/portal/wiki/Utility.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- Utility.java 18 Jan 2006 22:06:37 -0000 1.2 +++ Utility.java 31 Jan 2006 19:59:20 -0000 1.3 @@ -12,9 +12,12 @@ package org.cobricks.portal.wiki; +import java.util.Iterator; +import java.util.NoSuchElementException; import java.util.Map; import java.util.HashMap; import java.util.Properties; +import java.util.regex.Pattern; import org.cobricks.core.CoreManager; import org.cobricks.core.util.PropertiesUtil; @@ -70,15 +73,24 @@ private final static String ENDIMG = "]]"; private final static String SEPARATORIMG = "|"; + //Short links + private final static String BEGINSHORTLINK = "[[:"; + private final static String ENDSCHORTLINK = "]]"; + private final static String SEPARATORSHORTLINK = "|"; + //Comments private final static String BEGINCOMMENT = "<!--"; private final static String ENDCOMMENT = "-->"; //delete this token private static String[] deleteToken = null; - + //Read porperties from property file - private static Properties properties = new Properties(); + private static Properties properties = null; + + //property name, which contains all links for the link abbreviation + private static String linkAbbreviationProperty = "portal.linkabbreviations"; + /** * Initialize the lists with the strings to search in the parse Sting. */ @@ -166,8 +178,9 @@ * 3. parseSearchToken(tmpParse) * 4. parseSearchTokenWholeLine(tmpParse) * 5. parseImages(tmpParse) - * 6. parseLinks(tmpParse) - * 7. insertNowikiElements(tmpParse) + * 6. parseShortLinks(tmpParse); + * 7. parseLinks(tmpParse) + * 8. insertNowikiElements(tmpParse) * * */ @@ -188,6 +201,9 @@ //parse the images tmpParse = parseImages(tmpParse); + //parse the short links + tmpParse = parseShortLinks(tmpParse); + //parse the links tmpParse = parseLinks(tmpParse); @@ -207,7 +223,6 @@ if((pagefooterwiki != null) && (!pagefooterwiki.equals(""))) tmpParse = tmpParse + pagefooterwiki + ENDOFLINE; - return tmpParse; } @@ -580,7 +595,6 @@ position = tmpToParse.indexOf(BEGINNOWIKITEXT, position+1); } - return tmpToParse.toString(); } @@ -641,6 +655,79 @@ } /** + * Insert the link to the pages by exchanging the 'short link' placeholder. + * + * @param toParse Will be parsed + * @return return the parsed String, where the 'short links' are + * changed by the right link. + */ + private static String parseShortLinks(String toParse) + { + if(toParse == null) + return toParse; + + loadProperties(); + + StringBuffer tmpToParse = new StringBuffer(toParse); + + Map abbreviations = getLinkAbbreviations(); + String linkAbbreviation = null; + String beginLink = null; + + int position = -1; + int firstExtractPosition = -1; + int lastExtractPosition = -1; + int separatorPosition = -1; + String parsedLink = null; + String additionalText; + try { + //For all short link do + for(Iterator it = abbreviations.keySet().iterator();it.hasNext();) { + linkAbbreviation = (String)it.next(); + beginLink = BEGINSHORTLINK + linkAbbreviation; + position = tmpToParse.indexOf(beginLink); + while(position > -1) { + firstExtractPosition = position+BEGINSHORTLINK.length(); + lastExtractPosition = tmpToParse.indexOf(ENDSCHORTLINK, + position); + + parsedLink = tmpToParse.substring(firstExtractPosition, + lastExtractPosition); + + //Change it to replace the tokens + lastExtractPosition += ENDSCHORTLINK.length(); + + //Check if the link has a description + separatorPosition = parsedLink.indexOf(SEPARATORSHORTLINK); + if(separatorPosition > -1) { + additionalText = parsedLink.substring(separatorPosition+1); + + tmpToParse = tmpToParse.replace( + position, + lastExtractPosition, + "<a href=\""+abbreviations.get(linkAbbreviation) + +"/"+additionalText+"\">"+ + abbreviations.get(linkAbbreviation)+ + "/"+additionalText+"</a>"); + } else { + tmpToParse = tmpToParse.replace( + position, + lastExtractPosition, + "<a href=\""+abbreviations.get(linkAbbreviation) + +"\">"+abbreviations.get( + linkAbbreviation)+"</a>"); + + } + position = tmpToParse.indexOf(beginLink, position+1); + } + } + } catch (NoSuchElementException e) { + + } + return tmpToParse.toString(); + } + + /** * Insert the right links. * @param toParse Will be parsed * @return Return toParse with the inserted links. If there are no links @@ -716,7 +803,6 @@ } position = tmpToParse.indexOf(BEGINLINK,position+1); } - return tmpToParse.toString(); } @@ -837,7 +923,37 @@ * Load the porperties from the property file in 'org.cobrcks.portal'. */ private static void loadProperties() { - PropertiesUtil.loadPropertiesResource("org.cobricks.portal", - "properties.txt", properties, new CoreManager()); + if(properties == null) { + properties = new Properties(); + CoreManager coreManager = new CoreManager(); + + PropertiesUtil.loadPropertiesResource("org.cobricks.portal", + "properties.txt", properties, coreManager); + } + } + + /** + * Get the link abbreviations which are stored in the file "properties.txt" + * in "org.cobricks.portal" + * @return Map with the link abbreviations and the whole link. + */ + public static Map getLinkAbbreviations() { + loadProperties(); + + Map linkMap = new HashMap(); + + //Conatins the property names, where the string abbreviations are stored + StringBuffer linkAbbreviationPropertyNames = new StringBuffer( + properties.getProperty(linkAbbreviationProperty)); + + Pattern pattern = Pattern.compile(";"); + String [] linkNames = pattern.split(linkAbbreviationPropertyNames.subSequence(0,linkAbbreviationPropertyNames.length())); + + String link = null; + for(int i = 0; i < linkNames.length; i++) { + linkMap.put(linkNames[i], properties.get(linkNames[i])); + } + + return linkMap; } } |