LibreOffice Module xmerge (master) 1
StyleCatalog.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 java.lang.reflect.Constructor;
22import java.util.ArrayList;
23
25import org.w3c.dom.Element;
26import org.w3c.dom.NamedNodeMap;
27import org.w3c.dom.Node;
28import org.w3c.dom.NodeList;
29
40public class StyleCatalog {
41
42 private final ArrayList<Style> styles; // The actual styles
43
50 public StyleCatalog(int initialEntries) {
51 styles = new ArrayList<Style>(initialEntries);
52 }
53
86 public void add(Node node, String families[], Class<?> classes[],
87 Class<?> defaultClass, boolean alwaysCreateDefault) {
88
89 if (node == null)
90 return;
91
92 if (families == null)
93 families = new String[0];
94 if (classes == null)
95 classes = new Class[0];
96 if (!node.hasChildNodes()) {
97 return;
98 }
99 NodeList children = node.getChildNodes();
100 int len = children.getLength();
101
102 for (int i = 0; i < len; i++) {
103 boolean found = false;
104 Node child = children.item(i);
105 String name = child.getNodeName();
106 if (name.equals("style:default-style")
107 || name.equals("style:style")) {
108 String familyName = getFamilyName(child);
109 if (familyName == null) {
110 Debug.log(Debug.ERROR, "familyName is null!");
111 continue;
112 }
113
114 for (int j = 0; j < families.length; j++) {
115 if (families[j].equals(familyName)) {
116 callConstructor(classes[j], child);
117 found = true;
118 }
119 }
120 if ((!found || alwaysCreateDefault)
121 && (defaultClass != null))
122 callConstructor(defaultClass, child);
123 }
124 }
125 }
126
134 private void callConstructor(Class<?> cls, Node node) {
135 Class<?> params[] = new Class[2];
136 params[0] = Node.class;
137 params[1] = this.getClass();
138 try {
139 Constructor<?> c = cls.getConstructor(params);
140 Object p[] = new Object[2];
141 p[0] = node;
142 p[1] = this;
143 styles.add((Style) c.newInstance(p));
144 } catch (Exception e) {
145 Debug.log(Debug.ERROR, "Exception when calling constructor", e);
146 }
147 }
148
154 public void add(Style s) {
155 styles.add(s);
156 }
157
173 public Style lookup(String name, String family, String parent,
174 Class<?> styleClass) {
175 int nStyles = styles.size();
176 for (int i = 0; i < nStyles; i++) {
177 Style s = styles.get(i);
178 if ((name != null) && (s.getName() != null)
179 && (!s.getName().equals(name)))
180 continue;
181 if ((family != null) && (s.getFamily() != null)
182 && (!s.getFamily().equals(family)))
183 continue;
184 if ((parent != null) && (s.getParent() != null)
185 && (!s.getParent().equals(parent)))
186 continue;
187 if ((styleClass != null) && (s.getClass() != styleClass))
188 continue;
189 if (s.getName() == null) continue; // DJP: workaround for "null name" problem
190 return s;
191 }
192 return null; // none found
193 }
194
203 public Style[] getMatching(Style s) {
204
205 // Run through and count the matching styles so we know how big of
206 // an array to allocate.
207 int matchCount = 0;
208 int nStyles = styles.size();
209 for (int j = 0; j < nStyles; j++) {
210 Style p = styles.get(j).getResolved();
211 if (p.isSubset(s)) matchCount++;
212 }
213
214 // Now allocate the array, and run through again, populating it.
215 Style[] matchArray = new Style[matchCount];
216 matchCount = 0;
217 for (int j = 0; j < nStyles; j++) {
218 Style p = styles.get(j).getResolved();
219 if (p.isSubset(s)) matchArray[matchCount++] = p;
220 }
221 return matchArray;
222 }
223
237 public Element writeNode(org.w3c.dom.Document parentDoc, String name) {
238 Element rootNode = parentDoc.createElement(name);
239
240 int len = styles.size();
241 for (int j = 0; j < len; j++) {
242 Style s = styles.get(j);
243
244 Element styleNode = parentDoc.createElement("style:style");
245
246 if (s.getName() != null)
247 styleNode.setAttribute("style:name", s.getName());
248 if (s.getParent() != null)
249 styleNode.setAttribute("style:parent-style-name", s.getParent());
250 if (s.getFamily() != null)
251 styleNode.setAttribute("style:family", s.getFamily());
252
253 Element propertiesNode = (Element) s.createNode(parentDoc, "style:properties");
254 // DJP: only add node if has children OR attributes
255 if (propertiesNode != null)
256 styleNode.appendChild(propertiesNode);
257
258 rootNode.appendChild(styleNode);
259 }
260
261 return rootNode;
262 }
263
271 private String getFamilyName(Node node) {
272 NamedNodeMap attributes = node.getAttributes();
273 if (attributes != null) {
274 int len = attributes.getLength();
275 for (int i = 0; i < len; i++) {
276 Node attr = attributes.item(i);
277 if (attr.getNodeName().equals("style:family")) {
278 return attr.getNodeValue();
279 }
280 }
281 }
282 return null;
283 }
284}
A StyleCatalog holds a collection of Style objects.
Style[] getMatching(Style s)
Given a Style s return all Style objects that match.
Element writeNode(org.w3c.dom.Document parentDoc, String name)
Create a Node named name in Document parentDoc, and write the entire StyleCatalog to it.
StyleCatalog(int initialEntries)
Constructor.
void add(Style s)
Add a Style to the catalog.
Style lookup(String name, String family, String parent, Class<?> styleClass)
Return the first Style matching the specified names.
void callConstructor(Class<?> cls, Node node)
Call the constructor of class cls with parameters node, and add the resulting Style to the catalog.
void add(Node node, String families[], Class<?> classes[], Class<?> defaultClass, boolean alwaysCreateDefault)
Parse the Document starting from node and working downward, and add all styles found,...
String getFamilyName(Node node)
Find the family attribute of a Style Node.
An object of class Style represents a style in an OpenOffice document.
Definition: Style.java:34
Node createNode(org.w3c.dom.Document parentDoc, String name)
Write a Node in parentDoc representing this Style.
Definition: Style.java:168
String getName()
Returns the name of this Style.
Definition: Style.java:116
String getFamily()
Return the family of this Style.
Definition: Style.java:134
String getParent()
Return the name of the parent of this Style.
Definition: Style.java:143
This class is used for logging debug messages.
Definition: Debug.java:39
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
const char * name
void * p
@ Exception
int i
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)
const char * matchArray(const char *pSource, sal_Int32 nSourceSize, const char *pSearch, sal_Int32 nSearchSize)