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.macro.book;
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.Messages;
038: import org.radeox.util.Encoder;
039:
040: /**
041: * Manages links to keys, mapping is read from a text file
042: *
043: * @author Stephan J. Schmidt
044: * @version $Id: TextFileUrlMapper.java 7707 2006-04-12 17:30:19Z
045: * ian@caret.cam.ac.uk $
046: */
047:
048: public abstract class TextFileUrlMapper implements UrlMapper {
049: private static Log log = LogFactory.getLog(TextFileUrlMapper.class);
050:
051: private Map services;
052:
053: public abstract String getFileName();
054:
055: public abstract String getKeyName();
056:
057: public TextFileUrlMapper(Class klass) {
058: services = new HashMap();
059:
060: boolean fileNotFound = false;
061: try {
062: BufferedReader br = new BufferedReader(
063: new InputStreamReader(new FileInputStream(
064: getFileName())));
065: addMapping(br);
066: } catch (IOException e) {
067: log.warn("Unable to read " + getFileName()); //$NON-NLS-1$
068: fileNotFound = true;
069: }
070:
071: if (fileNotFound) {
072: BufferedReader br = null;
073: try {
074: br = new BufferedReader(new InputStreamReader(klass
075: .getResourceAsStream("/" + getFileName()))); //$NON-NLS-1$
076: addMapping(br);
077: } catch (Exception e) {
078: log
079: .warn("Unable to read /" + getFileName() + " from jar"); //$NON-NLS-1$ //$NON-NLS-2$
080: }
081: }
082: }
083:
084: public void addMapping(BufferedReader reader) throws IOException {
085: String line;
086: while ((line = reader.readLine()) != null) {
087: if (!line.startsWith("#")) //$NON-NLS-1$
088: {
089: int index = line.indexOf(" "); //$NON-NLS-1$
090: services.put(line.substring(0, index), Encoder
091: .escape(line.substring(index + 1)));
092: }
093: }
094: }
095:
096: public Writer appendTo(Writer writer) throws IOException {
097: Iterator iterator = services.entrySet().iterator();
098: writer.write("{table}\n"); //$NON-NLS-1$
099: writer.write(Messages.getString("TextFileUrlMapper.7")); //$NON-NLS-1$
100: while (iterator.hasNext()) {
101: Map.Entry entry = (Map.Entry) iterator.next();
102: writer.write((String) entry.getKey());
103: writer.write("|"); //$NON-NLS-1$
104: writer.write((String) entry.getValue());
105: writer.write("\n"); //$NON-NLS-1$
106: }
107: writer.write("{table}"); //$NON-NLS-1$
108: return writer;
109: }
110:
111: public boolean contains(String external) {
112: return services.containsKey(external);
113: }
114:
115: public Writer appendUrl(Writer writer, String key)
116: throws IOException {
117: if (services.size() == 0) {
118: writer.write(getKeyName());
119: writer.write(":"); //$NON-NLS-1$
120: writer.write(key);
121: } else {
122: // SnipLink.appendImage(writer, "external-link", ">>");
123: writer.write("("); //$NON-NLS-1$
124: Iterator iterator = services.entrySet().iterator();
125: while (iterator.hasNext()) {
126: Map.Entry entry = (Map.Entry) iterator.next();
127: writer.write("<a href=\""); //$NON-NLS-1$
128: writer.write((String) entry.getValue());
129: writer.write(key);
130: writer.write("\">"); //$NON-NLS-1$
131: writer.write((String) entry.getKey());
132: writer.write("</a>"); //$NON-NLS-1$
133: if (iterator.hasNext()) {
134: writer.write(" | "); //$NON-NLS-1$
135: }
136: }
137: writer.write(")"); //$NON-NLS-1$
138: }
139: return writer;
140: }
141: }
|