summaryrefslogtreecommitdiff
path: root/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/ParaStyle.java
blob: 3faeb876fd14c5ff7bea6af5b4db5970c1f9d0b2 (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
 * 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;

import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Element;

import org.openoffice.xmerge.util.Debug;

abstract class conversionAlgorithm {
    abstract int I(String val);
}

/**
 * This algorithm expects only values in millimeters, e.g. {@literal "20.3mm"}.
 */
class horizSize extends conversionAlgorithm {
    @Override
    int I(String value) {
        if (value.endsWith("mm")) {
            float size = (float)0.0;
            String num = value.substring(0, value.length() - 2);
            try {
                size = Float.parseFloat(num);
            } catch (Exception e) {
                Debug.log(Debug.ERROR, "Error parsing " + value, e);
            }
            size *= 100;
            return (int)size;
        } else {
            Debug.log(Debug.ERROR, "Unexpected value (" + value
            + ") in horizSize.I()");
            return 0;
        }
    }
}

/**
 * This algorithm does line height {@literal -} can be either millimeters or a
 * percentage.
 */
class lineHeight extends conversionAlgorithm {
    @Override
    int I(String value) {
        if (value.endsWith("mm")) {
            float size = (float)0.0;
            String num = value.substring(0, value.length() - 2);
            try {
                size = Float.parseFloat(num);
            } catch (Exception e) {
                Debug.log(Debug.ERROR, "Error parsing " + value, e);
            }
            size *= 100;
            return (int)size;
        } else if (value.endsWith("%")) {
            float size = (float)0.0;
            String num = value.substring(0, value.length() - 1);
            try {
                size = Float.parseFloat(num);
            } catch (Exception e) {
                Debug.log(Debug.ERROR, "Error parsing " + value, e);
            }
            int retval = (int) size;
            retval |= ParaStyle.LH_PCT;
            return retval;
        }
        return 0;
    }
}

/**
 * This class converts alignment values.
 */
class alignment extends conversionAlgorithm {
    @Override
    int I(String value) {
        if (value.equals("end"))
            return ParaStyle.ALIGN_RIGHT;
        if (value.equals("right"))
            return ParaStyle.ALIGN_RIGHT;
        if (value.equals("center"))
            return ParaStyle.ALIGN_CENTER;
        if (value.equals("justify"))
            return ParaStyle.ALIGN_JUST;
        if (value.equals("justified"))
            return ParaStyle.ALIGN_JUST;
        if (value.equals("start"))
            return ParaStyle.ALIGN_LEFT;
        if (value.equals("left"))
            return ParaStyle.ALIGN_LEFT;
        Debug.log(Debug.ERROR, "Unknown string ("
        + value + ") in alignment.I()");
        return ParaStyle.ALIGN_LEFT;
    }
}

/**
 * This class represents a paragraph {@code Style}.
 *
 * <blockquote><table summary="Paragraph style attributes and their values"
 * border="1" cellpadding="3" cellspacing="0">
 *   <caption>Table with all paragraph style attributes and their values</caption>
 *   <tr><th>Attribute</th><th>Value</th></tr>
 *   <tr><td>MARGIN_LEFT</td><td>mm * 100</td></tr>
 *   <tr><td>MARGIN_RIGHT</td><td>mm * 100</td></tr>
 *   <tr><td>MARGIN_TOP</td><td>mm * 100 (space on top of paragraph)</td></tr>
 *   <tr><td>MARGIN_BOTTOM</td><td>mm * 100</td></tr>
 *   <tr><td>TEXT_INDENT</td><td>mm * 100 (first line indent)</td></tr>
 *   <tr>
 *     <td>LINE_HEIGHT</td>
 *     <td>mm * 100, unless or'ed with LH_PCT, in which case it is a percentage
 *         (e.g. 200% for double spacing) Can also be or'ed with LH_ATLEAST.
 *         Value is stored in bits indicated by LH_VALUEMASK.</td>
 *   </tr>
 *   <tr>
 *     <td>TEXT_ALIGN</td>
 *     <td>ALIGN_RIGHT, ALIGN_CENTER, ALIGN_JUST, ALIGN_LEFT</td>
 *   </tr>
 * </table></blockquote>
 */
public class ParaStyle extends Style implements Cloneable {

    /** Indent left property. */
    private static final int TEXT_INDENT      = 4;
    /** Indent right property. */
    private static final int LINE_HEIGHT      = 5;
    /** Align text property. */
    private static final int TEXT_ALIGN       = 6;
    // This must always be one more than highest property
    /** Total number of properties. */
    private static final int NR_PROPERTIES    = 7;

    /**
     * Array of flags indicating which attributes are set for this paragraph
     * {@code Style}.
     */
    private boolean isSet[] = new boolean[NR_PROPERTIES];
    /** Array of attribute values for this paragraph {@code Style}. */
    private int  value[] = new int[NR_PROPERTIES];
    /** Array of attribute names for this paragraph {@code Style}. */
    private String attrName[] = {
        "fo:margin-left",
        "fo:margin-right",
        "fo:margin-top",
        "fo:margin-bottom",
        "fo:text-indent",
        "fo:line-height",
        "fo:text-align"
    };

    /** Array of attribute structures for this paragraph {@code Style}. */
    private Class<?> algor[] = {
        horizSize.class,
        horizSize.class,
        horizSize.class,
        horizSize.class,
        horizSize.class,
        lineHeight.class,
        alignment.class
    };

    /** Align right. */
    public static final int ALIGN_RIGHT   = 1;
    /** Align center. */
    public static final int ALIGN_CENTER  = 2;
    /** Align justified. */
    public static final int ALIGN_JUST    = 3;
    /** Align left. */
    public static final int ALIGN_LEFT    = 4;

    /** Line height percentage.  */
    public static final int LH_PCT        = 0x40000000;

    /** Line height mask.  */
    private static final int LH_VALUEMASK = 0x00FFFFFF;

    /** Ignored tags. */
    private static String[] ignored = {
        "style:font-name", "fo:font-size", "fo:font-weight", "fo:color",
        "fo:language", "fo:country", "style:font-name-asian",
        "style:font-size-asian", "style:language-asian",
        "style:country-asian", "style:font-name-complex",
        "style:font-size-complex", "style:language-complex",
        "style:country-complex", "style:text-autospace", "style:punctuation-wrap",
        "style:line-break", "fo:keep-with-next", "fo:font-style",
        "text:number-lines", "text:line-number"
    };

    /**
     * Constructor for use when going from DOM to client device format.
     *
     * @param  node  A <i>style:style</i> {@code Node} which, which is assumed
     *               to have <i>family</i> attribute of <i>paragraph</i>.
     * @param  sc    The {@code StyleCatalog}, which is used for looking up
     *               ancestor {@code Style} objects.
     */
    public ParaStyle(Node node, StyleCatalog sc) {

        super(node, sc);

        // Look for children.  Only ones we care about are "style:properties"
        // nodes.  If any are found, recursively traverse them, passing
        // along the style element to add properties to.

        if (node.hasChildNodes()) {
            NodeList children = node.getChildNodes();
            int len = children.getLength();
            for (int i = 0; i < len; i++) {
                Node child = children.item(i);
                String nodeName = child.getNodeName();
                if (nodeName.equals("style:properties")) {
                    NamedNodeMap childAttrNodes = child.getAttributes();
                    if (childAttrNodes != null) {
                        int nChildAttrNodes = childAttrNodes.getLength();
                        for (int j = 0; j < nChildAttrNodes; j++) {
                            Node attr = childAttrNodes.item(j);
                            setAttribute(attr.getNodeName(), attr.getNodeValue());
                        }
                    }
                }
            }
        }
    }

    /**
     * Constructor for use when going from client device format to DOM.
     *
     * @param  name       Name of the {@code Style}.  Can be {@code null}.
     * @param  familyName Family of the {@code Style} {@literal -} usually
     *                    <i>paragraph</i>, <i>text</i>, etc. Can be {@code null}.
     * @param  parentName Name of the parent {@code Style}, or {@code null}
     *                    if none.
     * @param  attribs    Array of attributes to set.
     * @param  values     Array of values to set.
     * @param  sc         The {@code StyleCatalog}, which is used for looking up
     *                    ancestor {@code Style} objects.
     */
    public ParaStyle(String name, String familyName, String parentName,
    String attribs[], String values[], StyleCatalog sc) {
        super(name, familyName, parentName, sc);
        if (attribs != null)
            for (int i = 0; i < attribs.length; i++)
                setAttribute(attribs[i], values[i]);
    }

    /**
     * Alternate constructor for use when going from client device format to DOM.
     *
     * @param  name       Name of the {@code Style}.  Can be {@code null}.
     * @param  familyName Family of the {@code Style} {@literal -} usually
     *                    <i>paragraph</i>, <i>text</i>, etc. Can be {@code null}.
     * @param  parentName Name of the parent {@code Style}, or null if none.
     * @param  attribs    Array of attributes indices to set.
     * @param  values     Array of values to set.
     * @param  lookup     The {@code StyleCatalog}, which is used for looking
     *                    up ancestor {@code Style} objects.
     */
    public ParaStyle(String name, String familyName, String parentName,
    int attribs[], String values[], StyleCatalog lookup) {
        super(name, familyName, parentName, lookup);
        if (attribs != null)
            for (int i = 0; i < attribs.length; i++)
                setAttribute(attribs[i], values[i]);
    }

    /**
     * This code checks whether an attribute is one that we intentionally ignore.
     *
     * @param  attribute  The attribute to check.
     *
     * @return  {@code true} if attribute can be ignored, {@code false} otherwise.
     */
    private boolean isIgnored(String attribute) {
        for (int i = 0; i < ignored.length; i++) {
            if (ignored[i].equals(attribute))
                return true;
        }
        return false;
    }

    /**
     * Set an attribute for this paragraph {@code Style}.
     *
     * @param  attr   The attribute to set.
     * @param  value  The attribute value to set.
     */
    private void setAttribute(String attr, String value) {
        for (int i = 0; i < NR_PROPERTIES; i++) {
            if (attr.equals(attrName[i])) {
                setAttribute(i, value);
                return;
            }
        }
        if (!isIgnored(attr))
            Debug.log(Debug.INFO, "ParaStyle Unhandled: " + attr + "=" + value);
    }

    /**
     * Set an attribute for this paragraph {@code Style}.
     *
     * @param  attr   The attribute index to set.
     * @param  value  The attribute value to set.
     */
    private void setAttribute(int attr, String value) {
        isSet[attr] = true;
        try {
            this.value[attr] = (((conversionAlgorithm)algor[attr].newInstance())).I(value);
        } catch (Exception e) {
            Debug.log(Debug.ERROR, "Instantiation error", e);
        }
    }

    /**
     * Return the {@code Style} in use.
     *
     * @return  The fully-resolved copy of the {@code Style} in use.
     */
    @Override
    public Style getResolved() {
        ParaStyle resolved = null;
        try {
            resolved = (ParaStyle)this.clone();
        } catch (Exception e) {
            Debug.log(Debug.ERROR, "Can't clone", e);
        }

        // Look up the parent style.  (If there is no style catalog
        // specified, we can't do any lookups).
        ParaStyle parentStyle = null;
        if (sc != null) {
            if (parent != null) {
                parentStyle = (ParaStyle)sc.lookup(parent, family, null,
                              this.getClass());
                if (parentStyle == null)
                    Debug.log(Debug.ERROR, "parent style lookup of "
                              + parent + " failed!");
                else
                    parentStyle = (ParaStyle)parentStyle.getResolved();
            } else if (!name.equals("DEFAULT_STYLE")) {
                parentStyle = (ParaStyle)sc.lookup("DEFAULT_STYLE", null, null,
                                                   this.getClass());
            }
        }

        // If we found a parent, for any attributes which we don't have
        // set, try to get the values from the parent.
        if (parentStyle != null) {
            parentStyle = (ParaStyle)parentStyle.getResolved();
            for (int i = 0; i < NR_PROPERTIES; i++) {
                if (!isSet[i] && parentStyle.isSet[i]) {
                    resolved.isSet[i] = true;
                    resolved.value[i] = parentStyle.value[i];
                }
            }
        }
        return resolved;
    }

    /**
     * Private function to return the value as an element in a Comma Separated
     * Value (CSV) format.
     *
     * @param   value  The value to format.
     *
     * @return  The formatted value.
     */
    private static String toCSV(String value) {
        if (value != null)
            return "\"" + value + "\",";
        else
            return "\"\",";
    }

    /**
     * Private function to return the value as a last element in a Comma
     * Separated Value (CSV) format.
     *
     * @param   value  The value to format.
     *
     * @return  The formatted value.
     */
    private static String toLastCSV(String value) {
        if (value != null)
            return "\"" + value + "\"";
        else
            return "\"\"";
    }

    /**
     * Print a Comma Separated Value (CSV) header line for the spreadsheet dump.
     */
    public static void dumpHdr() {
        System.out.println(toCSV("Name") + toCSV("Family") + toCSV("parent")
        + toCSV("left mgn") + toCSV("right mgn")
        + toCSV("top mgn") + toCSV("bottom mgn") + toCSV("txt indent")
        + toCSV("line height") + toLastCSV("txt align"));
    }

    /**
     * Dump this {@code Style} as a Comma Separated Value (CSV) line.
     */
    public void dumpCSV() {
        String attributes = "";
        for (int index = 0; index <= 6; index++) {
            if (isSet[index]) {
                attributes += toCSV("" + value[index]);
            }
            else
                attributes += toCSV(null);  // unspecified
        }
        System.out.println(toCSV(name) + toCSV(family) + toCSV(parent)
        + attributes + toLastCSV(null));
    }

    /**
     * Create the {@code Node} with the specified elements.
     *
     * @param   parentDoc  Parent {@code Document} of the {@code Node} to create.
     * @param   name       Name of the {@code Node}.
     *
     * @return  The created {@code Node}.
     */
    @Override
    public Node createNode(org.w3c.dom.Document parentDoc, String name) {
        Element node = parentDoc.createElement(name);
        writeAttributes(node);
        return node;
    }

    /**
     * Return {@code true} if {@code style} is a subset of the {@code Style}.
     *
     * @param   style  {@code Style} to check.
     *
     * @return  {@code true} if <code>style</code> is a subset, {@code false}
     *          otherwise.
     */
    @Override
    public boolean isSubset(Style style) {

        if (!super.isSubset(style))
            return false;
        if (!this.getClass().isAssignableFrom(style.getClass()))
            return false;
        ParaStyle ps = (ParaStyle)style;

        for (int i = 0; i < NR_PROPERTIES; i++) {
            if (ps.isSet[i]) {
                if (i < NR_PROPERTIES - 1) {
                    // Compare the actual values.  We allow a margin of error
                    // here because the conversion loses precision.
                    int diff;
                    if (value[i] > ps.value[i])
                        diff = value[i] - ps.value[i];
                    else
                        diff = ps.value[i] - value[i];
                    if (diff > 32)
                        return false;
                } else {
                    if (i == TEXT_ALIGN)
                        if ((value[i] == 0) && (ps.value[i] == 4))
                            continue;
                    if (value[i] != ps.value[i])
                        return false;
                }
            }
        }
        return true;
    }

    /**
     * Add {@code Style} attributes to the given {@code Node}.
     *
     * <p>This may involve writing child {@code Node} objects as well.</p>
     *
     * @param  node  The {@code Node} to add {@code Style} attributes.
     */
    private void writeAttributes(Element node) {
        for (int i = 0; i <= TEXT_INDENT; i++) {
            if (isSet[i]) {
                double temp = value[i] / 100.0;
                String stringVal = Double.toString(temp) + "mm";
                node.setAttribute(attrName[i], stringVal);
            }
        }

        if (isSet[LINE_HEIGHT]) {
            String stringVal;
            if ((value[LINE_HEIGHT] & LH_PCT) != 0)
                stringVal = Integer.toString(value[LINE_HEIGHT] & LH_VALUEMASK) + "%";
            else {
                double temp = (value[LINE_HEIGHT] & LH_VALUEMASK) / 100.0;
                stringVal = Double.toString(temp) + "mm";
            }
            node.setAttribute(attrName[LINE_HEIGHT], stringVal);
        }

        if (isSet[TEXT_ALIGN]) {
            String val;
            switch (value[TEXT_ALIGN]) {
                case ALIGN_RIGHT:  val = "end"; break;
                case ALIGN_CENTER: val = "center"; break;
                case ALIGN_JUST:   val = "justify"; break;
                case ALIGN_LEFT:   val = "left"; break;
                default:           val = "unknown"; break;
            }
            node.setAttribute(attrName[TEXT_ALIGN], val);
        }
    }
}