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.xref;
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: import java.util.StringTokenizer;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038:
039: /**
040: * Stores information and links to xref Java source code e.g.
041: * http://nanning.sourceforge.net/xref/com/tirsen/nanning/MixinInstance.html#83
042: *
043: * @author Stephan J. Schmidt
044: * @version $Id: XrefMapper.java 7756 2006-04-13 12:25:49Z ian@caret.cam.ac.uk $
045: */
046:
047: public class XrefMapper {
048: private static Log log = LogFactory.getLog(XrefMapper.class);
049:
050: private final static String FILENAME = "conf/xref.txt";
051:
052: private static XrefMapper instance;
053:
054: private Map xrefMap;
055:
056: public static synchronized XrefMapper getInstance() {
057: if (null == instance) {
058: instance = new XrefMapper();
059: }
060: return instance;
061: }
062:
063: public XrefMapper() {
064: xrefMap = new HashMap();
065:
066: boolean fileNotFound = false;
067: try {
068: BufferedReader br = new BufferedReader(
069: new InputStreamReader(new FileInputStream(FILENAME)));
070: addXref(br);
071: } catch (IOException e) {
072: log.warn("Unable to read " + FILENAME);
073: fileNotFound = true;
074: }
075:
076: if (fileNotFound) {
077: BufferedReader br = null;
078: try {
079: br = new BufferedReader(new InputStreamReader(
080: XrefMapper.class.getResourceAsStream("/"
081: + FILENAME)));
082: addXref(br);
083: } catch (Exception e) {
084: log.warn("Unable to read " + FILENAME);
085: }
086: }
087: }
088:
089: public void addXref(BufferedReader reader) throws IOException {
090: String line;
091: while ((line = reader.readLine()) != null) {
092: StringTokenizer tokenizer = new StringTokenizer(line, " ");
093: String site = tokenizer.nextToken();
094: String baseUrl = tokenizer.nextToken();
095: xrefMap.put(site.toLowerCase(), baseUrl);
096: }
097: }
098:
099: public boolean contains(String external) {
100: return xrefMap.containsKey(external);
101: }
102:
103: public Writer expand(Writer writer, String className, String site,
104: int lineNumber) throws IOException {
105: site = site.toLowerCase();
106: if (xrefMap.containsKey(site)) {
107: writer.write("<a href=\"");
108: writer.write((String) xrefMap.get(site));
109: writer.write("/");
110: writer.write(className.replace('.', '/'));
111: writer.write(".html");
112: if (lineNumber > 0) {
113: writer.write("#");
114: writer.write("" + lineNumber);
115: }
116: writer.write("\">");
117: writer.write(className);
118: writer.write("</a>");
119: } else {
120: log.debug("Xrefs : " + xrefMap);
121: log.warn(site + " not found");
122: }
123: return writer;
124: }
125:
126: public Writer appendTo(Writer writer) throws IOException {
127: writer.write("{table}\n");
128: writer.write("Binding|Site\n");
129: Iterator iterator = xrefMap.entrySet().iterator();
130: while (iterator.hasNext()) {
131: Map.Entry entry = (Map.Entry) iterator.next();
132: writer.write((String) entry.getKey());
133: writer.write("|");
134: writer.write((String) entry.getValue());
135: writer.write("\n");
136: }
137: writer.write("{table}");
138: return writer;
139: }
140:
141: }
|