LibreOffice Module xmerge (master)  1
ColumnStyle.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 
19 package org.openoffice.xmerge.converter.xml.sxc;
20 
21 import org.w3c.dom.NodeList;
22 import org.w3c.dom.Node;
23 import org.w3c.dom.NamedNodeMap;
24 import org.w3c.dom.Element;
25 
30 
34 public class ColumnStyle extends Style implements Cloneable {
35 
36  private int colWidth = 0;
46  public ColumnStyle(Node node, StyleCatalog sc) {
47  super(node, sc);
48 
49  // Run through the attributes of this node, saving
50  // the ones we're interested in.
51  NamedNodeMap attrNodes = node.getAttributes();
52  if (attrNodes != null) {
53  int len = attrNodes.getLength();
54  for (int i = 0; i < len; i++) {
55  Node attr = attrNodes.item(i);
56  handleAttribute(attr.getNodeName(), attr.getNodeValue());
57  }
58  }
59 
60  // Look for children. Only ones we care about are "style:properties"
61  // nodes. If any are found, recursively traverse them, passing
62  // along the style element to add properties to.
63  if (node.hasChildNodes()) {
64  NodeList children = node.getChildNodes();
65  int len = children.getLength();
66  for (int i = 0; i < len; i++) {
67  Node child = children.item(i);
68  String nodeName = child.getNodeName();
69  if (nodeName.equals("style:properties")) {
70  NamedNodeMap childAttrNodes = child.getAttributes();
71  if (childAttrNodes != null) {
72  int nChildAttrNodes = childAttrNodes.getLength();
73  for (int j = 0; j < nChildAttrNodes; j++) {
74  Node attr = childAttrNodes.item(j);
75  handleAttribute(attr.getNodeName(),
76  attr.getNodeValue());
77  }
78  }
79  }
80  }
81  }
82  }
83 
96  public ColumnStyle(String name, String family, String parent, int colWidth, StyleCatalog sc) {
97  super(name, family, parent, sc);
98  this.colWidth = colWidth;
99  }
100 
106  public int getColWidth() {
107  return colWidth;
108  }
109 
115  private void setColWidth(int colWidth) {
116 
117  this.colWidth = colWidth;
118  }
119 
127  private int parseColWidth(String value) {
128  return TwipsConverter.convert2twips(value, 255);
129  }
130 
137  private void handleAttribute(String attr, String value) {
138 
139  if (attr.equals("style:column-width")) {
140  colWidth = parseColWidth(value);
141  }
142  else {
143  Debug.log(Debug.INFO, "ColumnStyle Unhandled: " + attr + "=" + value);
144  }
145  }
146 
156  @Override
157  public Style getResolved() {
158  // Create a new object to return, which is a clone of this one.
159  ColumnStyle resolved = null;
160  try {
161  resolved = (ColumnStyle)this.clone();
162  } catch (Exception e) {
163  Debug.log(Debug.ERROR, "Can't clone", e);
164  }
165 
166  // Look up the parentStyle. (If there is no style catalog
167  // specified, we can't do any lookups.)
168  ColumnStyle parentStyle = null;
169  if (sc != null) {
170  if (parent != null) {
171  parentStyle = (ColumnStyle)sc.lookup(parent, family, null,
172  this.getClass());
173  if (parentStyle == null)
174  Debug.log(Debug.ERROR, "parent style lookup of "
175  + parent + " failed!");
176  else
177  parentStyle = (ColumnStyle)parentStyle.getResolved();
178 
179  } else if (!name.equals("DEFAULT_STYLE")) {
180  parentStyle = (ColumnStyle)sc.lookup("DEFAULT_STYLE", null,
181  null, this.getClass());
182  }
183  }
184 
185  // If we found a parent, for any attributes which we don't have
186  // set, try to get the values from the parent.
187  if (parentStyle != null) {
188  parentStyle = (ColumnStyle)parentStyle.getResolved();
189 
190  if ((colWidth == 0) && (parentStyle.getColWidth() != 0))
191  resolved.setColWidth(parentStyle.getColWidth());
192  }
193  return resolved;
194  }
195 
206  @Override
207  public Node createNode(org.w3c.dom.Document parentDoc, String name) {
208  Element node = parentDoc.createElement(name);
209  writeAttributes(node);
210  return node;
211  }
212 
222  @Override
223  public boolean isSubset(Style style) {
224  if (style.getClass() != this.getClass())
225  return false;
226  ColumnStyle tStyle = (ColumnStyle)style;
227 
228  return colWidth == tStyle.getColWidth();
229  }
230 
237  private void writeAttributes(Element node) {
238 
239  if(colWidth!=0) {
240  String width = TwipsConverter.twips2cm(colWidth) + "cm";
241  node.setAttribute("style:column-width", width);
242  }
243  }
244 }
static float twips2cm(int value)
Convert from twips to cm's.
Node createNode(org.w3c.dom.Document parentDoc, String name)
Create a new.
static final int INFO
Informational messages.
Definition: Debug.java:42
ColumnStyle(String name, String family, String parent, int colWidth, StyleCatalog sc)
Constructor for use when going from client device format to DOM.
Provides general purpose utilities.
static void log(int flag, String msg)
Log message based on the flag type.
Definition: Debug.java:205
int parseColWidth(String value)
Parse a colwidth in the form { "1.234cm"} to twips.
Helper class providing static methods to convert data to/from twips.
static int convert2twips(String value, int defaultValue)
Convert.
static final int ERROR
Error messages.
Definition: Debug.java:44
void handleAttribute(String attr, String value)
Set an attribute.
int i
PyRef getClass(const OUString &name, const Runtime &runtime)
ColumnStyle(Node node, StyleCatalog sc)
Constructor for use when going from DOM to client device format.
T * clone(T *const other)
int getColWidth()
Returns the width of this column.
void setColWidth(int colWidth)
Sets the width of this column.
Provides interfaces for converting between two.
Definition: Convert.java:19
This class is used for logging debug messages.
Definition: Debug.java:39