summaryrefslogtreecommitdiff
path: root/xmerge/source/pocketword/java/org/openoffice/xmerge/converter/xml/sxw/pocketword/DocumentDeserializerImpl.java
blob: 8cc2e2311e7c044e01295b921f3c243aafd9b237 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

package org.openoffice.xmerge.converter.xml.sxw.pocketword;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.openoffice.xmerge.ConvertData;
import org.openoffice.xmerge.ConvertException;
import org.openoffice.xmerge.Document;
import org.openoffice.xmerge.DocumentDeserializer;
import org.openoffice.xmerge.converter.xml.OfficeConstants;
import org.openoffice.xmerge.converter.xml.ParaStyle;
import org.openoffice.xmerge.converter.xml.StyleCatalog;
import org.openoffice.xmerge.converter.xml.TextStyle;
import org.openoffice.xmerge.converter.xml.sxw.SxwDocument;
import org.openoffice.xmerge.util.OfficeUtil;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


/**
 * <p>Pocket Word implementation of <code>DocumentDeserializer</code>
 * for the {@link
 * org.openoffice.xmerge.converter.xml.sxw.pocketword.PluginFactoryImpl
 * PluginFactoryImpl}.</p>
 *
 * <p>This converts a Pocket Word file to an OpenOffice Writer XML DOM.</p>
 *
 * @author  Mark Murnane
 * @version 1.1
 */
