KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xtpdoc > Anchor


1 /*
2  * Copyright (c) 1998-2000 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Emil Ong
28  */

29
30 package com.caucho.xtpdoc;
31
32 import javax.xml.stream.XMLStreamException;
33 import javax.xml.stream.XMLStreamWriter;
34 import java.io.IOException JavaDoc;
35 import java.io.PrintWriter JavaDoc;
36 import java.net.URI JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 public class Anchor extends FormattedText {
40   private static final Logger JavaDoc log = Logger.getLogger(Anchor.class.getName());
41
42   private String JavaDoc _configTag;
43   private String JavaDoc _href = "";
44
45   public Anchor(Document document)
46   {
47     super(document);
48   }
49
50   public void setConfigTag(String JavaDoc configTag)
51   {
52     _configTag = configTag;
53   }
54
55   public void setHref(String JavaDoc href)
56   {
57     _href = href;
58   }
59
60   public void writeHtml(XMLStreamWriter out)
61     throws XMLStreamException
62   {
63     out.writeStartElement("a");
64
65     if (_href.startsWith("javadoc|")) {
66       String JavaDoc name = _href.substring("javadoc|".length());
67
68       name = name.replace('.', '/') + ".html";
69       
70       out.writeAttribute("href", "http://www.caucho.com/resin-javadoc/" + name);
71     }
72     else if (_href.indexOf('|') >= 0) {
73       String JavaDoc href = getDocument().getContextPath() + '/' + _href.replace('|', '/');
74       
75       out.writeAttribute("href", href);
76     }
77     else
78       out.writeAttribute("href", _href);
79
80     super.writeHtml(out);
81
82     out.writeEndElement();
83   }
84
85   public void writeLaTeX(PrintWriter JavaDoc out)
86     throws IOException JavaDoc
87   {
88     if (_href == null) {
89       super.writeLaTeX(out);
90     } else if (_href.startsWith("doc|")) {
91       String JavaDoc link = _href.substring("doc|".length()).replace("|", "/");
92       link = link.replace("#", ":");
93
94       out.print("\\hyperlink{" + link + "}{");
95
96       super.writeLaTeX(out);
97
98       out.print("}");
99     } else {
100       try {
101         URI JavaDoc uri = new URI JavaDoc(_href);
102
103         if (uri.getScheme() != null) {
104           out.print("\\href{" + _href + "}");
105
106           out.print("{");
107
108           super.writeLaTeX(out);
109
110           out.print("}");
111
112           return;
113         } else if (uri.getPath() != null && uri.getPath().length() != 0) {
114           out.print("\\hyperlink{" + uri.getPath());
115
116           if (uri.getFragment() != null)
117             out.print(":" + uri.getFragment());
118
119           out.print("}{");
120           super.writeLaTeX(out);
121           out.print("}");
122
123           return;
124         } else if (uri.getFragment() != null &&
125                    uri.getFragment().length() != 0) {
126           // XXX
127
String JavaDoc documentName = getDocument().getDocumentPath().getTail();
128
129           if (documentName != null) {
130             out.print("\\hyperlink{" + documentName + ":" + uri.getFragment());
131             out.print("}{");
132             super.writeLaTeX(out);
133             out.print("}");
134
135             return;
136           }
137         }
138
139       } catch (Exception JavaDoc e) {
140       }
141
142       out.print("\\href{" + _href + "}");
143
144       out.print("{");
145       super.writeLaTeX(out);
146       out.print("}");
147     }
148   }
149 }
150
Popular Tags