LibreOffice Module xmerge (master) 1
ParaStyle.java
Go to the documentation of this file.
1/*
2 * This file is part of the LibreOffice project.
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 *
8 * This file incorporates work covered by the following license notice:
9 *
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 */
18
19package org.openoffice.xmerge.converter.xml;
20
21import org.w3c.dom.NodeList;
22import org.w3c.dom.Node;
23import org.w3c.dom.NamedNodeMap;
24import org.w3c.dom.Element;
25
27
28abstract class conversionAlgorithm {
29 abstract int I(String val);
30}
31
35class horizSize extends conversionAlgorithm {
36 @Override
37 int I(String value) {
38 if (value.endsWith("mm")) {
39 float size = (float)0.0;
40 String num = value.substring(0, value.length() - 2);
41 try {
42 size = Float.parseFloat(num);
43 } catch (Exception e) {
44 Debug.log(Debug.ERROR, "Error parsing " + value, e);
45 }
46 size *= 100;
47 return (int)size;
48 } else {
49 Debug.log(Debug.ERROR, "Unexpected value (" + value
50 + ") in horizSize.I()");
51 return 0;
52 }
53 }
54}
55
60class lineHeight extends conversionAlgorithm {
61 @Override
62 int I(String value) {
63 if (value.endsWith("mm")) {
64 float size = (float)0.0;
65 String num = value.substring(0, value.length() - 2);
66 try {
67 size = Float.parseFloat(num);
68 } catch (Exception e) {
69 Debug.log(Debug.ERROR, "Error parsing " + value, e);
70 }
71 size *= 100;
72 return (int)size;
73 } else if (value.endsWith("%")) {
74 float size = (float)0.0;
75 String num = value.substring(0, value.length() - 1);
76 try {
77 size = Float.parseFloat(num);
78 } catch (Exception e) {
79 Debug.log(Debug.ERROR, "Error parsing " + value, e);
80 }
81 int retval = (int) size;
82 retval |= ParaStyle.LH_PCT;
83 return retval;
84 }
85 return 0;
86 }
87}
88
92class alignment extends conversionAlgorithm {
93 @Override
94 int I(String value) {
95 if (value.equals("end"))
96 return ParaStyle.ALIGN_RIGHT;
97 if (value.equals("right"))
98 return ParaStyle.ALIGN_RIGHT;
99 if (value.equals("center"))
100 return ParaStyle.ALIGN_CENTER;
101 if (value.equals("justify"))
102 return ParaStyle.ALIGN_JUST;
103 if (value.equals("justified"))
104 return ParaStyle.ALIGN_JUST;
105 if (value.equals("start"))
106 return ParaStyle.ALIGN_LEFT;
107 if (value.equals("left"))
108 return ParaStyle.ALIGN_LEFT;
109 Debug.log(Debug.ERROR, "Unknown string ("
110 + value + ") in alignment.I()");
111 return ParaStyle.ALIGN_LEFT;
112 }
113}
114
139public class ParaStyle extends Style implements Cloneable {
140
142 private static final int TEXT_INDENT = 4;
144 private static final int LINE_HEIGHT = 5;
146 private static final int TEXT_ALIGN = 6;
147 // This must always be one more than highest property
149 private static final int NR_PROPERTIES = 7;
150
155 private boolean isSet[] = new boolean[NR_PROPERTIES];
157 private int value[] = new int[NR_PROPERTIES];
159 private final String attrName[] = {
160 "fo:margin-left",
161 "fo:margin-right",
162 "fo:margin-top",
163 "fo:margin-bottom",
164 "fo:text-indent",
165 "fo:line-height",
166 "fo:text-align"
167 };
168
170 private final Class<?> algor[] = {
171 horizSize.class,
172 horizSize.class,
173 horizSize.class,
174 horizSize.class,
175 horizSize.class,
176 lineHeight.class,
177 alignment.class
178 };
179
181 public static final int ALIGN_RIGHT = 1;
183 public static final int ALIGN_CENTER = 2;
185 public static final int ALIGN_JUST = 3;
187 public static final int ALIGN_LEFT = 4;
188
190 public static final int LH_PCT = 0x40000000;
191
193 private static final int LH_VALUEMASK = 0x00FFFFFF;
194
196 private static String[] ignored = {
197 "style:font-name", "fo:font-size", "fo:font-weight", "fo:color",
198 "fo:language", "fo:country", "style:font-name-asian",
199 "style:font-size-asian", "style:language-asian",
200 "style:country-asian", "style:font-name-complex",
201 "style:font-size-complex", "style:language-complex",
202 "style:country-complex", "style:text-autospace", "style:punctuation-wrap",
203 "style:line-break", "fo:keep-with-next", "fo:font-style",
204 "text:number-lines", "text:line-number"
205 };
206
215 public ParaStyle(Node node, StyleCatalog sc) {
216
217 super(node, sc);
218
219 // Look for children. Only ones we care about are "style:properties"
220 // nodes. If any are found, recursively traverse them, passing
221 // along the style element to add properties to.
222
223 if (node.hasChildNodes()) {
224 NodeList children = node.getChildNodes();
225 int len = children.getLength();
226 for (int i = 0; i < len; i++) {
227 Node child = children.item(i);
228 String nodeName = child.getNodeName();
229 if (nodeName.equals("style:properties")) {
230 NamedNodeMap childAttrNodes = child.getAttributes();
231 if (childAttrNodes != null) {
232 int nChildAttrNodes = childAttrNodes.getLength();
233 for (int j = 0; j < nChildAttrNodes; j++) {
234 Node attr = childAttrNodes.item(j);
235 setAttribute(attr.getNodeName(), attr.getNodeValue());
236 }
237 }
238 }
239 }
240 }
241 }
242
256 public ParaStyle(String name, String familyName, String parentName,
257 String attribs[], String values[], StyleCatalog sc) {
258 super(name, familyName, parentName, sc);
259 if (attribs != null)
260 for (int i = 0; i < attribs.length; i++)
261 setAttribute(attribs[i], values[i]);
262 }
263
276 public ParaStyle(String name, String familyName, String parentName,
277 int attribs[], String values[], StyleCatalog lookup) {
278 super(name, familyName, parentName, lookup);
279 if (attribs != null)
280 for (int i = 0; i < attribs.length; i++)
281 setAttribute(attribs[i], values[i]);
282 }
283
291 private boolean isIgnored(String attribute) {
292 for (int i = 0; i < ignored.length; i++) {
293 if (ignored[i].equals(attribute))
294 return true;
295 }
296 return false;
297 }
298
305 private void setAttribute(String attr, String value) {
306 for (int i = 0; i < NR_PROPERTIES; i++) {
307 if (attr.equals(attrName[i])) {
309 return;
310 }
311 }
312 if (!isIgnored(attr))
313 Debug.log(Debug.INFO, "ParaStyle Unhandled: " + attr + "=" + value);
314 }
315
322 private void setAttribute(int attr, String value) {
323 isSet[attr] = true;
324 try {
325 this.value[attr] = ((conversionAlgorithm)algor[attr].newInstance()).I(value);
326 } catch (Exception e) {
327 Debug.log(Debug.ERROR, "Instantiation error", e);
328 }
329 }
330
336 @Override
338 ParaStyle resolved = null;
339 try {
340 resolved = (ParaStyle)this.clone();
341 } catch (Exception e) {
342 Debug.log(Debug.ERROR, "Can't clone", e);
343 }
344
345 // Look up the parent style. (If there is no style catalog
346 // specified, we can't do any lookups).
347 ParaStyle parentStyle = null;
348 if (sc != null) {
349 if (parent != null) {
350 parentStyle = (ParaStyle)sc.lookup(parent, family, null,
351 this.getClass());
352 if (parentStyle == null)
353 Debug.log(Debug.ERROR, "parent style lookup of "
354 + parent + " failed!");
355 else
356 parentStyle = (ParaStyle)parentStyle.getResolved();
357 } else if (!name.equals("DEFAULT_STYLE")) {
358 parentStyle = (ParaStyle)sc.lookup("DEFAULT_STYLE", null, null,
359 this.getClass());
360 }
361 }
362
363 // If we found a parent, for any attributes which we don't have
364 // set, try to get the values from the parent.
365 if (parentStyle != null) {
366 parentStyle = (ParaStyle)parentStyle.getResolved();
367 for (int i = 0; i < NR_PROPERTIES; i++) {
368 if (!isSet[i] && parentStyle.isSet[i]) {
369 resolved.isSet[i] = true;
370 resolved.value[i] = parentStyle.value[i];
371 }
372 }
373 }
374 return resolved;
375 }
376
385 private static String toCSV(String value) {
386 if (value != null)
387 return "\"" + value + "\",";
388 else
389 return "\"\",";
390 }
391
400 private static String toLastCSV(String value) {
401 if (value != null)
402 return "\"" + value + "\"";
403 else
404 return "\"\"";
405 }
406
410 public static void dumpHdr() {
411 System.out.println(toCSV("Name") + toCSV("Family") + toCSV("parent")
412 + toCSV("left mgn") + toCSV("right mgn")
413 + toCSV("top mgn") + toCSV("bottom mgn") + toCSV("txt indent")
414 + toCSV("line height") + toLastCSV("txt align"));
415 }
416
420 public void dumpCSV() {
421 String attributes = "";
422 for (int index = 0; index <= 6; index++) {
423 if (isSet[index]) {
424 attributes += toCSV("" + value[index]);
425 }
426 else
427 attributes += toCSV(null); // unspecified
428 }
429 System.out.println(toCSV(name) + toCSV(family) + toCSV(parent)
430 + attributes + toLastCSV(null));
431 }
432
441 @Override
442 public Node createNode(org.w3c.dom.Document parentDoc, String name) {
443 Element node = parentDoc.createElement(name);
444 writeAttributes(node);
445 return node;
446 }
447
456 @Override
457 public boolean isSubset(Style style) {
458
459 if (!super.isSubset(style))
460 return false;
461 if (!this.getClass().isAssignableFrom(style.getClass()))
462 return false;
463 ParaStyle ps = (ParaStyle)style;
464
465 for (int i = 0; i < NR_PROPERTIES; i++) {
466 if (ps.isSet[i]) {
467 if (i < NR_PROPERTIES - 1) {
468 // Compare the actual values. We allow a margin of error
469 // here because the conversion loses precision.
470 int diff;
471 if (value[i] > ps.value[i])
472 diff = value[i] - ps.value[i];
473 else
474 diff = ps.value[i] - value[i];
475 if (diff > 32)
476 return false;
477 } else {
478 if (i == TEXT_ALIGN)
479 if ((value[i] == 0) && (ps.value[i] == 4))
480 continue;
481 if (value[i] != ps.value[i])
482 return false;
483 }
484 }
485 }
486 return true;
487 }
488
496 private void writeAttributes(Element node) {
497 for (int i = 0; i <= TEXT_INDENT; i++) {
498 if (isSet[i]) {
499 double temp = value[i] / 100.0;
500 String stringVal = Double.toString(temp) + "mm";
501 node.setAttribute(attrName[i], stringVal);
502 }
503 }
504
505 if (isSet[LINE_HEIGHT]) {
506 String stringVal;
507 if ((value[LINE_HEIGHT] & LH_PCT) != 0)
508 stringVal = Integer.toString(value[LINE_HEIGHT] & LH_VALUEMASK) + "%";
509 else {
510 double temp = (value[LINE_HEIGHT] & LH_VALUEMASK) / 100.0;
511 stringVal = Double.toString(temp) + "mm";
512 }
513 node.setAttribute(attrName[LINE_HEIGHT], stringVal);
514 }
515
516 if (isSet[TEXT_ALIGN]) {
517 String val;
518 switch (value[TEXT_ALIGN]) {
519 case ALIGN_RIGHT: val = "end"; break;
520 case ALIGN_CENTER: val = "center"; break;
521 case ALIGN_JUST: val = "justify"; break;
522 case ALIGN_LEFT: val = "left"; break;
523 default: val = "unknown"; break;
524 }
525 node.setAttribute(attrName[TEXT_ALIGN], val);
526 }
527 }
528}
This class represents a paragraph Style.
Definition: ParaStyle.java:139
static void dumpHdr()
Print a Comma Separated Value (CSV) header line for the spreadsheet dump.
Definition: ParaStyle.java:410
int value[]
Array of attribute values for this paragraph Style.
Definition: ParaStyle.java:157
static final int LH_PCT
Line height percentage.
Definition: ParaStyle.java:190
static final int ALIGN_JUST
Align justified.
Definition: ParaStyle.java:185
ParaStyle(String name, String familyName, String parentName, String attribs[], String values[], StyleCatalog sc)
Constructor for use when going from client device format to DOM.
Definition: ParaStyle.java:256
final String attrName[]
Array of attribute names for this paragraph Style.
Definition: ParaStyle.java:159
Node createNode(org.w3c.dom.Document parentDoc, String name)
Create the Node with the specified elements.
Definition: ParaStyle.java:442
static final int NR_PROPERTIES
Total number of properties.
Definition: ParaStyle.java:149
boolean isIgnored(String attribute)
This code checks whether an attribute is one that we intentionally ignore.
Definition: ParaStyle.java:291
static final int LINE_HEIGHT
Indent right property.
Definition: ParaStyle.java:144
ParaStyle(Node node, StyleCatalog sc)
Constructor for use when going from DOM to client device format.
Definition: ParaStyle.java:215
Style getResolved()
Return the Style in use.
Definition: ParaStyle.java:337
void dumpCSV()
Dump this Style as a Comma Separated Value (CSV) line.
Definition: ParaStyle.java:420
static String toCSV(String value)
Private function to return the value as an element in a Comma Separated Value (CSV) format.
Definition: ParaStyle.java:385
ParaStyle(String name, String familyName, String parentName, int attribs[], String values[], StyleCatalog lookup)
Alternate constructor for use when going from client device format to DOM.
Definition: ParaStyle.java:276
static final int TEXT_INDENT
Indent left property.
Definition: ParaStyle.java:142
void setAttribute(String attr, String value)
Set an attribute for this paragraph Style.
Definition: ParaStyle.java:305
static final int ALIGN_CENTER
Align center.
Definition: ParaStyle.java:183
static final int LH_VALUEMASK
Line height mask.
Definition: ParaStyle.java:193
final Class<?> algor[]
Array of attribute structures for this paragraph Style.
Definition: ParaStyle.java:170
boolean isSet[]
Array of flags indicating which attributes are set for this paragraph Style.
Definition: ParaStyle.java:155
static final int ALIGN_RIGHT
Align right.
Definition: ParaStyle.java:181
boolean isSubset(Style style)
Return true if style is a subset of the Style.
Definition: ParaStyle.java:457
static final int ALIGN_LEFT
Align left.
Definition: ParaStyle.java:187
static String toLastCSV(String value)
Private function to return the value as a last element in a Comma Separated Value (CSV) format.
Definition: ParaStyle.java:400
static String[] ignored
Ignored tags.
Definition: ParaStyle.java:196
static final int TEXT_ALIGN
Align text property.
Definition: ParaStyle.java:146
void setAttribute(int attr, String value)
Set an attribute for this paragraph Style.
Definition: ParaStyle.java:322
void writeAttributes(Element node)
Add Style attributes to the given Node.
Definition: ParaStyle.java:496
A StyleCatalog holds a collection of Style objects.
An object of class Style represents a style in an OpenOffice document.
Definition: Style.java:34
String parent
Parent of the Style.
Definition: Style.java:41
String family
Family of the Style.
Definition: Style.java:39
String name
Name of the Style.
Definition: Style.java:37
This class is used for logging debug messages.
Definition: Debug.java:39
static final int INFO
Informational messages.
Definition: Debug.java:42
static void log(int flag, String msg)
Log message based on the flag type.
Definition: Debug.java:205
static final int ERROR
Error messages.
Definition: Debug.java:44
T * clone(T *const other)
Any value
size
@ Exception
bool isAssignableFrom(const Type &_rAssignable, const Type &_rFrom)
int i
index
Provides general purpose utilities.
Provides interfaces for converting between two Document formats, and supports a "merge" interface for...
Definition: Convert.java:19
PyRef getClass(const OUString &name, const Runtime &runtime)
std::vector< char * > values
sal_Int32 attribute
const wchar_t *typedef int(__stdcall *DllNativeUnregProc)(int