LibreOffice Module xmerge (master) 1
OfficeUtil.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.util;
20
21import org.w3c.dom.Document;
22import org.w3c.dom.Element;
23import org.w3c.dom.Node;
24import org.w3c.dom.Text;
25
27
28import java.util.ArrayList;
29
35public class OfficeUtil implements OfficeConstants {
36
46 public static Node[] parseText(String text, Document parentDoc) {
47 ArrayList<Node> nodeVec = new ArrayList<Node>();
48
49 /*
50 * Break up the text from the text run into Open
51 * Office text runs. There may be more runs in OO because
52 * runs of 2 or more spaces map to nodes.
53 */
54 while ((text.indexOf(" ") != -1) || (text.indexOf('\t') != 1)) {
55
56 /*
57 * Find the indices of tabs and multiple spaces, and
58 * figure out which of them occurs first in the string.
59 */
60 int spaceIndex = text.indexOf(" ");
61 int tabIndex = text.indexOf('\t');
62 if ((spaceIndex == -1) && (tabIndex == -1))
63 break; // DJP This should not be necessary. What is wrong
64 // with the while() stmt up above?
65 int closerIndex; // Index of the first of these
66 if (spaceIndex == -1)
67 closerIndex = tabIndex;
68 else if (tabIndex == -1)
69 closerIndex = spaceIndex;
70 else
71 closerIndex = (spaceIndex > tabIndex) ? tabIndex : spaceIndex;
72
73 /*
74 * If there is any text prior to the first occurrence of a
75 * tab or spaces, create a text node from it, then chop it
76 * off the string we're working with.
77 */
78 if (closerIndex > 0) {
79 String beginningText = text.substring(0, closerIndex);
80 Text textNode = parentDoc.createTextNode(beginningText);
81 nodeVec.add(textNode);
82 }
83 text = text.substring(closerIndex);
84
85 /*
86 * Handle either tab character or space sequence by creating
87 * an element for it, and then chopping out the text that
88 * represented it in "text".
89 */
90 if (closerIndex == tabIndex) {
91 Element tabNode = parentDoc.createElement(TAG_TAB_STOP);
92 nodeVec.add(tabNode);
93 text = text.substring(1); // tab is always a single character
94 } else {
95 // Compute length of space sequence.
96 int nrSpaces = 2;
97 while ((nrSpaces < text.length())
98 && text.substring(nrSpaces, nrSpaces + 1).equals(" ")) {
99 nrSpaces++;
100 }
101
102 Element spaceNode = parentDoc.createElement(TAG_SPACE);
103 spaceNode.setAttribute(ATTRIBUTE_SPACE_COUNT,
104 Integer.toString(nrSpaces));
105 nodeVec.add(spaceNode);
106 text = text.substring(nrSpaces);
107 }
108 }
109
110 /*
111 * No more tabs or space sequences. If there's any remaining
112 * text create a text node for it.
113 */
114 if (text.length() > 0) {
115 Text textNode = parentDoc.createTextNode(text);
116 nodeVec.add(textNode);
117 }
118
119 // Now create and populate an array to return the nodes in.
120 Node nodes[] = nodeVec.toArray(new Node[nodeVec.size()]);
121 return nodes;
122 }
123}
Class providing utility methods for OpenOffice plug-ins.
Definition: OfficeUtil.java:35
static Node[] parseText(String text, Document parentDoc)
Method to replace whitespace character within text with appropriate OpenOffice tags.
Definition: OfficeUtil.java:46
A Document represents any Document to be converted and the resulting Document from any conversion.
Definition: Document.java:36
This interface contains constants for StarOffice XML tags, attributes (StarCalc cell types,...
String TAG_TAB_STOP
Element tag for text:tab-stop.
String ATTRIBUTE_SPACE_COUNT
Attribute tag for text:c of element text:s.
def text(shape, orig_st)
Document and PluginFactory implementations for XML based formats.
Provides interfaces for converting between two Document formats, and supports a "merge" interface for...
Definition: Convert.java:19