001: /*
002: * Copyright (c) 1998-2000 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Emil Ong
028: */
029:
030: package com.caucho.xtpdoc;
031:
032: import com.caucho.config.types.RawString;
033:
034: import javax.xml.stream.XMLStreamException;
035: import javax.xml.stream.XMLStreamWriter;
036: import java.io.IOException;
037: import java.io.PrintWriter;
038: import java.net.URI;
039: import java.util.logging.Logger;
040:
041: public class Anchor extends FormattedText {
042: private static final Logger log = Logger.getLogger(Anchor.class
043: .getName());
044:
045: private String _configTag;
046: private String _href = "";
047:
048: public Anchor(Document document) {
049: super (document);
050: }
051:
052: public void setConfigTag(String configTag) {
053: _configTag = configTag;
054: }
055:
056: public void setHref(String href) {
057: _href = href;
058: }
059:
060: private void setDefaultText(String text) {
061: if (getItems().isEmpty()) {
062: addText(new RawString(text));
063: }
064: }
065:
066: public void writeHtml(XMLStreamWriter out)
067: throws XMLStreamException {
068: if (_configTag != null) {
069: out.writeStartElement("a");
070: // XXX: href
071:
072: setDefaultText(_configTag);
073: super .writeHtml(out);
074: out.writeEndElement(); // XMLStreamWriter
075:
076: return;
077: }
078:
079: out.writeStartElement("a");
080:
081: if (_href.startsWith("javadoc|")) {
082: String name = _href.substring("javadoc|".length());
083:
084: // XXX: method name is just stripped here
085: int i = name.indexOf('|');
086:
087: while (i >= 0) {
088: if (i == 0)
089: name = name.substring(1);
090: else if (i > 0)
091: name = name.substring(0, i);
092:
093: i = name.indexOf('|');
094: }
095:
096: setDefaultText(name);
097:
098: name = name.replace('.', '/') + ".html";
099:
100: out.writeAttribute("href",
101: "http://www.caucho.com/resin-javadoc/" + name);
102: } else if (_href.indexOf('|') >= 0) {
103: String href = getDocument().getContextPath() + '/'
104: + _href.replace('|', '/');
105:
106: out.writeAttribute("href", href);
107: } else
108: out.writeAttribute("href", _href);
109:
110: super .writeHtml(out);
111:
112: out.writeEndElement();
113: }
114:
115: public void writeLaTeX(PrintWriter out) throws IOException {
116: if (_href == null) {
117: super .writeLaTeX(out);
118: } else if (_href.startsWith("doc|")) {
119: String link = _href.substring("doc|".length()).replace("|",
120: "/");
121: link = link.replace("#", ":");
122:
123: out.print("\\hyperlink{" + link + "}{");
124:
125: super .writeLaTeX(out);
126:
127: out.print("}");
128: } else {
129: try {
130: URI uri = new URI(_href);
131:
132: if (uri.getScheme() != null) {
133: out.print("\\href{" + _href + "}");
134:
135: out.print("{");
136:
137: super .writeLaTeX(out);
138:
139: out.print("}");
140:
141: return;
142: } else if (uri.getPath() != null
143: && uri.getPath().length() != 0) {
144: out.print("\\hyperlink{" + uri.getPath());
145:
146: if (uri.getFragment() != null)
147: out.print(":" + uri.getFragment());
148:
149: out.print("}{");
150: super .writeLaTeX(out);
151: out.print("}");
152:
153: return;
154: } else if (uri.getFragment() != null
155: && uri.getFragment().length() != 0) {
156: // XXX
157: String documentName = getDocument()
158: .getDocumentPath().getTail();
159:
160: if (documentName != null) {
161: out.print("\\hyperlink{" + documentName + ":"
162: + uri.getFragment());
163: out.print("}{");
164: super .writeLaTeX(out);
165: out.print("}");
166:
167: return;
168: }
169: }
170:
171: } catch (Exception e) {
172: }
173:
174: out.print("\\href{" + _href + "}");
175:
176: out.print("{");
177: super .writeLaTeX(out);
178: out.print("}");
179: }
180: }
181: }
|