1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /* $Id: RtfListTable.java 1805173 2017-08-16 10:50:04Z ssteiner $ */
19 
20 package org.apache.fop.render.rtf.rtflib.rtfdoc;
21 
22 /*
23  * This file is part of the RTF library of the FOP project, which was originally
24  * created by Bertrand Delacretaz bdelacretaz@codeconsult.ch and by other
25  * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
26  * the FOP project.
27  */
28 
29 import java.io.IOException;
30 import java.io.Writer;
31 import java.util.LinkedList;
32 
33 /**
34  * <p>RtfListTable: used to make the list table in the header section of the RtfFile.
35  * This is the method that Word uses to make lists in RTF and the way most RTF readers,
36  * esp. Adobe FrameMaker read lists from RTF.</p>
37  *
38  * <p>This work was authored by Christopher Scott (scottc@westinghouse.com).</p>
39  */
40 public class RtfListTable extends RtfContainer {
41     private LinkedList lists;
42     private LinkedList styles;
43 
44 //static data members
45     /** constant for a list table */
46     public static final String LIST_TABLE = "listtable";
47     /** constant for a list */
48     public static final String LIST = "list";
49     /** constant for a list template id */
50     public static final String LIST_TEMPLATE_ID = "listtemplateid";
51     /** constant for a list level */
52     public static final String LIST_LEVEL = "listlevel";
53     /** constant for a list number type */
54     public static final String LIST_NUMBER_TYPE = "levelnfc";
55     /** constant for a list justification */
56     public static final String LIST_JUSTIFICATION = "leveljc";
57     /** constant for list following character */
58     public static final String LIST_FOLLOWING_CHAR = "levelfollow";
59     /** constant for list start at */
60     public static final String LIST_START_AT = "levelstartat";
61     /** constant for list space */
62     public static final String LIST_SPACE = "levelspace";
63     /** constant for list indentation */
64     public static final String LIST_INDENT = "levelindent";
65     /** constant for list text format */
66     public static final String LIST_TEXT_FORM = "leveltext";
67     /** constant for list number positioning */
68     public static final String LIST_NUM_POSITION = "levelnumbers";
69     /** constant for list name */
70     public static final String LIST_NAME = "listname ;";
71     /** constant for list ID */
72     public static final String LIST_ID = "listid";
73     /** constant for list font type */
74     public static final String LIST_FONT_TYPE = "f";
75     /** constant for list override table */
76     public static final String LIST_OVR_TABLE = "listoverridetable";
77     /** constant for list override */
78     public static final String LIST_OVR = "listoverride";
79     /** constant for list override count */
80     public static final String LIST_OVR_COUNT = "listoverridecount";
81     /** constant for list number */
82     public static final String LIST_NUMBER = "ls";
83 
84     /** String array of list table attributes */
85     public static final String [] LIST_TABLE_ATTR = {
86         LIST_TABLE,             LIST,                   LIST_TEMPLATE_ID,
87         LIST_NUMBER_TYPE,       LIST_JUSTIFICATION,     LIST_FOLLOWING_CHAR,
88         LIST_START_AT,          LIST_SPACE,             LIST_INDENT,
89         LIST_TEXT_FORM,         LIST_NUM_POSITION,      LIST_ID,
90         LIST_OVR_TABLE,         LIST_OVR,               LIST_OVR_COUNT,
91         LIST_NUMBER,            LIST_LEVEL
92     };
93 
94     /**
95      * RtfListTable Constructor: sets the number of the list, and allocates
96      * for the RtfAttributes
97      * @param parent RtfContainer holding this RtfListTable
98      * @param w Writer
99      * @param num number of the list in the document
100      * @param attrs attributes of new RtfListTable
101      * @throws IOException for I/O problems
102      */
RtfListTable(RtfContainer parent, Writer w, Integer num, RtfAttributes attrs)103     public RtfListTable(RtfContainer parent, Writer w, Integer num, RtfAttributes attrs)
104     throws IOException {
105         super(parent, w, attrs);
106 
107         styles = new LinkedList();
108     }
109 
110     /**
111      * Add List
112      * @param list RtfList to add
113      * @return number of lists in the table after adding
114      */
addList(RtfList list)115     public int addList(RtfList list) {
116         if (lists == null) {
117             lists = new LinkedList();
118         }
119 
120         lists.add(list);
121 
122         return lists.size();
123     }
124 
125     /**
126      * Write the content
127      * @throws IOException for I/O problems
128      */
writeRtfContent()129     public void writeRtfContent() throws IOException {
130         newLine();
131         if (lists != null) {
132             //write '\listtable'
133             writeGroupMark(true);
134             writeStarControlWordNS(LIST_TABLE);
135             newLine();
136             for (Object list1 : lists) {
137                 final RtfList list = (RtfList) list1;
138                 writeListTableEntry(list);
139                 newLine();
140             }
141             writeGroupMark(false);
142 
143             newLine();
144             //write '\listoveridetable'
145             writeGroupMark(true);
146             writeStarControlWordNS(LIST_OVR_TABLE);
147             int z = 1;
148             newLine();
149             for (Object style1 : styles) {
150                 final RtfListStyle style = (RtfListStyle) style1;
151 
152                 writeGroupMark(true);
153                 writeStarControlWordNS(LIST_OVR);
154                 writeGroupMark(true);
155 
156                 writeOneAttributeNS(LIST_ID, style.getRtfList().getListId().toString());
157                 writeOneAttributeNS(LIST_OVR_COUNT, 0);
158                 writeOneAttributeNS(LIST_NUMBER, z++);
159 
160                 writeGroupMark(false);
161                 writeGroupMark(false);
162                 newLine();
163             }
164 
165             writeGroupMark(false);
166             newLine();
167         }
168     }
169 
170     /**
171      * Since this has no text content we have to overwrite isEmpty to print
172      * the table
173      * @return false (always)
174      */
isEmpty()175     public boolean isEmpty() {
176         return false;
177     }
178 
writeListTableEntry(RtfList list)179     private void writeListTableEntry(RtfList list)
180     throws IOException {
181         //write list-specific attributes
182         writeGroupMark(true);
183         writeControlWordNS(LIST);
184         writeOneAttributeNS(LIST_TEMPLATE_ID, list.getListTemplateId().toString());
185         writeOneAttributeNS(LIST, attrib.getValue(LIST));
186 
187         // write level-specific attributes
188         writeGroupMark(true);
189         writeControlWordNS(LIST_LEVEL);
190 
191         writeOneAttributeNS(LIST_JUSTIFICATION, attrib.getValue(LIST_JUSTIFICATION));
192         writeOneAttributeNS(LIST_FOLLOWING_CHAR, attrib.getValue(LIST_FOLLOWING_CHAR));
193         writeOneAttributeNS(LIST_SPACE, 0);
194         writeOneAttributeNS(LIST_INDENT, attrib.getValue(LIST_INDENT));
195 
196         RtfListItem item = (RtfListItem)list.getChildren().get(0);
197         if (item != null) {
198             item.getRtfListStyle().writeLevelGroup(this);
199         }
200 
201         writeGroupMark(false);
202 
203         writeGroupMark(true);
204         writeControlWordNS(LIST_NAME);
205         writeGroupMark(false);
206 
207         writeOneAttributeNS(LIST_ID, list.getListId().toString());
208 
209         writeGroupMark(false);
210     }
211 
212     /**
213      * Add list style
214      * @param ls ListStyle to set
215      * @return number of styles after adding
216      */
addRtfListStyle(RtfListStyle ls)217     public int addRtfListStyle(RtfListStyle ls) {
218         styles.add(ls);
219         return styles.size();
220     }
221 }
222