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.api;
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: import org.radeox.Messages;
039:
040: /**
041: * Stores information and links to api documentation, e.g. for Java, Ruby, JBoss
042: *
043: * @author Stephan J. Schmidt
044: * @version $Id: ApiDoc.java 20417 2007-01-18 18:14:15Z ian@caret.cam.ac.uk $
045: */
046:
047: public class ApiDoc {
048:
049: private static Log log = LogFactory.getLog(ApiDoc.class);
050:
051: private static ApiDoc instance;
052:
053: private Map apiDocs;
054:
055: public static synchronized ApiDoc getInstance() {
056: if (null == instance) {
057: instance = new ApiDoc();
058: }
059: return instance;
060: }
061:
062: public ApiDoc() {
063: apiDocs = new HashMap();
064:
065: boolean fileNotFound = false;
066: try {
067: BufferedReader br = new BufferedReader(
068: new InputStreamReader(new FileInputStream(
069: "conf/apidocs.txt"))); //$NON-NLS-1$
070: addApiDoc(br);
071: } catch (IOException e) {
072: log.warn("Unable to read conf/apidocs.txt"); //$NON-NLS-1$
073: fileNotFound = true;
074: }
075:
076: if (fileNotFound) {
077: BufferedReader br = null;
078: try {
079: br = new BufferedReader(
080: new InputStreamReader(
081: ApiDoc.class
082: .getResourceAsStream("/conf/apidocs.txt"))); //$NON-NLS-1$
083: addApiDoc(br);
084: } catch (Exception e) {
085: log.warn("Unable to read conf/apidocs.txt from jar"); //$NON-NLS-1$
086: }
087: }
088: }
089:
090: public void addApiDoc(BufferedReader reader) throws IOException {
091: String line;
092: while ((line = reader.readLine()) != null) {
093: StringTokenizer tokenizer = new StringTokenizer(line, " "); //$NON-NLS-1$
094: String mode = tokenizer.nextToken();
095: String baseUrl = tokenizer.nextToken();
096: String converterName = tokenizer.nextToken();
097: ApiConverter converter = null;
098: try {
099: converter = (ApiConverter) Class.forName(
100: "org.radeox.macro.api." + converterName //$NON-NLS-1$
101: + "ApiConverter").newInstance(); //$NON-NLS-1$
102: } catch (Exception e) {
103: log.warn("Unable to load converter: " + converterName //$NON-NLS-1$
104: + "ApiConverter", e); //$NON-NLS-1$
105: }
106: converter.setBaseUrl(baseUrl);
107: apiDocs.put(mode.toLowerCase(), converter);
108: }
109: }
110:
111: public boolean contains(String external) {
112: return apiDocs.containsKey(external);
113: }
114:
115: public Writer expand(Writer writer, String className, String mode)
116: throws IOException {
117: mode = mode.toLowerCase();
118: if (apiDocs.containsKey(mode)) {
119: writer.write("<a href=\""); //$NON-NLS-1$
120: ((ApiConverter) apiDocs.get(mode)).appendUrl(writer,
121: className);
122: writer.write("\">"); //$NON-NLS-1$
123: writer.write(className);
124: writer.write("</a>"); //$NON-NLS-1$
125: } else {
126: log.warn(mode + " not found"); //$NON-NLS-1$
127: }
128: return writer;
129: }
130:
131: public Writer appendTo(Writer writer) throws IOException {
132: writer.write("{table}\n"); //$NON-NLS-1$
133: writer.write(Messages.getString("ApiDoc.14")); //$NON-NLS-1$
134: Iterator iterator = apiDocs.entrySet().iterator();
135: while (iterator.hasNext()) {
136: Map.Entry entry = (Map.Entry) iterator.next();
137: writer.write((String) entry.getKey());
138: ApiConverter converter = (ApiConverter) entry.getValue();
139: writer.write("|"); //$NON-NLS-1$
140: writer.write(converter.getBaseUrl());
141: writer.write("|"); //$NON-NLS-1$
142: writer.write(converter.getName());
143: writer.write("\n"); //$NON-NLS-1$
144: }
145: writer.write("{table}"); //$NON-NLS-1$
146: return writer;
147: }
148:
149: }
|