001: // LinkToken.java
002: // ---------
003: // part of YaCy
004: // (C) by Michael Peter Christen; mc@anomic.de
005: // first published on http://www.anomic.de
006: // Frankfurt, Germany, 2007
007: // Created 22.02.2007
008: //
009: // This file is contributed by Franz Brausze
010: //
011: // $LastChangedDate: $
012: // $LastChangedRevision: $
013: // $LastChangedBy: $
014: //
015: // This program is free software; you can redistribute it and/or modify
016: // it under the terms of the GNU General Public License as published by
017: // the Free Software Foundation; either version 2 of the License, or
018: // (at your option) any later version.
019: //
020: // This program is distributed in the hope that it will be useful,
021: // but WITHOUT ANY WARRANTY; without even the implied warranty of
022: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: // GNU General Public License for more details.
024: //
025: // You should have received a copy of the GNU General Public License
026: // along with this program; if not, write to the Free Software
027: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
028: //
029: // Using this software in any meaning (reading, learning, copying, compiling,
030: // running) means that you agree that the Author(s) is (are) not responsible
031: // for cost, loss of data or any harm that may be caused directly or indirectly
032: // by usage of this softare or this documentation. The usage of this software
033: // is on your own risk. The installation and usage (starting/running) of this
034: // software may allow other people or application to access your computer and
035: // any attached devices and is highly dependent on the configuration of the
036: // software which must be done by the user of the software; the author(s) is
037: // (are) also not responsible for proper configuration and usage of the
038: // software, even if provoked by documentation provided together with
039: // the software.
040: //
041: // Any changes to this file according to the GPL as documented in the file
042: // gpl.txt aside this file in the shipment you received can be done to the
043: // lines that follows this copyright notice here, but changes must not be
044: // done inside the copyright notive above. A re-distribution must contain
045: // the intact and unchanged copyright notice.
046: // Contributions and changes to the program code must be marked as such.
047:
048: package de.anomic.data.wiki.tokens;
049:
050: import java.util.ArrayList;
051: import java.util.Iterator;
052: import java.util.regex.Matcher;
053: import java.util.regex.Pattern;
054:
055: import de.anomic.data.bookmarksDB;
056: import de.anomic.data.bookmarksDB.Bookmark;
057: import de.anomic.data.bookmarksDB.Tag;
058: import de.anomic.data.wiki.wikiParserException;
059: import de.anomic.plasma.plasmaSwitchboard;
060:
061: public class LinkToken extends AbstractToken {
062:
063: private static final int IMG = 0;
064: private static final int BKM = 1;
065: private static final int INT = 2;
066: private static final int EXT = 3;
067:
068: private static final Pattern imgPattern = Pattern.compile("\\[\\[" + // begin
069: "(Image:([^\\]|]|\\][^\\]])*)" + // "Image:" + URL
070: "(" + // <optional>
071: "(\\|(bottom|left|center|right|middle|top))?" + // optional align
072: "(\\|(([^\\]]|\\][^\\]])*))" + // description
073: ")?" + // </optional>
074: "\\]\\]"); // end
075:
076: private static final Pattern bkmPattern = Pattern.compile("\\[\\[" + // begin
077: "(Bookmark:([^\\]|]|\\][^\\]])*)" + // "Bookmark:" + URL
078: "(\\|(([^\\]]|\\][^\\]])*?))?" + // optional description
079: "\\]\\]"); // end
080:
081: private static final Pattern intPattern = Pattern.compile("\\[\\[" + // begin
082: "(([^\\]|]|\\][^\\]])*?)" + // wiki-page
083: "(\\|(([^\\]]|\\][^\\]])*?))?" + // optional desciption
084: "\\]\\]"); // end
085:
086: private static final Pattern extPattern = Pattern.compile("\\[" + // begin
087: "([^\\] ]*)" + // URL
088: "( ([^\\]]*))?" + // optional description
089: "\\]"); // end
090:
091: private static final Pattern[] patterns = new Pattern[] {
092: imgPattern, bkmPattern, intPattern, extPattern };
093:
094: private final String localhost;
095: private final String wikiPath;
096: private final plasmaSwitchboard sb;
097: private int patternNr = 0;
098:
099: public LinkToken(String localhost, String wikiPath,
100: plasmaSwitchboard sb) {
101: this .localhost = localhost;
102: this .wikiPath = wikiPath;
103: this .sb = sb;
104: }
105:
106: protected void parse() throws wikiParserException {
107: StringBuffer sb = new StringBuffer();
108: if (this .patternNr < 0 || this .patternNr >= patterns.length)
109: throw new wikiParserException(
110: "patternNr was not set correctly: "
111: + this .patternNr);
112: Matcher m = patterns[this .patternNr].matcher(this .text);
113: if (!m.find())
114: throw new wikiParserException("Didn't find match for: ("
115: + this .patternNr + ") " + this .text);
116:
117: switch (this .patternNr) {
118: case IMG:
119: sb.append("<img src=\"").append(
120: formatHref(m.group(1).substring(6))).append("\"");
121: if (m.group(5) != null)
122: sb.append(" align=\"").append(m.group(5)).append("\"");
123: sb.append(" alt=\"").append(
124: (m.group(7) == null) ? formatHref(m.group(1)
125: .substring(6)) : m.group(7)).append("\"");
126: sb.append(" />");
127: break;
128:
129: case BKM:
130: Link[] links = getLinksFromBookmarkTag(m.group(2));
131: if (links == null) {
132: sb
133: .append(
134: "<span class=\"error\">Couldn't find Bookmark-Tag '")
135: .append(m.group(2)).append("'.</span>");
136: } else {
137: appendLinks(links, sb);
138: }
139: break;
140:
141: case INT:
142: sb.append(new Link("http://" + this .localhost + "/"
143: + this .wikiPath + m.group(1), m.group(4), (m
144: .group(4) == null) ? m.group(1) : m.group(4))
145: .toString());
146: break;
147:
148: case EXT:
149: sb.append(new Link(m.group(1), m.group(3),
150: (m.group(3) == null) ? m.group(1) : m.group(3))
151: .toString());
152: break;
153: }
154: this .parsed = true;
155: this .markup = new String(sb);
156: }
157:
158: private String formatHref(String link) {
159: if (link.indexOf("://") == -1) { // DATA/HTDOCS-link
160: return "http://" + this .localhost + "/share/" + link;
161: } else { // 'normal' link
162: return link;
163: }
164: }
165:
166: private StringBuffer appendLinks(Link[] links, StringBuffer sb) {
167: for (int i = 0; i < links.length; i++)
168: sb.append(links[i].toString());
169: return sb;
170: }
171:
172: private Link[] getLinksFromBookmarkTag(String tagName) {
173: Tag tag = this .sb.bookmarksDB.getTag(bookmarksDB
174: .tagHash(tagName));
175: if (tag == null)
176: return null;
177: ArrayList<Link> r = new ArrayList<Link>();
178: Iterator<String> it = tag.getUrlHashes().iterator();
179: String hash;
180: Bookmark bm;
181: while (it.hasNext())
182: if ((hash = it.next()) != null)
183: if ((bm = this .sb.bookmarksDB.getBookmark(hash)) != null)
184: r.add(new Link(bm.getUrl(), bm.getTitle(), bm
185: .getDescription()));
186: return (Link[]) r.toArray(new Link[r.size()]);
187: }
188:
189: private static class Link {
190:
191: private final String href;
192: private final String title;
193: private final String desc;
194:
195: public Link(String href, String title, String desc) {
196: this .href = href;
197: this .title = title;
198: this .desc = desc;
199: }
200:
201: public String toString() {
202: StringBuffer sb = new StringBuffer();
203: sb.append("<a href=\"").append(this .href).append("\"");
204: if (this .title != null)
205: sb.append(" title=\"").append(this .title).append("\"");
206: sb.append(">");
207: if (this .desc == null)
208: sb.append(this .href);
209: else
210: sb.append(this .desc);
211: sb.append("</a>");
212: return new String(sb);
213: }
214: }
215:
216: public String[] getBlockElementNames() {
217: return null;
218: }
219:
220: public Pattern[] getRegex() {
221: return patterns;
222: }
223:
224: public boolean setText(String text, int patternNr) {
225: this .text = text;
226: this .patternNr = patternNr;
227: this .parsed = false;
228: if (text == null) {
229: this .markup = null;
230: this .patternNr = -1;
231: }
232: return true;
233: }
234: }
|