001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.ui;
038:
039: import javax.swing.event.HyperlinkListener;
040: import javax.swing.event.HyperlinkEvent;
041: import java.net.URL;
042: import java.net.MalformedURLException;
043: import java.io.File;
044: import java.io.FileReader;
045: import java.io.BufferedReader;
046: import java.io.IOException;
047:
048: import edu.rice.cs.util.UnexpectedException;
049:
050: /**
051: * DrJava's Javadoc viewing frame
052: * @version $Id: JavadocFrame.java 4255 2007-08-28 19:17:37Z mgricken $
053: */
054: public class JavadocFrame extends HTMLFrame {
055:
056: private static final int MAX_READ_PACKAGES_LINES = 100;
057: private static final int MAX_READ_FOR_LINK_LINES = 100;
058: private static final String[] INTRO_PAGE = {
059: "overview-summary.html", "packages.html" };
060: private static final String INDEX_PAGE = "allclasses-frame.html";
061:
062: private static String introPagePath(File destDir, String curClass) {
063: // Iterate through possible intro pages, looking for one that exists.
064: File test = new File(destDir, curClass + ".html");
065: for (int i = 0; !test.exists() && (i < INTRO_PAGE.length); i++) {
066: test = new File(destDir, INTRO_PAGE[i]);
067: }
068:
069: // Packages.html might just be a pointer to another file
070: if (test.exists()) {
071: if (test.getName().equals("packages.html")) {
072: test = _parsePackagesFile(test, destDir);
073: }
074: } else {
075: throw new IllegalStateException(
076: "No Javadoc HTML output files found!");
077: }
078: return test.getAbsolutePath();
079: }
080:
081: /**
082: * Reads through the beginning of the packages.html file to determine
083: * if it is just a pointer to another file. Returns either the same
084: * file (if it's not a pointer), or the file used for "No frames" (if
085: * it is a pointer).
086: * @param packages Full path to the packages.html file
087: */
088: private static File _parsePackagesFile(File packages, File destDir) {
089: try {
090: FileReader fr = new FileReader(packages);
091: BufferedReader br = new BufferedReader(fr);
092: try { // process the opened file
093: String line = br.readLine();
094: int numLinesRead = 1;
095: boolean found = false;
096: while ((!found)
097: && (numLinesRead < MAX_READ_PACKAGES_LINES)
098: && (line != null)) {
099: found = (line
100: .indexOf("The front page has been relocated") != -1);
101: if (!found) {
102: line = br.readLine();
103: numLinesRead++;
104: }
105: }
106:
107: // Replace packages.html with the No Frames link.
108: if (found) {
109: boolean foundLink = false;
110: while ((!foundLink)
111: && (numLinesRead < MAX_READ_FOR_LINK_LINES)
112: && (line != null)) {
113: foundLink = (line.indexOf("Non-frame version") != -1);
114: if (!foundLink) {
115: line = br.readLine();
116: numLinesRead++;
117: }
118: }
119:
120: if (foundLink) {
121: String start = "HREF=\"";
122: int startIndex = line.indexOf(start)
123: + start.length();
124: int endIndex = line.indexOf("\">");
125: if ((startIndex != -1) && (endIndex != -1)) {
126: String fileName = line.substring(
127: startIndex, endIndex);
128: return new File(destDir, fileName);
129: }
130: }
131: }
132: } finally {
133: br.close();
134: }
135: } catch (IOException ioe) {
136: throw new UnexpectedException(ioe);
137: }
138: return packages;
139: }
140:
141: /**
142: * Constructor.
143: * @param destDir Directory holding the Javadoc
144: * @param curClass Name of the class to try to show by default
145: * @param allDocs Whether Javadoc was run for all open documents
146: */
147: public JavadocFrame(File destDir, String curClass, boolean allDocs)
148: throws MalformedURLException {
149: // This call has to happen first!
150: super ("Javadoc Viewer", new URL("file", "", introPagePath(
151: destDir, curClass)), new URL("file", "", (new File(
152: destDir, INDEX_PAGE)).getAbsolutePath()),
153: "DrJavadoc.png", destDir);
154:
155: addHyperlinkListener(new HyperlinkListener() {
156: public void hyperlinkUpdate(HyperlinkEvent event) {
157: if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
158: URL url = event.getURL();
159: jumpTo(url);
160: }
161: }
162: });
163:
164: if (!allDocs) {
165: _hideNavigationPane();
166: }
167: }
168: }
|