public final class DocumentDeserializerImpl
            implements DocumentDeserializer, OfficeConstants {

    private PocketWordDocument pswDoc = null;
    private SxwDocument sxwDoc = null;
    private String docName;

    private StyleCatalog styleCat = null;


    /**
     * Initialises a new <code>DocumentDeserializerImpl</code> using the
     * supplied <code>ConvertData</code>.</p>
     *
     * <p>The <code>Document</code> objects in the <code>ConvertData</code>
     *    should be {@link
     *    org.openoffice.xmerge.converter.xml.sxw.pocketword.PocketWordDocument
     *    PocketWordDocument} objects.</p>
     *
     * @param   cd      ConvertData containing a <code>PocketWordDocument</code>
     *                  for conversion.
     */
    public DocumentDeserializerImpl(ConvertData cd) {
        Iterator<Object> e = cd.getDocumentEnumeration();

        // A Pocket Word file is composed of one binary file
        while (e.hasNext()) {
            pswDoc = (PocketWordDocument)e.next();
        }

        docName = pswDoc.getName();
    }


    /**
     *  <p>Convert the data passed into the <code>DocumentDeserializer</code>
     *  constructor into the OpenOffice Writer <code>Document</code>
     *  format.</p>
     *
     *  <p>This method may or may not be thread-safe.  It is expected
     *  that the user code does not call this method in more than one
     *  thread.  And for most cases, this method is only done once.</p>
     *
     *  @return  The resulting <code>Document</code> object from conversion.
     *
     *  @throws  ConvertException  If any Convert error occurs.
     *  @throws  IOException       If any I/O error occurs.
     */
    public Document deserialize() throws IOException, ConvertException {
        Iterator<Paragraph> pe = pswDoc.getParagraphEnumeration();

        sxwDoc = new SxwDocument (docName);
        sxwDoc.initContentDOM();

        // Default to an initial 5 entries in the catalog.
        styleCat = new StyleCatalog(5);

        try {
            buildDocument(pe);
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new ConvertException("Error building OpenOffice Writer DOM: "
                                        + e.toString());

        }

        return sxwDoc;
    }


    /**
     * This method actually takes care of the conversion.
     *
     * @param   data    An Enumeration of all Paragraphs in the Pocket Word doc.
     *
     * @throws  IOException     If any I/O errors occur.
     */
    private void buildDocument(Iterator<Paragraph> data) throws IOException {

        org.w3c.dom.Document doc = sxwDoc.getContentDOM();

        /*
         * There should be only one each of office:body and
         * office:automatic-styles in each document.
         */
        Node bodyNode = doc.getElementsByTagName(TAG_OFFICE_BODY).item(0);

        // Not every document has an automatic style tag
        Node autoStylesNode = doc.getElementsByTagName(
                                        TAG_OFFICE_AUTOMATIC_STYLES).item(0);
        if (autoStylesNode == null) {
            autoStylesNode = doc.createElement(TAG_OFFICE_AUTOMATIC_STYLES);
            doc.insertBefore(autoStylesNode, bodyNode);
        }


        // Needed for naming new styles
        int paraStyles = 1;
        int textStyles = 1;

        // Pocket Word has no concept of a list.
        Element listNode = null;


        // Down to business ...
        while (data.hasNext()) {
            Paragraph p = data.next();
            Element paraNode  = doc.createElement(TAG_PARAGRAPH);

            // Set paragraph style information here
            ParaStyle pStyle = p.makeStyle();
            if (pStyle == null) {
                paraNode.setAttribute(ATTRIBUTE_TEXT_STYLE_NAME,
                                        PocketWordConstants.DEFAULT_STYLE);
            }
            else {
                // Create paragraph style
                pStyle.setName(new String("PS" + paraStyles++));
                paraNode.setAttribute(ATTRIBUTE_TEXT_STYLE_NAME, pStyle.getName());
                styleCat.add(pStyle);
            }


            /*
             * For each of the paragraphs, process each segment.
             * There will always be at least one.
             */
            Iterator<ParagraphTextSegment> paraData = p.getSegmentsEnumerator();
            ArrayList<Element> textSpans = new ArrayList<Element>(0);

            do {
                ParagraphTextSegment pts = paraData.next();
                Element span = doc.createElement(OfficeConstants.TAG_SPAN);

                TextStyle ts = pts.getStyle();

                if (ts != null) {
                    ts.setName(new String("TS" + textStyles++));
                    span.setAttribute(ATTRIBUTE_TEXT_STYLE_NAME, ts.getName());
                    styleCat.add(ts);
                }
                else {
                    span.setAttribute(ATTRIBUTE_TEXT_STYLE_NAME,
                                        PocketWordConstants.DEFAULT_STYLE);
                }

                // If this isn't a blank paragraph
                if (pts.getText() != null && !pts.getText().equals("")) {
                    Node[] children = OfficeUtil.parseText(pts.getText(), doc);

                    for (int j = 0; j < children.length; j++) {
                        span.appendChild(children[j]);
                    }
                }

                textSpans.add(span);

            } while (paraData.hasNext());


            /*
             * Special case for the first span.  If it has no style, then
             * it shouldn't be a span, so just add its children with style
             * set as standard.
             */
            Element firstSpan = textSpans.get(0);
            String  styleName = firstSpan.getAttribute(ATTRIBUTE_TEXT_STYLE_NAME);
            if (styleName.equals(PocketWordConstants.DEFAULT_STYLE)) {
                NodeList nl = firstSpan.getChildNodes();
                int len = nl.getLength();

                for (int i = 0; i < len; i++) {
                    /*
                     * Always take item 0 as the DOM tree event model will
                     * cause the NodeList to shrink as each Node is reparented.
                     *
                     * By taking the first item from the list, we essentially
                     * traverse the list in order.
                     */
                    paraNode.appendChild(nl.item(0));
                }
            }
            else {
                paraNode.appendChild(firstSpan);
            }

            // The rest are spans, so just add them
            for (int i = 1; i < textSpans.size(); i++) {
                paraNode.appendChild(textSpans.get(i));
            }


            /*
             * Pocket Word doesn't support lists, but it does have bulleted
             * paragraphs that are essentially the same thing.
             *
             * Unlike OpenOffice Writer, a blank paragraph can be bulleted
             * as well.  This will be handled by inserting a blank paragraph
             * into the unordered list, but OpenOffice Writer will not display
             * an item at that point in the list.
             */
            if (p.isBulleted()) {
                if (listNode == null) {
                    listNode = doc.createElement(TAG_UNORDERED_LIST);
                }
                Element listItem = doc.createElement(TAG_LIST_ITEM);
                listItem.appendChild(paraNode);
                listNode.appendChild(listItem);
            }
            else {
                if (listNode != null) {
                    bodyNode.appendChild(listNode);
                    listNode = null;
                }
                bodyNode.appendChild(paraNode);
            }
        } // End processing paragraphs


        // Now write the style catalog to the document
        NodeList nl = styleCat.writeNode(doc, "dummy").getChildNodes();
        int nlLen = nl.getLength();     // nl.item reduces the length
        for (int i = 0; i < nlLen; i++) {
            autoStylesNode.appendChild(nl.item(0));
        }
    }
}