001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: RtfHyperLink.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.render.rtf.rtflib.rtfdoc;
021:
022: /*
023: * This file is part of the RTF library of the FOP project, which was originally
024: * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
025: * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
026: * the FOP project.
027: */
028:
029: import java.io.Writer;
030: import java.io.IOException;
031:
032: /**
033: * Creates an hyperlink.
034: * This class belongs to the <fo:basic-link> tag processing.
035: * @author <a href="mailto:a.putz@skynamics.com">Andreas Putz</a>
036: *
037: * {\field {\*\fldinst HYPERLINK "http://www.test.de" }{\fldrslt Joe Smith}}
038: */
039: public class RtfHyperLink extends RtfContainer implements
040: IRtfTextContainer, IRtfTextrunContainer {
041:
042: //////////////////////////////////////////////////
043: // @@ Members
044: //////////////////////////////////////////////////
045:
046: /** The url of the image */
047: protected String url = null;
048:
049: /** RtfText */
050: protected RtfText mText = null;
051:
052: //////////////////////////////////////////////////
053: // @@ Construction
054: //////////////////////////////////////////////////
055:
056: /**
057: * Default constructor.
058: *
059: * @param parent a <code>RtfContainer</code> value
060: * @param writer a <code>Writer</code> value
061: * @param str text of the link
062: * @param attr a <code>RtfAttributes</code> value
063: * @throws IOException for I/O problems
064: */
065: public RtfHyperLink(IRtfTextContainer parent, Writer writer,
066: String str, RtfAttributes attr) throws IOException {
067: super ((RtfContainer) parent, writer, attr);
068: new RtfText(this , writer, str, attr);
069: }
070:
071: public RtfHyperLink(RtfTextrun parent, Writer writer,
072: RtfAttributes attr) throws IOException {
073: super ((RtfContainer) parent, writer, attr);
074: }
075:
076: //////////////////////////////////////////////////
077: // @@ RtfElement implementation
078: //////////////////////////////////////////////////
079:
080: /**
081: * Writes the RTF content to m_writer.
082: *
083: * @exception IOException On error
084: */
085: public void writeRtfPrefix() throws IOException {
086: super .writeGroupMark(true);
087: super .writeControlWord("field");
088:
089: super .writeGroupMark(true);
090: super .writeStarControlWord("fldinst");
091:
092: writer.write("HYPERLINK \"" + url + "\" ");
093: super .writeGroupMark(false);
094:
095: super .writeGroupMark(true);
096: super .writeControlWord("fldrslt");
097:
098: // start a group for this paragraph and write our own attributes if needed
099: if (attrib != null && attrib.isSet("cs")) {
100: writeGroupMark(true);
101: writeAttributes(attrib, new String[] { "cs" });
102: }
103: }
104:
105: /**
106: * Writes the RTF content to m_writer.
107: *
108: * @exception IOException On error
109: */
110: public void writeRtfSuffix() throws IOException {
111: if (attrib != null && attrib.isSet("cs")) {
112: writeGroupMark(false);
113: }
114: super .writeGroupMark(false);
115: super .writeGroupMark(false);
116: }
117:
118: //////////////////////////////////////////////////
119: // @@ IRtfContainer implementation
120: //////////////////////////////////////////////////
121:
122: /**
123: * close current text run if any and start a new one with default attributes
124: * @param str if not null, added to the RtfText created
125: * @throws IOException for I/O problems
126: * @return new RtfText object
127: */
128: public RtfText newText(String str) throws IOException {
129: return newText(str, null);
130: }
131:
132: /**
133: * close current text run if any and start a new one
134: * @param str if not null, added to the RtfText created
135: * @param attr attributes of text to add
136: * @throws IOException for I/O problems
137: * @return the new RtfText object
138: */
139: public RtfText newText(String str, RtfAttributes attr)
140: throws IOException {
141: closeAll();
142: mText = new RtfText(this , writer, str, attr);
143: return mText;
144: }
145:
146: /**
147: * IRtfTextContainer requirement:
148: * @return a copy of our attributes
149: */
150: public RtfAttributes getTextContainerAttributes() {
151: if (attrib == null) {
152: return null;
153: }
154: return (RtfAttributes) this .attrib.clone();
155: }
156:
157: /**
158: * add a line break
159: * @throws IOException for I/O problems
160: */
161: public void newLineBreak() throws IOException {
162: new RtfLineBreak(this , writer);
163: }
164:
165: //////////////////////////////////////////////////
166: // @@ Common container methods
167: //////////////////////////////////////////////////
168:
169: private void closeCurrentText() throws IOException {
170: if (mText != null) {
171: mText.close();
172: }
173: }
174:
175: private void closeAll() throws IOException {
176: closeCurrentText();
177: }
178:
179: //////////////////////////////////////////////////
180: // @@ Member access
181: //////////////////////////////////////////////////
182:
183: /**
184: * Sets the url of the external link.
185: *
186: * @param url Link url like "http://..."
187: */
188: public void setExternalURL(String url) {
189: this .url = url;
190: }
191:
192: /**
193: * Sets the url of the external link.
194: *
195: * @param jumpTo Name of the text mark
196: */
197: public void setInternalURL(String jumpTo) {
198: int now = jumpTo.length();
199: int max = RtfBookmark.MAX_BOOKMARK_LENGTH;
200: this .url = "#" + jumpTo.substring(0, now > max ? max : now);
201: this .url = this .url.replace('.', RtfBookmark.REPLACE_CHARACTER);
202: this .url = this .url.replace(' ', RtfBookmark.REPLACE_CHARACTER);
203: }
204:
205: /**
206: *
207: * @return false (always)
208: */
209: public boolean isEmpty() {
210: return false;
211: }
212:
213: public RtfTextrun getTextrun() throws IOException {
214: RtfTextrun textrun = RtfTextrun.getTextrun(this, writer, null);
215: return textrun;
216: }
217: }
|