001: /*
002: * This file is part of "SnipSnap Radeox Rendering Engine".
003: *
004: * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
005: * All Rights Reserved.
006: *
007: * Please visit http://radeox.org/ for updates and contact.
008: *
009: * --LICENSE NOTICE--
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * --LICENSE NOTICE--
022: */
023:
024: package org.radeox.filter.interwiki;
025:
026: import java.io.BufferedReader;
027: import java.io.FileInputStream;
028: import java.io.IOException;
029: import java.io.InputStreamReader;
030: import java.io.Writer;
031: import java.util.HashMap;
032: import java.util.Iterator;
033: import java.util.Map;
034:
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037: import org.radeox.util.Encoder;
038:
039: /**
040: * Stores information and links to other wikis forming a InterWiki
041: *
042: * @author Stephan J. Schmidt
043: * @version $Id: InterWiki.java 7756 2006-04-13 12:25:49Z ian@caret.cam.ac.uk $
044: */
045:
046: public class InterWiki {
047: private static Log log = LogFactory.getLog(InterWiki.class);
048:
049: private static InterWiki instance;
050:
051: private Map interWiki;
052:
053: public static synchronized InterWiki getInstance() {
054: if (null == instance) {
055: instance = new InterWiki();
056: }
057: return instance;
058: }
059:
060: public InterWiki() {
061: interWiki = new HashMap();
062: interWiki.put("LCOM", "http://www.langreiter.com/space/");
063: interWiki.put("ESA", "http://earl.strain.at/space/");
064: interWiki.put("C2", "http://www.c2.com/cgi/wiki?");
065: interWiki.put("WeblogKitchen",
066: "http://www.weblogkitchen.com/wiki.cgi?");
067: interWiki.put("Meatball",
068: "http://www.usemod.com/cgi-bin/mb.pl?");
069: interWiki.put("SnipSnap", "http://snipsnap.org/space/");
070:
071: try {
072: BufferedReader br = new BufferedReader(
073: new InputStreamReader(new FileInputStream(
074: "conf/intermap.txt")));
075: addInterMap(br);
076: } catch (IOException e) {
077: log.warn("Unable to read conf/intermap.txt");
078: }
079: }
080:
081: public void addInterMap(BufferedReader reader) throws IOException {
082: String line;
083: while ((line = reader.readLine()) != null) {
084: int index = line.indexOf(" ");
085: interWiki.put(line.substring(0, index), Encoder.escape(line
086: .substring(index + 1)));
087: }
088: ;
089: }
090:
091: public Writer appendTo(Writer writer) throws IOException {
092: Iterator iterator = interWiki.entrySet().iterator();
093: writer.write("{table}\n");
094: writer.write("Wiki|Url\n");
095: while (iterator.hasNext()) {
096: Map.Entry entry = (Map.Entry) iterator.next();
097: writer.write((String) entry.getKey());
098: writer.write("|");
099: writer.write((String) entry.getValue());
100: writer.write("\n");
101: }
102: writer.write("{table}");
103: return writer;
104: }
105:
106: public boolean contains(String external) {
107: return interWiki.containsKey(external);
108: }
109:
110: public String getWikiUrl(String wiki, String name) {
111: return ((String) interWiki.get(wiki)) + name;
112: }
113:
114: public Writer expand(Writer writer, String wiki, String name,
115: String view, String anchor) throws IOException {
116: writer.write("<a href=\"");
117: writer.write((String) interWiki.get(wiki));
118: writer.write(name);
119: if (!"".equals(anchor)) {
120: writer.write("#");
121: writer.write(anchor);
122: }
123: writer.write("\">");
124: writer.write(view);
125: writer.write("</a>");
126: return writer;
127: }
128:
129: public Writer expand(Writer writer, String wiki, String name,
130: String view) throws IOException {
131: return expand(writer, wiki, name, view, "");
132: }
133: }
|