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: RtfBookmark.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: * RTF Bookmark.
034: * Create an RTF bookmark as a child of given container with default attributes.
035: * This class belongs to the "id" attribute processing.
036: * @author <a href="mailto:a.putz@skynamics.com">Andreas Putz</a>
037: */
038: public class RtfBookmark extends RtfElement {
039: //////////////////////////////////////////////////
040: // @@ Members
041: //////////////////////////////////////////////////
042:
043: /** Name of the bokkmark */
044: private String bookmark = null;
045: /** Word 2000 supports a length of 40 characters only */
046: public static final int MAX_BOOKMARK_LENGTH = 40;
047: /** Word 2000 converts '.' in bookmarks to "_", thats why we control this replacement. */
048: public static final char REPLACE_CHARACTER = '_';
049:
050: //////////////////////////////////////////////////
051: // @@ Construction
052: //////////////////////////////////////////////////
053:
054: /**
055: * Constructor.
056: *
057: * @param parent a <code>RtfBookmarkContainer</code> value
058: * @param writer a <code>Writer</code> value
059: * @param bookmark Name of the bookmark
060: */
061: RtfBookmark(RtfContainer parent, Writer w, String bookmark)
062: throws IOException {
063: super (parent, w);
064:
065: int now = bookmark.length();
066:
067: this .bookmark = bookmark.substring(0,
068: now < MAX_BOOKMARK_LENGTH ? now : MAX_BOOKMARK_LENGTH);
069: this .bookmark = this .bookmark.replace('.', REPLACE_CHARACTER);
070: this .bookmark = this .bookmark.replace(' ', REPLACE_CHARACTER);
071: }
072:
073: //////////////////////////////////////////////////
074: // @@ RtfElement implementation
075: //////////////////////////////////////////////////
076:
077: /**
078: * Is called before writing the Rtf content.
079: *
080: * @throws IOException On Error
081: */
082: public void writeRtfPrefix() throws IOException {
083: startBookmark();
084: }
085:
086: /**
087: * Writes the RTF content to m_writer.
088: *
089: * @exception IOException On error
090: */
091: public void writeRtfContent() throws IOException {
092: // this.getRtfFile ().getLog ().logInfo ("Write bookmark '" + bookmark + "'.");
093: // No content to write
094: }
095:
096: /**
097: * Is called after writing the Rtf content.
098: *
099: * @throws IOException On Error
100: */
101: public void writeRtfSuffix() throws IOException {
102: endBookmark();
103: }
104:
105: //////////////////////////////////////////////////
106: // @@ Private methods
107: //////////////////////////////////////////////////
108:
109: /**
110: * Writes RTF content to begin the bookmark.
111: *
112: * @throws IOException On error
113: */
114: private void startBookmark() throws IOException {
115:
116: // {\*\bkmkstart test}
117: writeRtfBookmark("bkmkstart");
118: }
119:
120: /**
121: * Writes RTF content to close the bookmark.
122: *
123: * @throws IOException On error
124: */
125: private void endBookmark() throws IOException {
126:
127: // {\*\bkmkend test}
128: writeRtfBookmark("bkmkend");
129: }
130:
131: /**
132: * Writes the rtf bookmark.
133: *
134: * @param tag Begin or close tag
135: *
136: * @throws IOException On error
137: */
138: private void writeRtfBookmark(String tag) throws IOException {
139: if (bookmark == null) {
140: return;
141:
142: }
143:
144: this .writeGroupMark(true);
145:
146: //changed. Now using writeStarControlWord
147: this .writeStarControlWord(tag);
148:
149: writer.write(bookmark);
150: this .writeGroupMark(false);
151: }
152:
153: /**
154: * @return true if this element would generate no "useful" RTF content
155: */
156: public boolean isEmpty() {
157: return bookmark == null || bookmark.trim().length() == 0;
158: }
159: }
|